export_test.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. '''Test exporting.'''
  2. import functools
  3. import lzma
  4. import os.path
  5. import time
  6. import unittest
  7. import zipfile
  8. from terroroftinytown.test.random_result import MockResult, MockProject
  9. from terroroftinytown.tracker.export import ExporterBootstrap, Exporter
  10. class TestExport(unittest.TestCase):
  11. def test_beacon(self):
  12. config_path = os.path.join(os.path.dirname(__file__), 'tracker_unittest.conf')
  13. project_boot = MockProject(delete_everything='yes-really!')
  14. project_boot.start(
  15. args=[config_path, '--count', '10'],
  16. )
  17. shortcode_boot = MockResult()
  18. shortcode_boot.start(
  19. args=[config_path, '--count', '100000', '--projects', '10'],
  20. )
  21. export_dir = '/tmp/tinytown_test_export{0}/'.format(int(time.time()))
  22. boot = ExporterBootstrap()
  23. args = [
  24. config_path, '--format', 'beacon',
  25. '--include-settings', '--zip',
  26. '--dir-length', '0', '--file-length', '0', '--max-right', '8',
  27. '--delete',
  28. export_dir,
  29. ]
  30. boot.start(args=args)
  31. count = 0
  32. for filename in os.listdir(export_dir):
  33. print('filename', filename)
  34. with zipfile.ZipFile(os.path.join(export_dir, filename), 'r') as zip_file:
  35. for name in zip_file.namelist():
  36. print(' name', name)
  37. if not name.endswith('.txt.xz'):
  38. continue
  39. in_file = zip_file.open(name)
  40. is_prefix = False
  41. for line in lzma.LZMAFile(in_file):
  42. if line.startswith(b'#'):
  43. if b'PREFIX' in line:
  44. is_prefix = True
  45. # Check if "{shortcode}" was accidentally replaced out
  46. self.assertNotIn(b'//', line.replace(b'http://', b''))
  47. continue
  48. line = line.strip()
  49. if line:
  50. count += 1
  51. self.assertGreaterEqual(line.index(b'|'), 1)
  52. if is_prefix:
  53. self.assertFalse(line.startswith(b'http'))
  54. else:
  55. self.assertTrue(line.startswith(b'http'))
  56. in_file.close()
  57. self.assertEqual(100000, count)
  58. def test_split_shortcode(self):
  59. split = functools.partial(
  60. Exporter.split_shortcode,
  61. dir_length=2, max_right=4, file_length=2)
  62. self.assertEqual(
  63. ([], '', 'a'),
  64. split('a')
  65. )
  66. self.assertEqual(
  67. ([], '', 'ab'),
  68. split('ab')
  69. )
  70. self.assertEqual(
  71. ([], '', 'abc'),
  72. split('abc')
  73. )
  74. self.assertEqual(
  75. ([], '', 'abcd'),
  76. split('abcd')
  77. )
  78. self.assertEqual(
  79. ([], 'a', 'bcde'),
  80. split('abcde')
  81. )
  82. self.assertEqual(
  83. ([], 'ab', 'cdef'),
  84. split('abcdef')
  85. )
  86. self.assertEqual(
  87. (['ab'], 'abc', 'defg'),
  88. split('abcdefg')
  89. )
  90. self.assertEqual(
  91. (['ab'], 'abcd', 'efgh'),
  92. split('abcdefgh')
  93. )
  94. self.assertEqual(
  95. (['ab', 'cd'], 'abcde', 'fghi'),
  96. split('abcdefghi')
  97. )
  98. self.assertEqual(
  99. (['ab', 'cd'], 'abcdef', 'ghij'),
  100. split('abcdefghij')
  101. )
  102. self.assertEqual(
  103. (['ab', 'cd', 'ef'], 'abcdefg', 'hijk'),
  104. split('abcdefghijk')
  105. )
  106. def test_split_shortcode_2(self):
  107. split = functools.partial(
  108. Exporter.split_shortcode,
  109. dir_length=1, max_right=6, file_length=1)
  110. self.assertEqual(
  111. ([], '', 'a'),
  112. split('a')
  113. )
  114. self.assertEqual(
  115. ([], '', 'ab'),
  116. split('ab')
  117. )
  118. self.assertEqual(
  119. ([], '', 'abc'),
  120. split('abc')
  121. )
  122. self.assertEqual(
  123. ([], '', 'abcd'),
  124. split('abcd')
  125. )
  126. self.assertEqual(
  127. ([], '', 'abcde'),
  128. split('abcde')
  129. )
  130. self.assertEqual(
  131. ([], '', 'abcdef'),
  132. split('abcdef')
  133. )
  134. self.assertEqual(
  135. ([], 'a', 'bcdefg'),
  136. split('abcdefg')
  137. )
  138. self.assertEqual(
  139. (['a'], 'ab', 'cdefgh'),
  140. split('abcdefgh')
  141. )
  142. self.assertEqual(
  143. (['a', 'b'], 'abc', 'defghi'),
  144. split('abcdefghi')
  145. )
  146. self.assertEqual(
  147. (['a', 'b', 'c'], 'abcd', 'efghij'),
  148. split('abcdefghij')
  149. )
  150. self.assertEqual(
  151. (['a', 'b', 'c', 'd'], 'abcde', 'fghijk'),
  152. split('abcdefghijk')
  153. )
  154. def test_split_shortcode_3(self):
  155. split = functools.partial(
  156. Exporter.split_shortcode,
  157. dir_length=0, max_right=6, file_length=1)
  158. self.assertEqual(
  159. ([], '', 'a'),
  160. split('a')
  161. )
  162. self.assertEqual(
  163. ([], 'abcde', 'fghijk'),
  164. split('abcdefghijk')
  165. )
  166. def test_split_shortcode_4(self):
  167. split = functools.partial(
  168. Exporter.split_shortcode,
  169. dir_length=0, max_right=6, file_length=0)
  170. self.assertEqual(
  171. ([], '', 'a'),
  172. split('a')
  173. )
  174. self.assertEqual(
  175. ([], 'abcde', 'fghijk'),
  176. split('abcdefghijk')
  177. )
  178. def test_split_shortcode_5(self):
  179. split = functools.partial(
  180. Exporter.split_shortcode,
  181. dir_length=0, max_right=0, file_length=0)
  182. self.assertEqual(
  183. ([], 'a', ''),
  184. split('a')
  185. )
  186. self.assertEqual(
  187. ([], 'abcdefghijk', ''),
  188. split('abcdefghijk')
  189. )