tests.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_public_callable_field(self):
  41. c = Contrib.objects.create(
  42. name='John',
  43. phone='010101010101',
  44. orientations=['N'],
  45. contrib_type=Contrib.CONTRIB_CONNECT,
  46. privacy_name=True,
  47. )
  48. self.assertEqual(c.get_public_field('angles'), [[-23, 22]])
  49. def test_private_field(self):
  50. c = Contrib.objects.create(
  51. name='John',
  52. phone='010101010101',
  53. contrib_type=Contrib.CONTRIB_CONNECT,
  54. )
  55. self.assertEqual(c.privacy_name, False)
  56. self.assertEqual(c.get_public_field('name'), None)
  57. class TestViews(APITestCase):
  58. def test_public_json(self):
  59. response = self.client.json_get('/map/public.json')
  60. self.assertEqual(response.status_code, 200)
  61. self.assertEqual(len(response.data['features']), 0)
  62. Contrib.objects.create(
  63. name='John',
  64. phone='010101010101',
  65. contrib_type=Contrib.CONTRIB_CONNECT,
  66. privacy_coordinates=False,
  67. )
  68. response = self.client.json_get('/map/public.json')
  69. self.assertEqual(response.status_code, 200)
  70. self.assertEqual(len(response.data['features']), 1)
  71. class TestDataImport(TestCase):
  72. fixtures = ['bottle_data.yaml']
  73. def test_re_save(self):
  74. for contrib in Contrib.objects.all():
  75. contrib.full_clean()
  76. contrib.save()