test.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import unittest
  2. import zipfly
  3. import os
  4. import string
  5. import random
  6. def rs(N):
  7. return ''.join(random.SystemRandom().choice(
  8. string.ascii_uppercase + \
  9. string.ascii_lowercase + \
  10. string.digits) for _ in range(N))
  11. def pick_n():
  12. return int(''.join(random.SystemRandom().choice(string.digits) for _ in range(3)))
  13. p1 = os.getcwd()
  14. p2 = p1
  15. p3 = p1
  16. """
  17. idx1 = p1.rfind("/zipfly")
  18. p1 = p1[0:idx1]
  19. p1 = p1+"/dist/"
  20. idx2 = p2.rfind("/zipfly")
  21. p2 = p2[0:idx2]
  22. p2 = p2+"/zipfly.egg-info/"
  23. idx3 = p3.rfind("/zipfly")
  24. p3 = p3[0:idx3]
  25. p3 = p3+"/examples/"
  26. """
  27. p1 = p1+"/dist/"
  28. p2 = p2+"/zipfly.egg-info/"
  29. p3 = p3+"/examples/"
  30. paths1 = []
  31. for dirpath, dnames, fnames in os.walk(p1):
  32. for f in fnames:
  33. paths1.append(
  34. {
  35. 'fs':'{}{}'.format(p1,f),
  36. 'n':'{}{}'.format(p1,f),
  37. }
  38. )
  39. for dirpath, dnames, fnames in os.walk(p2):
  40. for f in fnames:
  41. paths1.append(
  42. {
  43. 'fs':'{}{}'.format(p2,f),
  44. 'n':'{}{}'.format(p2,f),
  45. }
  46. )
  47. for dirpath, dnames, fnames in os.walk(p3):
  48. for f in fnames:
  49. paths1.append(
  50. {
  51. 'fs':'{}{}'.format(p3,f),
  52. 'n':'{}{}'.format(p3,f),
  53. }
  54. )
  55. class TestBufferPredictionSize(unittest.TestCase):
  56. def test_buffer_prediction_size(self):
  57. print (
  58. """
  59. TEST IF REAL ZIP SIZE IS EQUAL TO PREDICTION SIZE
  60. # # # # # # # # # # # # # # # # # # # # # # # # #
  61. """
  62. )
  63. for test_n in range(1, 50):
  64. with self.subTest(i=test_n):
  65. storesize = 0
  66. for path in paths1:
  67. f = open(path['fs'], 'rb')
  68. storesize += os.fstat(f.fileno()).st_size
  69. f.close()
  70. zfly = zipfly.ZipFly( paths = paths1, storesize = storesize )
  71. zfly.set_comment(rs(pick_n()))
  72. # zip size before creating it in bytes
  73. ps = zfly.buffer_prediction_size()
  74. with open("test{}.zip".format(test_n), "wb") as f:
  75. for i in zfly.generator():
  76. f.write(i)
  77. f = open("test{}.zip".format(test_n), 'rb')
  78. zs = os.fstat(f.fileno()).st_size
  79. f.close()
  80. # fetch
  81. print (
  82. "test-{}.zip ->".format(test_n),
  83. "{} KB".format(round(zs/1024,2)),
  84. "({} bytes)".format(zs),
  85. (" ---- OK" if zs==ps else " ---- FAIL")
  86. )
  87. self.assertEqual(zs,ps)
  88. if __name__ == '__main__':
  89. unittest.main()