Exemplo n.º 1
0
 def test_flush(self, file):
     file.truncate(9)
     file.write(b'IS', 5)
     file.write(b'IS a nice test content.', 5)
     file.dirty = False
     file.version = 2
     vlob_id = '1234'
     content = b'This is a test content.'
     block_ids = ['4567', '5678', '6789']
     # Original content
     chunk_1 = content[:5]
     chunk_2 = content[5:14]
     chunk_3 = content[14:]
     blob = [{
         'blocks': [{
             'block': block_ids[0],
             'digest': digest(chunk_1),
             'size': len(chunk_1)
         }, {
             'block': block_ids[1],
             'digest': digest(chunk_2),
             'size': len(chunk_2)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000001>')
     }, {
         'blocks': [{
             'block': block_ids[2],
             'digest': digest(chunk_3),
             'size': len(chunk_3)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000002>')
     }]
     blob = ejson_dumps(blob).encode()
     blob = to_jsonb64(blob)
     # New content after truncate
     new_chuck_2 = b'is a'
     new_block_id = '7654'
     new_blob = [{
         'blocks': [{
             'block': block_ids[0],
             'digest': digest(chunk_1),
             'size': len(chunk_1)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000001>')
     }, {
         'blocks': [{
             'block': new_block_id,
             'digest': digest(new_chuck_2),
             'size': len(new_chuck_2)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000003>')
     }]
     new_blob = ejson_dumps(new_blob).encode()
     new_blob = to_jsonb64(new_blob)
     # New content after write
     new_block_2_id = '6543'
     new_chunk_4 = b'IS a nice test content.'
     new_blob_2 = [{
         'blocks': [{
             'block': block_ids[0],
             'digest': digest(chunk_1),
             'size': len(chunk_1)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000001>')
     }, {
         'blocks': [{
             'block': new_block_2_id,
             'digest': digest(new_chunk_4),
             'size': len(new_chunk_4)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000004>')
     }]
     new_blob_2 = ejson_dumps(new_blob_2).encode()
     new_blob_2 = to_jsonb64(new_blob_2)
     sequence = [
         (
             EVlobRead(vlob_id, '42', 2),  # Get blocks
             const({
                 'id': vlob_id,
                 'blob': blob,
                 'version': 2
             })),
         (
             EVlobRead(vlob_id, '42', 2),  # Matching blocks
             const({
                 'id': vlob_id,
                 'blob': blob,
                 'version': 2
             })),
         (EBlockRead(block_ids[1]),
          const({
              'content': to_jsonb64(chunk_2),
              'creation_date': '2012-01-01T00:00:00'
          })),
         (EBlockCreate(to_jsonb64(new_chuck_2)), const(new_block_id)),
         (EVlobUpdate(vlob_id, '43', 3, new_blob), noop),
         (
             EVlobRead(vlob_id, '42', 3),  # Matching blocks
             const({
                 'id': vlob_id,
                 'blob': new_blob,
                 'version': 3
             })),
         (EBlockCreate(to_jsonb64(new_chunk_4)), const(new_block_2_id)),
         (EVlobUpdate(vlob_id, '43', 3, new_blob_2), noop),
         (EVlobRead(vlob_id, '42', 3),
          const({
              'id': vlob_id,
              'blob': new_blob_2,
              'version': 3
          })),
         (EBlockDelete('5678'), conste(BlockNotFound('Block not found.'))),
         (EBlockDelete('6789'), noop),
     ]
     ret = perform_sequence(sequence, file.flush())
     assert ret is None
     assert file.dirty is True
     assert file.version == 2
Exemplo n.º 2
0
 def test_restore(self, file):
     vlob_id = '1234'
     content = b'This is a test content.'
     block_ids = ['4567', '5678', '6789']
     # Original content
     chunk_1 = content[:5]
     chunk_2 = content[5:14]
     chunk_3 = content[14:]
     blob = [{
         'blocks': [{
             'block': block_ids[0],
             'digest': digest(chunk_1),
             'size': len(chunk_1)
         }, {
             'block': block_ids[1],
             'digest': digest(chunk_2),
             'size': len(chunk_2)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000001>')
     }, {
         'blocks': [{
             'block': block_ids[2],
             'digest': digest(chunk_3),
             'size': len(chunk_3)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000002>')
     }]
     blob = ejson_dumps(blob).encode()
     blob = to_jsonb64(blob)
     # New content
     new_chuck_2 = b'is A test'
     new_block_id = '7654'
     new_blob = [{
         'blocks': [{
             'block': block_ids[0],
             'digest': digest(chunk_1),
             'size': len(chunk_1)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000001>')
     }, {
         'blocks': [{
             'block': new_block_id,
             'digest': digest(new_chuck_2),
             'size': len(new_chuck_2)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000003>')
     }, {
         'blocks': [{
             'block': block_ids[2],
             'digest': digest(chunk_3),
             'size': len(chunk_3)
         }],
         'key':
         to_jsonb64(b'<dummy-key-00000000000000000002>')
     }]
     new_blob = ejson_dumps(new_blob).encode()
     new_blob = to_jsonb64(new_blob)
     # Restore not commited file with version = 1
     file.dirty = False
     with pytest.raises(FileError):
         perform_sequence([], file.restore())
     assert file.dirty is False
     file.dirty = True
     # Restore commited file with version = 1
     file.dirty = False
     file.version = 1
     with pytest.raises(FileError):
         perform_sequence([], file.restore())
     assert file.dirty is False
     # Restore not commited file with version = current version
     file.dirty = True
     file.version = 5
     with pytest.raises(FileError):
         perform_sequence([], file.restore(6))
     assert file.dirty is True
     # Restore commited file with version = current version
     file.dirty = False
     file.version = 6
     with pytest.raises(FileError):
         perform_sequence([], file.restore(6))
     assert file.dirty is False
     # Restore previous version
     sequence = [
         (
             EVlobRead(vlob_id, '42', 6),  # Discard
             const({
                 'id': vlob_id,
                 'blob': blob,
                 'version': 6
             })),
         (EBlockDelete('4567'), conste(BlockNotFound('Block not found.'))),
         (EBlockDelete('5678'), noop),
         (EBlockDelete('6789'), noop),
         (EVlobDelete('1234'), noop),
         (EVlobRead('1234', '42', 5),
          const({
              'id': vlob_id,
              'blob': new_blob,
              'version': 5
          })),
         (EVlobUpdate(vlob_id, '43', 7, new_blob), noop)
     ]
     ret = perform_sequence(sequence, file.restore())
     assert ret is None
     assert file.dirty is True
     assert file.version == 6
     # Restore specific version
     sequence = [
         (EVlobRead(vlob_id, '42', 7),
          const({
              'id': vlob_id,
              'blob': new_blob,
              'version': 7
          })),
         (EBlockDelete('4567'), conste(BlockNotFound('Block not found.'))),
         (EBlockDelete('7654'), noop), (EBlockDelete('6789'), noop),
         (EVlobDelete('1234'), noop),
         (EVlobRead('1234', '42',
                    2), const({
                        'id': vlob_id,
                        'blob': blob,
                        'version': 2
                    })), (EVlobUpdate(vlob_id, '43', 7, blob), noop)
     ]
     ret = perform_sequence(sequence, file.restore(2))
     assert ret is None
     assert file.dirty is True
     assert file.version == 6