def patch_fee_storage(fee_storage: FeeStorage): memory_db = {} # noinspection PyUnusedLocal def put(context, key, value): memory_db[key] = value # noinspection PyUnusedLocal def put_deposit(context, deposit): memory_db[deposit.id] = deposit # noinspection PyUnusedLocal def get(context, key): return memory_db[key] if key in memory_db else None # noinspection PyUnusedLocal def delete(context, key): del memory_db[key] fee_storage.put_deposit_meta = put fee_storage.get_deposit_meta = get fee_storage.delete_deposit_meta = delete fee_storage.put_deposit = put_deposit fee_storage.get_deposit = get fee_storage.delete_deposit = delete
class TestFeeStorage(TestCase): def setUp(self): self.db_name = 'fee.db' self.address = create_address(AddressPrefix.EOA) db = ContextDatabase.from_path(self.db_name) self.assertIsNotNone(db) self.storage = FeeStorage(db) context = IconScoreContext(IconScoreContextType.DIRECT) context.tx_batch = TransactionBatch() context.block_batch = BlockBatch() self.context = context def tearDown(self): context = self.context self.storage.close(context) rmtree(self.db_name) def test_get_put_delete_score_fee(self): context = self.context score_address = create_address(AddressPrefix.CONTRACT) deposit_meta = DepositMeta() deposit_meta.head_id = create_tx_hash() deposit_meta.tail_id = create_tx_hash() deposit_meta.available_head_id_of_deposit = create_tx_hash() deposit_meta.available_head_id_of_virtual_step = create_tx_hash() self.storage.put_deposit_meta(context, score_address, deposit_meta) deposit_meta_2 = self.storage.get_deposit_meta(context, score_address) self.assertEqual(deposit_meta, deposit_meta_2) self.storage.delete_deposit_meta(context, score_address) deposit_meta_2 = self.storage.get_deposit_meta(context, score_address) self.assertIsNone(deposit_meta_2) def test_get_put_delete_score_fee_with_none_type(self): context = self.context score_address = create_address(AddressPrefix.CONTRACT) deposit_meta = DepositMeta() self.storage.put_deposit_meta(context, score_address, deposit_meta) deposit_meta_2 = self.storage.get_deposit_meta(context, score_address) self.assertEqual(deposit_meta, deposit_meta_2) self.storage.delete_deposit_meta(context, score_address) deposit_meta_2 = self.storage.get_deposit_meta(context, score_address) self.assertIsNone(deposit_meta_2) def test_get_put_delete_deposit(self): context = self.context deposit = Deposit() deposit.id = create_tx_hash() deposit.score_address = create_address(AddressPrefix.CONTRACT) deposit.sender = create_address(AddressPrefix.EOA) deposit.deposit_amount = 10000 deposit.deposit_used = 10000 deposit.created = 10 deposit.expires = 1000000 deposit.virtual_step_issued = 100000000000 deposit.virtual_step_used = 200000000000 deposit.prev_id = create_tx_hash() deposit.next_id = create_tx_hash() deposit.version = 2 self.storage.put_deposit(context, deposit) deposit2 = self.storage.get_deposit(context, deposit.id) self.assertEqual(deposit, deposit2) self.assertEqual(deposit.id, deposit2.id) self.storage.delete_deposit(context, deposit.id) deposit2 = self.storage.get_deposit(context, deposit.id) self.assertIsNone(deposit2) def test_get_put_delete_deposit_with_none_type(self): context = self.context deposit = Deposit() deposit.id = create_tx_hash() deposit.score_address = create_address(AddressPrefix.CONTRACT) deposit.sender = create_address(AddressPrefix.EOA) deposit.deposit_amount = 10000 deposit.deposit_used = 10000 deposit.created = 10 deposit.expires = 1000000 deposit.virtual_step_issued = 100000000000 deposit.virtual_step_used = 200000000000 deposit.version = 1 self.storage.put_deposit(context, deposit) deposit2 = self.storage.get_deposit(context, deposit.id) self.assertEqual(deposit, deposit2) self.assertEqual(deposit.id, deposit2.id) self.storage.delete_deposit(context, deposit.id) deposit2 = self.storage.get_deposit(context, deposit.id) self.assertIsNone(deposit2)