<s> import json <EOL> import mock <EOL> import struct <EOL> import unittest <EOL> from shoebox import disk_storage <EOL> class TestVersion0 ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . v0 = disk_storage . Version0 ( ) <EOL> def test_make_preamble ( self ) : <EOL> self . assertEqual ( <NUM_LIT:6> , len ( self . v0 . make_preamble ( <NUM_LIT> ) ) ) <EOL> def test_load_preamble_bad_bor ( self ) : <EOL> file_handle = mock . Mock ( ) <EOL> file_handle . read . return_value = "<STR_LIT>" <EOL> self . assertRaises ( disk_storage . OutOfSync , self . v0 . load_preamble , <EOL> file_handle ) <EOL> def test_load_preamble ( self ) : <EOL> file_handle = mock . Mock ( ) <EOL> file_handle . read . return_value = struct . pack ( <EOL> "<STR_LIT>" , <EOL> disk_storage . BOR_MAGIC_NUMBER , <EOL> <NUM_LIT> ) <EOL> self . assertEqual ( <NUM_LIT> , self . v0 . load_preamble ( file_handle ) ) <EOL> class TestVersion1 ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . v1 = disk_storage . Version1 ( ) <EOL> def test_no_metadata ( self ) : <EOL> metadata = { } <EOL> payload = "<STR_LIT>" <EOL> package = self . v1 . pack ( payload , metadata ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( package ) ) <EOL> self . assertEqual ( <NUM_LIT:12> , len ( package [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( package [ <NUM_LIT:2> ] ) ) <EOL> self . assertEqual ( "<STR_LIT>" , package [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( <NUM_LIT:11> , len ( package [ <NUM_LIT:3> ] ) ) <EOL> def test_empty_payload ( self ) : <EOL> metadata = { "<STR_LIT:key>" : "<STR_LIT:value>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> payload = "<STR_LIT>" <EOL> package = self . v1 . pack ( payload , metadata ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( package ) ) <EOL> self . assertEqual ( <NUM_LIT:12> , len ( package [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( <NUM_LIT> , len ( package [ <NUM_LIT:2> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( package [ <NUM_LIT:3> ] ) ) <EOL> self . assertEqual ( "<STR_LIT>" , package [ <NUM_LIT:3> ] ) <EOL> def test_unpack_happy_day ( self ) : <EOL> metadata = { "<STR_LIT:key>" : "<STR_LIT:value>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> payload = { "<STR_LIT>" : <NUM_LIT> } <EOL> jpayload = json . dumps ( payload ) <EOL> blocks = self . v1 . pack ( jpayload , metadata ) <EOL> blocks = blocks [ <NUM_LIT:1> : ] <EOL> file_handle = mock . Mock ( ) <EOL> file_handle . read . side_effect = blocks <EOL> m , jp = self . v1 . unpack ( file_handle ) <EOL> p = json . loads ( jp ) <EOL> self . assertEqual ( metadata , m ) <EOL> self . assertEqual ( payload , p ) <EOL> def test_unpack_bad_eor ( self ) : <EOL> metadata = { "<STR_LIT:key>" : "<STR_LIT:value>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> payload = { "<STR_LIT>" : <NUM_LIT> } <EOL> jpayload = json . dumps ( payload ) <EOL> blocks = self . v1 . pack ( jpayload , metadata ) <EOL> blocks = blocks [ <NUM_LIT:1> : ] <EOL> print ( len ( blocks [ <NUM_LIT:0> ] ) ) <EOL> newblock = blocks [ <NUM_LIT:0> ] [ : <NUM_LIT:8> ] + '<STR_LIT>' <EOL> blocks = list ( blocks ) <EOL> blocks [ <NUM_LIT:0> ] = newblock <EOL> blocks = tuple ( blocks ) <EOL> file_handle = mock . Mock ( ) <EOL> file_handle . read . side_effect = blocks <EOL> self . assertRaises ( disk_storage . OutOfSync , self . v1 . unpack , file_handle ) <EOL> class TestHelpers ( unittest . TestCase ) : <EOL> def test_get_version_handler_bad ( self ) : <EOL> self . assertRaises ( disk_storage . InvalidVersion , <EOL> disk_storage . get_version_handler , <NUM_LIT> ) <EOL> def test_get_version_handler ( self ) : <EOL> self . assertTrue ( isinstance ( disk_storage . get_version_handler ( <NUM_LIT:1> ) , <EOL> disk_storage . Version1 ) ) <EOL> self . assertTrue ( isinstance ( disk_storage . get_version_handler ( ) , <EOL> disk_storage . Version1 ) ) <EOL> def test_pack_notification ( self ) : <EOL> with mock . patch ( '<STR_LIT>' ) as h : <EOL> fake_handler = mock . Mock ( ) <EOL> h . return_value = fake_handler <EOL> disk_storage . pack_notification ( "<STR_LIT>" , { } ) <EOL> self . assertTrue ( fake_handler . pack . called ) <EOL> def test_unpack_notification ( self ) : <EOL> file_handle = mock . Mock ( ) <EOL> file_handle . read . return_value = struct . pack ( <EOL> "<STR_LIT>" , <EOL> disk_storage . BOR_MAGIC_NUMBER , <EOL> <NUM_LIT> ) <EOL> with mock . patch ( '<STR_LIT>' ) as h : <EOL> fake_handler = mock . Mock ( ) <EOL> h . return_value = fake_handler <EOL> disk_storage . unpack_notification ( file_handle ) <EOL> h . assert_called_with ( <NUM_LIT> ) <EOL> self . assertTrue ( fake_handler . unpack . called ) </s>
