test_frame_6.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import pytest
  3. import lz4.frame as lz4frame
  4. test_data = [
  5. b'',
  6. (128 * (32 * os.urandom(32))),
  7. (5 * 128 * os.urandom(1024)),
  8. ]
  9. @pytest.fixture(
  10. params=test_data,
  11. ids=[
  12. 'data' + str(i) for i in range(len(test_data))
  13. ]
  14. )
  15. def data(request):
  16. return request.param
  17. compression_levels = [
  18. (lz4frame.COMPRESSIONLEVEL_MIN),
  19. # (lz4frame.COMPRESSIONLEVEL_MINHC),
  20. # (lz4frame.COMPRESSIONLEVEL_MAX),
  21. ]
  22. @pytest.fixture(
  23. params=compression_levels
  24. )
  25. def compression_level(request):
  26. return request.param
  27. def test_lz4frame_open_write(data):
  28. with lz4frame.open('testfile', mode='wb') as fp:
  29. fp.write(data)
  30. def test_lz4frame_open_write_read_defaults(data):
  31. with lz4frame.open('testfile', mode='wb') as fp:
  32. fp.write(data)
  33. with lz4frame.open('testfile', mode='r') as fp:
  34. data_out = fp.read()
  35. assert data_out == data
  36. def test_lz4frame_open_write_read_text():
  37. data = u'This is a test string'
  38. with lz4frame.open('testfile', mode='wt') as fp:
  39. fp.write(data)
  40. with lz4frame.open('testfile', mode='rt') as fp:
  41. data_out = fp.read()
  42. assert data_out == data
  43. def test_lz4frame_open_write_read_text_iter():
  44. data = u'This is a test string'
  45. with lz4frame.open('testfile', mode='wt') as fp:
  46. fp.write(data)
  47. data_out = ''
  48. with lz4frame.open('testfile', mode='rt') as fp:
  49. for line in fp:
  50. data_out += line
  51. assert data_out == data
  52. def test_lz4frame_open_write_read(
  53. data,
  54. compression_level,
  55. block_linked,
  56. block_checksum,
  57. block_size,
  58. content_checksum,
  59. auto_flush,
  60. store_size,
  61. return_bytearray):
  62. kwargs = {}
  63. if store_size is True:
  64. kwargs['source_size'] = len(data)
  65. kwargs['compression_level'] = compression_level
  66. kwargs['block_size'] = block_size
  67. kwargs['block_linked'] = block_linked
  68. kwargs['content_checksum'] = content_checksum
  69. kwargs['block_checksum'] = block_checksum
  70. kwargs['auto_flush'] = auto_flush
  71. kwargs['return_bytearray'] = return_bytearray
  72. kwargs['mode'] = 'wb'
  73. with lz4frame.open('testfile', **kwargs) as fp:
  74. fp.write(data)
  75. with lz4frame.open('testfile', mode='r') as fp:
  76. data_out = fp.read()
  77. assert data_out == data