tests.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import json
  2. from django.test import TestCase, Client
  3. from contribmap.models import Contrib
  4. class APITestClient(Client):
  5. def json_get(self, *args, **kwargs):
  6. """ Annotate the response with a .data containing parsed JSON
  7. """
  8. response = super().get(*args, **kwargs)
  9. response.data = json.loads(response.content.decode('utf-8'))
  10. return response
  11. class APITestCase(TestCase):
  12. def setUp(self):
  13. super().setUp()
  14. self.client = APITestClient()
  15. class TestContrib(TestCase):
  16. def test_comma_separatedcharfield(self):
  17. co = Contrib(name='foo', orientations=['SO', 'NE'])
  18. co.save()
  19. self.assertEqual(
  20. Contrib.objects.get(name='foo').orientations,
  21. ['SO', 'NE'])
  22. co.orientations = ['S']
  23. co.save()
  24. class TestContribPrivacy(TestCase):
  25. def test_always_private_field(self):
  26. c = Contrib.objects.create(
  27. name='John',
  28. phone='010101010101',
  29. contrib_type=Contrib.CONTRIB_CONNECT,
  30. )
  31. self.assertEqual(c.get_public_field('phone'), None)
  32. def test_public_field(self):
  33. c = Contrib.objects.create(
  34. name='John',
  35. phone='010101010101',
  36. contrib_type=Contrib.CONTRIB_CONNECT,
  37. privacy_name=True,
  38. )
  39. self.assertEqual(c.get_public_field('name'), 'John')
  40. def test_private_field(self):
  41. c = Contrib.objects.create(
  42. name='John',
  43. phone='010101010101',
  44. contrib_type=Contrib.CONTRIB_CONNECT,
  45. )
  46. self.assertEqual(c.privacy_name, False)
  47. self.assertEqual(c.get_public_field('name'), None)
  48. class TestViews(APITestCase):
  49. def test_public_json(self):
  50. response = self.client.json_get('/map/public.json')
  51. self.assertEqual(response.status_code, 200)
  52. self.assertEqual(len(response.data['features']), 0)
  53. Contrib.objects.create(
  54. name='John',
  55. phone='010101010101',
  56. contrib_type=Contrib.CONTRIB_CONNECT,
  57. privacy_coordinates=False,
  58. )
  59. response = self.client.json_get('/map/public.json')
  60. self.assertEqual(response.status_code, 200)
  61. self.assertEqual(len(response.data['features']), 1)
  62. class TestDataImport(TestCase):
  63. fixtures = ['bottle_data.yaml']
  64. def test_re_save(self):
  65. for contrib in Contrib.objects.all():
  66. contrib.full_clean()
  67. contrib.save()