/odk_logger/tests/test_import_tools.py

https://github.com/SEL-Columbia/formhub
Python | 79 lines | 45 code | 15 blank | 19 comment | 2 complexity | f24766ed1a4f1ca407fcc61bcfffd7fd MD5 | raw file
  1. from main.tests.test_base import MainTransactionTestCase
  2. #from django.test import TestCase
  3. from odk_logger.models import Instance
  4. import os
  5. import glob
  6. from odk_logger.import_tools import import_instances_from_zip
  7. CUR_PATH = os.path.abspath(__file__)
  8. CUR_DIR = os.path.dirname(CUR_PATH)
  9. DB_FIXTURES_PATH = os.path.join(CUR_DIR, 'data_from_sdcard')
  10. from django.conf import settings
  11. def images_count(username="bob"):
  12. images = glob.glob(
  13. os.path.join(settings.MEDIA_ROOT, username, 'attachments', '*'))
  14. return len(images)
  15. class TestImportingDatabase(MainTransactionTestCase):
  16. def setUp(self):
  17. MainTransactionTestCase.setUp(self)
  18. self._publish_xls_file(
  19. os.path.join(
  20. settings.PROJECT_ROOT,
  21. "odk_logger", "fixtures", "test_forms", "tutorial.xls"))
  22. def tearDown(self):
  23. # delete everything we imported
  24. Instance.objects.all().delete() # ?
  25. if settings.TESTING_MODE:
  26. images = glob.glob(
  27. os.path.join(
  28. settings.MEDIA_ROOT, self.user.username, 'attachments', '*'))
  29. for image in images:
  30. os.remove(image)
  31. def test_importing_b1_and_b2(self):
  32. """
  33. b1 and b2 are from the *same phone* at different times. (this
  34. might not be a realistic test)
  35. b1:
  36. 1 photo survey (completed)
  37. 1 simple survey (not marked complete)
  38. b2:
  39. 1 photo survey (duplicate, completed)
  40. 1 simple survey (marked as complete)
  41. """
  42. # import from sd card
  43. initial_instance_count = Instance.objects.count()
  44. initial_image_count = images_count()
  45. import_instances_from_zip(os.path.join(
  46. DB_FIXTURES_PATH, "bulk_submission.zip"), self.user)
  47. instance_count = Instance.objects.count()
  48. image_count = images_count()
  49. #Images are not duplicated
  50. # TODO: Figure out how to get this test passing.
  51. self.assertEqual(image_count, initial_image_count + 2)
  52. # Instance count should have incremented
  53. # by 1 (or 2) based on the b1 & b2 data sets
  54. self.assertEqual(instance_count, initial_instance_count + 2)
  55. def test_badzipfile_import(self):
  56. total, success, errors = import_instances_from_zip(
  57. os.path.join(
  58. CUR_DIR, "Water_Translated_2011_03_10.xml"), self.user)
  59. self.assertEqual(total, 0)
  60. self.assertEqual(success, 0)
  61. expected_errors = [u'File is not a zip file']
  62. self.assertEqual(errors, expected_errors)