def test_initialise(self): initialise_database('test_database_file') with self.assertRaisesRegex(pmpi.database.Database.InitialisationError, "close opened database first"): initialise_database('test_database_file2') with self.assertRaises(OSError): os.remove('test_database_file2') close_database() initialise_database('test_database_file2') close_database() with self.assertRaisesRegex(pmpi.database.Database.InitialisationError, "there is no database to close"): close_database() os.remove('test_database_file') os.remove('test_database_file2')
def tearDown(self): close_database() os.remove('test_database_file')
def test(): initialise_database('test_database_file') private_keys = [SigningKey.generate() for _ in range(3)] public_keys = [PublicKey.from_signing_key(private_key) for private_key in private_keys] uuids = [uuid4() for _ in range(3)] # BUILD # blocks = add_blocks() # # for block in blocks: # block.put() # # block_chain = BlockChain() # # block_chain_records_pattern = [ # BlockChain.Record(1, b'\x00'*32, [blocks[1].id]), # BlockChain.Record(2, blocks[0].id, [blocks[2].id]), # BlockChain.Record(3, blocks[1].id, sorted([blocks[3].id, blocks[4].id])), # BlockChain.Record(4, blocks[2].id, [blocks[5].id]), # BlockChain.Record(4, blocks[2].id, [blocks[6].id]), # BlockChain.Record(5, blocks[3].id, []), # BlockChain.Record(5, blocks[4].id, []) # ] # # for i in range(6): # assertEqual(block_chain.get(blocks[i].id), block_chain_records_pattern[i]) # UPDATE BLOCKS # blocks = add_blocks() # bc = get_blockchain() # # with patch.object(BlockChain, '_get_new_blocks', return_value=blocks): # bc.update_blocks() # # assertEqual(bc.max_depth, 5) # assertEqual(bc.head, blocks[5].id) # # for block in blocks: # assertEqual(bc.get(block.id).previous_id, block.previous_block_rev.id) # MULTIPLE UPDATE BLOCKS # blocks = add_blocks() # bc = get_blockchain() # # def patched_update_blocks(block_list): # with patch.object(BlockChain, '_get_new_blocks', return_value=block_list): # bc.update_blocks() # # for blocks_to_add, max_depth, head in ( # (blocks[0:2], 2, blocks[1].id), # (blocks[2:3], 3, blocks[2].id), # (blocks[4:5], 4, blocks[4].id), # (blocks[3:4], 4, blocks[4].id), # (blocks[5:6], 5, blocks[5].id), # (blocks[6:7], 5, blocks[5].id) # ): # patched_update_blocks(blocks_to_add) # assertEqual(bc.max_depth, max_depth) # assertEqual(bc.head, head) # DELETE blocks = add_blocks(uuids, private_keys, public_keys) bc = get_blockchain() with patch.object(BlockChain, '_get_new_blocks', return_value=blocks): bc.update_blocks() try: blocks[4].remove() raise AssertionError except Block.ChainOperationBlockedError: pass assert bc.max_depth == 5 assert bc.head == blocks[5].id for block_to_remove, max_depth, heads in ( (blocks[5], 5, [blocks[6].id]), (blocks[6], 4, [blocks[3].id, blocks[4].id]), (blocks[4], 4, [blocks[3].id]), (blocks[3], 3, [blocks[2].id]), (blocks[2], 2, [blocks[1].id]), (blocks[1], 1, [blocks[0].id]), (blocks[0], 0, [BlockRev().id]) ): block_to_remove.remove() assert bc.max_depth == max_depth assert bc.head in heads with patch.object(BlockChain, '_get_new_blocks', return_value=blocks): bc.update_blocks() assert bc.max_depth == 5 assert bc.head == blocks[5].id assert sorted([op.uuid for op in blocks[0].operations + blocks[2].operations[1:2]]) == sorted( Identifier.get_uuid_list()) close_database() os.remove('test_database_file')
def test(): try: os.remove('test_database_file') except OSError: pass initialise_database('test_database_file') obj_private_key = SigningKey.generate() obj_public_key = PublicKey.from_signing_key(obj_private_key) obj_uuids = [uuid4() for _ in range(3)] obj_operations = [[ Operation(OperationRev(), 'http://example0.com/v0/', [obj_public_key]), Operation(OperationRev(), 'http://example1.com/v0/', [obj_public_key]) ]] for op in obj_operations[0]: sign_object(obj_public_key, obj_private_key, op) obj_operations.append([ Operation(OperationRev.from_obj(obj_operations[0][0]), 'http://example0.com/v1/', [obj_public_key]), Operation(OperationRev.from_obj(obj_operations[0][1]), 'http://example1.com/v1/', [obj_public_key]), Operation(OperationRev(), 'http://example2.com/v0/', [obj_public_key]) ]) for op in obj_operations[1]: sign_object(obj_public_key, obj_private_key, op) obj_operations.append([ Operation(OperationRev.from_obj(obj_operations[1][0]), 'http://example0.com/v2/', [obj_public_key]), Operation(OperationRev.from_obj(obj_operations[1][1]), 'http://example1.com/v2/', [obj_public_key]) ]) for op in obj_operations[2]: sign_object(obj_public_key, obj_private_key, op) obj_operations.append([ Operation(OperationRev.from_obj(obj_operations[1][1]), 'http://alternative1.com/', [obj_public_key]), Operation(OperationRev.from_obj(obj_operations[1][2]), 'http://alternative2.com/', [obj_public_key]) ]) for op in obj_operations[3]: sign_object(obj_public_key, obj_private_key, op) timestamp = int(time.time()) - 100 obj_blocks = [Block.from_operations_list(BlockRev(), timestamp, obj_operations[0])] obj_blocks[0].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[0]) obj_blocks.append( Block.from_operations_list(BlockRev.from_obj(obj_blocks[0]), timestamp + 20, obj_operations[1])) obj_blocks[1].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[1]) obj_blocks.append( Block.from_operations_list(BlockRev.from_obj(obj_blocks[1]), timestamp + 40, obj_operations[2])) obj_blocks[2].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[2]) obj_blocks.append( Block.from_operations_list(BlockRev.from_obj(obj_blocks[1]), timestamp + 60, obj_operations[3])) obj_blocks[3].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[3]) for block in obj_blocks: block.put() for block in obj_blocks: new_block = Block.get(block.id) assert new_block.id == block.id for block in obj_blocks[:2]: try: block.remove() raise AssertionError except Block.ChainOperationBlockedError: pass assert sorted(Block.get_ids_list()) == sorted([block.id for block in obj_blocks]) obj_blocks[2].remove() for block in obj_blocks[:2]: try: block.remove() raise AssertionError except Block.ChainOperationBlockedError: pass obj_blocks[3].remove() try: obj_blocks[0].remove() raise AssertionError except Block.ChainOperationBlockedError: pass obj_blocks[1].remove() obj_blocks[0].remove() for block in obj_blocks: try: block.remove() raise AssertionError except Block.DoesNotExist: pass assert Block.get_ids_list() == [] close_database() os.remove('test_database_file')