test_datacollector.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding: utf-8 -*-
  2. import os
  3. from django.core.files import File
  4. from misago.conf import settings
  5. from misago.users.datacollector import DataCollector
  6. from misago.users.testutils import AuthenticatedUserTestCase
  7. TESTFILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testfiles')
  8. TEST_AVATAR_PATH = os.path.join(TESTFILES_DIR, 'avatar.png')
  9. DATA_DOWNLOADS_WORKING_DIR = settings.MISAGO_USER_DATA_DOWNLOADS_WORKING_DIR
  10. class DataArchiverTests(AuthenticatedUserTestCase):
  11. def test_init_with_dirs(self):
  12. """data collector initializes with valid tmp directories"""
  13. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  14. self.assertTrue(os.path.exists(data_collector.tmp_dir_path))
  15. self.assertTrue(os.path.exists(data_collector.data_dir_path))
  16. data_downloads_working_dir = str(DATA_DOWNLOADS_WORKING_DIR)
  17. tmp_dir_path = str(data_collector.tmp_dir_path)
  18. data_dir_path = str(data_collector.data_dir_path)
  19. self.assertTrue(tmp_dir_path.startswith(data_downloads_working_dir))
  20. self.assertTrue(data_dir_path.startswith(data_downloads_working_dir))
  21. self.assertTrue(data_dir_path.startswith(data_downloads_working_dir))
  22. self.assertTrue(data_dir_path.startswith(tmp_dir_path))
  23. def test_write_data_file(self):
  24. """write_data_file creates new data file in data_dir_path"""
  25. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  26. data_to_write = {'hello': "I am test!", 'nice': u"łał!"}
  27. data_file_path = data_collector.write_data_file("testfile", data_to_write)
  28. self.assertTrue(os.path.isfile(data_file_path))
  29. valid_output_path = os.path.join(data_collector.data_dir_path, 'testfile.txt')
  30. self.assertEqual(data_file_path, valid_output_path)
  31. with open(data_file_path, 'r') as fp:
  32. saved_data = fp.read().strip().splitlines()
  33. self.assertEqual(saved_data, ["hello: I am test!", u"nice: łał!"])
  34. def test_write_model_file(self):
  35. """write_model_file includes model file in data_dir_path"""
  36. with open(TEST_AVATAR_PATH, 'rb') as avatar:
  37. self.user.avatar_tmp = File(avatar)
  38. self.user.save()
  39. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  40. file_path = data_collector.write_model_file(self.user.avatar_tmp)
  41. self.assertTrue(os.path.isfile(file_path))
  42. data_dir_path = str(data_collector.data_dir_path)
  43. self.assertTrue(str(file_path).startswith(data_dir_path))
  44. def test_write_model_file_empty(self):
  45. """write_model_file is noop if model file field is none"""
  46. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  47. file_path = data_collector.write_model_file(self.user.avatar_tmp)
  48. self.assertIsNone(file_path)
  49. self.assertFalse(os.listdir(data_collector.data_dir_path))
  50. def test_create_collection(self):
  51. """create_collection creates new directory for collection"""
  52. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  53. collection = data_collector.create_collection('collection')
  54. data_dir_path = str(data_collector.data_dir_path)
  55. collection_dir_path = str(collection.data_dir_path)
  56. self.assertNotEqual(data_dir_path, collection_dir_path)
  57. self.assertTrue(collection_dir_path.startswith(data_dir_path))
  58. self.assertTrue(os.path.exists(collection.data_dir_path))
  59. def test_collection_write_data_file(self):
  60. """write_data_file creates new data file in collection data_dir_path"""
  61. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  62. collection = data_collector.create_collection('collection')
  63. data_to_write = {'hello': "I am test!", 'nice': u"łał!"}
  64. data_file_path = collection.write_data_file("testfile", data_to_write)
  65. self.assertTrue(os.path.isfile(data_file_path))
  66. valid_output_path = os.path.join(collection.data_dir_path, 'testfile.txt')
  67. self.assertEqual(data_file_path, valid_output_path)
  68. with open(data_file_path, 'r') as fp:
  69. saved_data = fp.read().strip().splitlines()
  70. self.assertEqual(saved_data, ["hello: I am test!", u"nice: łał!"])
  71. def test_collection_write_model_file(self):
  72. """write_model_file includes model file in collection data_dir_path"""
  73. with open(TEST_AVATAR_PATH, 'rb') as avatar:
  74. self.user.avatar_tmp = File(avatar)
  75. self.user.save()
  76. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  77. collection = data_collector.create_collection('collection')
  78. file_path = collection.write_model_file(self.user.avatar_tmp)
  79. self.assertTrue(os.path.isfile(file_path))
  80. data_dir_path = str(collection.data_dir_path)
  81. self.assertTrue(str(file_path).startswith(data_dir_path))
  82. def test_collection_write_model_file_empty(self):
  83. """write_model_file is noop if model file field is none"""
  84. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  85. collection = data_collector.create_collection('collection')
  86. file_path = collection.write_model_file(self.user.avatar_tmp)
  87. self.assertIsNone(file_path)
  88. self.assertFalse(os.listdir(collection.data_dir_path))
  89. def test_create_archive(self):
  90. """create_archive creates zip file from collected data"""
  91. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  92. data_to_write = {'hello': "I am test!", 'nice': u"łał!"}
  93. data_collector.write_data_file("testfile", data_to_write)
  94. with open(TEST_AVATAR_PATH, 'rb') as avatar:
  95. self.user.avatar_tmp = File(avatar)
  96. self.user.save()
  97. data_collector.write_model_file(self.user.avatar_tmp)
  98. archive_path = data_collector.create_archive()
  99. self.assertEqual(data_collector.archive_path, archive_path)
  100. self.assertTrue(os.path.isfile(archive_path))
  101. def test_delete_archive(self):
  102. """delete_archive deletes zip file"""
  103. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  104. archive_path = data_collector.create_archive()
  105. data_collector.delete_archive()
  106. self.assertFalse(os.path.isfile(archive_path))
  107. def test_delete_archive_none(self):
  108. """delete_archive is noop if zip file doesnt exist"""
  109. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  110. self.assertIsNone(data_collector.archive_path)
  111. data_collector.delete_archive()
  112. def test_delete_tmp_dir(self):
  113. """delete_tmp_dir delete's directory but leaves archive"""
  114. data_collector = DataCollector(self.user, DATA_DOWNLOADS_WORKING_DIR)
  115. tmp_dir_path = data_collector.tmp_dir_path
  116. data_collector.delete_tmp_dir()
  117. self.assertFalse(os.path.exists(tmp_dir_path))