def change_cards(account_id, operation_type, to_add=(), to_remove=(), storage=relations.STORAGE.FAST): operations = [] for card in to_remove: operations.append( storage_pb2.Operation(destroy=storage_pb2.OperationDestroy( item_id=card.uid.hex, owner_id=account_id, operation_type=operation_type))) for card in to_add: operations.append( storage_pb2.Operation(create=storage_pb2.OperationCreate( item_id=card.uid.hex, owner_id=account_id, storage_id=storage.value, base_type=card.item_base_type, full_type=card.item_full_type, data=s11n.to_json(card.serialize()), operation_type=operation_type))) tt_api.sync_request(url=conf.settings.TT_STORAGE_APPLY_URL, data=storage_pb2.ApplyRequest(operations=operations), AnswerType=storage_pb2.ApplyResponse)
async def test_failed_operations(self): request = await self.client.post('/apply', data=storage_pb2.ApplyRequest(operations=[self.operations[2]]).SerializeToString()) await self.check_answer(request, storage_pb2.ApplyResponse) request = await self.client.post('/apply', data=storage_pb2.ApplyRequest(operations=self.operations).SerializeToString()) await self.check_answer(request, storage_pb2.ApplyResponse, api_status=base_pb2.ApiResponse.ERROR) result = await db.sql('SELECT * FROM items ORDER BY updated_at DESC') self.assertEqual(set((row['id'], row['owner']) for row in result), {(self.item_3_id, 3)}) result = await db.sql('SELECT * FROM log_records ORDER BY created_at ASC') self.assertEqual([(row['item'], row['data']) for row in result], [(self.item_3_id, {'item': {'c': 3}, 'operation_type': '#test', 'owner_id': 3, 'storage_id': 3, 'base_type': 'base-type', 'full_type': 'full-type'})])
async def test_success(self): request = await self.client.post('/apply', data=storage_pb2.ApplyRequest(operations=self.operations).SerializeToString()) await self.check_answer(request, storage_pb2.ApplyResponse) result = await db.sql('SELECT * FROM items ORDER BY updated_at DESC') self.assertEqual(set((row['id'], row['owner']) for row in result), {(self.item_1_id, 1), (self.item_2_id, 3)}) result = await db.sql('SELECT * FROM log_records ORDER BY created_at ASC') self.assertEqual([(row['item'], row['data']) for row in result], [(self.item_1_id, {'item': {'a': 1}, 'operation_type': '#test', 'owner_id': 1, 'storage_id': 1, 'base_type': 'base-type', 'full_type': 'full-type'}), (self.item_2_id, {'item': {'b': 2}, 'operation_type': '#test', 'owner_id': 2, 'storage_id': 2, 'base_type': 'base-type', 'full_type': 'full-type'}), (self.item_3_id, {'item': {'c': 3}, 'operation_type': '#test', 'owner_id': 3, 'storage_id': 3, 'base_type': 'base-type', 'full_type': 'full-type'}), (self.item_2_id, {'operation_type': '#test', 'new_owner_id': 3, 'old_owner_id': 2, 'new_storage_id': 4}), (self.item_3_id, {'operation_type': '#test', 'owner_id': 3})])
def change_cards_storage(account_id, operation_type, cards, old_storage, new_storage): operations = [] for card in cards: operations.append( storage_pb2.Operation( change_storage=storage_pb2.OperationChangeStorage( item_id=card.uid.hex, owner_id=account_id, old_storage_id=old_storage.value, new_storage_id=new_storage.value, operation_type=operation_type))) tt_api.sync_request(url=conf.settings.TT_STORAGE_APPLY_URL, data=storage_pb2.ApplyRequest(operations=operations), AnswerType=storage_pb2.ApplyResponse)
async def test_no_operations(self): request = await self.client.post('/apply', data=storage_pb2.ApplyRequest(operations=[]).SerializeToString()) await self.check_answer(request, storage_pb2.ApplyResponse)
async def fill_database(self): request = await self.client.post('/apply', data=storage_pb2.ApplyRequest(operations=self.operations).SerializeToString()) await self.check_answer(request, storage_pb2.ApplyResponse)