def test_ledger_action_can_be_edited(database, function_scope_messages_aggregator): db = DBLedgerActions(database, function_scope_messages_aggregator) query = 'SELECT * FROM ledger_actions WHERE identifier=?' cursor = database.conn.cursor() # Add the entry that we want to edit action = LedgerAction( identifier=0, # whatever timestamp=1, action_type=LedgerActionType.INCOME, location=Location.EXTERNAL, amount=FVal(1), asset=A_ETH, rate=None, rate_asset=None, link=None, notes=None, ) identifier = db.add_ledger_action(action) # Data for the new entry new_entry = LedgerAction( identifier=identifier, timestamp=2, action_type=LedgerActionType.GIFT, location=Location.EXTERNAL, amount=FVal(3), asset=A_ETH, rate=FVal(100), rate_asset=A_USD, link='foo', notes='updated', ) assert db.edit_ledger_action(new_entry) is None # Check that changes have been committed cursor.execute(query, (identifier, )) updated_entry = LedgerAction.deserialize_from_db(cursor.fetchone()) new_entry.identifier = identifier assert updated_entry == new_entry # now try to see if the optional assets can also be set to None new_entry.rate = new_entry.rate_asset = new_entry.link = new_entry.notes = None assert db.edit_ledger_action(new_entry) is None cursor.execute(query, (identifier, )) updated_entry = LedgerAction.deserialize_from_db(cursor.fetchone()) assert updated_entry.rate is None assert updated_entry.rate_asset is None assert updated_entry.link is None assert updated_entry.notes is None
def get_ledger_actions( self, filter_query: LedgerActionsFilterQuery, has_premium: bool, ) -> List[LedgerAction]: """Returns a list of ledger actions optionally filtered by the given filter. Returned list is ordered according to the passed filter query """ cursor = self.db.conn.cursor() query_filter, bindings = filter_query.prepare() if has_premium: query = 'SELECT * from ledger_actions ' + query_filter results = cursor.execute(query, bindings) else: query = 'SELECT * FROM (SELECT * from ledger_actions ORDER BY timestamp DESC LIMIT ?) ' + query_filter # noqa: E501 results = cursor.execute(query, [FREE_LEDGER_ACTIONS_LIMIT] + bindings) original_query = 'SELECT identifier from ledger_actions ' + query_filter gitcoin_query = f'SELECT * from ledger_actions_gitcoin_data WHERE parent_id IN ({original_query});' # noqa: E501 cursor2 = self.db.conn.cursor() gitcoin_results = cursor2.execute(gitcoin_query, bindings) gitcoin_map = {x[0]: x for x in gitcoin_results} actions = [] for result in results: try: action = LedgerAction.deserialize_from_db(result, gitcoin_map) except DeserializationError as e: self.msg_aggregator.add_error( f'Error deserializing Ledger Action from the DB. Skipping it.' f'Error was: {str(e)}', ) continue except UnknownAsset as e: self.msg_aggregator.add_error( f'Error deserializing Ledger Action from the DB. Skipping it. ' f'Unknown asset {e.asset_name} found', ) continue actions.append(action) return actions
def get_ledger_actions( self, from_ts: Optional[Timestamp], to_ts: Optional[Timestamp], location: Optional[Location], ) -> List[LedgerAction]: cursor = self.db.conn.cursor() query = ('SELECT identifier,' ' timestamp,' ' type,' ' location,' ' amount,' ' asset,' ' rate,' ' rate_asset,' ' link,' ' notes FROM ledger_actions ') if location is not None: query += f'WHERE location="{location.serialize_for_db()}" ' query, bindings = form_query_to_filter_timestamps( query, 'timestamp', from_ts, to_ts) results = cursor.execute(query, bindings) actions = [] for result in results: try: action = LedgerAction.deserialize_from_db(result) except DeserializationError as e: self.msg_aggregator.add_error( f'Error deserializing Ledger Action from the DB. Skipping it.' f'Error was: {str(e)}', ) continue except UnknownAsset as e: self.msg_aggregator.add_error( f'Error deserializing Ledger Action from the DB. Skipping it. ' f'Unknown asset {e.asset_name} found', ) continue actions.append(action) return actions