From 98574299282d90496911a0302402b0c542695803 Mon Sep 17 00:00:00 2001 From: Yosuke Otosu Date: Mon, 7 Aug 2023 19:42:52 +0900 Subject: [PATCH] test: SQLAlchemy 2.0 Style --- app/routers/share.py | 10 +- .../model/blockchain/test_token_IbetShare.py | 15 +- .../blockchain/test_token_IbetStraightBond.py | 15 +- tests/test_app_routers_accounts_POST.py | 10 +- ...outers_accounts_{issuer_address}_DELETE.py | 4 +- ...ounts_{issuer_address}_authtoken_DELETE.py | 62 +- ...ccounts_{issuer_address}_authtoken_POST.py | 102 +- ...unts_{issuer_address}_eoa_password_POST.py | 3 +- ...ts_{issuer_address}_rsa_passphrase_POST.py | 3 +- ...s_accounts_{issuer_address}_rsakey_POST.py | 12 +- ...est_app_routers_bond_bulk_transfer_POST.py | 40 +- tests/test_app_routers_bond_tokens_POST.py | 31 +- ...outers_bond_tokens_{token_address}_POST.py | 37 +- ...en_address}_additional_issue_batch_POST.py | 18 +- ...token_address}_personal_info_batch_POST.py | 18 +- ...okens_{token_address}_redeem_batch_POST.py | 18 +- ...s_{token_address}_scheduled_events_POST.py | 33 +- ...uled_events_{scheduled_event_id}_DELETE.py | 9 +- ...fer_approvals_{token_Address}_{id}_POST.py | 13 +- ...app_routers_e2e_messaging_accounts_POST.py | 26 +- ...aging_accounts_{account_address}_DELETE.py | 6 +- ...nts_{account_address}_eoa_password_POST.py | 3 +- ...accounts_{account_address}_rsa_key_POST.py | 6 +- ...s_{account_address}_rsa_passphrase_POST.py | 11 +- ..._app_routers_e2e_messaging_messages_GET.py | 12 +- ...routers_e2e_messaging_messages_{id}_GET.py | 12 +- tests/test_app_routers_files_POST.py | 7 +- ...test_app_routers_files_{file_id}_DELETE.py | 4 +- ...edger_{token_address}_details_data_POST.py | 8 +- ..._address}_details_data_{data_id}_DELETE.py | 4 +- ...en_address}_details_data_{data_id}_POST.py | 8 +- ..._ledger_{token_address}_template_DELETE.py | 6 +- ...rs_ledger_{token_address}_template_POST.py | 18 +- ...outers_notifications_{notice_id}_DELETE.py | 6 +- ...st_app_routers_share_bulk_transfer_POST.py | 40 +- tests/test_app_routers_share_tokens_POST.py | 41 +- ...uters_share_tokens_{token_address}_POST.py | 61 +- ...en_address}_additional_issue_batch_POST.py | 18 +- ...token_address}_personal_info_batch_POST.py | 18 +- ...okens_{token_address}_redeem_batch_POST.py | 18 +- ...s_{token_address}_scheduled_events_POST.py | 29 +- ...uled_events_{scheduled_event_id}_DELETE.py | 9 +- ...fer_approvals_{token_Address}_{id}_POST.py | 13 +- ...holders_{token_address}_collection_POST.py | 9 +- ...address}_collection_{collection_id}_GET.py | 3 +- tests/test_batch_indexer_block_tx_data.py | 89 +- tests/test_batch_indexer_e2e_messaging.py | 181 ++-- tests/test_batch_indexer_issue_redeem.py | 43 +- tests/test_batch_indexer_personal_info.py | 47 +- tests/test_batch_indexer_position_bond.py | 895 ++++++++++-------- tests/test_batch_indexer_position_share.py | 895 ++++++++++-------- tests/test_batch_indexer_token_holders.py | 358 ++++--- tests/test_batch_indexer_transfer.py | 43 +- tests/test_batch_indexer_transfer_approval.py | 81 +- ...test_batch_processor_batch_issue_redeem.py | 181 ++-- tests/test_batch_processor_bulk_transfer.py | 197 ++-- tests/test_batch_processor_create_utxo.py | 47 +- .../test_batch_processor_generate_rsa_key.py | 3 +- ...st_batch_processor_modify_personal_info.py | 17 +- ...test_batch_processor_monitor_block_sync.py | 33 +- ..._batch_processor_register_personal_info.py | 137 ++- ..._processor_rotate_e2e_messaging_rsa_key.py | 79 +- .../test_batch_processor_scheduled_events.py | 193 ++-- tests/test_batch_processor_update_token.py | 81 +- tests/test_utils_contract_utils.py | 10 +- tests/test_utils_ledger_utils.py | 21 +- 66 files changed, 2479 insertions(+), 2001 deletions(-) diff --git a/app/routers/share.py b/app/routers/share.py index d0cbcec3..25596ee7 100644 --- a/app/routers/share.py +++ b/app/routers/share.py @@ -500,20 +500,20 @@ def list_share_history( total = db.scalar(select(func.count()).select_from(stmt.subquery())) if request_query.trigger: - stmt = stmt.filter(UpdateToken.trigger == request_query.trigger) + stmt = stmt.where(UpdateToken.trigger == request_query.trigger) if request_query.modified_contents: - stmt = stmt.filter( + stmt = stmt.where( cast(UpdateToken.arguments, String).like( "%" + request_query.modified_contents + "%" ) ) if request_query.created_from: - stmt = stmt.filter( + stmt = stmt.where( UpdateToken.created >= local_tz.localize(request_query.created_from).astimezone(utc_tz) ) if request_query.created_to: - stmt = stmt.filter( + stmt = stmt.where( UpdateToken.created <= local_tz.localize(request_query.created_to).astimezone(utc_tz) ) @@ -1618,7 +1618,7 @@ def list_all_holders( IDXLockedPosition.account_address == IDXPosition.account_address, ), ) - .filter(IDXPosition.token_address == token_address) + .where(IDXPosition.token_address == token_address) .group_by( IDXPosition.id, IDXLockedPosition.token_address, diff --git a/tests/model/blockchain/test_token_IbetShare.py b/tests/model/blockchain/test_token_IbetShare.py index c4abdc4a..4ad491dd 100644 --- a/tests/model/blockchain/test_token_IbetShare.py +++ b/tests/model/blockchain/test_token_IbetShare.py @@ -25,6 +25,7 @@ import pytest from eth_keyfile import decode_keyfile_json from pydantic.error_wrappers import ValidationError +from sqlalchemy import select from web3.exceptions import ( ContractLogicError, InvalidAddress, @@ -603,7 +604,7 @@ def test_normal_1(self, db): assert share_contract.is_canceled is False assert share_contract.memo == "" - _token_attr_update = db.query(TokenAttrUpdate).first() + _token_attr_update = db.scalars(select(TokenAttrUpdate).limit(1)).first() assert _token_attr_update is None # @@ -680,7 +681,7 @@ def test_normal_2(self, db): assert share_contract.principal_value == 9000 assert share_contract.is_canceled is True assert share_contract.memo == "memo_test" - _token_attr_update = db.query(TokenAttrUpdate).first() + _token_attr_update = db.scalars(select(TokenAttrUpdate).limit(1)).first() assert _token_attr_update.id == 1 assert _token_attr_update.token_address == contract_address assert _token_attr_update.updated_datetime > pre_datetime @@ -1321,7 +1322,7 @@ def test_normal_1(self, db): balance = share_contract.get_account_balance(issuer_address) assert balance == arguments[3] + 10 - _token_attr_update = db.query(TokenAttrUpdate).first() + _token_attr_update = db.scalars(select(TokenAttrUpdate).limit(1)).first() assert _token_attr_update.id == 1 assert _token_attr_update.token_address == contract_address assert _token_attr_update.updated_datetime > pre_datetime @@ -1637,7 +1638,7 @@ def test_normal_1(self, db): balance = share_contract.get_account_balance(issuer_address) assert balance == arguments[3] - 10 - _token_attr_update = db.query(TokenAttrUpdate).first() + _token_attr_update = db.scalars(select(TokenAttrUpdate).limit(1)).first() assert _token_attr_update.id == 1 assert _token_attr_update.token_address == contract_address assert _token_attr_update.updated_datetime > pre_datetime @@ -2070,7 +2071,7 @@ def test_normal_1(self, db): share_contract.record_attr_update(db) # assertion - _update = db.query(TokenAttrUpdate).first() + _update = db.scalars(select(TokenAttrUpdate).limit(1)).first() assert _update.id == 1 assert _update.token_address == self.token_address assert _update.updated_datetime == datetime(2021, 4, 27, 12, 34, 56) @@ -2093,7 +2094,9 @@ def test_normal_2(self, db, freezer): share_contract.record_attr_update(db) # assertion - _update = db.query(TokenAttrUpdate).filter(TokenAttrUpdate.id == 2).first() + _update = db.scalars( + select(TokenAttrUpdate).where(TokenAttrUpdate.id == 2).limit(1) + ).first() assert _update.id == 2 assert _update.token_address == self.token_address assert _update.updated_datetime == datetime(2021, 4, 27, 12, 34, 56) diff --git a/tests/model/blockchain/test_token_IbetStraightBond.py b/tests/model/blockchain/test_token_IbetStraightBond.py index a612f469..42e28707 100644 --- a/tests/model/blockchain/test_token_IbetStraightBond.py +++ b/tests/model/blockchain/test_token_IbetStraightBond.py @@ -25,6 +25,7 @@ import pytest from eth_keyfile import decode_keyfile_json from pydantic.error_wrappers import ValidationError +from sqlalchemy import select from web3.exceptions import ( ContractLogicError, InvalidAddress, @@ -681,7 +682,7 @@ def test_normal_1(self, db): assert bond_contract.transfer_approval_required is False assert bond_contract.memo == "" - _token_attr_update = db.query(TokenAttrUpdate).first() + _token_attr_update = db.scalars(select(TokenAttrUpdate).limit(1)).first() assert _token_attr_update is None # @@ -770,7 +771,7 @@ def test_normal_2(self, db): assert bond_contract.transfer_approval_required is True assert bond_contract.memo == "memo test" - _token_attr_update = db.query(TokenAttrUpdate).first() + _token_attr_update = db.scalars(select(TokenAttrUpdate).limit(1)).first() assert _token_attr_update.id == 1 assert _token_attr_update.token_address == contract_address assert _token_attr_update.updated_datetime > pre_datetime @@ -1431,7 +1432,7 @@ def test_normal_1(self, db): balance = bond_contract.get_account_balance(issuer_address) assert balance == arguments[2] + 10 - _token_attr_update = db.query(TokenAttrUpdate).first() + _token_attr_update = db.scalars(select(TokenAttrUpdate).limit(1)).first() assert _token_attr_update.id == 1 assert _token_attr_update.token_address == contract_address assert _token_attr_update.updated_datetime > pre_datetime @@ -1747,7 +1748,7 @@ def test_normal_1(self, db): balance = bond_contract.get_account_balance(issuer_address) assert balance == arguments[2] - 10 - _token_attr_update = db.query(TokenAttrUpdate).first() + _token_attr_update = db.scalars(select(TokenAttrUpdate).limit(1)).first() assert _token_attr_update.id == 1 assert _token_attr_update.token_address == contract_address assert _token_attr_update.updated_datetime > pre_datetime @@ -2180,7 +2181,7 @@ def test_normal_1(self, db): bond_contract.record_attr_update(db) # assertion - _update = db.query(TokenAttrUpdate).first() + _update = db.scalars(select(TokenAttrUpdate).limit(1)).first() assert _update.id == 1 assert _update.token_address == self.token_address assert _update.updated_datetime == datetime(2021, 4, 27, 12, 34, 56) @@ -2203,7 +2204,9 @@ def test_normal_2(self, db, freezer): bond_contract.record_attr_update(db) # assertion - _update = db.query(TokenAttrUpdate).filter(TokenAttrUpdate.id == 2).first() + _update = db.scalars( + select(TokenAttrUpdate).where(TokenAttrUpdate.id == 2).limit(1) + ).first() assert _update.id == 2 assert _update.token_address == self.token_address assert _update.updated_datetime == datetime(2021, 4, 27, 12, 34, 56) diff --git a/tests/test_app_routers_accounts_POST.py b/tests/test_app_routers_accounts_POST.py index e2cb6677..607ceb70 100644 --- a/tests/test_app_routers_accounts_POST.py +++ b/tests/test_app_routers_accounts_POST.py @@ -19,6 +19,8 @@ import base64 from unittest import mock +from sqlalchemy import select + from app.model.db import Account, AccountRsaStatus from app.utils.e2ee_utils import E2EEUtils from config import EOA_PASSWORD_PATTERN_MSG @@ -37,7 +39,7 @@ class TestAppRoutersAccountsPOST: # def test_normal_1(self, client, db): - accounts_before = db.query(Account).all() + accounts_before = db.scalars(select(Account)).all() password = self.valid_password req_param = {"eoa_password": E2EEUtils.encrypt(password)} @@ -51,7 +53,7 @@ def test_normal_1(self, client, db): assert resp.json()["rsa_status"] == AccountRsaStatus.UNSET.value assert resp.json()["is_deleted"] is False - accounts_after = db.query(Account).all() + accounts_after = db.scalars(select(Account)).all() assert 0 == len(accounts_before) assert 1 == len(accounts_after) @@ -70,7 +72,7 @@ def test_normal_1(self, client, db): @mock.patch("app.routers.account.AWS_KMS_GENERATE_RANDOM_ENABLED", True) @mock.patch("boto3.client") def test_normal_2(self, boto3_mock, client, db): - accounts_before = db.query(Account).all() + accounts_before = db.scalars(select(Account)).all() password = self.valid_password req_param = {"eoa_password": E2EEUtils.encrypt(password)} @@ -92,7 +94,7 @@ def generate_random(self, NumberOfBytes): assert resp.json()["rsa_status"] == AccountRsaStatus.UNSET.value assert resp.json()["is_deleted"] is False - accounts_after = db.query(Account).all() + accounts_after = db.scalars(select(Account)).all() assert 0 == len(accounts_before) assert 1 == len(accounts_after) diff --git a/tests/test_app_routers_accounts_{issuer_address}_DELETE.py b/tests/test_app_routers_accounts_{issuer_address}_DELETE.py index b4d49b95..d7f84b48 100644 --- a/tests/test_app_routers_accounts_{issuer_address}_DELETE.py +++ b/tests/test_app_routers_accounts_{issuer_address}_DELETE.py @@ -16,6 +16,8 @@ SPDX-License-Identifier: Apache-2.0 """ +from sqlalchemy import select + from app.model.db import Account, AccountRsaStatus from tests.account_config import config_eth_account @@ -53,7 +55,7 @@ def test_normal_1(self, client, db): "rsa_status": AccountRsaStatus.UNSET.value, "is_deleted": True, } - _account_after = db.query(Account).first() + _account_after = db.scalars(select(Account).limit(1)).first() assert _account_after.issuer_address == _admin_account["address"] assert _account_after.is_deleted == True diff --git a/tests/test_app_routers_accounts_{issuer_address}_authtoken_DELETE.py b/tests/test_app_routers_accounts_{issuer_address}_authtoken_DELETE.py index e7380530..979c5234 100644 --- a/tests/test_app_routers_accounts_{issuer_address}_authtoken_DELETE.py +++ b/tests/test_app_routers_accounts_{issuer_address}_authtoken_DELETE.py @@ -19,6 +19,8 @@ import hashlib from datetime import datetime +from sqlalchemy import select + from app.model.db import Account, AuthToken from app.utils.e2ee_utils import E2EEUtils from tests.account_config import config_eth_account @@ -63,11 +65,11 @@ def test_normal_1(self, client, db): # assertion assert resp.status_code == 200 - auth_token: AuthToken = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token: AuthToken = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is None # Normal_2 @@ -97,11 +99,11 @@ def test_normal_2(self, client, db): # assertion assert resp.status_code == 200 - auth_token: AuthToken = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token: AuthToken = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is None ########################################################################### @@ -147,11 +149,11 @@ def test_error_1(self, client, db): ], } - auth_token: AuthToken = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token: AuthToken = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is not None # Error_2 @@ -193,11 +195,11 @@ def test_error_2(self, client, db): ], } - auth_token: AuthToken = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token: AuthToken = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is not None # Error_3_1 @@ -232,11 +234,11 @@ def test_error_3_1(self, client, db): "detail": "issuer does not exist, or password mismatch", } - auth_token: AuthToken = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token: AuthToken = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is not None # Error_3_2 @@ -274,11 +276,11 @@ def test_error_3_2(self, client, db): "detail": "issuer does not exist, or password mismatch", } - auth_token: AuthToken = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token: AuthToken = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is not None # Error_4 diff --git a/tests/test_app_routers_accounts_{issuer_address}_authtoken_POST.py b/tests/test_app_routers_accounts_{issuer_address}_authtoken_POST.py index 039c3ffc..59fc0fb0 100644 --- a/tests/test_app_routers_accounts_{issuer_address}_authtoken_POST.py +++ b/tests/test_app_routers_accounts_{issuer_address}_authtoken_POST.py @@ -19,6 +19,8 @@ import hashlib from datetime import datetime +from sqlalchemy import select + from app.model.db import Account, AuthToken from app.utils.e2ee_utils import E2EEUtils from tests.account_config import config_eth_account @@ -56,11 +58,11 @@ def test_normal_1(self, client, db, freezer): ) # assertion - auth_token: AuthToken = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token: AuthToken = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token.issuer_address == test_account["address"] assert auth_token.usage_start == datetime(2022, 7, 15, 12, 34, 56) assert auth_token.valid_duration == 120 @@ -104,11 +106,11 @@ def test_normal_2(self, client, db, freezer): ) # assertion - auth_token: AuthToken = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token: AuthToken = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token.issuer_address == test_account["address"] assert auth_token.usage_start == datetime(2022, 7, 15, 12, 34, 56) assert auth_token.valid_duration == 120 @@ -146,11 +148,11 @@ def test_error_1_1(self, client, db, freezer): ) # assertion - auth_token = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is None assert resp.status_code == 422 @@ -186,11 +188,11 @@ def test_error_1_2(self, client, db, freezer): ) # assertion - auth_token = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is None assert resp.status_code == 422 @@ -227,11 +229,11 @@ def test_error_2(self, client, db, freezer): ) # assertion - auth_token = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is None assert resp.status_code == 422 @@ -270,11 +272,11 @@ def test_error_3(self, client, db, freezer): ) # assertion - auth_token = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is None assert resp.status_code == 422 @@ -311,11 +313,11 @@ def test_error_4_1(self, client, db, freezer): ) # assertion - auth_token = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is None assert resp.status_code == 422 @@ -352,11 +354,11 @@ def test_error_4_2(self, client, db, freezer): ) # assertion - auth_token = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is None assert resp.status_code == 422 @@ -394,11 +396,11 @@ def test_error_4_3(self, client, db, freezer): ) # assertion - auth_token = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is None assert resp.status_code == 422 @@ -437,11 +439,11 @@ def test_error_5(self, client, db, freezer): ) # assertion - auth_token = ( - db.query(AuthToken) - .filter(AuthToken.issuer_address == test_account["address"]) - .first() - ) + auth_token = db.scalars( + select(AuthToken) + .where(AuthToken.issuer_address == test_account["address"]) + .limit(1) + ).first() assert auth_token is None assert resp.status_code == 401 diff --git a/tests/test_app_routers_accounts_{issuer_address}_eoa_password_POST.py b/tests/test_app_routers_accounts_{issuer_address}_eoa_password_POST.py index 90b0721a..fd9f7a3c 100644 --- a/tests/test_app_routers_accounts_{issuer_address}_eoa_password_POST.py +++ b/tests/test_app_routers_accounts_{issuer_address}_eoa_password_POST.py @@ -17,6 +17,7 @@ SPDX-License-Identifier: Apache-2.0 """ import eth_keyfile +from sqlalchemy import select from app.model.blockchain import IbetStraightBondContract from app.model.db import Account, AccountRsaStatus @@ -59,7 +60,7 @@ def test_normal_1(self, client, db): # assertion assert resp.status_code == 200 assert resp.json() is None - _account = db.query(Account).first() + _account = db.scalars(select(Account).limit(1)).first() _account_keyfile = _account.keyfile _account_eoa_password = E2EEUtils.decrypt(_account.eoa_password) assert _account_keyfile != _old_keyfile diff --git a/tests/test_app_routers_accounts_{issuer_address}_rsa_passphrase_POST.py b/tests/test_app_routers_accounts_{issuer_address}_rsa_passphrase_POST.py index c0d8a9f0..d01cda8c 100644 --- a/tests/test_app_routers_accounts_{issuer_address}_rsa_passphrase_POST.py +++ b/tests/test_app_routers_accounts_{issuer_address}_rsa_passphrase_POST.py @@ -18,6 +18,7 @@ """ from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA +from sqlalchemy import select from app.model.db import Account, AccountRsaStatus from app.utils.e2ee_utils import E2EEUtils @@ -64,7 +65,7 @@ def test_normal_1(self, client, db): # assertion assert resp.status_code == 200 assert resp.json() is None - _account = db.query(Account).first() + _account = db.scalars(select(Account).limit(1)).first() _account_rsa_private_key = _account.rsa_private_key _account_rsa_passphrase = E2EEUtils.decrypt(_account.rsa_passphrase) assert _account_rsa_private_key != _old_rsa_private_key diff --git a/tests/test_app_routers_accounts_{issuer_address}_rsakey_POST.py b/tests/test_app_routers_accounts_{issuer_address}_rsakey_POST.py index d8141dc3..0e636e88 100644 --- a/tests/test_app_routers_accounts_{issuer_address}_rsakey_POST.py +++ b/tests/test_app_routers_accounts_{issuer_address}_rsakey_POST.py @@ -16,6 +16,8 @@ SPDX-License-Identifier: Apache-2.0 """ +from sqlalchemy import select + from app.model.db import Account, AccountRsaKeyTemporary, AccountRsaStatus from app.utils.e2ee_utils import E2EEUtils from config import ( @@ -64,7 +66,7 @@ def test_normal_1(self, client, db): "rsa_status": AccountRsaStatus.CREATING.value, "is_deleted": False, } - _account_after = db.query(Account).first() + _account_after = db.scalars(select(Account).limit(1)).first() assert _account_after.issuer_address == _user_1["address"] assert _account_after.keyfile == _user_1["keyfile_json"] assert _account_after.eoa_password == eoa_password @@ -92,7 +94,7 @@ def test_normal_2(self, client, db): db.add(_account_before) db.commit() - _temporary_before = db.query(AccountRsaKeyTemporary).all() + _temporary_before = db.scalars(select(AccountRsaKeyTemporary)).all() password = self.valid_password req_param = {"rsa_passphrase": E2EEUtils.encrypt(password)} @@ -107,7 +109,7 @@ def test_normal_2(self, client, db): "rsa_status": AccountRsaStatus.CHANGING.value, "is_deleted": False, } - _temporary_after = db.query(AccountRsaKeyTemporary).all() + _temporary_after = db.scalars(select(AccountRsaKeyTemporary)).all() assert len(_temporary_before) == 0 assert len(_temporary_after) == 1 _temporary = _temporary_after[0] @@ -115,7 +117,7 @@ def test_normal_2(self, client, db): assert _temporary.rsa_private_key == _user_1["rsa_private_key"] assert _temporary.rsa_public_key == _user_1["rsa_public_key"] assert _temporary.rsa_passphrase == rsa_passphrase - _account_after = db.query(Account).first() + _account_after = db.scalars(select(Account).limit(1)).first() assert _account_after.issuer_address == _user_1["address"] assert _account_after.keyfile == _user_1["keyfile_json"] assert _account_after.eoa_password == eoa_password @@ -150,7 +152,7 @@ def test_normal_3(self, client, db): "rsa_status": AccountRsaStatus.CREATING.value, "is_deleted": False, } - _account_after = db.query(Account).first() + _account_after = db.scalars(select(Account).limit(1)).first() assert _account_after.issuer_address == _user_1["address"] assert _account_after.keyfile == _user_1["keyfile_json"] assert _account_after.eoa_password == eoa_password diff --git a/tests/test_app_routers_bond_bulk_transfer_POST.py b/tests/test_app_routers_bond_bulk_transfer_POST.py index 730b559b..4c05ad39 100644 --- a/tests/test_app_routers_bond_bulk_transfer_POST.py +++ b/tests/test_app_routers_bond_bulk_transfer_POST.py @@ -18,6 +18,8 @@ """ import hashlib +from sqlalchemy import select + from app.model.db import ( Account, AuthToken, @@ -108,21 +110,20 @@ def test_normal_1(self, client, db): # assertion assert resp.status_code == 200 - bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == resp.json()["upload_id"]) - .all() - ) + bulk_transfer_upload = db.scalars( + select(BulkTransferUpload).where( + BulkTransferUpload.upload_id == resp.json()["upload_id"] + ) + ).all() assert len(bulk_transfer_upload) == 1 assert bulk_transfer_upload[0].issuer_address == self.admin_address assert bulk_transfer_upload[0].status == 0 - bulk_transfer = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == resp.json()["upload_id"]) + bulk_transfer = db.scalars( + select(BulkTransfer) + .where(BulkTransfer.upload_id == resp.json()["upload_id"]) .order_by(BulkTransfer.id) - .all() - ) + ).all() assert len(bulk_transfer) == 2 assert bulk_transfer[0].issuer_address == self.admin_address assert bulk_transfer[0].token_address == self.req_tokens[0] @@ -193,21 +194,20 @@ def test_normal_2(self, client, db): # assertion assert resp.status_code == 200 - bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == resp.json()["upload_id"]) - .all() - ) + bulk_transfer_upload = db.scalars( + select(BulkTransferUpload).where( + BulkTransferUpload.upload_id == resp.json()["upload_id"] + ) + ).all() assert len(bulk_transfer_upload) == 1 assert bulk_transfer_upload[0].issuer_address == self.admin_address assert bulk_transfer_upload[0].status == 0 - bulk_transfer = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == resp.json()["upload_id"]) + bulk_transfer = db.scalars( + select(BulkTransfer) + .where(BulkTransfer.upload_id == resp.json()["upload_id"]) .order_by(BulkTransfer.id) - .all() - ) + ).all() assert len(bulk_transfer) == 2 assert bulk_transfer[0].issuer_address == self.admin_address assert bulk_transfer[0].token_address == self.req_tokens[0] diff --git a/tests/test_app_routers_bond_tokens_POST.py b/tests/test_app_routers_bond_tokens_POST.py index 601e230a..f58d9327 100644 --- a/tests/test_app_routers_bond_tokens_POST.py +++ b/tests/test_app_routers_bond_tokens_POST.py @@ -22,6 +22,7 @@ from datetime import datetime, timezone from unittest.mock import ANY, patch +from sqlalchemy import select from web3 import Web3 from web3.middleware import geth_poa_middleware @@ -66,7 +67,7 @@ def test_normal_1(self, client, db): account.eoa_password = E2EEUtils.encrypt("password") db.add(account) - token_before = db.query(Token).all() + token_before = db.scalars(select(Token)).all() # mock IbetStraightBondContract_create = patch( @@ -124,7 +125,7 @@ def test_normal_1(self, client, db): assert resp.json()["token_address"] == "contract_address_test1" assert resp.json()["token_status"] == 1 - token_after = db.query(Token).all() + token_after = db.scalars(select(Token)).all() assert 0 == len(token_before) assert 1 == len(token_after) token_1 = token_after[0] @@ -136,7 +137,7 @@ def test_normal_1(self, client, db): assert token_1.abi == "abi_test1" assert token_1.token_status == 1 - position = db.query(IDXPosition).first() + position = db.scalars(select(IDXPosition).limit(1)).first() assert position.token_address == "contract_address_test1" assert position.account_address == test_account["address"] assert position.balance == req_param["total_supply"] @@ -144,7 +145,7 @@ def test_normal_1(self, client, db): assert position.exchange_commitment == 0 assert position.pending_transfer == 0 - utxo = db.query(UTXO).first() + utxo = db.scalars(select(UTXO).limit(1)).first() assert utxo.transaction_hash == "tx_hash_test1" assert utxo.account_address == test_account["address"] assert utxo.token_address == "contract_address_test1" @@ -152,7 +153,7 @@ def test_normal_1(self, client, db): assert utxo.block_number == 12345 assert utxo.block_timestamp == datetime(2021, 4, 27, 12, 34, 56) - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == "contract_address_test1" assert update_token.type == TokenType.IBET_STRAIGHT_BOND.value assert update_token.status == 1 @@ -170,7 +171,7 @@ def test_normal_2(self, client, db): account.eoa_password = E2EEUtils.encrypt("password") db.add(account) - token_before = db.query(Token).all() + token_before = db.scalars(select(Token)).all() # mock IbetStraightBondContract_create = patch( @@ -251,7 +252,7 @@ def test_normal_2(self, client, db): assert resp.json()["token_address"] == "contract_address_test1" assert resp.json()["token_status"] == 0 - token_after = db.query(Token).all() + token_after = db.scalars(select(Token)).all() assert 0 == len(token_before) assert 1 == len(token_after) token_1 = token_after[0] @@ -263,13 +264,13 @@ def test_normal_2(self, client, db): assert token_1.abi == "abi_test1" assert token_1.token_status == 0 - position = db.query(IDXPosition).first() + position = db.scalars(select(IDXPosition).limit(1)).first() assert position is None - utxo = db.query(UTXO).first() + utxo = db.scalars(select(UTXO).limit(1)).first() assert utxo is None - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.id == 1 assert update_token.token_address == "contract_address_test1" assert update_token.issuer_address == test_account["address"] @@ -296,7 +297,7 @@ def test_normal_3(self, client, db): auth_token.valid_duration = 0 db.add(auth_token) - token_before = db.query(Token).all() + token_before = db.scalars(select(Token)).all() # mock IbetStraightBondContract_create = patch( @@ -354,7 +355,7 @@ def test_normal_3(self, client, db): assert resp.json()["token_address"] == "contract_address_test1" assert resp.json()["token_status"] == 1 - token_after = db.query(Token).all() + token_after = db.scalars(select(Token)).all() assert 0 == len(token_before) assert 1 == len(token_after) token_1 = token_after[0] @@ -366,7 +367,7 @@ def test_normal_3(self, client, db): assert token_1.abi == "abi_test1" assert token_1.token_status == 1 - position = db.query(IDXPosition).first() + position = db.scalars(select(IDXPosition).limit(1)).first() assert position.token_address == "contract_address_test1" assert position.account_address == test_account["address"] assert position.balance == req_param["total_supply"] @@ -374,7 +375,7 @@ def test_normal_3(self, client, db): assert position.exchange_commitment == 0 assert position.pending_transfer == 0 - utxo = db.query(UTXO).first() + utxo = db.scalars(select(UTXO).limit(1)).first() assert utxo.transaction_hash == "tx_hash_test1" assert utxo.account_address == test_account["address"] assert utxo.token_address == "contract_address_test1" @@ -382,7 +383,7 @@ def test_normal_3(self, client, db): assert utxo.block_number == 12345 assert utxo.block_timestamp == datetime(2021, 4, 27, 12, 34, 56) - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == "contract_address_test1" assert update_token.type == TokenType.IBET_STRAIGHT_BOND.value assert update_token.status == 1 diff --git a/tests/test_app_routers_bond_tokens_{token_address}_POST.py b/tests/test_app_routers_bond_tokens_{token_address}_POST.py index 6526b70d..abf02bd9 100644 --- a/tests/test_app_routers_bond_tokens_{token_address}_POST.py +++ b/tests/test_app_routers_bond_tokens_{token_address}_POST.py @@ -21,6 +21,7 @@ from unittest.mock import MagicMock from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from web3 import Web3 from web3.middleware import geth_poa_middleware @@ -132,14 +133,14 @@ def test_normal_1(self, client, db): assert resp.status_code == 200 assert resp.json() is None - token_attr_update = ( - db.query(TokenAttrUpdate) - .filter(TokenAttrUpdate.token_address == _token_address) - .all() - ) + token_attr_update = db.scalars( + select(TokenAttrUpdate).where( + TokenAttrUpdate.token_address == _token_address + ) + ).all() assert len(token_attr_update) == 1 - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == _token_address assert update_token.issuer_address == _issuer_address assert update_token.type == TokenType.IBET_STRAIGHT_BOND.value @@ -232,14 +233,14 @@ def test_normal_2(self, client, db): assert resp.status_code == 200 assert resp.json() is None - token_attr_update = ( - db.query(TokenAttrUpdate) - .filter(TokenAttrUpdate.token_address == _token_address) - .all() - ) + token_attr_update = db.scalars( + select(TokenAttrUpdate).where( + TokenAttrUpdate.token_address == _token_address + ) + ).all() assert len(token_attr_update) == 0 - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token is None # @@ -311,14 +312,14 @@ def test_normal_3(self, client, db): assert resp.status_code == 200 assert resp.json() is None - token_attr_update = ( - db.query(TokenAttrUpdate) - .filter(TokenAttrUpdate.token_address == _token_address) - .all() - ) + token_attr_update = db.scalars( + select(TokenAttrUpdate).where( + TokenAttrUpdate.token_address == _token_address + ) + ).all() assert len(token_attr_update) == 1 - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == _token_address assert update_token.issuer_address == _issuer_address assert update_token.type == TokenType.IBET_STRAIGHT_BOND.value diff --git a/tests/test_app_routers_bond_tokens_{token_address}_additional_issue_batch_POST.py b/tests/test_app_routers_bond_tokens_{token_address}_additional_issue_batch_POST.py index b6962bab..76f9125d 100644 --- a/tests/test_app_routers_bond_tokens_{token_address}_additional_issue_batch_POST.py +++ b/tests/test_app_routers_bond_tokens_{token_address}_additional_issue_batch_POST.py @@ -18,6 +18,8 @@ """ from typing import Optional +from sqlalchemy import select + from app.model.db import ( Account, BatchIssueRedeem, @@ -76,8 +78,8 @@ def test_normal_1(self, client, db): ) # assertion - upload: Optional[BatchIssueRedeemUpload] = db.query( - BatchIssueRedeemUpload + upload: Optional[BatchIssueRedeemUpload] = db.scalars( + select(BatchIssueRedeemUpload).limit(1) ).first() assert upload.issuer_address == issuer_address assert upload.token_type == TokenType.IBET_STRAIGHT_BOND.value @@ -85,7 +87,9 @@ def test_normal_1(self, client, db): assert upload.category == BatchIssueRedeemProcessingCategory.ISSUE.value assert upload.processed is False - batch_data_list: BatchIssueRedeem = db.query(BatchIssueRedeem).all() + batch_data_list: list[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem) + ).all() assert len(batch_data_list) == 1 batch_data_1: BatchIssueRedeem = batch_data_list[0] @@ -139,8 +143,8 @@ def test_normal_2(self, client, db): ) # assertion - upload: Optional[BatchIssueRedeemUpload] = db.query( - BatchIssueRedeemUpload + upload: Optional[BatchIssueRedeemUpload] = db.scalars( + select(BatchIssueRedeemUpload).limit(1) ).first() assert upload.issuer_address == issuer_address assert upload.token_type == TokenType.IBET_STRAIGHT_BOND.value @@ -148,7 +152,9 @@ def test_normal_2(self, client, db): assert upload.category == BatchIssueRedeemProcessingCategory.ISSUE.value assert upload.processed is False - batch_data_list: BatchIssueRedeem = db.query(BatchIssueRedeem).all() + batch_data_list: list[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem) + ).all() assert len(batch_data_list) == 2 batch_data_1: BatchIssueRedeem = batch_data_list[0] diff --git a/tests/test_app_routers_bond_tokens_{token_address}_personal_info_batch_POST.py b/tests/test_app_routers_bond_tokens_{token_address}_personal_info_batch_POST.py index 4d2ead53..627a884a 100644 --- a/tests/test_app_routers_bond_tokens_{token_address}_personal_info_batch_POST.py +++ b/tests/test_app_routers_bond_tokens_{token_address}_personal_info_batch_POST.py @@ -20,6 +20,8 @@ from typing import Optional from unittest.mock import ANY +from sqlalchemy import select + from app.model.db import ( Account, AuthToken, @@ -98,14 +100,14 @@ def test_normal_1(self, client, db): "created": ANY, } - _upload: Optional[BatchRegisterPersonalInfoUpload] = db.query( - BatchRegisterPersonalInfoUpload + _upload: Optional[BatchRegisterPersonalInfoUpload] = db.scalars( + select(BatchRegisterPersonalInfoUpload).limit(1) ).first() assert _upload.status == BatchRegisterPersonalInfoUploadStatus.PENDING.value assert _upload.issuer_address == _issuer_address - _register_list: list[BatchRegisterPersonalInfo] = db.query( - BatchRegisterPersonalInfo + _register_list: list[BatchRegisterPersonalInfo] = db.scalars( + select(BatchRegisterPersonalInfo) ).all() assert len(_register_list) == 10 @@ -172,14 +174,14 @@ def test_normal_2(self, client, db): "created": ANY, } - _upload: Optional[BatchRegisterPersonalInfoUpload] = db.query( - BatchRegisterPersonalInfoUpload + _upload: Optional[BatchRegisterPersonalInfoUpload] = db.scalars( + select(BatchRegisterPersonalInfoUpload).limit(1) ).first() assert _upload.status == BatchRegisterPersonalInfoUploadStatus.PENDING.value assert _upload.issuer_address == _issuer_address - _register_list: list[BatchRegisterPersonalInfo] = db.query( - BatchRegisterPersonalInfo + _register_list: list[BatchRegisterPersonalInfo] = db.scalars( + select(BatchRegisterPersonalInfo) ).all() assert len(_register_list) == 10 diff --git a/tests/test_app_routers_bond_tokens_{token_address}_redeem_batch_POST.py b/tests/test_app_routers_bond_tokens_{token_address}_redeem_batch_POST.py index bf475eea..81bda7a9 100644 --- a/tests/test_app_routers_bond_tokens_{token_address}_redeem_batch_POST.py +++ b/tests/test_app_routers_bond_tokens_{token_address}_redeem_batch_POST.py @@ -18,6 +18,8 @@ """ from typing import Optional +from sqlalchemy import select + from app.model.db import ( Account, BatchIssueRedeem, @@ -76,8 +78,8 @@ def test_normal_1(self, client, db): ) # assertion - upload: Optional[BatchIssueRedeemUpload] = db.query( - BatchIssueRedeemUpload + upload: Optional[BatchIssueRedeemUpload] = db.scalars( + select(BatchIssueRedeemUpload).limit(1) ).first() assert upload.issuer_address == issuer_address assert upload.token_type == TokenType.IBET_STRAIGHT_BOND.value @@ -85,7 +87,9 @@ def test_normal_1(self, client, db): assert upload.category == BatchIssueRedeemProcessingCategory.REDEEM.value assert upload.processed is False - batch_data_list: BatchIssueRedeem = db.query(BatchIssueRedeem).all() + batch_data_list: list[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem) + ).all() assert len(batch_data_list) == 1 batch_data_1: BatchIssueRedeem = batch_data_list[0] @@ -139,8 +143,8 @@ def test_normal_2(self, client, db): ) # assertion - upload: Optional[BatchIssueRedeemUpload] = db.query( - BatchIssueRedeemUpload + upload: Optional[BatchIssueRedeemUpload] = db.scalars( + select(BatchIssueRedeemUpload).limit(1) ).first() assert upload.issuer_address == issuer_address assert upload.token_type == TokenType.IBET_STRAIGHT_BOND.value @@ -148,7 +152,9 @@ def test_normal_2(self, client, db): assert upload.category == BatchIssueRedeemProcessingCategory.REDEEM.value assert upload.processed is False - batch_data_list: BatchIssueRedeem = db.query(BatchIssueRedeem).all() + batch_data_list: list[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem) + ).all() assert len(batch_data_list) == 2 batch_data_1: BatchIssueRedeem = batch_data_list[0] diff --git a/tests/test_app_routers_bond_tokens_{token_address}_scheduled_events_POST.py b/tests/test_app_routers_bond_tokens_{token_address}_scheduled_events_POST.py index 2fd59651..3a9ff561 100644 --- a/tests/test_app_routers_bond_tokens_{token_address}_scheduled_events_POST.py +++ b/tests/test_app_routers_bond_tokens_{token_address}_scheduled_events_POST.py @@ -19,6 +19,7 @@ from datetime import datetime, timezone from pytz import timezone as tz +from sqlalchemy import and_, select from app.model.db import Account, ScheduledEvents, ScheduledEventType, Token, TokenType from app.utils.e2ee_utils import E2EEUtils @@ -92,12 +93,16 @@ def test_normal_1(self, client, db): ) # assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.issuer_address == _issuer_address) - .filter(ScheduledEvents.token_address == _token_address) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where( + and_( + ScheduledEvents.issuer_address == _issuer_address, + ScheduledEvents.token_address == _token_address, + ) + ) + .limit(1) + ).first() assert resp.status_code == 200 assert resp.json() == {"scheduled_event_id": _scheduled_event.event_id} assert _scheduled_event.token_type == TokenType.IBET_STRAIGHT_BOND.value @@ -167,12 +172,16 @@ def test_normal_2(self, client, db): ) # assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.issuer_address == _issuer_address) - .filter(ScheduledEvents.token_address == _token_address) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where( + and_( + ScheduledEvents.issuer_address == _issuer_address, + ScheduledEvents.token_address == _token_address, + ) + ) + .limit(1) + ).first() assert resp_1.status_code == 200 assert resp_1.json() == {"scheduled_event_id": _scheduled_event.event_id} assert _scheduled_event.token_type == TokenType.IBET_STRAIGHT_BOND.value diff --git a/tests/test_app_routers_bond_tokens_{token_address}_scheduled_events_{scheduled_event_id}_DELETE.py b/tests/test_app_routers_bond_tokens_{token_address}_scheduled_events_{scheduled_event_id}_DELETE.py index 9d17187a..6c7ceda2 100644 --- a/tests/test_app_routers_bond_tokens_{token_address}_scheduled_events_{scheduled_event_id}_DELETE.py +++ b/tests/test_app_routers_bond_tokens_{token_address}_scheduled_events_{scheduled_event_id}_DELETE.py @@ -20,6 +20,7 @@ from datetime import datetime from pytz import timezone +from sqlalchemy import select from app.model.db import Account, ScheduledEvents, ScheduledEventType, TokenType from app.utils.e2ee_utils import E2EEUtils @@ -107,11 +108,9 @@ def test_normal_1(self, client, db): "data": data, "created": datetime_now_str, } - token_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.event_id == event_id) - .first() - ) + token_event = db.scalars( + select(ScheduledEvents).where(ScheduledEvents.event_id == event_id).limit(1) + ).first() assert token_event is None ######################################################################### diff --git a/tests/test_app_routers_bond_transfer_approvals_{token_Address}_{id}_POST.py b/tests/test_app_routers_bond_transfer_approvals_{token_Address}_{id}_POST.py index e1fd9187..f35da0ed 100644 --- a/tests/test_app_routers_bond_transfer_approvals_{token_Address}_{id}_POST.py +++ b/tests/test_app_routers_bond_transfer_approvals_{token_Address}_{id}_POST.py @@ -23,6 +23,7 @@ import pytest from pytz import timezone +from sqlalchemy import select import config from app.exceptions import ContractRevertError, SendTransactionError @@ -136,8 +137,8 @@ def test_normal_1_1(self, client, db): private_key=ANY, ) - approval_op_list: list[TransferApprovalHistory] = db.query( - TransferApprovalHistory + approval_op_list: list[TransferApprovalHistory] = db.scalars( + select(TransferApprovalHistory) ).all() assert len(approval_op_list) == 1 approval_op = approval_op_list[0] @@ -218,8 +219,8 @@ def test_normal_1_2(self, client, db): private_key=ANY, ) - approval_op_list: list[TransferApprovalHistory] = db.query( - TransferApprovalHistory + approval_op_list: list[TransferApprovalHistory] = db.scalars( + select(TransferApprovalHistory) ).all() assert len(approval_op_list) == 1 approval_op = approval_op_list[0] @@ -299,8 +300,8 @@ def test_normal_2_1(self, client, db): private_key=ANY, ) - cancel_op_list: list[TransferApprovalHistory] = db.query( - TransferApprovalHistory + cancel_op_list: list[TransferApprovalHistory] = db.scalars( + select(TransferApprovalHistory) ).all() assert len(cancel_op_list) == 1 cancel_op = cancel_op_list[0] diff --git a/tests/test_app_routers_e2e_messaging_accounts_POST.py b/tests/test_app_routers_e2e_messaging_accounts_POST.py index 481d3c3d..4cc22b62 100644 --- a/tests/test_app_routers_e2e_messaging_accounts_POST.py +++ b/tests/test_app_routers_e2e_messaging_accounts_POST.py @@ -21,6 +21,8 @@ from unittest import mock from unittest.mock import ANY, MagicMock +from sqlalchemy import select + from app.exceptions import SendTransactionError from app.model.blockchain import E2EMessaging from app.model.db import E2EMessagingAccount, E2EMessagingAccountRsaKey, TransactionLock @@ -47,9 +49,9 @@ class TestAppRoutersE2EMessagingAccountsPOST: # def test_normal_1(self, client, db, e2e_messaging_contract): - _accounts_before = db.query(E2EMessagingAccount).all() - _rsa_key_before = db.query(E2EMessagingAccountRsaKey).all() - _transaction_before = db.query(TransactionLock).all() + _accounts_before = db.scalars(select(E2EMessagingAccount)).all() + _rsa_key_before = db.scalars(select(E2EMessagingAccountRsaKey)).all() + _transaction_before = db.scalars(select(TransactionLock)).all() # mock mock_E2EMessaging_set_public_key = mock.patch( @@ -99,9 +101,9 @@ def test_normal_1(self, client, db, e2e_messaging_contract): "is_deleted": False, } - _accounts_after = db.query(E2EMessagingAccount).all() - _rsa_key_after = db.query(E2EMessagingAccountRsaKey).all() - _transaction_after = db.query(TransactionLock).all() + _accounts_after = db.scalars(select(E2EMessagingAccount)).all() + _rsa_key_after = db.scalars(select(E2EMessagingAccountRsaKey)).all() + _transaction_after = db.scalars(select(TransactionLock)).all() assert 0 == len(_accounts_before) assert 1 == len(_accounts_after) @@ -133,9 +135,9 @@ def test_normal_1(self, client, db, e2e_messaging_contract): # # use AWS KMS def test_normal_2(self, client, db, e2e_messaging_contract): - _accounts_before = db.query(E2EMessagingAccount).all() - _rsa_key_before = db.query(E2EMessagingAccountRsaKey).all() - _transaction_before = db.query(TransactionLock).all() + _accounts_before = db.scalars(select(E2EMessagingAccount)).all() + _rsa_key_before = db.scalars(select(E2EMessagingAccountRsaKey)).all() + _transaction_before = db.scalars(select(TransactionLock)).all() # mock class KMSClientMock: @@ -202,9 +204,9 @@ def generate_random(self, NumberOfBytes): "is_deleted": False, } - _accounts_after = db.query(E2EMessagingAccount).all() - _rsa_key_after = db.query(E2EMessagingAccountRsaKey).all() - _transaction_after = db.query(TransactionLock).all() + _accounts_after = db.scalars(select(E2EMessagingAccount)).all() + _rsa_key_after = db.scalars(select(E2EMessagingAccountRsaKey)).all() + _transaction_after = db.scalars(select(TransactionLock)).all() assert 0 == len(_accounts_before) assert 1 == len(_accounts_after) diff --git a/tests/test_app_routers_e2e_messaging_accounts_{account_address}_DELETE.py b/tests/test_app_routers_e2e_messaging_accounts_{account_address}_DELETE.py index 5241b36e..d90263d9 100644 --- a/tests/test_app_routers_e2e_messaging_accounts_{account_address}_DELETE.py +++ b/tests/test_app_routers_e2e_messaging_accounts_{account_address}_DELETE.py @@ -19,6 +19,8 @@ import time from datetime import datetime +from sqlalchemy import select + from app.model.db import E2EMessagingAccount, E2EMessagingAccountRsaKey @@ -77,9 +79,9 @@ def test_normal_1(self, client, db): "rsa_public_key": None, "is_deleted": True, } - _account = db.query(E2EMessagingAccount).first() + _account = db.scalars(select(E2EMessagingAccount).limit(1)).first() assert _account.is_deleted is True - _rsa_key_list = db.query(E2EMessagingAccountRsaKey).all() + _rsa_key_list = db.scalars(select(E2EMessagingAccountRsaKey)).all() assert len(_rsa_key_list) == 0 ########################################################################### diff --git a/tests/test_app_routers_e2e_messaging_accounts_{account_address}_eoa_password_POST.py b/tests/test_app_routers_e2e_messaging_accounts_{account_address}_eoa_password_POST.py index 24c34753..a9cccea6 100644 --- a/tests/test_app_routers_e2e_messaging_accounts_{account_address}_eoa_password_POST.py +++ b/tests/test_app_routers_e2e_messaging_accounts_{account_address}_eoa_password_POST.py @@ -17,6 +17,7 @@ SPDX-License-Identifier: Apache-2.0 """ from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from app.model.blockchain import IbetStraightBondContract from app.model.db import E2EMessagingAccount @@ -59,7 +60,7 @@ def test_normal_1(self, client, db, e2e_messaging_contract): # assertion assert resp.status_code == 200 assert resp.json() is None - _account = db.query(E2EMessagingAccount).first() + _account = db.scalars(select(E2EMessagingAccount).limit(1)).first() assert _account.keyfile != user_keyfile_1 assert E2EEUtils.decrypt(_account.eoa_password) == new_password diff --git a/tests/test_app_routers_e2e_messaging_accounts_{account_address}_rsa_key_POST.py b/tests/test_app_routers_e2e_messaging_accounts_{account_address}_rsa_key_POST.py index 3ca96e90..9fb488dd 100644 --- a/tests/test_app_routers_e2e_messaging_accounts_{account_address}_rsa_key_POST.py +++ b/tests/test_app_routers_e2e_messaging_accounts_{account_address}_rsa_key_POST.py @@ -19,6 +19,8 @@ import time from datetime import datetime +from sqlalchemy import select + from app.model.db import E2EMessagingAccount, E2EMessagingAccountRsaKey @@ -79,7 +81,7 @@ def test_normal_1(self, client, db): "rsa_public_key": "rsa_public_key_1_3", "is_deleted": False, } - _account = db.query(E2EMessagingAccount).first() + _account = db.scalars(select(E2EMessagingAccount).limit(1)).first() assert _account.rsa_key_generate_interval == 1 assert _account.rsa_generation == 2 @@ -130,7 +132,7 @@ def test_normal_2(self, client, db): "rsa_public_key": "rsa_public_key_1_3", "is_deleted": False, } - _account = db.query(E2EMessagingAccount).first() + _account = db.scalars(select(E2EMessagingAccount).limit(1)).first() assert _account.rsa_key_generate_interval == 24 assert _account.rsa_generation == 7 diff --git a/tests/test_app_routers_e2e_messaging_accounts_{account_address}_rsa_passphrase_POST.py b/tests/test_app_routers_e2e_messaging_accounts_{account_address}_rsa_passphrase_POST.py index 7552aaba..008b112c 100644 --- a/tests/test_app_routers_e2e_messaging_accounts_{account_address}_rsa_passphrase_POST.py +++ b/tests/test_app_routers_e2e_messaging_accounts_{account_address}_rsa_passphrase_POST.py @@ -21,6 +21,7 @@ from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA +from sqlalchemy import select from app.model.db import E2EMessagingAccount, E2EMessagingAccountRsaKey from app.utils.e2ee_utils import E2EEUtils @@ -152,11 +153,11 @@ def test_normal_1(self, client, db, e2e_messaging_contract): # assertion assert resp.status_code == 200 assert resp.json() is None - _rsa_key_list = ( - db.query(E2EMessagingAccountRsaKey) - .order_by(E2EMessagingAccountRsaKey.block_timestamp) - .all() - ) + _rsa_key_list = db.scalars( + select(E2EMessagingAccountRsaKey).order_by( + E2EMessagingAccountRsaKey.block_timestamp + ) + ).all() assert len(_rsa_key_list) == 3 _rsa_key = _rsa_key_list[2] assert _rsa_key.rsa_private_key != self.rsa_private_key diff --git a/tests/test_app_routers_e2e_messaging_messages_GET.py b/tests/test_app_routers_e2e_messaging_messages_GET.py index 3cf132d5..2f283aa7 100644 --- a/tests/test_app_routers_e2e_messaging_messages_GET.py +++ b/tests/test_app_routers_e2e_messaging_messages_GET.py @@ -19,6 +19,8 @@ import json from datetime import datetime +from sqlalchemy import select + from app.model.db import E2EMessagingAccount, IDXE2EMessaging @@ -35,11 +37,11 @@ def insert_data(self, db, e2e_messaging): _e2e_messaging.send_timestamp = e2e_messaging["send_timestamp"] db.add(_e2e_messaging) - _account = ( - db.query(E2EMessagingAccount) - .filter(E2EMessagingAccount.account_address == e2e_messaging["to_address"]) - .first() - ) + _account = db.scalars( + select(E2EMessagingAccount) + .where(E2EMessagingAccount.account_address == e2e_messaging["to_address"]) + .limit(1) + ).first() if _account is None: _account = E2EMessagingAccount() _account.account_address = e2e_messaging["to_address"] diff --git a/tests/test_app_routers_e2e_messaging_messages_{id}_GET.py b/tests/test_app_routers_e2e_messaging_messages_{id}_GET.py index 479df4d0..17ce8b0a 100644 --- a/tests/test_app_routers_e2e_messaging_messages_{id}_GET.py +++ b/tests/test_app_routers_e2e_messaging_messages_{id}_GET.py @@ -19,6 +19,8 @@ import json from datetime import datetime +from sqlalchemy import select + from app.model.db import E2EMessagingAccount, IDXE2EMessaging @@ -37,11 +39,11 @@ def insert_data(self, db, e2e_messaging): _e2e_messaging.send_timestamp = e2e_messaging["send_timestamp"] db.add(_e2e_messaging) - _account = ( - db.query(E2EMessagingAccount) - .filter(E2EMessagingAccount.account_address == e2e_messaging["to_address"]) - .first() - ) + _account = db.scalars( + select(E2EMessagingAccount) + .where(E2EMessagingAccount.account_address == e2e_messaging["to_address"]) + .limit(1) + ).first() if _account is None: _account = E2EMessagingAccount() _account.account_address = e2e_messaging["to_address"] diff --git a/tests/test_app_routers_files_POST.py b/tests/test_app_routers_files_POST.py index e26936e5..7151521c 100644 --- a/tests/test_app_routers_files_POST.py +++ b/tests/test_app_routers_files_POST.py @@ -20,6 +20,7 @@ from unittest import mock import pytz +from sqlalchemy import select from app.model.db import UploadFile from config import TZ @@ -68,7 +69,7 @@ def test_normal_1(self, client, db): # assertion assert resp.status_code == 200 - _upload_file = db.query(UploadFile).first() + _upload_file = db.scalars(select(UploadFile).limit(1)).first() assert _upload_file.file_id is not None assert _upload_file.issuer_address == self.issuer_address assert _upload_file.relation == self.token_address @@ -115,7 +116,7 @@ def test_normal_2(self, client, db): # assertion assert resp.status_code == 200 - _upload_file = db.query(UploadFile).first() + _upload_file = db.scalars(select(UploadFile).limit(1)).first() assert _upload_file.file_id is not None assert _upload_file.issuer_address == self.issuer_address assert _upload_file.relation == self.token_address @@ -162,7 +163,7 @@ def test_normal_3(self, client, db): # assertion assert resp.status_code == 200 - _upload_file = db.query(UploadFile).first() + _upload_file = db.scalars(select(UploadFile).limit(1)).first() assert _upload_file.file_id is not None assert _upload_file.issuer_address == self.issuer_address assert _upload_file.relation == self.token_address diff --git a/tests/test_app_routers_files_{file_id}_DELETE.py b/tests/test_app_routers_files_{file_id}_DELETE.py index c6e8321f..5b025c23 100644 --- a/tests/test_app_routers_files_{file_id}_DELETE.py +++ b/tests/test_app_routers_files_{file_id}_DELETE.py @@ -18,6 +18,8 @@ """ import base64 +from sqlalchemy import select + from app.model.db import UploadFile @@ -65,7 +67,7 @@ def test_normal_1(self, client, db): assert resp.status_code == 200 assert resp.json() is None - _upload_file_list = db.query(UploadFile).all() + _upload_file_list = db.scalars(select(UploadFile)).all() assert len(_upload_file_list) == 0 ########################################################################### diff --git a/tests/test_app_routers_ledger_{token_address}_details_data_POST.py b/tests/test_app_routers_ledger_{token_address}_details_data_POST.py index 82387b61..e967a8fe 100644 --- a/tests/test_app_routers_ledger_{token_address}_details_data_POST.py +++ b/tests/test_app_routers_ledger_{token_address}_details_data_POST.py @@ -16,6 +16,8 @@ SPDX-License-Identifier: Apache-2.0 """ +from sqlalchemy import select + from app.model.db import LedgerDetailsData, Token, TokenType from tests.account_config import config_eth_account @@ -73,9 +75,9 @@ def test_normal_1(self, client, db): # assertion assert resp.status_code == 200 assert resp.json()["data_id"] is not None - _details_data_list = ( - db.query(LedgerDetailsData).order_by(LedgerDetailsData.id).all() - ) + _details_data_list = db.scalars( + select(LedgerDetailsData).order_by(LedgerDetailsData.id) + ).all() assert len(_details_data_list) == 2 _details_data = _details_data_list[0] assert _details_data.id == 1 diff --git a/tests/test_app_routers_ledger_{token_address}_details_data_{data_id}_DELETE.py b/tests/test_app_routers_ledger_{token_address}_details_data_{data_id}_DELETE.py index 7b2c3af3..9f2c2e2f 100644 --- a/tests/test_app_routers_ledger_{token_address}_details_data_{data_id}_DELETE.py +++ b/tests/test_app_routers_ledger_{token_address}_details_data_{data_id}_DELETE.py @@ -16,6 +16,8 @@ SPDX-License-Identifier: Apache-2.0 """ +from sqlalchemy import select + from app.model.db import LedgerDetailsData, Token, TokenType from tests.account_config import config_eth_account @@ -89,7 +91,7 @@ def test_normal_1(self, client, db): # assertion assert resp.status_code == 200 assert resp.json() is None - _details_data_list = db.query(LedgerDetailsData).all() + _details_data_list = db.scalars(select(LedgerDetailsData)).all() assert len(_details_data_list) == 1 assert _details_data_list[0].id == 3 assert _details_data_list[0].token_address == token_address diff --git a/tests/test_app_routers_ledger_{token_address}_details_data_{data_id}_POST.py b/tests/test_app_routers_ledger_{token_address}_details_data_{data_id}_POST.py index cfbbfe10..c8ed5585 100644 --- a/tests/test_app_routers_ledger_{token_address}_details_data_{data_id}_POST.py +++ b/tests/test_app_routers_ledger_{token_address}_details_data_{data_id}_POST.py @@ -18,6 +18,8 @@ """ from unittest import mock +from sqlalchemy import select + from app.model.db import LedgerDetailsData, Token, TokenType from tests.account_config import config_eth_account @@ -88,9 +90,9 @@ def test_normal_1(self, mock_func, client, db): # assertion assert resp.status_code == 200 assert resp.json() is None - _details_data_list = ( - db.query(LedgerDetailsData).order_by(LedgerDetailsData.id).all() - ) + _details_data_list = db.scalars( + select(LedgerDetailsData).order_by(LedgerDetailsData.id) + ).all() assert len(_details_data_list) == 2 _details_data = _details_data_list[0] assert _details_data.id == 2 diff --git a/tests/test_app_routers_ledger_{token_address}_template_DELETE.py b/tests/test_app_routers_ledger_{token_address}_template_DELETE.py index 6f69fe33..c46d975e 100644 --- a/tests/test_app_routers_ledger_{token_address}_template_DELETE.py +++ b/tests/test_app_routers_ledger_{token_address}_template_DELETE.py @@ -16,6 +16,8 @@ SPDX-License-Identifier: Apache-2.0 """ +from sqlalchemy import select + from app.model.db import ( LedgerDetailsDataType, LedgerDetailsTemplate, @@ -128,9 +130,9 @@ def test_normal_1(self, client, db): # assertion assert resp.status_code == 200 assert resp.json() is None - _template_list = db.query(LedgerTemplate).all() + _template_list = db.scalars(select(LedgerTemplate)).all() assert len(_template_list) == 0 - _details_list = db.query(LedgerDetailsTemplate).all() + _details_list = db.scalars(select(LedgerDetailsTemplate)).all() assert len(_details_list) == 0 ########################################################################### diff --git a/tests/test_app_routers_ledger_{token_address}_template_POST.py b/tests/test_app_routers_ledger_{token_address}_template_POST.py index 1a2e79de..0aee63fd 100644 --- a/tests/test_app_routers_ledger_{token_address}_template_POST.py +++ b/tests/test_app_routers_ledger_{token_address}_template_POST.py @@ -18,6 +18,8 @@ """ from unittest import mock +from sqlalchemy import select + from app.model.db import ( LedgerDetailsDataType, LedgerDetailsTemplate, @@ -144,7 +146,7 @@ def test_normal_1(self, mock_func, client, db): # assertion assert resp.status_code == 200 assert resp.json() is None - _template = db.query(LedgerTemplate).first() + _template = db.scalars(select(LedgerTemplate).limit(1)).first() assert _template.token_address == token_address assert _template.issuer_address == issuer_address assert _template.token_name == "テスト原簿" @@ -168,9 +170,9 @@ def test_normal_1(self, mock_func, client, db): "f-fuga": "bbb", }, ] - _details_list = ( - db.query(LedgerDetailsTemplate).order_by(LedgerDetailsTemplate.id).all() - ) + _details_list = db.scalars( + select(LedgerDetailsTemplate).order_by(LedgerDetailsTemplate.id) + ).all() assert len(_details_list) == 2 _details = _details_list[0] assert _details.id == 1 @@ -414,7 +416,7 @@ def test_normal_2(self, mock_func, client, db): # assertion assert resp.status_code == 200 - _template = db.query(LedgerTemplate).first() + _template = db.scalars(select(LedgerTemplate).limit(1)).first() assert _template.token_address == token_address assert _template.issuer_address == issuer_address assert _template.token_name == "テスト原簿_update" @@ -438,9 +440,9 @@ def test_normal_2(self, mock_func, client, db): "f-fuga_update": "bbb_update", }, ] - _details_list = ( - db.query(LedgerDetailsTemplate).order_by(LedgerDetailsTemplate.id).all() - ) + _details_list = db.scalars( + select(LedgerDetailsTemplate).order_by(LedgerDetailsTemplate.id) + ).all() assert len(_details_list) == 2 _details = _details_list[0] assert _details.id == 1 diff --git a/tests/test_app_routers_notifications_{notice_id}_DELETE.py b/tests/test_app_routers_notifications_{notice_id}_DELETE.py index 71283e8f..22773207 100644 --- a/tests/test_app_routers_notifications_{notice_id}_DELETE.py +++ b/tests/test_app_routers_notifications_{notice_id}_DELETE.py @@ -18,6 +18,8 @@ """ from datetime import datetime +from sqlalchemy import select + from app.model.db import Notification, NotificationType from tests.account_config import config_eth_account @@ -98,7 +100,9 @@ def test_normal_1(self, client, db): # assertion assert resp.status_code == 200 assert resp.json() is None - _notification_list = db.query(Notification).order_by(Notification.created).all() + _notification_list = db.scalars( + select(Notification).order_by(Notification.created) + ).all() assert len(_notification_list) == 3 _notification = _notification_list[0] assert _notification.id == 1 diff --git a/tests/test_app_routers_share_bulk_transfer_POST.py b/tests/test_app_routers_share_bulk_transfer_POST.py index 6b213092..c5d49028 100644 --- a/tests/test_app_routers_share_bulk_transfer_POST.py +++ b/tests/test_app_routers_share_bulk_transfer_POST.py @@ -18,6 +18,8 @@ """ import hashlib +from sqlalchemy import select + from app.model.db import ( Account, AuthToken, @@ -109,21 +111,20 @@ def test_normal_1(self, client, db): # assertion assert resp.status_code == 200 - bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == resp.json()["upload_id"]) - .all() - ) + bulk_transfer_upload = db.scalars( + select(BulkTransferUpload).where( + BulkTransferUpload.upload_id == resp.json()["upload_id"] + ) + ).all() assert len(bulk_transfer_upload) == 1 assert bulk_transfer_upload[0].issuer_address == self.admin_address assert bulk_transfer_upload[0].status == 0 - bulk_transfer = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == resp.json()["upload_id"]) + bulk_transfer = db.scalars( + select(BulkTransfer) + .where(BulkTransfer.upload_id == resp.json()["upload_id"]) .order_by(BulkTransfer.id) - .all() - ) + ).all() assert len(bulk_transfer) == 2 assert bulk_transfer[0].issuer_address == self.admin_address assert bulk_transfer[0].token_address == self.req_tokens[0] @@ -194,21 +195,20 @@ def test_normal_2(self, client, db): # assertion assert resp.status_code == 200 - bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == resp.json()["upload_id"]) - .all() - ) + bulk_transfer_upload = db.scalars( + select(BulkTransferUpload).where( + BulkTransferUpload.upload_id == resp.json()["upload_id"] + ) + ).all() assert len(bulk_transfer_upload) == 1 assert bulk_transfer_upload[0].issuer_address == self.admin_address assert bulk_transfer_upload[0].status == 0 - bulk_transfer = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == resp.json()["upload_id"]) + bulk_transfer = db.scalars( + select(BulkTransfer) + .where(BulkTransfer.upload_id == resp.json()["upload_id"]) .order_by(BulkTransfer.id) - .all() - ) + ).all() assert len(bulk_transfer) == 2 assert bulk_transfer[0].issuer_address == self.admin_address assert bulk_transfer[0].token_address == self.req_tokens[0] diff --git a/tests/test_app_routers_share_tokens_POST.py b/tests/test_app_routers_share_tokens_POST.py index b39f2ec2..dff36934 100644 --- a/tests/test_app_routers_share_tokens_POST.py +++ b/tests/test_app_routers_share_tokens_POST.py @@ -22,6 +22,7 @@ from datetime import datetime, timezone from unittest.mock import ANY, patch +from sqlalchemy import select from web3 import Web3 from web3.middleware import geth_poa_middleware @@ -66,7 +67,7 @@ def test_normal_1_1(self, client, db): account.eoa_password = E2EEUtils.encrypt("password") db.add(account) - token_before = db.query(Token).all() + token_before = db.scalars(select(Token)).all() # mock IbetShareContract_create = patch( @@ -139,7 +140,7 @@ def test_normal_1_1(self, client, db): assert resp.json()["token_address"] == "contract_address_test1" assert resp.json()["token_status"] == 1 - token_after = db.query(Token).all() + token_after = db.scalars(select(Token)).all() assert 0 == len(token_before) assert 1 == len(token_after) token_1 = token_after[0] @@ -151,7 +152,7 @@ def test_normal_1_1(self, client, db): assert token_1.abi == "abi_test1" assert token_1.token_status == 1 - position = db.query(IDXPosition).first() + position = db.scalars(select(IDXPosition).limit(1)).first() assert position.token_address == "contract_address_test1" assert position.account_address == test_account["address"] assert position.balance == req_param["total_supply"] @@ -159,7 +160,7 @@ def test_normal_1_1(self, client, db): assert position.exchange_commitment == 0 assert position.pending_transfer == 0 - utxo = db.query(UTXO).first() + utxo = db.scalars(select(UTXO).limit(1)).first() assert utxo.transaction_hash == "tx_hash_test1" assert utxo.account_address == test_account["address"] assert utxo.token_address == "contract_address_test1" @@ -167,7 +168,7 @@ def test_normal_1_1(self, client, db): assert utxo.block_number == 12345 assert utxo.block_timestamp == datetime(2021, 4, 27, 12, 34, 56) - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == "contract_address_test1" assert update_token.type == TokenType.IBET_SHARE.value assert update_token.status == 1 @@ -186,7 +187,7 @@ def test_normal_1_2(self, client, db): account.eoa_password = E2EEUtils.encrypt("password") db.add(account) - token_before = db.query(Token).all() + token_before = db.scalars(select(Token)).all() # mock IbetShareContract_create = patch( @@ -244,7 +245,7 @@ def test_normal_1_2(self, client, db): assert resp.json()["token_address"] == "contract_address_test1" assert resp.json()["token_status"] == 1 - token_after = db.query(Token).all() + token_after = db.scalars(select(Token)).all() assert 0 == len(token_before) assert 1 == len(token_after) token_1 = token_after[0] @@ -256,7 +257,7 @@ def test_normal_1_2(self, client, db): assert token_1.abi == "abi_test1" assert token_1.token_status == 1 - position = db.query(IDXPosition).first() + position = db.scalars(select(IDXPosition).limit(1)).first() assert position.token_address == "contract_address_test1" assert position.account_address == test_account["address"] assert position.balance == req_param["total_supply"] @@ -264,7 +265,7 @@ def test_normal_1_2(self, client, db): assert position.exchange_commitment == 0 assert position.pending_transfer == 0 - utxo = db.query(UTXO).first() + utxo = db.scalars(select(UTXO).limit(1)).first() assert utxo.transaction_hash == "tx_hash_test1" assert utxo.account_address == test_account["address"] assert utxo.token_address == "contract_address_test1" @@ -272,7 +273,7 @@ def test_normal_1_2(self, client, db): assert utxo.block_number == 12345 assert utxo.block_timestamp == datetime(2021, 4, 27, 12, 34, 56) - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == "contract_address_test1" assert update_token.type == TokenType.IBET_SHARE.value assert update_token.status == 1 @@ -290,7 +291,7 @@ def test_normal_2(self, client, db): account.eoa_password = E2EEUtils.encrypt("password") db.add(account) - token_before = db.query(Token).all() + token_before = db.scalars(select(Token)).all() # mock IbetShareContract_create = patch( @@ -367,7 +368,7 @@ def test_normal_2(self, client, db): assert resp.json()["token_address"] == "contract_address_test1" assert resp.json()["token_status"] == 0 - token_after = db.query(Token).all() + token_after = db.scalars(select(Token)).all() assert 0 == len(token_before) assert 1 == len(token_after) token_1 = token_after[0] @@ -379,13 +380,13 @@ def test_normal_2(self, client, db): assert token_1.abi == "abi_test1" assert token_1.token_status == 0 - position = db.query(IDXPosition).first() + position = db.scalars(select(IDXPosition).limit(1)).first() assert position is None - utxo = db.query(UTXO).first() + utxo = db.scalars(select(UTXO).limit(1)).first() assert utxo is None - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.id == 1 assert update_token.token_address == "contract_address_test1" assert update_token.issuer_address == test_account["address"] @@ -412,7 +413,7 @@ def test_normal_3(self, client, db): auth_token.valid_duration = 0 db.add(auth_token) - token_before = db.query(Token).all() + token_before = db.scalars(select(Token)).all() # mock IbetShareContract_create = patch( @@ -485,7 +486,7 @@ def test_normal_3(self, client, db): assert resp.json()["token_address"] == "contract_address_test1" assert resp.json()["token_status"] == 1 - token_after = db.query(Token).all() + token_after = db.scalars(select(Token)).all() assert 0 == len(token_before) assert 1 == len(token_after) token_1 = token_after[0] @@ -497,7 +498,7 @@ def test_normal_3(self, client, db): assert token_1.abi == "abi_test1" assert token_1.token_status == 1 - position = db.query(IDXPosition).first() + position = db.scalars(select(IDXPosition).limit(1)).first() assert position.token_address == "contract_address_test1" assert position.account_address == test_account["address"] assert position.balance == req_param["total_supply"] @@ -505,7 +506,7 @@ def test_normal_3(self, client, db): assert position.exchange_commitment == 0 assert position.pending_transfer == 0 - utxo = db.query(UTXO).first() + utxo = db.scalars(select(UTXO).limit(1)).first() assert utxo.transaction_hash == "tx_hash_test1" assert utxo.account_address == test_account["address"] assert utxo.token_address == "contract_address_test1" @@ -513,7 +514,7 @@ def test_normal_3(self, client, db): assert utxo.block_number == 12345 assert utxo.block_timestamp == datetime(2021, 4, 27, 12, 34, 56) - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == "contract_address_test1" assert update_token.type == TokenType.IBET_SHARE.value assert update_token.status == 1 diff --git a/tests/test_app_routers_share_tokens_{token_address}_POST.py b/tests/test_app_routers_share_tokens_{token_address}_POST.py index 6451588c..ae5235ce 100644 --- a/tests/test_app_routers_share_tokens_{token_address}_POST.py +++ b/tests/test_app_routers_share_tokens_{token_address}_POST.py @@ -21,6 +21,7 @@ from unittest.mock import MagicMock from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from web3 import Web3 from web3.middleware import geth_poa_middleware @@ -135,14 +136,14 @@ def test_normal_1(self, client, db): assert resp.status_code == 200 assert resp.json() is None - token_attr_update = ( - db.query(TokenAttrUpdate) - .filter(TokenAttrUpdate.token_address == _token_address) - .all() - ) + token_attr_update = db.scalars( + select(TokenAttrUpdate).where( + TokenAttrUpdate.token_address == _token_address + ) + ).all() assert len(token_attr_update) == 1 - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == _token_address assert update_token.issuer_address == _issuer_address assert update_token.type == TokenType.IBET_SHARE.value @@ -237,14 +238,14 @@ def test_normal_2(self, client, db): assert resp.status_code == 200 assert resp.json() is None - token_attr_update = ( - db.query(TokenAttrUpdate) - .filter(TokenAttrUpdate.token_address == _token_address) - .all() - ) + token_attr_update = db.scalars( + select(TokenAttrUpdate).where( + TokenAttrUpdate.token_address == _token_address + ) + ).all() assert len(token_attr_update) == 0 - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token is None # @@ -317,14 +318,14 @@ def test_normal_3(self, client, db): assert resp.status_code == 200 assert resp.json() is None - token_attr_update = ( - db.query(TokenAttrUpdate) - .filter(TokenAttrUpdate.token_address == _token_address) - .all() - ) + token_attr_update = db.scalars( + select(TokenAttrUpdate).where( + TokenAttrUpdate.token_address == _token_address + ) + ).all() assert len(token_attr_update) == 1 - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == _token_address assert update_token.issuer_address == _issuer_address assert update_token.type == TokenType.IBET_SHARE.value @@ -424,14 +425,14 @@ def test_normal_4_1(self, client, db): assert resp.status_code == 200 assert resp.json() is None - token_attr_update = ( - db.query(TokenAttrUpdate) - .filter(TokenAttrUpdate.token_address == _token_address) - .all() - ) + token_attr_update = db.scalars( + select(TokenAttrUpdate).where( + TokenAttrUpdate.token_address == _token_address + ) + ).all() assert len(token_attr_update) == 1 - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == _token_address assert update_token.issuer_address == _issuer_address assert update_token.type == TokenType.IBET_SHARE.value @@ -520,14 +521,14 @@ def test_normal_4_2(self, client, db): assert resp.status_code == 200 assert resp.json() is None - token_attr_update = ( - db.query(TokenAttrUpdate) - .filter(TokenAttrUpdate.token_address == _token_address) - .all() - ) + token_attr_update = db.scalars( + select(TokenAttrUpdate).where( + TokenAttrUpdate.token_address == _token_address + ) + ).all() assert len(token_attr_update) == 1 - update_token = db.query(UpdateToken).first() + update_token = db.scalars(select(UpdateToken).limit(1)).first() assert update_token.token_address == _token_address assert update_token.issuer_address == _issuer_address assert update_token.type == TokenType.IBET_SHARE.value diff --git a/tests/test_app_routers_share_tokens_{token_address}_additional_issue_batch_POST.py b/tests/test_app_routers_share_tokens_{token_address}_additional_issue_batch_POST.py index 6ae63a95..a924fa6f 100644 --- a/tests/test_app_routers_share_tokens_{token_address}_additional_issue_batch_POST.py +++ b/tests/test_app_routers_share_tokens_{token_address}_additional_issue_batch_POST.py @@ -18,6 +18,8 @@ """ from typing import Optional +from sqlalchemy import select + from app.model.db import ( Account, BatchIssueRedeem, @@ -76,8 +78,8 @@ def test_normal_1(self, client, db): ) # assertion - upload: Optional[BatchIssueRedeemUpload] = db.query( - BatchIssueRedeemUpload + upload: Optional[BatchIssueRedeemUpload] = db.scalars( + select(BatchIssueRedeemUpload).limit(1) ).first() assert upload.issuer_address == issuer_address assert upload.token_type == TokenType.IBET_SHARE.value @@ -85,7 +87,9 @@ def test_normal_1(self, client, db): assert upload.category == BatchIssueRedeemProcessingCategory.ISSUE.value assert upload.processed is False - batch_data_list: BatchIssueRedeem = db.query(BatchIssueRedeem).all() + batch_data_list: list[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem) + ).all() assert len(batch_data_list) == 1 batch_data_1: BatchIssueRedeem = batch_data_list[0] @@ -139,8 +143,8 @@ def test_normal_2(self, client, db): ) # assertion - upload: Optional[BatchIssueRedeemUpload] = db.query( - BatchIssueRedeemUpload + upload: Optional[BatchIssueRedeemUpload] = db.scalars( + select(BatchIssueRedeemUpload).limit(1) ).first() assert upload.issuer_address == issuer_address assert upload.token_type == TokenType.IBET_SHARE.value @@ -148,7 +152,9 @@ def test_normal_2(self, client, db): assert upload.category == BatchIssueRedeemProcessingCategory.ISSUE.value assert upload.processed is False - batch_data_list: BatchIssueRedeem = db.query(BatchIssueRedeem).all() + batch_data_list: list[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem) + ).all() assert len(batch_data_list) == 2 batch_data_1: BatchIssueRedeem = batch_data_list[0] diff --git a/tests/test_app_routers_share_tokens_{token_address}_personal_info_batch_POST.py b/tests/test_app_routers_share_tokens_{token_address}_personal_info_batch_POST.py index 44b07c36..b8ae13ea 100644 --- a/tests/test_app_routers_share_tokens_{token_address}_personal_info_batch_POST.py +++ b/tests/test_app_routers_share_tokens_{token_address}_personal_info_batch_POST.py @@ -20,6 +20,8 @@ from typing import Optional from unittest.mock import ANY +from sqlalchemy import select + from app.model.db import ( Account, AuthToken, @@ -98,14 +100,14 @@ def test_normal_1(self, client, db): "created": ANY, } - _upload: Optional[BatchRegisterPersonalInfoUpload] = db.query( - BatchRegisterPersonalInfoUpload + _upload: Optional[BatchRegisterPersonalInfoUpload] = db.scalars( + select(BatchRegisterPersonalInfoUpload).limit(1) ).first() assert _upload.status == BatchRegisterPersonalInfoUploadStatus.PENDING.value assert _upload.issuer_address == _issuer_address - _register_list: list[BatchRegisterPersonalInfo] = db.query( - BatchRegisterPersonalInfo + _register_list: list[BatchRegisterPersonalInfo] = db.scalars( + select(BatchRegisterPersonalInfo) ).all() assert len(_register_list) == 10 @@ -172,14 +174,14 @@ def test_normal_2(self, client, db): "created": ANY, } - _upload: Optional[BatchRegisterPersonalInfoUpload] = db.query( - BatchRegisterPersonalInfoUpload + _upload: Optional[BatchRegisterPersonalInfoUpload] = db.scalars( + select(BatchRegisterPersonalInfoUpload).limit(1) ).first() assert _upload.status == BatchRegisterPersonalInfoUploadStatus.PENDING.value assert _upload.issuer_address == _issuer_address - _register_list: list[BatchRegisterPersonalInfo] = db.query( - BatchRegisterPersonalInfo + _register_list: list[BatchRegisterPersonalInfo] = db.scalars( + select(BatchRegisterPersonalInfo) ).all() assert len(_register_list) == 10 diff --git a/tests/test_app_routers_share_tokens_{token_address}_redeem_batch_POST.py b/tests/test_app_routers_share_tokens_{token_address}_redeem_batch_POST.py index a03a9eeb..a2ea87b9 100644 --- a/tests/test_app_routers_share_tokens_{token_address}_redeem_batch_POST.py +++ b/tests/test_app_routers_share_tokens_{token_address}_redeem_batch_POST.py @@ -18,6 +18,8 @@ """ from typing import Optional +from sqlalchemy import select + from app.model.db import ( Account, BatchIssueRedeem, @@ -76,8 +78,8 @@ def test_normal_1(self, client, db): ) # assertion - upload: Optional[BatchIssueRedeemUpload] = db.query( - BatchIssueRedeemUpload + upload: Optional[BatchIssueRedeemUpload] = db.scalars( + select(BatchIssueRedeemUpload).limit(1) ).first() assert upload.issuer_address == issuer_address assert upload.token_type == TokenType.IBET_SHARE.value @@ -85,7 +87,9 @@ def test_normal_1(self, client, db): assert upload.category == BatchIssueRedeemProcessingCategory.REDEEM.value assert upload.processed is False - batch_data_list: BatchIssueRedeem = db.query(BatchIssueRedeem).all() + batch_data_list: list[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem) + ).all() assert len(batch_data_list) == 1 batch_data_1: BatchIssueRedeem = batch_data_list[0] @@ -139,8 +143,8 @@ def test_normal_2(self, client, db): ) # assertion - upload: Optional[BatchIssueRedeemUpload] = db.query( - BatchIssueRedeemUpload + upload: Optional[BatchIssueRedeemUpload] = db.scalars( + select(BatchIssueRedeemUpload).limit(1) ).first() assert upload.issuer_address == issuer_address assert upload.token_type == TokenType.IBET_SHARE.value @@ -148,7 +152,9 @@ def test_normal_2(self, client, db): assert upload.category == BatchIssueRedeemProcessingCategory.REDEEM.value assert upload.processed is False - batch_data_list: BatchIssueRedeem = db.query(BatchIssueRedeem).all() + batch_data_list: list[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem) + ).all() assert len(batch_data_list) == 2 batch_data_1: BatchIssueRedeem = batch_data_list[0] diff --git a/tests/test_app_routers_share_tokens_{token_address}_scheduled_events_POST.py b/tests/test_app_routers_share_tokens_{token_address}_scheduled_events_POST.py index f7aa9411..883518a1 100644 --- a/tests/test_app_routers_share_tokens_{token_address}_scheduled_events_POST.py +++ b/tests/test_app_routers_share_tokens_{token_address}_scheduled_events_POST.py @@ -19,6 +19,7 @@ from datetime import datetime, timezone from pytz import timezone as tz +from sqlalchemy import and_, select from app.model.db import Account, ScheduledEvents, ScheduledEventType, Token, TokenType from app.utils.e2ee_utils import E2EEUtils @@ -93,12 +94,16 @@ def test_normal_1(self, client, db): ) # assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.issuer_address == _issuer_address) - .filter(ScheduledEvents.token_address == _token_address) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where( + and_( + ScheduledEvents.issuer_address == _issuer_address, + ScheduledEvents.token_address == _token_address, + ) + ) + .limit(1) + ).first() assert resp_1.status_code == 200 assert resp_1.json() == {"scheduled_event_id": _scheduled_event.event_id} assert _scheduled_event.token_type == TokenType.IBET_SHARE.value @@ -169,12 +174,12 @@ def test_normal_2(self, client, db): ) # assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.issuer_address == _issuer_address) - .filter(ScheduledEvents.token_address == _token_address) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.issuer_address == _issuer_address) + .where(ScheduledEvents.token_address == _token_address) + .limit(1) + ).first() assert resp.status_code == 200 assert resp.json() == {"scheduled_event_id": _scheduled_event.event_id} assert _scheduled_event.token_type == TokenType.IBET_SHARE.value diff --git a/tests/test_app_routers_share_tokens_{token_address}_scheduled_events_{scheduled_event_id}_DELETE.py b/tests/test_app_routers_share_tokens_{token_address}_scheduled_events_{scheduled_event_id}_DELETE.py index 0f1b4e43..68c55388 100644 --- a/tests/test_app_routers_share_tokens_{token_address}_scheduled_events_{scheduled_event_id}_DELETE.py +++ b/tests/test_app_routers_share_tokens_{token_address}_scheduled_events_{scheduled_event_id}_DELETE.py @@ -20,6 +20,7 @@ from datetime import datetime from pytz import timezone +from sqlalchemy import select from app.model.db import Account, ScheduledEvents, ScheduledEventType, TokenType from app.utils.e2ee_utils import E2EEUtils @@ -107,11 +108,9 @@ def test_normal_1(self, client, db): "data": data, "created": datetime_now_str, } - token_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.event_id == event_id) - .first() - ) + token_event = db.scalars( + select(ScheduledEvents).where(ScheduledEvents.event_id == event_id).limit(1) + ).first() assert token_event is None ######################################################################### diff --git a/tests/test_app_routers_share_transfer_approvals_{token_Address}_{id}_POST.py b/tests/test_app_routers_share_transfer_approvals_{token_Address}_{id}_POST.py index a5eb7160..0f1e47be 100644 --- a/tests/test_app_routers_share_transfer_approvals_{token_Address}_{id}_POST.py +++ b/tests/test_app_routers_share_transfer_approvals_{token_Address}_{id}_POST.py @@ -23,6 +23,7 @@ import pytest from pytz import timezone +from sqlalchemy import select import config from app.exceptions import ContractRevertError, SendTransactionError @@ -136,8 +137,8 @@ def test_normal_1_1(self, client, db): private_key=ANY, ) - approval_op_list: list[TransferApprovalHistory] = db.query( - TransferApprovalHistory + approval_op_list: list[TransferApprovalHistory] = db.scalars( + select(TransferApprovalHistory) ).all() assert len(approval_op_list) == 1 approval_op = approval_op_list[0] @@ -218,8 +219,8 @@ def test_normal_1_2(self, client, db): private_key=ANY, ) - approval_op_list: list[TransferApprovalHistory] = db.query( - TransferApprovalHistory + approval_op_list: list[TransferApprovalHistory] = db.scalars( + select(TransferApprovalHistory) ).all() assert len(approval_op_list) == 1 approval_op = approval_op_list[0] @@ -299,8 +300,8 @@ def test_normal_2_1(self, client, db): private_key=ANY, ) - cancel_op_list: list[TransferApprovalHistory] = db.query( - TransferApprovalHistory + cancel_op_list: list[TransferApprovalHistory] = db.scalars( + select(TransferApprovalHistory) ).all() assert len(cancel_op_list) == 1 cancel_op = cancel_op_list[0] diff --git a/tests/test_app_routers_token_holders_{token_address}_collection_POST.py b/tests/test_app_routers_token_holders_{token_address}_collection_POST.py index 3e01f4f8..5783c7ae 100644 --- a/tests/test_app_routers_token_holders_{token_address}_collection_POST.py +++ b/tests/test_app_routers_token_holders_{token_address}_collection_POST.py @@ -19,6 +19,7 @@ import uuid from unittest import mock +from sqlalchemy import select from web3 import Web3 from web3.middleware import geth_poa_middleware @@ -68,11 +69,9 @@ def test_normal_1(self, client, db): headers={"issuer-address": issuer_address}, ) - stored_data: TokenHoldersList = ( - db.query(TokenHoldersList) - .filter(TokenHoldersList.list_id == list_id) - .first() - ) + stored_data: TokenHoldersList = db.scalars( + select(TokenHoldersList).where(TokenHoldersList.list_id == list_id).limit(1) + ).first() # assertion assert resp.status_code == 200 assert resp.json() == { diff --git a/tests/test_app_routers_token_holders_{token_address}_collection_{collection_id}_GET.py b/tests/test_app_routers_token_holders_{token_address}_collection_{collection_id}_GET.py index 2f735bcb..82b158d2 100644 --- a/tests/test_app_routers_token_holders_{token_address}_collection_{collection_id}_GET.py +++ b/tests/test_app_routers_token_holders_{token_address}_collection_{collection_id}_GET.py @@ -19,6 +19,7 @@ import uuid from unittest import mock +from sqlalchemy import select from web3 import Web3 from web3.middleware import geth_poa_middleware @@ -128,7 +129,7 @@ def test_normal_2(self, client, db): self.base_url.format(token_address=token_address, list_id=list_id), headers={"issuer-address": issuer_address}, ) - db.query(TokenHolder).filter().all() + db.scalars(select(TokenHolder)).all() sorted_holders = sorted(holders, key=lambda x: x["account_address"]) # assertion assert resp.status_code == 200 diff --git a/tests/test_batch_indexer_block_tx_data.py b/tests/test_batch_indexer_block_tx_data.py index eb7c1937..c20b4cc0 100644 --- a/tests/test_batch_indexer_block_tx_data.py +++ b/tests/test_batch_indexer_block_tx_data.py @@ -22,6 +22,7 @@ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session from utils.contract_utils import IbetStandardTokenUtils @@ -74,17 +75,17 @@ def test_normal_1(self, processor, db, caplog): processor.process() # Assertion - indexed_block = ( - db.query(IDXBlockDataBlockNumber) - .filter(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) - .first() - ) + indexed_block = db.scalars( + select(IDXBlockDataBlockNumber) + .where(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) + .limit(1) + ).first() assert indexed_block.latest_block_number == before_block_number - block_data = db.query(IDXBlockData).all() + block_data = db.scalars(select(IDXBlockData)).all() assert len(block_data) == 0 - tx_data = db.query(IDXTxData).all() + tx_data = db.scalars(select(IDXTxData)).all() assert len(tx_data) == 0 assert 1 == caplog.record_tuples.count( @@ -105,18 +106,18 @@ def test_normal_2(self, processor, db, caplog): after_block_number = web3.eth.block_number # Assertion: Data - indexed_block = ( - db.query(IDXBlockDataBlockNumber) - .filter(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) - .first() - ) + indexed_block = db.scalars( + select(IDXBlockDataBlockNumber) + .where(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) + .limit(1) + ).first() assert indexed_block.latest_block_number == after_block_number - block_data: list[IDXBlockData] = db.query(IDXBlockData).all() + block_data: list[IDXBlockData] = db.scalars(select(IDXBlockData)).all() assert len(block_data) == 1 assert block_data[0].number == before_block_number + 1 - tx_data = db.query(IDXTxData).all() + tx_data = db.scalars(select(IDXTxData)).all() assert len(tx_data) == 0 # Assertion: Log @@ -162,19 +163,19 @@ def test_normal_3_1(self, processor, db, caplog): after_block_number = web3.eth.block_number # Assertion - indexed_block = ( - db.query(IDXBlockDataBlockNumber) - .filter(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) - .first() - ) + indexed_block = db.scalars( + select(IDXBlockDataBlockNumber) + .where(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) + .limit(1) + ).first() assert indexed_block.latest_block_number == after_block_number - block_data: list[IDXBlockData] = db.query(IDXBlockData).all() + block_data: list[IDXBlockData] = db.scalars(select(IDXBlockData)).all() assert len(block_data) == 1 assert block_data[0].number == before_block_number + 1 assert len(block_data[0].transactions) == 1 - tx_data: list[IDXTxData] = db.query(IDXTxData).all() + tx_data: list[IDXTxData] = db.scalars(select(IDXTxData)).all() assert len(tx_data) == 1 assert tx_data[0].block_hash == block_data[0].hash assert tx_data[0].block_number == before_block_number + 1 @@ -220,16 +221,16 @@ def test_normal_3_2(self, processor, db, caplog): after_block_number = web3.eth.block_number # Assertion - indexed_block = ( - db.query(IDXBlockDataBlockNumber) - .filter(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) - .first() - ) + indexed_block = db.scalars( + select(IDXBlockDataBlockNumber) + .where(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) + .limit(1) + ).first() assert indexed_block.latest_block_number == after_block_number - block_data: list[IDXBlockData] = ( - db.query(IDXBlockData).order_by(IDXBlockData.number).all() - ) + block_data: list[IDXBlockData] = db.scalars( + select(IDXBlockData).order_by(IDXBlockData.number) + ).all() assert len(block_data) == 2 assert block_data[0].number == before_block_number + 1 @@ -238,7 +239,7 @@ def test_normal_3_2(self, processor, db, caplog): assert block_data[1].number == before_block_number + 2 assert len(block_data[1].transactions) == 1 - tx_data: list[IDXTxData] = db.query(IDXTxData).all() + tx_data: list[IDXTxData] = db.scalars(select(IDXTxData)).all() assert len(tx_data) == 2 assert tx_data[0].block_hash == block_data[0].hash @@ -271,17 +272,17 @@ def test_error_1(self, processor, db): processor.process() # Assertion - indexed_block = ( - db.query(IDXBlockDataBlockNumber) - .filter(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) - .first() - ) + indexed_block = db.scalars( + select(IDXBlockDataBlockNumber) + .where(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) + .limit(1) + ).first() assert indexed_block.latest_block_number == before_block_number - block_data = db.query(IDXBlockData).all() + block_data = db.scalars(select(IDXBlockData)).all() assert len(block_data) == 0 - tx_data = db.query(IDXTxData).all() + tx_data = db.scalars(select(IDXTxData)).all() assert len(tx_data) == 0 # Error_2: SQLAlchemyError @@ -299,15 +300,15 @@ def test_error_2(self, processor, db): processor.process() # Assertion - indexed_block = ( - db.query(IDXBlockDataBlockNumber) - .filter(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) - .first() - ) + indexed_block = db.scalars( + select(IDXBlockDataBlockNumber) + .where(IDXBlockDataBlockNumber.chain_id == str(CHAIN_ID)) + .limit(1) + ).first() assert indexed_block.latest_block_number == before_block_number - block_data = db.query(IDXBlockData).all() + block_data = db.scalars(select(IDXBlockData)).all() assert len(block_data) == 0 - tx_data = db.query(IDXTxData).all() + tx_data = db.scalars(select(IDXTxData)).all() assert len(tx_data) == 0 diff --git a/tests/test_batch_indexer_e2e_messaging.py b/tests/test_batch_indexer_e2e_messaging.py index 09e024f6..58076414 100644 --- a/tests/test_batch_indexer_e2e_messaging.py +++ b/tests/test_batch_indexer_e2e_messaging.py @@ -28,6 +28,7 @@ from Crypto.PublicKey import RSA from Crypto.Util.Padding import pad from eth_keyfile import decode_keyfile_json +from sqlalchemy import select import batch.indexer_e2e_messaging as indexer_e2e_messaging from app.model.blockchain import E2EMessaging @@ -143,11 +144,13 @@ def test_normal_1(self, processor, db): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -206,9 +209,9 @@ def test_normal_2_1(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 1 _e2e_messaging = _e2e_messaging_list[0] assert _e2e_messaging.id == 1 @@ -219,7 +222,9 @@ def test_normal_2_1(self, processor, db, e2e_messaging_contract): assert _e2e_messaging.message == message_message_str assert _e2e_messaging.send_timestamp == sending_block_timestamp assert _e2e_messaging.block_timestamp == sending_block_timestamp - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -318,9 +323,9 @@ def test_normal_2_2(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 1 _e2e_messaging = _e2e_messaging_list[0] assert _e2e_messaging.id == 1 @@ -331,7 +336,9 @@ def test_normal_2_2(self, processor, db, e2e_messaging_contract): assert _e2e_messaging.message == message_message_str assert _e2e_messaging.send_timestamp == sending_block_timestamp assert _e2e_messaging.block_timestamp == sending_block_timestamp - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -464,9 +471,9 @@ def test_normal_3(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 4 _e2e_messaging = _e2e_messaging_list[0] assert _e2e_messaging.id == 1 @@ -504,7 +511,9 @@ def test_normal_3(self, processor, db, e2e_messaging_contract): assert _e2e_messaging.message == message_message_str_4 assert _e2e_messaging.send_timestamp == sending_block_timestamp_4 assert _e2e_messaging.block_timestamp == sending_block_timestamp_4 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -560,11 +569,13 @@ def test_normal_4(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -613,11 +624,13 @@ def test_error_1_1(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -679,11 +692,13 @@ def test_error_1_2(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -732,11 +747,13 @@ def test_error_1_3(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -796,11 +813,13 @@ def test_error_1_4(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -859,11 +878,13 @@ def test_error_1_5(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -919,11 +940,13 @@ def test_error_1_6(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -983,11 +1006,13 @@ def test_error_1_7(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -1043,11 +1068,13 @@ def test_error_2(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -1104,11 +1131,13 @@ def test_error_3_1(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -1166,11 +1195,13 @@ def test_error_3_2(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -1233,11 +1264,13 @@ def test_error_3_3(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -1291,11 +1324,13 @@ def test_error_3_4(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number @@ -1356,10 +1391,12 @@ def test_error_3_5(self, processor, db, e2e_messaging_contract): processor.process() # Assertion - _e2e_messaging_list = ( - db.query(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp).all() - ) + _e2e_messaging_list = db.scalars( + select(IDXE2EMessaging).order_by(IDXE2EMessaging.block_timestamp) + ).all() assert len(_e2e_messaging_list) == 0 - _idx_e2e_messaging_block_number = db.query(IDXE2EMessagingBlockNumber).first() + _idx_e2e_messaging_block_number = db.scalars( + select(IDXE2EMessagingBlockNumber).limit(1) + ).first() assert _idx_e2e_messaging_block_number.id == 1 assert _idx_e2e_messaging_block_number.latest_block_number == block_number diff --git a/tests/test_batch_indexer_issue_redeem.py b/tests/test_batch_indexer_issue_redeem.py index 121a0cb4..69714144 100644 --- a/tests/test_batch_indexer_issue_redeem.py +++ b/tests/test_batch_indexer_issue_redeem.py @@ -23,6 +23,7 @@ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from sqlalchemy.exc import InvalidRequestError from sqlalchemy.orm import Session @@ -168,10 +169,12 @@ def test_normal_1(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - event_list = db.query(IDXIssueRedeem).all() + event_list = db.scalars(select(IDXIssueRedeem)).all() assert len(event_list) == 0 - idx_block_number = db.query(IDXIssueRedeemBlockNumber).first() + idx_block_number = db.scalars( + select(IDXIssueRedeemBlockNumber).limit(1) + ).first() assert idx_block_number.id == 1 assert idx_block_number.latest_block_number == block_number @@ -207,10 +210,12 @@ def test_normal_2(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - event_list = db.query(IDXIssueRedeem).all() + event_list = db.scalars(select(IDXIssueRedeem)).all() assert len(event_list) == 0 - idx_block_number = db.query(IDXIssueRedeemBlockNumber).first() + idx_block_number = db.scalars( + select(IDXIssueRedeemBlockNumber).limit(1) + ).first() assert idx_block_number.id == 1 assert idx_block_number.latest_block_number == block_number @@ -260,7 +265,7 @@ def test_normal_3_1(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - event_list: list[IDXIssueRedeem] = db.query(IDXIssueRedeem).all() + event_list: list[IDXIssueRedeem] = db.scalars(select(IDXIssueRedeem)).all() assert len(event_list) == 1 event_0 = event_list[0] assert event_0.id == 1 @@ -273,7 +278,9 @@ def test_normal_3_1(self, processor, db, personal_info_contract): block = web3.eth.get_block(tx_receipt_1["blockNumber"]) assert event_0.block_timestamp == datetime.utcfromtimestamp(block["timestamp"]) - idx_block_number = db.query(IDXIssueRedeemBlockNumber).first() + idx_block_number = db.scalars( + select(IDXIssueRedeemBlockNumber).limit(1) + ).first() assert idx_block_number.id == 1 assert idx_block_number.latest_block_number == block_number @@ -323,7 +330,7 @@ def test_normal_3_2(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - event_list: list[IDXIssueRedeem] = db.query(IDXIssueRedeem).all() + event_list: list[IDXIssueRedeem] = db.scalars(select(IDXIssueRedeem)).all() assert len(event_list) == 1 event_0 = event_list[0] assert event_0.id == 1 @@ -336,7 +343,9 @@ def test_normal_3_2(self, processor, db, personal_info_contract): block = web3.eth.get_block(tx_receipt_1["blockNumber"]) assert event_0.block_timestamp == datetime.utcfromtimestamp(block["timestamp"]) - idx_block_number = db.query(IDXIssueRedeemBlockNumber).first() + idx_block_number = db.scalars( + select(IDXIssueRedeemBlockNumber).limit(1) + ).first() assert idx_block_number.id == 1 assert idx_block_number.latest_block_number == block_number @@ -386,7 +395,7 @@ def test_normal_4_1(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - event_list: list[IDXIssueRedeem] = db.query(IDXIssueRedeem).all() + event_list: list[IDXIssueRedeem] = db.scalars(select(IDXIssueRedeem)).all() assert len(event_list) == 1 event_0 = event_list[0] assert event_0.id == 1 @@ -399,7 +408,9 @@ def test_normal_4_1(self, processor, db, personal_info_contract): block = web3.eth.get_block(tx_receipt_1["blockNumber"]) assert event_0.block_timestamp == datetime.utcfromtimestamp(block["timestamp"]) - idx_block_number = db.query(IDXIssueRedeemBlockNumber).first() + idx_block_number = db.scalars( + select(IDXIssueRedeemBlockNumber).limit(1) + ).first() assert idx_block_number.id == 1 assert idx_block_number.latest_block_number == block_number @@ -449,7 +460,7 @@ def test_normal_4_2(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - event_list: list[IDXIssueRedeem] = db.query(IDXIssueRedeem).all() + event_list: list[IDXIssueRedeem] = db.scalars(select(IDXIssueRedeem)).all() assert len(event_list) == 1 event_0 = event_list[0] assert event_0.id == 1 @@ -462,7 +473,9 @@ def test_normal_4_2(self, processor, db, personal_info_contract): block = web3.eth.get_block(tx_receipt_1["blockNumber"]) assert event_0.block_timestamp == datetime.utcfromtimestamp(block["timestamp"]) - idx_block_number = db.query(IDXIssueRedeemBlockNumber).first() + idx_block_number = db.scalars( + select(IDXIssueRedeemBlockNumber).limit(1) + ).first() assert idx_block_number.id == 1 assert idx_block_number.latest_block_number == block_number @@ -523,7 +536,7 @@ def test_normal_5(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - event_list: list[IDXIssueRedeem] = db.query(IDXIssueRedeem).all() + event_list: list[IDXIssueRedeem] = db.scalars(select(IDXIssueRedeem)).all() assert len(event_list) == 2 event_0 = event_list[0] @@ -548,7 +561,9 @@ def test_normal_5(self, processor, db, personal_info_contract): block = web3.eth.get_block(tx_receipt_2["blockNumber"]) assert event_1.block_timestamp == datetime.utcfromtimestamp(block["timestamp"]) - idx_block_number = db.query(IDXIssueRedeemBlockNumber).first() + idx_block_number = db.scalars( + select(IDXIssueRedeemBlockNumber).limit(1) + ).first() assert idx_block_number.id == 1 assert idx_block_number.latest_block_number == block_number diff --git a/tests/test_batch_indexer_personal_info.py b/tests/test_batch_indexer_personal_info.py index 0b1b484b..3b29a1f0 100644 --- a/tests/test_batch_indexer_personal_info.py +++ b/tests/test_batch_indexer_personal_info.py @@ -26,6 +26,7 @@ from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from sqlalchemy.exc import InvalidRequestError from sqlalchemy.orm import Session @@ -192,9 +193,11 @@ def test_normal_1_1(self, processor, db, personal_info_contract): processor.process() # Assertion - _personal_info_list = db.query(IDXPersonalInfo).all() + _personal_info_list = db.scalars(select(IDXPersonalInfo)).all() assert len(_personal_info_list) == 0 - _idx_personal_info_block_number = db.query(IDXPersonalInfoBlockNumber).first() + _idx_personal_info_block_number = db.scalars( + select(IDXPersonalInfoBlockNumber).limit(1) + ).first() assert _idx_personal_info_block_number.id == 1 assert _idx_personal_info_block_number.latest_block_number == block_number @@ -256,9 +259,11 @@ def test_normal_1_2(self, processor, db, personal_info_contract): processor.process() # Assertion - _personal_info_list = db.query(IDXPersonalInfo).all() + _personal_info_list = db.scalars(select(IDXPersonalInfo)).all() assert len(_personal_info_list) == 0 - _idx_personal_info_block_number = db.query(IDXPersonalInfoBlockNumber).first() + _idx_personal_info_block_number = db.scalars( + select(IDXPersonalInfoBlockNumber).limit(1) + ).first() assert _idx_personal_info_block_number.id == 1 assert _idx_personal_info_block_number.latest_block_number == block_number @@ -346,14 +351,16 @@ def test_normal_2_1(self, processor, db, personal_info_contract): processor.process() # Assertion - _personal_info_list = db.query(IDXPersonalInfo).all() + _personal_info_list = db.scalars(select(IDXPersonalInfo)).all() assert len(_personal_info_list) == 1 _personal_info = _personal_info_list[0] assert _personal_info.id == 1 assert _personal_info.account_address == user_address_1 assert _personal_info.issuer_address == issuer_address assert _personal_info.personal_info == personal_info_1 - _idx_personal_info_block_number = db.query(IDXPersonalInfoBlockNumber).first() + _idx_personal_info_block_number = db.scalars( + select(IDXPersonalInfoBlockNumber).limit(1) + ).first() assert _idx_personal_info_block_number.id == 1 assert _idx_personal_info_block_number.latest_block_number == block_number @@ -438,7 +445,7 @@ def test_normal_2_2(self, processor, db, personal_info_contract): # Before run(consume accumulated events) processor.process() - _personal_info_list = db.query(IDXPersonalInfo).all() + _personal_info_list = db.scalars(select(IDXPersonalInfo)).all() assert len(_personal_info_list) == 1 _personal_info = _personal_info_list[0] assert _personal_info.id == 1 @@ -481,14 +488,16 @@ def test_normal_2_2(self, processor, db, personal_info_contract): db.expire_all() # Assertion - _personal_info_list = db.query(IDXPersonalInfo).all() + _personal_info_list = db.scalars(select(IDXPersonalInfo)).all() assert len(_personal_info_list) == 1 _personal_info = _personal_info_list[0] assert _personal_info.id == 1 assert _personal_info.account_address == user_address_1 assert _personal_info.issuer_address == issuer_address assert _personal_info.personal_info == personal_info_2 - _idx_personal_info_block_number = db.query(IDXPersonalInfoBlockNumber).first() + _idx_personal_info_block_number = db.scalars( + select(IDXPersonalInfoBlockNumber).limit(1) + ).first() assert _idx_personal_info_block_number.id == 1 assert _idx_personal_info_block_number.latest_block_number == block_number @@ -573,7 +582,7 @@ def test_normal_3(self, processor, db, personal_info_contract): # Before run(consume accumulated events) processor.process() - _personal_info_list = db.query(IDXPersonalInfo).all() + _personal_info_list = db.scalars(select(IDXPersonalInfo)).all() assert len(_personal_info_list) == 1 _personal_info = _personal_info_list[0] assert _personal_info.id == 1 @@ -616,14 +625,16 @@ def test_normal_3(self, processor, db, personal_info_contract): db.expire_all() # Assertion - _personal_info_list = db.query(IDXPersonalInfo).all() + _personal_info_list = db.scalars(select(IDXPersonalInfo)).all() assert len(_personal_info_list) == 1 _personal_info = _personal_info_list[0] assert _personal_info.id == 1 assert _personal_info.account_address == user_address_1 assert _personal_info.issuer_address == issuer_address assert _personal_info.personal_info == personal_info_2 - _idx_personal_info_block_number = db.query(IDXPersonalInfoBlockNumber).first() + _idx_personal_info_block_number = db.scalars( + select(IDXPersonalInfoBlockNumber).limit(1) + ).first() assert _idx_personal_info_block_number.id == 1 assert _idx_personal_info_block_number.latest_block_number == block_number @@ -662,14 +673,16 @@ def test_normal_3(self, processor, db, personal_info_contract): db.expire_all() # Assertion - _personal_info_list = db.query(IDXPersonalInfo).all() + _personal_info_list = db.scalars(select(IDXPersonalInfo)).all() assert len(_personal_info_list) == 1 _personal_info = _personal_info_list[0] assert _personal_info.id == 1 assert _personal_info.account_address == user_address_1 assert _personal_info.issuer_address == issuer_address assert _personal_info.personal_info == personal_info_3 - _idx_personal_info_block_number = db.query(IDXPersonalInfoBlockNumber).first() + _idx_personal_info_block_number = db.scalars( + select(IDXPersonalInfoBlockNumber).limit(1) + ).first() assert _idx_personal_info_block_number.id == 1 assert _idx_personal_info_block_number.latest_block_number == block_number @@ -834,7 +847,7 @@ def test_normal_4(self, processor, db): stored_address_order = [line["issuer_address"] for line in unique_list] # Assertion - _personal_info_list = db.query(IDXPersonalInfo).all() + _personal_info_list = db.scalars(select(IDXPersonalInfo)).all() assert len(_personal_info_list) == 2 for i in range(2): @@ -847,7 +860,9 @@ def test_normal_4(self, processor, db): == personal_info_dict[stored_address_order[i]] ) - _idx_personal_info_block_number = db.query(IDXPersonalInfoBlockNumber).first() + _idx_personal_info_block_number = db.scalars( + select(IDXPersonalInfoBlockNumber).limit(1) + ).first() assert _idx_personal_info_block_number.id == 1 assert _idx_personal_info_block_number.latest_block_number == block_number diff --git a/tests/test_batch_indexer_position_bond.py b/tests/test_batch_indexer_position_bond.py index 642933e5..dcc6d482 100644 --- a/tests/test_batch_indexer_position_bond.py +++ b/tests/test_batch_indexer_position_bond.py @@ -22,6 +22,7 @@ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import and_, select from sqlalchemy.exc import InvalidRequestError from sqlalchemy.orm import Session @@ -153,9 +154,11 @@ def test_normal_1_1( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -216,20 +219,22 @@ def test_normal_1_2( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -300,31 +305,33 @@ def test_normal_2_1( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 2 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -395,31 +402,33 @@ def test_normal_2_2_1( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 2 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -495,20 +504,22 @@ def test_normal_2_2_2( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 40 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -583,13 +594,13 @@ def test_normal_2_2_3( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -631,31 +642,33 @@ def test_normal_2_2_3( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 2 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 40 - 30 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 0 assert _position.exchange_balance == 30 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -724,14 +737,14 @@ def test_normal_2_3( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -739,18 +752,22 @@ def test_normal_2_3( assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _locked_position = ( - db.query(IDXLockedPosition) - .filter(IDXLockedPosition.token_address == token_address_1) - .filter(IDXLockedPosition.account_address == issuer_address) - .first() - ) + _locked_position = db.scalars( + select(IDXLockedPosition) + .where( + and_( + IDXLockedPosition.token_address == token_address_1, + IDXLockedPosition.account_address == issuer_address, + ) + ) + .limit(1) + ).first() assert _locked_position.token_address == token_address_1 assert _locked_position.lock_address == issuer_address assert _locked_position.account_address == issuer_address assert _locked_position.value == 40 - _lock_list = db.query(IDXLock).order_by(IDXLock.id).all() + _lock_list = db.scalars(select(IDXLock).order_by(IDXLock.id)).all() assert len(_lock_list) == 1 _lock1 = _lock_list[0] @@ -762,7 +779,9 @@ def test_normal_2_3( assert _lock1.value == 40 assert _lock1.data == {"message": "locked1"} - _notification_list = db.query(Notification).order_by(Notification.created).all() + _notification_list = db.scalars( + select(Notification).order_by(Notification.created) + ).all() assert len(_notification_list) == 1 _notification1 = _notification_list[0] @@ -779,7 +798,9 @@ def test_normal_2_3( "data": {"message": "locked1"}, } - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -846,14 +867,14 @@ def test_normal_2_4( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -883,14 +904,14 @@ def test_normal_2_4( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 + 30 @@ -898,18 +919,22 @@ def test_normal_2_4( assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _locked_position = ( - db.query(IDXLockedPosition) - .filter(IDXLockedPosition.token_address == token_address_1) - .filter(IDXLockedPosition.account_address == issuer_address) - .first() - ) + _locked_position = db.scalars( + select(IDXLockedPosition) + .where( + and_( + IDXLockedPosition.token_address == token_address_1, + IDXLockedPosition.account_address == issuer_address, + ) + ) + .limit(1) + ).first() assert _locked_position.token_address == token_address_1 assert _locked_position.lock_address == issuer_address assert _locked_position.account_address == issuer_address assert _locked_position.value == 40 - 30 - _lock_list = db.query(IDXLock).order_by(IDXLock.id).all() + _lock_list = db.scalars(select(IDXLock).order_by(IDXLock.id)).all() assert len(_lock_list) == 1 _lock1 = _lock_list[0] @@ -921,7 +946,7 @@ def test_normal_2_4( assert _lock1.value == 40 assert _lock1.data == {"message": "locked1"} - _unlock_list = db.query(IDXUnlock).order_by(IDXUnlock.id).all() + _unlock_list = db.scalars(select(IDXUnlock).order_by(IDXUnlock.id)).all() assert len(_unlock_list) == 1 _unlock1 = _unlock_list[0] @@ -934,7 +959,9 @@ def test_normal_2_4( assert _unlock1.value == 30 assert _unlock1.data == {"message": "unlocked1"} - _notification_list = db.query(Notification).order_by(Notification.created).all() + _notification_list = db.scalars( + select(Notification).order_by(Notification.created) + ).all() assert len(_notification_list) == 2 _notification1 = _notification_list[0] @@ -966,7 +993,9 @@ def test_normal_2_4( "data": {"message": "unlocked1"}, } - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -1035,20 +1064,22 @@ def test_normal_2_5( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -1131,20 +1162,22 @@ def test_normal_2_6( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 40 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -1222,13 +1255,13 @@ def test_normal_2_7(self, processor, db, personal_info_contract): # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -1256,20 +1289,22 @@ def test_normal_2_7(self, processor, db, personal_info_contract): db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -1349,13 +1384,13 @@ def test_normal_2_8( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -1383,31 +1418,33 @@ def test_normal_2_8( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 2 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -1480,13 +1517,13 @@ def test_normal_2_9_1( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -1516,20 +1553,22 @@ def test_normal_2_9_1( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 40 - 30 assert _position.exchange_commitment == 30 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -1640,42 +1679,44 @@ def test_normal_2_9_2( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 30 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -1789,42 +1830,44 @@ def test_normal_2_9_3( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 30 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -1938,42 +1981,44 @@ def test_normal_2_9_4( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 20 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 10 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -2096,42 +2141,44 @@ def test_normal_2_9_5( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 20 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 20 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -2254,42 +2301,44 @@ def test_normal_2_9_6( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 20 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 10 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -2364,13 +2413,13 @@ def test_normal_2_10_1( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -2400,20 +2449,22 @@ def test_normal_2_10_1( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 40 - 30 assert _position.exchange_commitment == 30 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -2525,42 +2576,44 @@ def test_normal_2_10_2( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 0 assert _position.exchange_balance == 30 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -2674,42 +2727,44 @@ def test_normal_2_10_3( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 0 assert _position.exchange_balance == 20 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 10 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -2814,42 +2869,44 @@ def test_normal_3_1( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 40 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -2978,68 +3035,72 @@ def test_normal_3_2( block_number = web3.eth.block_number processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 5 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 60 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 11 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 22 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_3) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_3) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_3 assert _position.balance == 33 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_4) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_4) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_4 assert _position.balance == 4 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -3165,42 +3226,44 @@ def test_normal_3_3( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 30 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -3313,42 +3376,44 @@ def test_normal_3_4( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 0 assert _position.exchange_balance == 30 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number @@ -3467,81 +3532,107 @@ def test_normal_4( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 6 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .filter(IDXPosition.token_address == token_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == issuer_address, + IDXPosition.token_address == token_address_1, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .filter(IDXPosition.token_address == token_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == user_address_1, + IDXPosition.token_address == token_address_1, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 30 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .filter(IDXPosition.token_address == token_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == user_address_2, + IDXPosition.token_address == token_address_1, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .filter(IDXPosition.token_address == token_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == issuer_address, + IDXPosition.token_address == token_address_2, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_2 assert _position.account_address == issuer_address assert _position.balance == 0 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .filter(IDXPosition.token_address == token_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == user_address_1, + IDXPosition.token_address == token_address_2, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_2 assert _position.account_address == user_address_1 assert _position.balance == 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .filter(IDXPosition.token_address == token_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == user_address_2, + IDXPosition.token_address == token_address_2, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_2 assert _position.account_address == user_address_2 assert _position.balance == 60 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_bond_block_number = db.query(IDXPositionBondBlockNumber).first() + _idx_position_bond_block_number = db.scalars( + select(IDXPositionBondBlockNumber).limit(1) + ).first() assert _idx_position_bond_block_number.id == 1 assert _idx_position_bond_block_number.latest_block_number == block_number diff --git a/tests/test_batch_indexer_position_share.py b/tests/test_batch_indexer_position_share.py index 9c0e3bdd..d0779055 100644 --- a/tests/test_batch_indexer_position_share.py +++ b/tests/test_batch_indexer_position_share.py @@ -22,6 +22,7 @@ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import and_, select from sqlalchemy.exc import InvalidRequestError from sqlalchemy.orm import Session @@ -153,9 +154,11 @@ def test_normal_1_1( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -216,20 +219,22 @@ def test_normal_1_2( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -300,31 +305,33 @@ def test_normal_2_1( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 2 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -395,31 +402,33 @@ def test_normal_2_2_1( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 2 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -495,20 +504,22 @@ def test_normal_2_2_2( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 40 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -583,13 +594,13 @@ def test_normal_2_2_3( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -631,31 +642,33 @@ def test_normal_2_2_3( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 2 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 40 - 30 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 0 assert _position.exchange_balance == 30 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -724,14 +737,14 @@ def test_normal_2_3( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -739,18 +752,22 @@ def test_normal_2_3( assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _locked_position = ( - db.query(IDXLockedPosition) - .filter(IDXLockedPosition.token_address == token_address_1) - .filter(IDXLockedPosition.account_address == issuer_address) - .first() - ) + _locked_position = db.scalars( + select(IDXLockedPosition) + .where( + and_( + IDXLockedPosition.token_address == token_address_1, + IDXLockedPosition.account_address == issuer_address, + ) + ) + .limit(1) + ).first() assert _locked_position.token_address == token_address_1 assert _locked_position.lock_address == issuer_address assert _locked_position.account_address == issuer_address assert _locked_position.value == 40 - _lock_list = db.query(IDXLock).order_by(IDXLock.id).all() + _lock_list = db.scalars(select(IDXLock).order_by(IDXLock.id)).all() assert len(_lock_list) == 1 _lock1 = _lock_list[0] @@ -762,7 +779,9 @@ def test_normal_2_3( assert _lock1.value == 40 assert _lock1.data == {"message": "locked1"} - _notification_list = db.query(Notification).order_by(Notification.created).all() + _notification_list = db.scalars( + select(Notification).order_by(Notification.created) + ).all() assert len(_notification_list) == 1 _notification1 = _notification_list[0] @@ -779,7 +798,9 @@ def test_normal_2_3( "data": {"message": "locked1"}, } - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -846,14 +867,14 @@ def test_normal_2_4( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -883,14 +904,14 @@ def test_normal_2_4( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 + 30 @@ -898,18 +919,22 @@ def test_normal_2_4( assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _locked_position = ( - db.query(IDXLockedPosition) - .filter(IDXLockedPosition.token_address == token_address_1) - .filter(IDXLockedPosition.account_address == issuer_address) - .first() - ) + _locked_position = db.scalars( + select(IDXLockedPosition) + .where( + and_( + IDXLockedPosition.token_address == token_address_1, + IDXLockedPosition.account_address == issuer_address, + ) + ) + .limit(1) + ).first() assert _locked_position.token_address == token_address_1 assert _locked_position.lock_address == issuer_address assert _locked_position.account_address == issuer_address assert _locked_position.value == 40 - 30 - _lock_list = db.query(IDXLock).order_by(IDXLock.id).all() + _lock_list = db.scalars(select(IDXLock).order_by(IDXLock.id)).all() assert len(_lock_list) == 1 _lock1 = _lock_list[0] @@ -921,7 +946,7 @@ def test_normal_2_4( assert _lock1.value == 40 assert _lock1.data == {"message": "locked1"} - _unlock_list = db.query(IDXUnlock).order_by(IDXUnlock.id).all() + _unlock_list = db.scalars(select(IDXUnlock).order_by(IDXUnlock.id)).all() assert len(_unlock_list) == 1 _unlock1 = _unlock_list[0] @@ -934,7 +959,9 @@ def test_normal_2_4( assert _unlock1.value == 30 assert _unlock1.data == {"message": "unlocked1"} - _notification_list = db.query(Notification).order_by(Notification.created).all() + _notification_list = db.scalars( + select(Notification).order_by(Notification.created) + ).all() assert len(_notification_list) == 2 _notification1 = _notification_list[0] @@ -966,7 +993,9 @@ def test_normal_2_4( "data": {"message": "unlocked1"}, } - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -1035,20 +1064,22 @@ def test_normal_2_5( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -1131,20 +1162,22 @@ def test_normal_2_6( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 40 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -1224,13 +1257,13 @@ def test_normal_2_7( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -1258,20 +1291,22 @@ def test_normal_2_7( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -1351,13 +1386,13 @@ def test_normal_2_8( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -1385,31 +1420,33 @@ def test_normal_2_8( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 2 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -1482,13 +1519,13 @@ def test_normal_2_9_1( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -1518,20 +1555,22 @@ def test_normal_2_9_1( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 40 - 30 assert _position.exchange_commitment == 30 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -1642,42 +1681,44 @@ def test_normal_2_9_2( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 30 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -1791,42 +1832,44 @@ def test_normal_2_9_3( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 30 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -1940,42 +1983,44 @@ def test_normal_2_9_4( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 20 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 10 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -2098,42 +2143,44 @@ def test_normal_2_9_5( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 20 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 20 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -2256,42 +2303,44 @@ def test_normal_2_9_6( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 20 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 10 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -2366,13 +2415,13 @@ def test_normal_2_10_1( # Before run(consume accumulated events) processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 @@ -2402,20 +2451,22 @@ def test_normal_2_10_1( db.expire_all() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 1 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 40 - 30 assert _position.exchange_commitment == 30 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -2527,42 +2578,44 @@ def test_normal_2_10_2( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 0 assert _position.exchange_balance == 30 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -2676,42 +2729,44 @@ def test_normal_2_10_3( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 0 assert _position.exchange_balance == 20 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 10 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -2816,42 +2871,44 @@ def test_normal_3_1( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 40 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -2980,68 +3037,72 @@ def test_normal_3_2( block_number = web3.eth.block_number processor.sync_new_logs() - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 5 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 60 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 11 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 22 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_3) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_3) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_3 assert _position.balance == 33 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_4) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_4) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_4 assert _position.balance == 4 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -3167,42 +3228,44 @@ def test_normal_3_3( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 30 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -3315,42 +3378,44 @@ def test_normal_3_4( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 3 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == issuer_address) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_1) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 0 assert _position.exchange_balance == 30 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where(IDXPosition.account_address == user_address_2) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number @@ -3469,81 +3534,107 @@ def test_normal_4( processor.sync_new_logs() # Assertion - _position_list = db.query(IDXPosition).all() + _position_list = db.scalars(select(IDXPosition)).all() assert len(_position_list) == 6 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .filter(IDXPosition.token_address == token_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == issuer_address, + IDXPosition.token_address == token_address_1, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == issuer_address assert _position.balance == 100 - 30 - 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .filter(IDXPosition.token_address == token_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == user_address_1, + IDXPosition.token_address == token_address_1, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_1 assert _position.balance == 30 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .filter(IDXPosition.token_address == token_address_1) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == user_address_2, + IDXPosition.token_address == token_address_1, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_1 assert _position.account_address == user_address_2 assert _position.balance == 10 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == issuer_address) - .filter(IDXPosition.token_address == token_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == issuer_address, + IDXPosition.token_address == token_address_2, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_2 assert _position.account_address == issuer_address assert _position.balance == 0 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_1) - .filter(IDXPosition.token_address == token_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == user_address_1, + IDXPosition.token_address == token_address_2, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_2 assert _position.account_address == user_address_1 assert _position.balance == 40 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _position = ( - db.query(IDXPosition) - .filter(IDXPosition.account_address == user_address_2) - .filter(IDXPosition.token_address == token_address_2) - .first() - ) + _position = db.scalars( + select(IDXPosition) + .where( + and_( + IDXPosition.account_address == user_address_2, + IDXPosition.token_address == token_address_2, + ) + ) + .limit(1) + ).first() assert _position.token_address == token_address_2 assert _position.account_address == user_address_2 assert _position.balance == 60 assert _position.exchange_balance == 0 assert _position.exchange_commitment == 0 assert _position.pending_transfer == 0 - _idx_position_share_block_number = db.query(IDXPositionShareBlockNumber).first() + _idx_position_share_block_number = db.scalars( + select(IDXPositionShareBlockNumber).limit(1) + ).first() assert _idx_position_share_block_number.id == 1 assert _idx_position_share_block_number.latest_block_number == block_number diff --git a/tests/test_batch_indexer_token_holders.py b/tests/test_batch_indexer_token_holders.py index ff37309e..f83652a7 100644 --- a/tests/test_batch_indexer_token_holders.py +++ b/tests/test_batch_indexer_token_holders.py @@ -23,6 +23,7 @@ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import and_, select from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session @@ -530,18 +531,26 @@ def test_normal_1( # Then execute processor. processor.collect() - user1_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_1) - .first() - ) - user2_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_2) - .first() - ) + user1_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_1, + ) + ) + .limit(1) + ).first() + user2_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_2, + ) + ) + .limit(1) + ).first() assert user1_record.hold_balance == 13000 assert user1_record.locked_balance == 3000 @@ -551,9 +560,11 @@ def test_normal_1( assert ( len( list( - db.query(TokenHolder).filter( - TokenHolder.holder_list_id == _token_holders_list.id - ) + db.scalars( + select(TokenHolder).where( + TokenHolder.holder_list_id == _token_holders_list.id + ) + ).all() ) ) == 2 @@ -747,18 +758,26 @@ def test_normal_2( # Then execute processor. processor.collect() - user1_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_1) - .first() - ) - user2_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_2) - .first() - ) + user1_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_1, + ) + ) + .limit(1) + ).first() + user2_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_2, + ) + ) + .limit(1) + ).first() assert user1_record.hold_balance == 10000 assert user1_record.locked_balance == 0 @@ -768,9 +787,11 @@ def test_normal_2( assert ( len( list( - db.query(TokenHolder).filter( - TokenHolder.holder_list_id == _token_holders_list.id - ) + db.scalars( + select(TokenHolder).where( + TokenHolder.holder_list_id == _token_holders_list.id + ) + ).all() ) ) == 2 @@ -920,18 +941,26 @@ def test_normal_3( # Then execute processor. processor.collect() - user1_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_1) - .first() - ) - user2_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_2) - .first() - ) + user1_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_1, + ) + ) + .limit(1) + ).first() + user2_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_2, + ) + ) + .limit(1) + ).first() assert user1_record.hold_balance == 17000 assert user1_record.locked_balance == 0 @@ -941,9 +970,11 @@ def test_normal_3( assert ( len( list( - db.query(TokenHolder).filter( - TokenHolder.holder_list_id == _token_holders_list.id - ) + db.scalars( + select(TokenHolder).where( + TokenHolder.holder_list_id == _token_holders_list.id + ) + ).all() ) ) == 2 @@ -1296,18 +1327,26 @@ def test_normal_4( # Then execute processor. processor.collect() - user1_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_1) - .first() - ) - user2_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_2) - .first() - ) + user1_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_1, + ) + ) + .limit(1) + ).first() + user2_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_2, + ) + ) + .limit(1) + ).first() assert user1_record.hold_balance == 3000 assert user1_record.locked_balance == 3000 @@ -1317,9 +1356,11 @@ def test_normal_4( assert ( len( list( - db.query(TokenHolder).filter( - TokenHolder.holder_list_id == _token_holders_list.id - ) + db.scalars( + select(TokenHolder).where( + TokenHolder.holder_list_id == _token_holders_list.id + ) + ).all() ) ) == 2 @@ -1513,18 +1554,26 @@ def test_normal_5( # Then execute processor. processor.collect() - user1_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_1) - .first() - ) - user2_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_2) - .first() - ) + user1_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_1, + ) + ) + .limit(1) + ).first() + user2_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_2, + ) + ) + .limit(1) + ).first() assert user1_record.hold_balance == 10000 assert user1_record.locked_balance == 0 @@ -1534,9 +1583,11 @@ def test_normal_5( assert ( len( list( - db.query(TokenHolder).filter( - TokenHolder.holder_list_id == _token_holders_list.id - ) + db.scalars( + select(TokenHolder).where( + TokenHolder.holder_list_id == _token_holders_list.id + ) + ).all() ) ) == 2 @@ -1687,18 +1738,26 @@ def test_normal_6( # Then execute processor. processor.collect() - user1_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_1) - .first() - ) - user2_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_2) - .first() - ) + user1_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_1, + ) + ) + .limit(1) + ).first() + user2_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_2, + ) + ) + .limit(1) + ).first() assert user1_record.hold_balance == 17000 assert user1_record.locked_balance == 0 @@ -1708,9 +1767,11 @@ def test_normal_6( assert ( len( list( - db.query(TokenHolder).filter( - TokenHolder.holder_list_id == _token_holders_list.id - ) + db.scalars( + select(TokenHolder).where( + TokenHolder.holder_list_id == _token_holders_list.id + ) + ).all() ) ) == 2 @@ -1970,18 +2031,26 @@ def test_normal_8( db.commit() processor.collect() - user1_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list2.id) - .filter(TokenHolder.account_address == user_address_1) - .first() - ) - user2_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list2.id) - .filter(TokenHolder.account_address == user_address_2) - .first() - ) + user1_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list2.id, + TokenHolder.account_address == user_address_1, + ) + ) + .limit(1) + ).first() + user2_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list2.id, + TokenHolder.account_address == user_address_2, + ) + ) + .limit(1) + ).first() assert user1_record.hold_balance == 30000 assert user1_record.locked_balance == 0 @@ -1991,9 +2060,11 @@ def test_normal_8( assert ( len( list( - db.query(TokenHolder).filter( - TokenHolder.holder_list_id == _token_holders_list1.id - ) + db.scalars( + select(TokenHolder).where( + TokenHolder.holder_list_id == _token_holders_list1.id + ) + ).all() ) ) == 2 @@ -2001,9 +2072,11 @@ def test_normal_8( assert ( len( list( - db.query(TokenHolder).filter( - TokenHolder.holder_list_id == _token_holders_list2.id - ) + db.scalars( + select(TokenHolder).where( + TokenHolder.holder_list_id == _token_holders_list2.id + ) + ).all() ) ) == 2 @@ -2127,18 +2200,26 @@ def test_normal_9( # Then execute processor. processor.collect() - user1_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_1) - .first() - ) - user2_record: TokenHolder = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .filter(TokenHolder.account_address == user_address_2) - .first() - ) + user1_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_1, + ) + ) + .limit(1) + ).first() + user2_record: TokenHolder = db.scalars( + select(TokenHolder) + .where( + and_( + TokenHolder.holder_list_id == _token_holders_list.id, + TokenHolder.account_address == user_address_2, + ) + ) + .limit(1) + ).first() assert user1_record is None assert user2_record is None @@ -2146,9 +2227,11 @@ def test_normal_9( assert ( len( list( - db.query(TokenHolder).filter( - TokenHolder.holder_list_id == _token_holders_list.id - ) + db.scalars( + select(TokenHolder).where( + TokenHolder.holder_list_id == _token_holders_list.id + ) + ).all() ) ) == 0 @@ -2244,11 +2327,11 @@ def test_normal_10( ) db.rollback() - processed_list = ( - db.query(TokenHoldersList) - .filter(TokenHoldersList.id == target_holders_list.id) - .first() - ) + processed_list = db.scalars( + select(TokenHoldersList) + .where(TokenHoldersList.id == target_holders_list.id) + .limit(1) + ).first() assert processed_list.block_number == 19999999 assert processed_list.batch_status == TokenHolderBatchStatus.DONE.value @@ -2312,9 +2395,12 @@ def test_error_2( # Batch status of token holders list expects to be "ERROR" error_record_num = len( list( - db.query(TokenHoldersList).filter( - TokenHoldersList.batch_status == TokenHolderBatchStatus.FAILED.value - ) + db.scalars( + select(TokenHoldersList).where( + TokenHoldersList.batch_status + == TokenHolderBatchStatus.FAILED.value + ) + ).all() ) ) assert error_record_num == 1 @@ -2411,11 +2497,11 @@ def test_error_3( # Then execute processor. __sync_all_mock.return_value = None processor.collect() - _records: List[TokenHolder] = ( - db.query(TokenHolder) - .filter(TokenHolder.holder_list_id == _token_holders_list.id) - .all() - ) + _records: List[TokenHolder] = db.scalars( + select(TokenHolder).where( + TokenHolder.holder_list_id == _token_holders_list.id + ) + ).all() assert len(_records) == 0 # diff --git a/tests/test_batch_indexer_transfer.py b/tests/test_batch_indexer_transfer.py index 6b4de320..775abfdd 100644 --- a/tests/test_batch_indexer_transfer.py +++ b/tests/test_batch_indexer_transfer.py @@ -24,6 +24,7 @@ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from sqlalchemy.exc import InvalidRequestError from sqlalchemy.orm import Session @@ -172,9 +173,11 @@ def test_normal_1_1(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_list = db.query(IDXTransfer).all() + _transfer_list = db.scalars(select(IDXTransfer)).all() assert len(_transfer_list) == 0 - _idx_transfer_block_number = db.query(IDXTransferBlockNumber).first() + _idx_transfer_block_number = db.scalars( + select(IDXTransferBlockNumber).limit(1) + ).first() assert _idx_transfer_block_number.id == 1 assert _idx_transfer_block_number.latest_block_number == block_number @@ -224,9 +227,11 @@ def test_normal_1_2(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_list = db.query(IDXTransfer).all() + _transfer_list = db.scalars(select(IDXTransfer)).all() assert len(_transfer_list) == 0 - _idx_transfer_block_number = db.query(IDXTransferBlockNumber).first() + _idx_transfer_block_number = db.scalars( + select(IDXTransferBlockNumber).limit(1) + ).first() assert _idx_transfer_block_number.id == 1 assert _idx_transfer_block_number.latest_block_number == block_number @@ -323,7 +328,7 @@ def test_normal_2_1(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_list = db.query(IDXTransfer).all() + _transfer_list = db.scalars(select(IDXTransfer)).all() assert len(_transfer_list) == 2 _transfer = _transfer_list[0] @@ -354,7 +359,9 @@ def test_normal_2_1(self, processor, db, personal_info_contract): block["timestamp"] ) - _idx_transfer_block_number = db.query(IDXTransferBlockNumber).first() + _idx_transfer_block_number = db.scalars( + select(IDXTransferBlockNumber).limit(1) + ).first() assert _idx_transfer_block_number.id == 1 assert _idx_transfer_block_number.latest_block_number == block_number @@ -430,10 +437,12 @@ def test_normal_2_2(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_list = db.query(IDXTransfer).all() + _transfer_list = db.scalars(select(IDXTransfer)).all() assert len(_transfer_list) == 0 - _idx_transfer_block_number = db.query(IDXTransferBlockNumber).first() + _idx_transfer_block_number = db.scalars( + select(IDXTransferBlockNumber).limit(1) + ).first() assert _idx_transfer_block_number.id == 1 assert _idx_transfer_block_number.latest_block_number == block_number @@ -573,7 +582,7 @@ def test_normal_3_1(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_list = db.query(IDXTransfer).all() + _transfer_list = db.scalars(select(IDXTransfer)).all() assert len(_transfer_list) == 4 _transfer = _transfer_list[0] @@ -632,7 +641,9 @@ def test_normal_3_1(self, processor, db, personal_info_contract): block["timestamp"] ) - _idx_transfer_block_number = db.query(IDXTransferBlockNumber).first() + _idx_transfer_block_number = db.scalars( + select(IDXTransferBlockNumber).limit(1) + ).first() assert _idx_transfer_block_number.id == 1 assert _idx_transfer_block_number.latest_block_number == block_number @@ -751,7 +762,7 @@ def test_normal_3_2(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_list = db.query(IDXTransfer).all() + _transfer_list = db.scalars(select(IDXTransfer)).all() assert len(_transfer_list) == 7 block = web3.eth.get_block(tx_receipt_1["blockNumber"]) @@ -784,7 +795,9 @@ def test_normal_3_2(self, processor, db, personal_info_contract): block["timestamp"] ) - _idx_transfer_block_number = db.query(IDXTransferBlockNumber).first() + _idx_transfer_block_number = db.scalars( + select(IDXTransferBlockNumber).limit(1) + ).first() assert _idx_transfer_block_number.id == 1 assert _idx_transfer_block_number.latest_block_number == block_number @@ -883,7 +896,7 @@ def test_normal_4(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_list = db.query(IDXTransfer).all() + _transfer_list = db.scalars(select(IDXTransfer)).all() assert len(_transfer_list) == 4 _transfer = _transfer_list[0] assert _transfer.id == 1 @@ -940,7 +953,9 @@ def test_normal_4(self, processor, db, personal_info_contract): block["timestamp"] ) - _idx_transfer_block_number = db.query(IDXTransferBlockNumber).first() + _idx_transfer_block_number = db.scalars( + select(IDXTransferBlockNumber).limit(1) + ).first() assert _idx_transfer_block_number.id == 1 assert _idx_transfer_block_number.latest_block_number == block_number diff --git a/tests/test_batch_indexer_transfer_approval.py b/tests/test_batch_indexer_transfer_approval.py index a94353fc..2df74a0d 100644 --- a/tests/test_batch_indexer_transfer_approval.py +++ b/tests/test_batch_indexer_transfer_approval.py @@ -24,6 +24,7 @@ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session @@ -177,14 +178,14 @@ def test_normal_1_1(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_approval_list = db.query(IDXTransferApproval).all() + _transfer_approval_list = db.scalars(select(IDXTransferApproval)).all() assert len(_transfer_approval_list) == 0 - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert len(_notification_list) == 0 - _idx_transfer_approval_block_number = db.query( - IDXTransferApprovalBlockNumber + _idx_transfer_approval_block_number = db.scalars( + select(IDXTransferApprovalBlockNumber).limit(1) ).first() assert _idx_transfer_approval_block_number.id == 1 assert _idx_transfer_approval_block_number.latest_block_number == block_number @@ -237,14 +238,14 @@ def test_normal_1_2(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_approval_list = db.query(IDXTransferApproval).all() + _transfer_approval_list = db.scalars(select(IDXTransferApproval)).all() assert len(_transfer_approval_list) == 0 - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert len(_notification_list) == 0 - _idx_transfer_approval_block_number = db.query( - IDXTransferApprovalBlockNumber + _idx_transfer_approval_block_number = db.scalars( + select(IDXTransferApprovalBlockNumber).limit(1) ).first() assert _idx_transfer_approval_block_number.id == 1 assert _idx_transfer_approval_block_number.latest_block_number == block_number @@ -329,7 +330,7 @@ def test_normal_2_1(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_approval_list = db.query(IDXTransferApproval).all() + _transfer_approval_list = db.scalars(select(IDXTransferApproval)).all() assert len(_transfer_approval_list) == 1 _transfer_approval = _transfer_approval_list[0] assert _transfer_approval.id == 1 @@ -351,7 +352,7 @@ def test_normal_2_1(self, processor, db, personal_info_contract): assert _transfer_approval.cancelled is None assert _transfer_approval.transfer_approved is None - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert len(_notification_list) == 1 _notification = _notification_list[0] assert _notification.id == 1 @@ -366,8 +367,8 @@ def test_normal_2_1(self, processor, db, personal_info_contract): "id": 1, } - _idx_transfer_approval_block_number = db.query( - IDXTransferApprovalBlockNumber + _idx_transfer_approval_block_number = db.scalars( + select(IDXTransferApprovalBlockNumber).limit(1) ).first() assert _idx_transfer_approval_block_number.id == 1 assert _idx_transfer_approval_block_number.latest_block_number == block_number @@ -458,7 +459,7 @@ def test_normal_2_2_1(self, processor, db, personal_info_contract): block_2 = web3.eth.get_block(tx_receipt_2["blockNumber"]) block_3 = web3.eth.get_block(tx_receipt_3["blockNumber"]) - _transfer_approval_list = db.query(IDXTransferApproval).all() + _transfer_approval_list = db.scalars(select(IDXTransferApproval)).all() assert len(_transfer_approval_list) == 1 _transfer_approval = _transfer_approval_list[0] assert _transfer_approval.id == 1 @@ -482,7 +483,7 @@ def test_normal_2_2_1(self, processor, db, personal_info_contract): assert _transfer_approval.cancelled is True assert _transfer_approval.transfer_approved is None - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert len(_notification_list) == 1 _notification = _notification_list[0] assert _notification.id == 1 @@ -497,8 +498,8 @@ def test_normal_2_2_1(self, processor, db, personal_info_contract): "id": 1, } - _idx_transfer_approval_block_number = db.query( - IDXTransferApprovalBlockNumber + _idx_transfer_approval_block_number = db.scalars( + select(IDXTransferApprovalBlockNumber).limit(1) ).first() assert _idx_transfer_approval_block_number.id == 1 assert _idx_transfer_approval_block_number.latest_block_number == block_number @@ -589,7 +590,7 @@ def test_normal_2_2_2(self, processor, db, personal_info_contract): block_2 = web3.eth.get_block(tx_receipt_2["blockNumber"]) block_3 = web3.eth.get_block(tx_receipt_3["blockNumber"]) - _transfer_approval_list = db.query(IDXTransferApproval).all() + _transfer_approval_list = db.scalars(select(IDXTransferApproval)).all() assert len(_transfer_approval_list) == 1 _transfer_approval = _transfer_approval_list[0] assert _transfer_approval.id == 1 @@ -613,7 +614,7 @@ def test_normal_2_2_2(self, processor, db, personal_info_contract): assert _transfer_approval.cancelled is True assert _transfer_approval.transfer_approved is None - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert len(_notification_list) == 2 _notification = _notification_list[1] assert _notification.id == 2 @@ -628,8 +629,8 @@ def test_normal_2_2_2(self, processor, db, personal_info_contract): "id": 1, } - _idx_transfer_approval_block_number = db.query( - IDXTransferApprovalBlockNumber + _idx_transfer_approval_block_number = db.scalars( + select(IDXTransferApprovalBlockNumber).limit(1) ).first() assert _idx_transfer_approval_block_number.id == 1 assert _idx_transfer_approval_block_number.latest_block_number == block_number @@ -719,7 +720,7 @@ def test_normal_2_3(self, processor, db, personal_info_contract): processor.sync_new_logs() # Assertion - _transfer_approval_list = db.query(IDXTransferApproval).all() + _transfer_approval_list = db.scalars(select(IDXTransferApproval)).all() assert len(_transfer_approval_list) == 1 _transfer_approval = _transfer_approval_list[0] @@ -744,7 +745,7 @@ def test_normal_2_3(self, processor, db, personal_info_contract): assert _transfer_approval.cancelled is None assert _transfer_approval.transfer_approved is True - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert len(_notification_list) == 2 _notification = _notification_list[1] assert _notification.id == 2 @@ -759,8 +760,8 @@ def test_normal_2_3(self, processor, db, personal_info_contract): "id": 1, } - _idx_transfer_approval_block_number = db.query( - IDXTransferApprovalBlockNumber + _idx_transfer_approval_block_number = db.scalars( + select(IDXTransferApprovalBlockNumber).limit(1) ).first() assert _idx_transfer_approval_block_number.id == 1 assert _idx_transfer_approval_block_number.latest_block_number == block_number @@ -858,7 +859,7 @@ def test_normal_2_4( processor.sync_new_logs() # Assertion - _transfer_approval_list = db.query(IDXTransferApproval).all() + _transfer_approval_list = db.scalars(select(IDXTransferApproval)).all() assert len(_transfer_approval_list) == 1 _transfer_approval = _transfer_approval_list[0] assert _transfer_approval.id == 1 @@ -883,7 +884,7 @@ def test_normal_2_4( assert _transfer_approval.cancelled is None assert _transfer_approval.transfer_approved is None - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert len(_notification_list) == 1 _notification = _notification_list[0] assert _notification.id == 1 @@ -898,8 +899,8 @@ def test_normal_2_4( "id": 1, } - _idx_transfer_approval_block_number = db.query( - IDXTransferApprovalBlockNumber + _idx_transfer_approval_block_number = db.scalars( + select(IDXTransferApprovalBlockNumber).limit(1) ).first() assert _idx_transfer_approval_block_number.id == 1 assert _idx_transfer_approval_block_number.latest_block_number == block_number @@ -1016,7 +1017,7 @@ def test_normal_2_5( processor.sync_new_logs() # Assertion - _transfer_approval_list = db.query(IDXTransferApproval).all() + _transfer_approval_list = db.scalars(select(IDXTransferApproval)).all() assert len(_transfer_approval_list) == 1 _transfer_approval = _transfer_approval_list[0] assert _transfer_approval.id == 1 @@ -1043,7 +1044,7 @@ def test_normal_2_5( assert _transfer_approval.cancelled is True assert _transfer_approval.transfer_approved is None - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert len(_notification_list) == 2 _notification = _notification_list[1] assert _notification.id == 2 @@ -1058,8 +1059,8 @@ def test_normal_2_5( "id": 1, } - _idx_transfer_approval_block_number = db.query( - IDXTransferApprovalBlockNumber + _idx_transfer_approval_block_number = db.scalars( + select(IDXTransferApprovalBlockNumber).limit(1) ).first() assert _idx_transfer_approval_block_number.id == 1 assert _idx_transfer_approval_block_number.latest_block_number == block_number @@ -1166,7 +1167,7 @@ def test_normal_2_6( processor.sync_new_logs() # Assertion - _transfer_approval_list = db.query(IDXTransferApproval).all() + _transfer_approval_list = db.scalars(select(IDXTransferApproval)).all() assert len(_transfer_approval_list) == 1 _transfer_approval = _transfer_approval_list[0] assert _transfer_approval.id == 1 @@ -1190,7 +1191,7 @@ def test_normal_2_6( assert _transfer_approval.cancelled is None assert _transfer_approval.transfer_approved is None - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert len(_notification_list) == 2 _notification = _notification_list[1] assert _notification.id == 2 @@ -1205,8 +1206,8 @@ def test_normal_2_6( "id": 1, } - _idx_transfer_approval_block_number = db.query( - IDXTransferApprovalBlockNumber + _idx_transfer_approval_block_number = db.scalars( + select(IDXTransferApprovalBlockNumber).limit(1) ).first() assert _idx_transfer_approval_block_number.id == 1 assert _idx_transfer_approval_block_number.latest_block_number == block_number @@ -1326,7 +1327,7 @@ def test_normal_2_7( processor.sync_new_logs() # Assertion - _transfer_approval_list = db.query(IDXTransferApproval).all() + _transfer_approval_list = db.scalars(select(IDXTransferApproval)).all() assert len(_transfer_approval_list) == 1 _transfer_approval = _transfer_approval_list[0] assert _transfer_approval.id == 1 @@ -1352,7 +1353,7 @@ def test_normal_2_7( assert _transfer_approval.cancelled is None assert _transfer_approval.transfer_approved is True - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert len(_notification_list) == 3 _notification = _notification_list[2] assert _notification.id == 3 @@ -1367,8 +1368,8 @@ def test_normal_2_7( "id": 1, } - _idx_transfer_approval_block_number = db.query( - IDXTransferApprovalBlockNumber + _idx_transfer_approval_block_number = db.scalars( + select(IDXTransferApprovalBlockNumber).limit(1) ).first() assert _idx_transfer_approval_block_number.id == 1 assert _idx_transfer_approval_block_number.latest_block_number == block_number diff --git a/tests/test_batch_processor_batch_issue_redeem.py b/tests/test_batch_processor_batch_issue_redeem.py index c109c86b..4c1cdc08 100644 --- a/tests/test_batch_processor_batch_issue_redeem.py +++ b/tests/test_batch_processor_batch_issue_redeem.py @@ -18,11 +18,12 @@ """ import logging import uuid -from typing import List +from typing import List, Sequence from unittest.mock import ANY, patch import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from sqlalchemy.orm import Session from app.exceptions import ContractRevertError, SendTransactionError @@ -127,18 +128,16 @@ def test_normal_1(self, processor, db, caplog): ) # Assertion: DB - _upload_after: BatchIssueRedeemUpload = ( - db.query(BatchIssueRedeemUpload) - .filter(BatchIssueRedeemUpload.upload_id == upload_id) - .first() - ) + _upload_after: BatchIssueRedeemUpload = db.scalars( + select(BatchIssueRedeemUpload) + .where(BatchIssueRedeemUpload.upload_id == upload_id) + .limit(1) + ).first() assert _upload_after.processed == True - _upload_data_after: List[BatchIssueRedeem] = ( - db.query(BatchIssueRedeem) - .filter(BatchIssueRedeem.upload_id == upload_id) - .all() - ) + _upload_data_after: List[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem).where(BatchIssueRedeem.upload_id == upload_id) + ).all() assert len(_upload_data_after) == 1 assert _upload_data_after[0].status == 1 @@ -150,7 +149,7 @@ def test_normal_1(self, processor, db, caplog): == 1 ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() for _notification in _notification_list: assert _notification.notice_id is not None assert _notification.issuer_address == issuer_address @@ -230,18 +229,16 @@ def test_normal_2(self, processor, db, caplog): ) # Assertion: DB - _upload_after: BatchIssueRedeemUpload = ( - db.query(BatchIssueRedeemUpload) - .filter(BatchIssueRedeemUpload.upload_id == upload_id) - .first() - ) + _upload_after: BatchIssueRedeemUpload = db.scalars( + select(BatchIssueRedeemUpload) + .where(BatchIssueRedeemUpload.upload_id == upload_id) + .limit(1) + ).first() assert _upload_after.processed == True - _upload_data_after: List[BatchIssueRedeem] = ( - db.query(BatchIssueRedeem) - .filter(BatchIssueRedeem.upload_id == upload_id) - .all() - ) + _upload_data_after: List[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem).where(BatchIssueRedeem.upload_id == upload_id) + ).all() assert len(_upload_data_after) == 1 assert _upload_data_after[0].status == 1 @@ -253,7 +250,7 @@ def test_normal_2(self, processor, db, caplog): == 1 ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() for _notification in _notification_list: assert _notification.notice_id is not None assert _notification.issuer_address == issuer_address @@ -333,18 +330,16 @@ def test_normal_3(self, processor, db, caplog): ) # Assertion: DB - _upload_after: BatchIssueRedeemUpload = ( - db.query(BatchIssueRedeemUpload) - .filter(BatchIssueRedeemUpload.upload_id == upload_id) - .first() - ) + _upload_after: BatchIssueRedeemUpload = db.scalars( + select(BatchIssueRedeemUpload) + .where(BatchIssueRedeemUpload.upload_id == upload_id) + .limit(1) + ).first() assert _upload_after.processed == True - _upload_data_after: List[BatchIssueRedeem] = ( - db.query(BatchIssueRedeem) - .filter(BatchIssueRedeem.upload_id == upload_id) - .all() - ) + _upload_data_after: List[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem).where(BatchIssueRedeem.upload_id == upload_id) + ).all() assert len(_upload_data_after) == 1 assert _upload_data_after[0].status == 1 @@ -356,7 +351,7 @@ def test_normal_3(self, processor, db, caplog): == 1 ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() for _notification in _notification_list: assert _notification.notice_id is not None assert _notification.issuer_address == issuer_address @@ -434,18 +429,16 @@ def test_normal_4(self, processor, db, caplog): ) # Assertion: DB - _upload_after: BatchIssueRedeemUpload = ( - db.query(BatchIssueRedeemUpload) - .filter(BatchIssueRedeemUpload.upload_id == upload_id) - .first() - ) + _upload_after: BatchIssueRedeemUpload = db.scalars( + select(BatchIssueRedeemUpload) + .where(BatchIssueRedeemUpload.upload_id == upload_id) + .limit(1) + ).first() assert _upload_after.processed == True - _upload_data_after: List[BatchIssueRedeem] = ( - db.query(BatchIssueRedeem) - .filter(BatchIssueRedeem.upload_id == upload_id) - .all() - ) + _upload_data_after: List[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem).where(BatchIssueRedeem.upload_id == upload_id) + ).all() assert len(_upload_data_after) == 1 assert _upload_data_after[0].status == 1 @@ -457,7 +450,7 @@ def test_normal_4(self, processor, db, caplog): == 1 ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() for _notification in _notification_list: assert _notification.notice_id is not None assert _notification.issuer_address == issuer_address @@ -521,18 +514,16 @@ def test_error_1(self, processor, db, caplog): IbetStraightBondContract_additional_issue.assert_not_called() # Assertion: DB - _upload_after: BatchIssueRedeemUpload = ( - db.query(BatchIssueRedeemUpload) - .filter(BatchIssueRedeemUpload.upload_id == upload_id) - .first() - ) + _upload_after: BatchIssueRedeemUpload = db.scalars( + select(BatchIssueRedeemUpload) + .where(BatchIssueRedeemUpload.upload_id == upload_id) + .limit(1) + ).first() assert _upload_after.processed == True - _upload_data_after: List[BatchIssueRedeem] = ( - db.query(BatchIssueRedeem) - .filter(BatchIssueRedeem.upload_id == upload_id) - .all() - ) + _upload_data_after: List[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem).where(BatchIssueRedeem.upload_id == upload_id) + ).all() assert len(_upload_data_after) == 1 assert _upload_data_after[0].status == 0 @@ -544,7 +535,7 @@ def test_error_1(self, processor, db, caplog): == 1 ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() for _notification in _notification_list: assert _notification.notice_id is not None assert _notification.issuer_address == issuer_address @@ -613,18 +604,16 @@ def test_error_2(self, processor, db, caplog): IbetStraightBondContract_additional_issue.assert_not_called() # Assertion: DB - _upload_after: BatchIssueRedeemUpload = ( - db.query(BatchIssueRedeemUpload) - .filter(BatchIssueRedeemUpload.upload_id == upload_id) - .first() - ) + _upload_after: BatchIssueRedeemUpload = db.scalars( + select(BatchIssueRedeemUpload) + .where(BatchIssueRedeemUpload.upload_id == upload_id) + .limit(1) + ).first() assert _upload_after.processed == True - _upload_data_after: List[BatchIssueRedeem] = ( - db.query(BatchIssueRedeem) - .filter(BatchIssueRedeem.upload_id == upload_id) - .all() - ) + _upload_data_after: List[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem).where(BatchIssueRedeem.upload_id == upload_id) + ).all() assert len(_upload_data_after) == 1 assert _upload_data_after[0].status == 0 @@ -636,7 +625,7 @@ def test_error_2(self, processor, db, caplog): == 1 ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() for _notification in _notification_list: assert _notification.notice_id is not None assert _notification.issuer_address == issuer_address @@ -709,18 +698,16 @@ def test_error_3(self, processor, db, caplog): processor.process() # Assertion: DB - _upload_after: BatchIssueRedeemUpload = ( - db.query(BatchIssueRedeemUpload) - .filter(BatchIssueRedeemUpload.upload_id == upload_id) - .first() - ) + _upload_after: BatchIssueRedeemUpload = db.scalars( + select(BatchIssueRedeemUpload) + .where(BatchIssueRedeemUpload.upload_id == upload_id) + .limit(1) + ).first() assert _upload_after.processed == True - _upload_data_after: List[BatchIssueRedeem] = ( - db.query(BatchIssueRedeem) - .filter(BatchIssueRedeem.upload_id == upload_id) - .all() - ) + _upload_data_after: List[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem).where(BatchIssueRedeem.upload_id == upload_id) + ).all() assert len(_upload_data_after) == 2 assert _upload_data_after[0].status == 2 assert _upload_data_after[1].status == 2 @@ -733,7 +720,7 @@ def test_error_3(self, processor, db, caplog): == 2 ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() for _notification in _notification_list: assert _notification.notice_id is not None assert _notification.issuer_address == issuer_address @@ -823,33 +810,29 @@ def test_error_4( processor.process() # Assertion: DB - _upload_1_after: BatchIssueRedeemUpload | None = ( - db.query(BatchIssueRedeemUpload) - .filter(BatchIssueRedeemUpload.upload_id == upload_1_id) - .first() - ) + _upload_1_after: BatchIssueRedeemUpload | None = db.scalars( + select(BatchIssueRedeemUpload) + .where(BatchIssueRedeemUpload.upload_id == upload_1_id) + .limit(1) + ).first() assert _upload_1_after.processed == True - _upload_1_data_after: List[BatchIssueRedeem] = ( - db.query(BatchIssueRedeem) - .filter(BatchIssueRedeem.upload_id == upload_1_id) - .all() - ) + _upload_1_data_after: Sequence[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem).where(BatchIssueRedeem.upload_id == upload_1_id) + ).all() assert len(_upload_1_data_after) == 1 assert _upload_1_data_after[0].status == 2 - _upload_2_after: BatchIssueRedeemUpload | None = ( - db.query(BatchIssueRedeemUpload) - .filter(BatchIssueRedeemUpload.upload_id == upload_2_id) - .first() - ) + _upload_2_after: BatchIssueRedeemUpload | None = db.scalars( + select(BatchIssueRedeemUpload) + .where(BatchIssueRedeemUpload.upload_id == upload_2_id) + .limit(1) + ).first() assert _upload_2_after.processed == True - _upload_2_data_after: List[BatchIssueRedeem] = ( - db.query(BatchIssueRedeem) - .filter(BatchIssueRedeem.upload_id == upload_2_id) - .all() - ) + _upload_2_data_after: Sequence[BatchIssueRedeem] = db.scalars( + select(BatchIssueRedeem).where(BatchIssueRedeem.upload_id == upload_2_id) + ).all() assert len(_upload_2_data_after) == 1 assert _upload_2_data_after[0].status == 2 @@ -875,7 +858,7 @@ def test_error_4( == 1 ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() assert _notification_list[0].notice_id is not None assert _notification_list[0].issuer_address == issuer_address assert _notification_list[0].priority == 1 diff --git a/tests/test_batch_processor_bulk_transfer.py b/tests/test_batch_processor_bulk_transfer.py index 8d383d62..6d5b4819 100644 --- a/tests/test_batch_processor_bulk_transfer.py +++ b/tests/test_batch_processor_bulk_transfer.py @@ -19,6 +19,7 @@ from unittest.mock import patch import pytest +from sqlalchemy import and_, select from app.exceptions import SendTransactionError from app.model.db import ( @@ -127,18 +128,18 @@ def test_normal_1(self, processor, db): processor.process() # Assertion - _bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == self.upload_id_list[0]) - .first() - ) + _bulk_transfer_upload = db.scalars( + select(BulkTransferUpload) + .where(BulkTransferUpload.upload_id == self.upload_id_list[0]) + .limit(1) + ).first() assert _bulk_transfer_upload.status == 1 - _bulk_transfer_list = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == self.upload_id_list[0]) - .all() - ) + _bulk_transfer_list = db.scalars( + select(BulkTransfer).where( + BulkTransfer.upload_id == self.upload_id_list[0] + ) + ).all() for _bulk_transfer in _bulk_transfer_list: assert _bulk_transfer.status == 1 @@ -192,18 +193,18 @@ def test_normal_2(self, processor, db): processor.process() # Assertion - _bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == self.upload_id_list[0]) - .first() - ) + _bulk_transfer_upload = db.scalars( + select(BulkTransferUpload) + .where(BulkTransferUpload.upload_id == self.upload_id_list[0]) + .limit(1) + ).first() assert _bulk_transfer_upload.status == 1 - _bulk_transfer_list = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == self.upload_id_list[0]) - .all() - ) + _bulk_transfer_list = db.scalars( + select(BulkTransfer).where( + BulkTransfer.upload_id == self.upload_id_list[0] + ) + ).all() for _bulk_transfer in _bulk_transfer_list: assert _bulk_transfer.status == 1 @@ -305,9 +306,9 @@ def test_normal_3(self, processor, db): processor.process() # Assertion - _bulk_transfer_upload_list = ( - db.query(BulkTransferUpload).order_by(BulkTransferUpload.created).all() - ) + _bulk_transfer_upload_list = db.scalars( + select(BulkTransferUpload).order_by(BulkTransferUpload.created) + ).all() _bulk_transfer_upload = _bulk_transfer_upload_list[0] assert _bulk_transfer_upload.status == 0 _bulk_transfer_upload = _bulk_transfer_upload_list[1] @@ -318,20 +319,18 @@ def test_normal_3(self, processor, db): assert _bulk_transfer_upload.status == 1 _bulk_transfer_upload = _bulk_transfer_upload_list[4] assert _bulk_transfer_upload.status == 1 - _bulk_transfer_list = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == self.upload_id_list[3]) + _bulk_transfer_list = db.scalars( + select(BulkTransfer) + .where(BulkTransfer.upload_id == self.upload_id_list[3]) .order_by(BulkTransfer.id) - .all() - ) + ).all() _bulk_transfer = _bulk_transfer_list[0] assert _bulk_transfer.status == 1 - _bulk_transfer_list = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == self.upload_id_list[4]) + _bulk_transfer_list = db.scalars( + select(BulkTransfer) + .where(BulkTransfer.upload_id == self.upload_id_list[4]) .order_by(BulkTransfer.id) - .all() - ) + ).all() _bulk_transfer = _bulk_transfer_list[0] assert _bulk_transfer.status == 1 @@ -430,9 +429,9 @@ def test_normal_4(self, processor, db): processor.process() # Assertion - _bulk_transfer_upload_list = ( - db.query(BulkTransferUpload).order_by(BulkTransferUpload.created).all() - ) + _bulk_transfer_upload_list = db.scalars( + select(BulkTransferUpload).order_by(BulkTransferUpload.created) + ).all() _bulk_transfer_upload = _bulk_transfer_upload_list[0] assert _bulk_transfer_upload.status == 0 _bulk_transfer_upload = _bulk_transfer_upload_list[1] @@ -441,20 +440,18 @@ def test_normal_4(self, processor, db): assert _bulk_transfer_upload.status == 1 _bulk_transfer_upload = _bulk_transfer_upload_list[3] assert _bulk_transfer_upload.status == 1 - _bulk_transfer_list = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == self.upload_id_list[2]) + _bulk_transfer_list = db.scalars( + select(BulkTransfer) + .where(BulkTransfer.upload_id == self.upload_id_list[2]) .order_by(BulkTransfer.id) - .all() - ) + ).all() _bulk_transfer = _bulk_transfer_list[0] assert _bulk_transfer.status == 1 - _bulk_transfer_list = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == self.upload_id_list[3]) + _bulk_transfer_list = db.scalars( + select(BulkTransfer) + .where(BulkTransfer.upload_id == self.upload_id_list[3]) .order_by(BulkTransfer.id) - .all() - ) + ).all() _bulk_transfer = _bulk_transfer_list[0] assert _bulk_transfer.status == 1 @@ -481,13 +478,13 @@ def test_error_1(self, processor, db): processor.process() # Assertion - _bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == self.upload_id_list[0]) - .first() - ) + _bulk_transfer_upload = db.scalars( + select(BulkTransferUpload) + .where(BulkTransferUpload.upload_id == self.upload_id_list[0]) + .limit(1) + ).first() assert _bulk_transfer_upload.status == 2 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _account["address"] @@ -526,13 +523,13 @@ def test_error_2(self, processor, db): processor.process() # Assertion - _bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == self.upload_id_list[0]) - .first() - ) + _bulk_transfer_upload = db.scalars( + select(BulkTransferUpload) + .where(BulkTransferUpload.upload_id == self.upload_id_list[0]) + .limit(1) + ).first() assert _bulk_transfer_upload.status == 2 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _account["address"] @@ -592,22 +589,26 @@ def test_error_3(self, processor, db): processor.process() # Assertion - _bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == self.upload_id_list[0]) - .first() - ) + _bulk_transfer_upload = db.scalars( + select(BulkTransferUpload) + .where(BulkTransferUpload.upload_id == self.upload_id_list[0]) + .limit(1) + ).first() assert _bulk_transfer_upload.status == 2 - _bulk_transfer = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == self.upload_id_list[0]) - .filter(BulkTransfer.token_address == self.bulk_transfer_token[0]) - .first() - ) + _bulk_transfer = db.scalars( + select(BulkTransfer) + .where( + and_( + BulkTransfer.upload_id == self.upload_id_list[0], + BulkTransfer.token_address == self.bulk_transfer_token[0], + ) + ) + .limit(1) + ).first() assert _bulk_transfer.status == 2 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _account["address"] @@ -667,22 +668,26 @@ def test_error_4(self, processor, db): processor.process() # Assertion - _bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == self.upload_id_list[0]) - .first() - ) + _bulk_transfer_upload = db.scalars( + select(BulkTransferUpload) + .where(BulkTransferUpload.upload_id == self.upload_id_list[0]) + .limit(1) + ).first() assert _bulk_transfer_upload.status == 2 - _bulk_transfer = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == self.upload_id_list[0]) - .filter(BulkTransfer.token_address == self.bulk_transfer_token[0]) - .first() - ) + _bulk_transfer = db.scalars( + select(BulkTransfer) + .where( + and_( + BulkTransfer.upload_id == self.upload_id_list[0], + BulkTransfer.token_address == self.bulk_transfer_token[0], + ) + ) + .limit(1) + ).first() assert _bulk_transfer.status == 2 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _account["address"] @@ -745,22 +750,26 @@ def test_error_5(self, processor, db): processor.process() # Assertion - _bulk_transfer_upload = ( - db.query(BulkTransferUpload) - .filter(BulkTransferUpload.upload_id == self.upload_id_list[0]) - .first() - ) + _bulk_transfer_upload = db.scalars( + select(BulkTransferUpload) + .where(BulkTransferUpload.upload_id == self.upload_id_list[0]) + .limit(1) + ).first() assert _bulk_transfer_upload.status == 2 - _bulk_transfer = ( - db.query(BulkTransfer) - .filter(BulkTransfer.upload_id == self.upload_id_list[0]) - .filter(BulkTransfer.token_address == self.bulk_transfer_token[0]) - .first() - ) + _bulk_transfer = db.scalars( + select(BulkTransfer) + .where( + and_( + BulkTransfer.upload_id == self.upload_id_list[0], + BulkTransfer.token_address == self.bulk_transfer_token[0], + ) + ) + .limit(1) + ).first() assert _bulk_transfer.status == 1 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _account["address"] diff --git a/tests/test_batch_processor_create_utxo.py b/tests/test_batch_processor_create_utxo.py index c40e0168..fd6db2c2 100644 --- a/tests/test_batch_processor_create_utxo.py +++ b/tests/test_batch_processor_create_utxo.py @@ -23,6 +23,7 @@ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from web3 import Web3 from web3.middleware import geth_poa_middleware @@ -164,9 +165,9 @@ def test_normal_1(self, mock_func, processor, db): processor.process() # assertion - _utox_list = db.query(UTXO).all() + _utox_list = db.scalars(select(UTXO)).all() assert len(_utox_list) == 0 - _utox_block_number = db.query(UTXOBlockNumber).first() + _utox_block_number = db.scalars(select(UTXOBlockNumber).limit(1)).first() assert _utox_block_number.latest_block_number == latest_block # Execute Transfer Event @@ -212,7 +213,7 @@ def test_normal_1(self, mock_func, processor, db): # assertion db.rollback() - _utox_list = db.query(UTXO).order_by(UTXO.created).all() + _utox_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() # 1.Bond token:issuer -> user1 (tx2) # 2.Bond token:issuer -> user2 (tx3) # 3.Share token:issuer -> user1 (tx1) @@ -246,7 +247,7 @@ def test_normal_1(self, mock_func, processor, db): assert _utox.amount == 10 assert _utox.block_number > _utox_list[1].block_number assert _utox.block_timestamp > _utox_list[1].block_timestamp - _utox_block_number = db.query(UTXOBlockNumber).first() + _utox_block_number = db.scalars(select(UTXOBlockNumber).limit(1)).first() assert _utox_block_number.latest_block_number == _utox_list[3].block_number mock_func.assert_has_calls( @@ -324,9 +325,9 @@ def test_normal_2(self, mock_func, processor, db): processor.process() # Assertion - _utxo_block_number = db.query(UTXOBlockNumber).first() + _utxo_block_number = db.scalars(select(UTXOBlockNumber).limit(1)).first() assert _utxo_block_number.latest_block_number == latest_block_number + 5 - _utox_list = db.query(UTXO).order_by(UTXO.created).all() + _utox_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() assert len(_utox_list) == 5 _utox = _utox_list[0] assert _utox.transaction_hash is not None @@ -443,7 +444,7 @@ def test_normal_3(self, mock_func, processor, db): processor.process() # Assertion - _utox_list = db.query(UTXO).order_by(UTXO.created).all() + _utox_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() assert len(_utox_list) == 2 _utox = _utox_list[0] assert _utox.transaction_hash is not None @@ -518,7 +519,7 @@ def test_normal_4(self, mock_func, processor, db): processor.process() # assertion - _utox_list = db.query(UTXO).all() + _utox_list = db.scalars(select(UTXO)).all() assert len(_utox_list) == 0 mock_func.assert_not_called() @@ -681,7 +682,7 @@ def test_normal_5( # assertion db.rollback() - _utox_list = db.query(UTXO).order_by(UTXO.created).all() + _utox_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() assert len(_utox_list) == 3 _utox = _utox_list[0] @@ -704,7 +705,7 @@ def test_normal_5( assert _utox.token_address == token_address_1 assert _utox.amount == 10 - _utox_block_number = db.query(UTXOBlockNumber).first() + _utox_block_number = db.scalars(select(UTXOBlockNumber).limit(1)).first() assert _utox_block_number.latest_block_number >= _utox_list[1].block_number mock_func.assert_has_calls([call(token_address=token_address_1, db=ANY)]) @@ -770,7 +771,7 @@ def test_normal_6(self, mock_func, processor, db): processor.process() # assertion - _utox_list = db.query(UTXO).order_by(UTXO.created).all() + _utox_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() assert len(_utox_list) == 2 _utox = _utox_list[0] assert _utox.transaction_hash is not None @@ -783,7 +784,7 @@ def test_normal_6(self, mock_func, processor, db): assert _utox.token_address == token_address_2 assert _utox.amount == 80 - _utox_block_number = db.query(UTXOBlockNumber).first() + _utox_block_number = db.scalars(select(UTXOBlockNumber).limit(1)).first() assert _utox_block_number.latest_block_number == latest_block # @@ -858,7 +859,7 @@ def test_normal_7(self, mock_func, processor, db): # Before execute processor.process() - _utox_list = db.query(UTXO).order_by(UTXO.created).all() + _utox_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() assert len(_utox_list) == 4 _utox = _utox_list[0] assert _utox.transaction_hash is not None @@ -904,7 +905,7 @@ def test_normal_7(self, mock_func, processor, db): # assertion db.rollback() - _utox_list = db.query(UTXO).order_by(UTXO.created).all() + _utox_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() assert len(_utox_list) == 4 _utox = _utox_list[0] assert _utox.account_address == user_address_1 @@ -923,7 +924,7 @@ def test_normal_7(self, mock_func, processor, db): assert _utox.token_address == token_address_2 assert _utox.amount == 30 - _utox_block_number = db.query(UTXOBlockNumber).first() + _utox_block_number = db.scalars(select(UTXOBlockNumber).limit(1)).first() assert _utox_block_number.latest_block_number == latest_block # @@ -1006,7 +1007,7 @@ def test_normal_8_1(self, mock_func, processor, db): # Before execute processor.process() - _utxo_list = db.query(UTXO).order_by(UTXO.created).all() + _utxo_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() assert len(_utxo_list) == 4 _utxo = _utxo_list[0] assert _utxo.transaction_hash is not None @@ -1076,7 +1077,7 @@ def test_normal_8_1(self, mock_func, processor, db): # assertion db.rollback() - _utxo_list = db.query(UTXO).order_by(UTXO.created).all() + _utxo_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() assert len(_utxo_list) == 6 _utxo = _utxo_list[0] assert _utxo.account_address == user_address_1 @@ -1103,7 +1104,7 @@ def test_normal_8_1(self, mock_func, processor, db): assert _utxo.token_address == token_address_2 assert _utxo.amount == 10 - _utxo_block_number = db.query(UTXOBlockNumber).first() + _utxo_block_number = db.scalars(select(UTXOBlockNumber).limit(1)).first() assert _utxo_block_number.latest_block_number == latest_block # @@ -1186,7 +1187,7 @@ def test_normal_8_2(self, mock_func, processor, db): # Before execute processor.process() - _utxo_list = db.query(UTXO).order_by(UTXO.created).all() + _utxo_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() assert len(_utxo_list) == 4 _utxo = _utxo_list[0] assert _utxo.transaction_hash is not None @@ -1256,7 +1257,7 @@ def test_normal_8_2(self, mock_func, processor, db): # assertion db.rollback() - _utxo_list = db.query(UTXO).order_by(UTXO.created).all() + _utxo_list = db.scalars(select(UTXO).order_by(UTXO.created)).all() assert len(_utxo_list) == 4 _utxo = _utxo_list[0] assert _utxo.account_address == user_address_1 @@ -1275,7 +1276,7 @@ def test_normal_8_2(self, mock_func, processor, db): assert _utxo.token_address == token_address_2 assert _utxo.amount == 40 - _utxo_block_number = db.query(UTXOBlockNumber).first() + _utxo_block_number = db.scalars(select(UTXOBlockNumber).limit(1)).first() assert _utxo_block_number.latest_block_number == latest_block ########################################################################### @@ -1311,7 +1312,7 @@ def test_error_1(self, processor, db): ) as web3_mock: processor.process() - _utox_list = db.query(UTXO).all() + _utox_list = db.scalars(select(UTXO)).all() assert len(_utox_list) == 0 - _utox_block_number = db.query(UTXOBlockNumber).first() + _utox_block_number = db.scalars(select(UTXOBlockNumber).limit(1)).first() assert _utox_block_number.latest_block_number == latest_block diff --git a/tests/test_batch_processor_generate_rsa_key.py b/tests/test_batch_processor_generate_rsa_key.py index 01a6c41d..e1bed0fc 100644 --- a/tests/test_batch_processor_generate_rsa_key.py +++ b/tests/test_batch_processor_generate_rsa_key.py @@ -19,6 +19,7 @@ from unittest import mock import pytest +from sqlalchemy import select from app.model.db import Account, AccountRsaStatus from app.utils.e2ee_utils import E2EEUtils @@ -130,7 +131,7 @@ def crypto_publickey_rsa_generate(*args, **kwargs): patch.stop() # assertion - account_after = db.query(Account).order_by(Account.created).all() + account_after = db.scalars(select(Account).order_by(Account.created)).all() assert len(account_after) == 4 _account = account_after[0] assert _account.issuer_address == issuer_address_1 diff --git a/tests/test_batch_processor_modify_personal_info.py b/tests/test_batch_processor_modify_personal_info.py index 4e81ba60..1bedc753 100644 --- a/tests/test_batch_processor_modify_personal_info.py +++ b/tests/test_batch_processor_modify_personal_info.py @@ -18,6 +18,7 @@ """ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import func, select from web3 import Web3 from web3.middleware import geth_poa_middleware @@ -315,7 +316,7 @@ def test_normal_1(self, processor, db): processor.process() # assertion(Run 1st) - _account = db.query(Account).first() + _account = db.scalars(select(Account).limit(1)).first() assert _account.issuer_address == user_1["address"] assert _account.keyfile == user_1["keyfile_json"] assert _account.eoa_password == eoa_password @@ -323,7 +324,7 @@ def test_normal_1(self, processor, db): assert _account.rsa_public_key == user_1["rsa_public_key"] assert _account.rsa_passphrase == rsa_passphrase assert _account.rsa_status == AccountRsaStatus.CHANGING.value - _temporary = db.query(AccountRsaKeyTemporary).first() + _temporary = db.scalars(select(AccountRsaKeyTemporary).limit(1)).first() assert temporary.issuer_address == user_1["address"] assert temporary.rsa_private_key == user_1["rsa_private_key"] assert temporary.rsa_public_key == user_1["rsa_public_key"] @@ -380,7 +381,7 @@ def test_normal_1(self, processor, db): } # RSA Key Change Completed - account = db.query(Account).first() + account = db.scalars(select(Account).limit(1)).first() account.rsa_private_key = personal_user_1["rsa_private_key"] account.rsa_public_key = personal_user_1["rsa_public_key"] db.merge(account) @@ -393,7 +394,7 @@ def test_normal_1(self, processor, db): # assertion(Run 2nd) db.rollback() - _account = db.query(Account).first() + _account = db.scalars(select(Account).limit(1)).first() assert _account.issuer_address == user_1["address"] assert _account.keyfile == user_1["keyfile_json"] assert _account.eoa_password == eoa_password @@ -401,7 +402,7 @@ def test_normal_1(self, processor, db): assert _account.rsa_public_key == personal_user_1["rsa_public_key"] assert _account.rsa_passphrase == rsa_passphrase assert _account.rsa_status == AccountRsaStatus.CHANGING.value - _temporary = db.query(AccountRsaKeyTemporary).first() + _temporary = db.scalars(select(AccountRsaKeyTemporary).limit(1)).first() assert temporary.issuer_address == user_1["address"] assert temporary.rsa_private_key == user_1["rsa_private_key"] assert temporary.rsa_public_key == user_1["rsa_public_key"] @@ -463,7 +464,7 @@ def test_normal_1(self, processor, db): # assertion(Run 3rd) db.rollback() - _account = db.query(Account).first() + _account = db.scalars(select(Account).limit(1)).first() assert _account.issuer_address == user_1["address"] assert _account.keyfile == user_1["keyfile_json"] assert _account.eoa_password == eoa_password @@ -471,7 +472,9 @@ def test_normal_1(self, processor, db): assert _account.rsa_public_key == personal_user_1["rsa_public_key"] assert _account.rsa_passphrase == rsa_passphrase assert _account.rsa_status == AccountRsaStatus.SET.value - _temporary_count = db.query(AccountRsaKeyTemporary).count() + _temporary_count = db.scalar( + select(func.count()).select_from(select(AccountRsaKeyTemporary).subquery()) + ) assert _temporary_count == 0 _personal_info_1 = PersonalInfoContract( db, user_1["address"], personal_info_contract_address_1 diff --git a/tests/test_batch_processor_monitor_block_sync.py b/tests/test_batch_processor_monitor_block_sync.py index 38934ff9..36693916 100644 --- a/tests/test_batch_processor_monitor_block_sync.py +++ b/tests/test_batch_processor_monitor_block_sync.py @@ -21,6 +21,7 @@ from unittest.mock import MagicMock import pytest +from sqlalchemy import select from web3 import Web3 from web3.middleware import geth_poa_middleware @@ -53,7 +54,7 @@ def test_normal_1(self, processor, db): processor.process() # assertion - _node = db.query(Node).first() + _node = db.scalars(select(Node).limit(1)).first() assert _node.id == 1 assert _node.endpoint_uri == WEB3_HTTP_PROVIDER assert _node.priority == 0 @@ -69,7 +70,7 @@ def test_normal_1(self, processor, db): # assertion db.rollback() - _node = db.query(Node).first() + _node = db.scalars(select(Node).limit(1)).first() assert _node.is_synced == False time.sleep(BLOCK_SYNC_STATUS_SLEEP_INTERVAL) @@ -79,7 +80,7 @@ def test_normal_1(self, processor, db): # assertion db.rollback() - _node = db.query(Node).first() + _node = db.scalars(select(Node).limit(1)).first() assert _node.is_synced == True time.sleep(BLOCK_SYNC_STATUS_SLEEP_INTERVAL) @@ -94,7 +95,7 @@ def test_normal_1(self, processor, db): # assertion db.rollback() - _node = db.query(Node).first() + _node = db.scalars(select(Node).limit(1)).first() assert _node.is_synced == False time.sleep(BLOCK_SYNC_STATUS_SLEEP_INTERVAL) @@ -109,7 +110,7 @@ def test_normal_1(self, processor, db): # assertion db.rollback() - _node = db.query(Node).first() + _node = db.scalars(select(Node).limit(1)).first() assert _node.is_synced == True # @@ -123,7 +124,7 @@ def test_normal_2(self, db): # pre assertion db.rollback() - _node = db.query(Node).first() + _node = db.scalars(select(Node).limit(1)).first() assert _node.id == 1 assert _node.endpoint_uri == "http://test1:1000" assert _node.priority == 1 @@ -143,7 +144,9 @@ def test_normal_2(self, db): # assertion db.rollback() - _node = db.query(Node).filter(Node.endpoint_uri == "http://test1:1000").first() + _node = db.scalars( + select(Node).where(Node.endpoint_uri == "http://test1:1000").limit(1) + ).first() assert _node.is_synced == True # @@ -160,11 +163,9 @@ def test_normal_3(self, db): processor = Processor() # assertion-1 - old_node = ( - db.query(Node) - .filter(Node.endpoint_uri.not_in(list(WEB3_HTTP_PROVIDER))) - .all() - ) + old_node = db.scalars( + select(Node).where(Node.endpoint_uri.not_in(list(WEB3_HTTP_PROVIDER))) + ).all() assert len(old_node) == 0 # process @@ -172,7 +173,7 @@ def test_normal_3(self, db): db.commit() # assertion-2 - new_node = db.query(Node).first() + new_node = db.scalars(select(Node).limit(1)).first() assert new_node.id == 1 assert new_node.endpoint_uri == WEB3_HTTP_PROVIDER assert new_node.priority == 0 @@ -197,7 +198,7 @@ def test_error_1(self, db): # assertion db.rollback() - _node_list = db.query(Node).order_by(Node.id).all() + _node_list = db.scalars(select(Node).order_by(Node.id)).all() assert len(_node_list) == 3 _node = _node_list[0] assert _node.id == 1 @@ -222,7 +223,7 @@ def test_error_2(self, processor, db): # assertion db.rollback() - _node = db.query(Node).first() + _node = db.scalars(select(Node).limit(1)).first() assert _node.id == 1 assert _node.endpoint_uri == WEB3_HTTP_PROVIDER assert _node.priority == 0 @@ -242,7 +243,7 @@ def test_error_2(self, processor, db): # assertion db.rollback() - _node = db.query(Node).first() + _node = db.scalars(select(Node).limit(1)).first() assert _node.id == 1 assert _node.endpoint_uri == WEB3_HTTP_PROVIDER assert _node.priority == 0 diff --git a/tests/test_batch_processor_register_personal_info.py b/tests/test_batch_processor_register_personal_info.py index c396c605..89069bb7 100644 --- a/tests/test_batch_processor_register_personal_info.py +++ b/tests/test_batch_processor_register_personal_info.py @@ -17,11 +17,12 @@ SPDX-License-Identifier: Apache-2.0 """ import logging -from typing import Optional +from typing import Optional, Sequence from unittest.mock import patch import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from sqlalchemy.orm import Session from app.exceptions import ContractRevertError, SendTransactionError @@ -249,29 +250,25 @@ def test_normal_2(self, processor: Processor, db: Session, personal_info_contrac processor.process() # Assertion - _batch_register_upload_list = ( - db.query(BatchRegisterPersonalInfoUpload) - .filter( + _batch_register_upload_list = db.scalars( + select(BatchRegisterPersonalInfoUpload).where( BatchRegisterPersonalInfoUpload.upload_id.in_( [self.upload_id_list[0], self.upload_id_list[4]] ) ) - .all() - ) + ).all() for _upload in _batch_register_upload_list: assert ( _upload.status == BatchRegisterPersonalInfoUploadStatus.DONE.value ) - _batch_register_list = ( - db.query(BatchRegisterPersonalInfo) - .filter( + _batch_register_list = db.scalars( + select(BatchRegisterPersonalInfo).where( BatchRegisterPersonalInfo.upload_id.in_( [self.upload_id_list[0], self.upload_id_list[4]] ) ) - .all() - ) + ).all() for _batch_register in _batch_register_list: assert _batch_register.status == 1 @@ -365,41 +362,41 @@ def test_normal_3(self, processor: Processor, db: Session, personal_info_contrac processor.process() # Assertion - _batch_register_upload_list: list[BatchRegisterPersonalInfoUpload] = ( - db.query(BatchRegisterPersonalInfoUpload) - .filter( + _batch_register_upload_list: Sequence[ + BatchRegisterPersonalInfoUpload + ] = db.scalars( + select(BatchRegisterPersonalInfoUpload) + .where( BatchRegisterPersonalInfoUpload.status == BatchRegisterPersonalInfoUploadStatus.DONE.value ) .order_by(BatchRegisterPersonalInfoUpload.created) - .all() - ) + ).all() assert len(_batch_register_upload_list) == 2 assert _batch_register_upload_list[0].issuer_address == _account["address"] assert _batch_register_upload_list[1].issuer_address == _account["address"] - _batch_register_list = ( - db.query(BatchRegisterPersonalInfo) - .filter( + _batch_register_list = db.scalars( + select(BatchRegisterPersonalInfo).where( BatchRegisterPersonalInfo.upload_id.in_( [r.upload_id for r in _batch_register_upload_list] ) ) - .all() - ) + ).all() for _batch_register in _batch_register_list: assert _batch_register.status == 1 - _batch_register_upload_list: list[BatchRegisterPersonalInfoUpload] = ( - db.query(BatchRegisterPersonalInfoUpload) - .filter( + _batch_register_upload_list: Sequence[ + BatchRegisterPersonalInfoUpload + ] = db.scalars( + select(BatchRegisterPersonalInfoUpload) + .where( BatchRegisterPersonalInfoUpload.status == BatchRegisterPersonalInfoUploadStatus.PENDING.value ) .order_by(BatchRegisterPersonalInfoUpload.created) - .all() - ) + ).all() assert len(_batch_register_upload_list) == 4 @@ -420,15 +417,13 @@ def test_normal_3(self, processor: Processor, db: Session, personal_info_contrac == _other_issuer["address"] ) - _batch_register_list = ( - db.query(BatchRegisterPersonalInfo) - .filter( + _batch_register_list = db.scalars( + select(BatchRegisterPersonalInfo).where( BatchRegisterPersonalInfo.upload_id.in_( [r.upload_id for r in _batch_register_upload_list] ) ) - .all() - ) + ).all() for _batch_register in _batch_register_list: assert _batch_register.status == 0 @@ -519,41 +514,41 @@ def test_normal_4(self, processor: Processor, db: Session, personal_info_contrac processor.process() # Assertion - _batch_register_upload_list: list[BatchRegisterPersonalInfoUpload] = ( - db.query(BatchRegisterPersonalInfoUpload) - .filter( + _batch_register_upload_list: Sequence[ + BatchRegisterPersonalInfoUpload + ] = db.scalars( + select(BatchRegisterPersonalInfoUpload) + .where( BatchRegisterPersonalInfoUpload.status == BatchRegisterPersonalInfoUploadStatus.DONE.value ) .order_by(BatchRegisterPersonalInfoUpload.created) - .all() - ) + ).all() assert len(_batch_register_upload_list) == 2 assert _batch_register_upload_list[0].issuer_address == _account["address"] assert _batch_register_upload_list[1].issuer_address == _account["address"] - _batch_register_list = ( - db.query(BatchRegisterPersonalInfo) - .filter( + _batch_register_list = db.scalars( + select(BatchRegisterPersonalInfo).where( BatchRegisterPersonalInfo.upload_id.in_( [r.upload_id for r in _batch_register_upload_list] ) ) - .all() - ) + ).all() for _batch_register in _batch_register_list: assert _batch_register.status == 1 - _batch_register_upload_list: list[BatchRegisterPersonalInfoUpload] = ( - db.query(BatchRegisterPersonalInfoUpload) - .filter( + _batch_register_upload_list: Sequence[ + BatchRegisterPersonalInfoUpload + ] = db.scalars( + select(BatchRegisterPersonalInfoUpload) + .where( BatchRegisterPersonalInfoUpload.status == BatchRegisterPersonalInfoUploadStatus.PENDING.value ) .order_by(BatchRegisterPersonalInfoUpload.created) - .all() - ) + ).all() assert len(_batch_register_upload_list) == 4 @@ -562,15 +557,13 @@ def test_normal_4(self, processor: Processor, db: Session, personal_info_contrac assert _batch_register_upload_list[2].issuer_address == _account["address"] assert _batch_register_upload_list[3].issuer_address == _account["address"] - _batch_register_list = ( - db.query(BatchRegisterPersonalInfo) - .filter( + _batch_register_list = db.scalars( + select(BatchRegisterPersonalInfo).where( BatchRegisterPersonalInfo.upload_id.in_( [r.upload_id for r in _batch_register_upload_list[0:3]] ) ) - .all() - ) + ).all() for _batch_register in _batch_register_list: assert _batch_register.status == 0 @@ -646,14 +639,16 @@ def test_error_1( mock.assert_not_called() # Assertion - _batch_register_upload: Optional[BatchRegisterPersonalInfoUpload] = ( - db.query(BatchRegisterPersonalInfoUpload) - .filter( + _batch_register_upload: Optional[ + BatchRegisterPersonalInfoUpload + ] = db.scalars( + select(BatchRegisterPersonalInfoUpload) + .where( BatchRegisterPersonalInfoUpload.issuer_address == _account["address"] ) - .first() - ) + .limit(1) + ).first() assert ( _batch_register_upload.status == BatchRegisterPersonalInfoUploadStatus.FAILED.value @@ -667,7 +662,7 @@ def test_error_1( ) ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() for _notification in _notification_list: assert _notification.notice_id is not None assert _notification.issuer_address == _account["address"] @@ -759,14 +754,16 @@ def test_error_2( processor.process() # Assertion - _batch_register_upload: Optional[BatchRegisterPersonalInfoUpload] = ( - db.query(BatchRegisterPersonalInfoUpload) - .filter( + _batch_register_upload: Optional[ + BatchRegisterPersonalInfoUpload + ] = db.scalars( + select(BatchRegisterPersonalInfoUpload) + .where( BatchRegisterPersonalInfoUpload.issuer_address == _account["address"] ) - .first() - ) + .limit(1) + ).first() assert ( _batch_register_upload.status == BatchRegisterPersonalInfoUploadStatus.FAILED.value @@ -794,7 +791,7 @@ def test_error_2( ) ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() for _notification in _notification_list: assert _notification.notice_id is not None assert _notification.issuer_address == _account["address"] @@ -888,14 +885,16 @@ def test_error_3( processor.process() # Assertion - _batch_register_upload: Optional[BatchRegisterPersonalInfoUpload] = ( - db.query(BatchRegisterPersonalInfoUpload) - .filter( + _batch_register_upload: Optional[ + BatchRegisterPersonalInfoUpload + ] = db.scalars( + select(BatchRegisterPersonalInfoUpload) + .where( BatchRegisterPersonalInfoUpload.issuer_address == _account["address"] ) - .first() - ) + .limit(1) + ).first() assert ( _batch_register_upload.status == BatchRegisterPersonalInfoUploadStatus.FAILED.value @@ -923,7 +922,7 @@ def test_error_3( ) ) - _notification_list = db.query(Notification).all() + _notification_list = db.scalars(select(Notification)).all() for _notification in _notification_list: assert _notification.notice_id is not None assert _notification.issuer_address == _account["address"] diff --git a/tests/test_batch_processor_rotate_e2e_messaging_rsa_key.py b/tests/test_batch_processor_rotate_e2e_messaging_rsa_key.py index 3fbf8892..451ec6d2 100644 --- a/tests/test_batch_processor_rotate_e2e_messaging_rsa_key.py +++ b/tests/test_batch_processor_rotate_e2e_messaging_rsa_key.py @@ -24,6 +24,7 @@ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from sqlalchemy.orm import Session import batch.processor_rotate_e2e_messaging_rsa_key as processor_rotate_e2e_messaging_rsa_key @@ -73,11 +74,11 @@ def test_normal_1_1(self, processor, db): processor.process() # Assertion - _rsa_key_list = ( - db.query(E2EMessagingAccountRsaKey) - .order_by(E2EMessagingAccountRsaKey.block_timestamp) - .all() - ) + _rsa_key_list = db.scalars( + select(E2EMessagingAccountRsaKey).order_by( + E2EMessagingAccountRsaKey.block_timestamp + ) + ).all() assert len(_rsa_key_list) == 0 # @@ -109,11 +110,11 @@ def test_normal_1_2(self, processor, db): processor.process() # Assertion - _rsa_key_list = ( - db.query(E2EMessagingAccountRsaKey) - .order_by(E2EMessagingAccountRsaKey.block_timestamp) - .all() - ) + _rsa_key_list = db.scalars( + select(E2EMessagingAccountRsaKey).order_by( + E2EMessagingAccountRsaKey.block_timestamp + ) + ).all() assert len(_rsa_key_list) == 1 _rsa_key = _rsa_key_list[0] assert _rsa_key.id == 1 @@ -153,11 +154,11 @@ def test_normal_1_3(self, processor, db): processor.process() # Assertion - _rsa_key_list = ( - db.query(E2EMessagingAccountRsaKey) - .order_by(E2EMessagingAccountRsaKey.block_timestamp) - .all() - ) + _rsa_key_list = db.scalars( + select(E2EMessagingAccountRsaKey).order_by( + E2EMessagingAccountRsaKey.block_timestamp + ) + ).all() assert len(_rsa_key_list) == 1 _rsa_key = _rsa_key_list[0] assert _rsa_key.id == 1 @@ -304,12 +305,11 @@ def test_normal_2(self, processor, db, e2e_messaging_contract): [call(tx_hash="tx_5"), call(tx_hash="tx_6")] ) - _rsa_key_list = ( - db.query(E2EMessagingAccountRsaKey) - .filter(E2EMessagingAccountRsaKey.account_address == user_address_1) + _rsa_key_list = db.scalars( + select(E2EMessagingAccountRsaKey) + .where(E2EMessagingAccountRsaKey.account_address == user_address_1) .order_by(E2EMessagingAccountRsaKey.block_timestamp) - .all() - ) + ).all() assert len(_rsa_key_list) == 2 _rsa_key = _rsa_key_list[0] assert _rsa_key.id == 3 @@ -329,12 +329,11 @@ def test_normal_2(self, processor, db, e2e_messaging_contract): assert _rsa_key.rsa_public_key == ANY assert E2EEUtils.decrypt(_rsa_key.rsa_passphrase) == "latest_passphrase_1" assert _rsa_key.block_timestamp == datetime(2099, 4, 27, 12, 34, 59) - _rsa_key_list = ( - db.query(E2EMessagingAccountRsaKey) - .filter(E2EMessagingAccountRsaKey.account_address == user_address_2) + _rsa_key_list = db.scalars( + select(E2EMessagingAccountRsaKey) + .where(E2EMessagingAccountRsaKey.account_address == user_address_2) .order_by(E2EMessagingAccountRsaKey.block_timestamp) - .all() - ) + ).all() assert len(_rsa_key_list) == 2 _rsa_key = _rsa_key_list[0] assert _rsa_key.id == 4 @@ -394,11 +393,11 @@ def test_error_1(self, processor, db): processor.process() # Assertion - _rsa_key_list = ( - db.query(E2EMessagingAccountRsaKey) - .order_by(E2EMessagingAccountRsaKey.block_timestamp) - .all() - ) + _rsa_key_list = db.scalars( + select(E2EMessagingAccountRsaKey).order_by( + E2EMessagingAccountRsaKey.block_timestamp + ) + ).all() # # Failed to send transaction @@ -452,11 +451,11 @@ def test_error_2(self, processor, db, e2e_messaging_contract): private_key=user_private_key_1, ) - _rsa_key_list = ( - db.query(E2EMessagingAccountRsaKey) - .order_by(E2EMessagingAccountRsaKey.block_timestamp) - .all() - ) + _rsa_key_list = db.scalars( + select(E2EMessagingAccountRsaKey).order_by( + E2EMessagingAccountRsaKey.block_timestamp + ) + ).all() assert len(_rsa_key_list) == 1 # @@ -517,11 +516,11 @@ def test_error_3( private_key=user_private_key_1, ) - _rsa_key_list = ( - db.query(E2EMessagingAccountRsaKey) - .order_by(E2EMessagingAccountRsaKey.block_timestamp) - .all() - ) + _rsa_key_list = db.scalars( + select(E2EMessagingAccountRsaKey).order_by( + E2EMessagingAccountRsaKey.block_timestamp + ) + ).all() assert len(_rsa_key_list) == 1 assert ( caplog.record_tuples.count( diff --git a/tests/test_batch_processor_scheduled_events.py b/tests/test_batch_processor_scheduled_events.py index 282fb45e..31eff204 100644 --- a/tests/test_batch_processor_scheduled_events.py +++ b/tests/test_batch_processor_scheduled_events.py @@ -21,6 +21,7 @@ from unittest.mock import patch import pytest +from sqlalchemy import select from app.exceptions import ContractRevertError, SendTransactionError from app.model.db import ( @@ -144,23 +145,23 @@ def test_normal_1_1(self, processor, db): processor.process() # Assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_1) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_1) + .limit(1) + ).first() assert _scheduled_event.status == 2 - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_2) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_2) + .limit(1) + ).first() assert _scheduled_event.status == 1 - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_3) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_3) + .limit(1) + ).first() assert _scheduled_event.status == 0 # @@ -255,23 +256,23 @@ def test_normal_1_2(self, processor, db): processor.process() # Assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_1) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_1) + .limit(1) + ).first() assert _scheduled_event.status == 2 - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_2) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_2) + .limit(1) + ).first() assert _scheduled_event.status == 1 - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_3) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_3) + .limit(1) + ).first() assert _scheduled_event.status == 0 # @@ -366,23 +367,23 @@ def test_normal_2_1(self, processor, db): processor.process() # Assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_1) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_1) + .limit(1) + ).first() assert _scheduled_event.status == 2 - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_2) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_2) + .limit(1) + ).first() assert _scheduled_event.status == 1 - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_3) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_3) + .limit(1) + ).first() assert _scheduled_event.status == 0 # @@ -477,23 +478,23 @@ def test_normal_2_2(self, processor, db): processor.process() # Assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_1) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_1) + .limit(1) + ).first() assert _scheduled_event.status == 2 - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_2) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_2) + .limit(1) + ).first() assert _scheduled_event.status == 1 - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address_3) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address_3) + .limit(1) + ).first() assert _scheduled_event.status == 0 ########################################################################### @@ -530,13 +531,13 @@ def test_error_1(self, processor, db): processor.process() # Assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address) + .limit(1) + ).first() assert _scheduled_event.status == 2 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _issuer_address @@ -585,13 +586,13 @@ def test_error_2(self, processor, db): processor.process() # Assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address) + .limit(1) + ).first() assert _scheduled_event.status == 2 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _issuer_address @@ -649,13 +650,13 @@ def test_error_3(self, processor, db): processor.process() # Assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address) + .limit(1) + ).first() assert _scheduled_event.status == 2 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _issuer_address @@ -714,13 +715,13 @@ def test_error_4(self, processor, db): processor.process() # Assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address) + .limit(1) + ).first() assert _scheduled_event.status == 2 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _issuer_address @@ -778,13 +779,13 @@ def test_error_5(self, processor, db, caplog): processor.process() # Assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address) + .limit(1) + ).first() assert _scheduled_event.status == 2 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _issuer_address @@ -853,13 +854,13 @@ def test_error_6(self, processor, db, caplog): processor.process() # Assertion - _scheduled_event = ( - db.query(ScheduledEvents) - .filter(ScheduledEvents.token_address == _token_address) - .first() - ) + _scheduled_event = db.scalars( + select(ScheduledEvents) + .where(ScheduledEvents.token_address == _token_address) + .limit(1) + ).first() assert _scheduled_event.status == 2 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == _issuer_address diff --git a/tests/test_batch_processor_update_token.py b/tests/test_batch_processor_update_token.py index 439c2a8c..e40865da 100644 --- a/tests/test_batch_processor_update_token.py +++ b/tests/test_batch_processor_update_token.py @@ -20,6 +20,7 @@ from unittest.mock import ANY, call, patch import pytest +from sqlalchemy import select from app.exceptions import SendTransactionError from app.model.db import ( @@ -251,7 +252,9 @@ def test_normal_1(self, processor, db): ) # assertion(DB) - _idx_position_list = db.query(IDXPosition).order_by(IDXPosition.id).all() + _idx_position_list = db.scalars( + select(IDXPosition).order_by(IDXPosition.id) + ).all() assert len(_idx_position_list) == 2 _idx_position = _idx_position_list[0] assert _idx_position.id == 1 @@ -268,7 +271,7 @@ def test_normal_1(self, processor, db): assert _idx_position.exchange_commitment == 0 assert _idx_position.pending_transfer == 0 - _utxo_list = db.query(UTXO).order_by(UTXO.transaction_hash).all() + _utxo_list = db.scalars(select(UTXO).order_by(UTXO.transaction_hash)).all() _utxo = _utxo_list[0] assert _utxo.transaction_hash == "tx_hash_1" assert _utxo.account_address == _issuer_address @@ -284,13 +287,15 @@ def test_normal_1(self, processor, db): assert _utxo.block_number == 12345 assert _utxo.block_timestamp == datetime(2021, 4, 27, 12, 34, 56) - _token_list = db.query(Token).order_by(Token.id).all() + _token_list = db.scalars(select(Token).order_by(Token.id)).all() _token = _token_list[0] assert _token.token_status == 1 _token = _token_list[1] assert _token.token_status == 1 - _update_token_list = db.query(UpdateToken).order_by(UpdateToken.id).all() + _update_token_list = db.scalars( + select(UpdateToken).order_by(UpdateToken.id) + ).all() _update_token = _update_token_list[0] assert _update_token.status == 1 _update_token = _update_token_list[1] @@ -300,7 +305,9 @@ def test_normal_1(self, processor, db): _update_token = _update_token_list[3] assert _update_token.status == 2 - _notification_list = db.query(Notification).order_by(Notification.id).all() + _notification_list = db.scalars( + select(Notification).order_by(Notification.id) + ).all() assert len(_notification_list) == 0 ########################################################################### @@ -427,19 +434,23 @@ def test_error_1(self, processor, db): processor.process() # assertion(DB) - _idx_position_list = db.query(IDXPosition).order_by(IDXPosition.id).all() + _idx_position_list = db.scalars( + select(IDXPosition).order_by(IDXPosition.id) + ).all() assert len(_idx_position_list) == 0 - _utxo_list = db.query(UTXO).order_by(UTXO.transaction_hash).all() + _utxo_list = db.scalars(select(UTXO).order_by(UTXO.transaction_hash)).all() assert len(_utxo_list) == 0 - _token_list = db.query(Token).order_by(Token.id).all() + _token_list = db.scalars(select(Token).order_by(Token.id)).all() _token = _token_list[0] assert _token.token_status == 2 _token = _token_list[1] assert _token.token_status == 2 - _update_token_list = db.query(UpdateToken).order_by(UpdateToken.id).all() + _update_token_list = db.scalars( + select(UpdateToken).order_by(UpdateToken.id) + ).all() _update_token = _update_token_list[0] assert _update_token.status == 2 _update_token = _update_token_list[1] @@ -449,7 +460,9 @@ def test_error_1(self, processor, db): _update_token = _update_token_list[3] assert _update_token.status == 2 - _notification_list = db.query(Notification).order_by(Notification.id).all() + _notification_list = db.scalars( + select(Notification).order_by(Notification.id) + ).all() _notification = _notification_list[0] assert _notification.id == 1 assert _notification.notice_id is not None @@ -635,19 +648,23 @@ def test_error_2(self, processor, db): processor.process() # assertion(DB) - _idx_position_list = db.query(IDXPosition).order_by(IDXPosition.id).all() + _idx_position_list = db.scalars( + select(IDXPosition).order_by(IDXPosition.id) + ).all() assert len(_idx_position_list) == 0 - _utxo_list = db.query(UTXO).order_by(UTXO.transaction_hash).all() + _utxo_list = db.scalars(select(UTXO).order_by(UTXO.transaction_hash)).all() assert len(_utxo_list) == 0 - _token_list = db.query(Token).order_by(Token.id).all() + _token_list = db.scalars(select(Token).order_by(Token.id)).all() _token = _token_list[0] assert _token.token_status == 2 _token = _token_list[1] assert _token.token_status == 2 - _update_token_list = db.query(UpdateToken).order_by(UpdateToken.id).all() + _update_token_list = db.scalars( + select(UpdateToken).order_by(UpdateToken.id) + ).all() _update_token = _update_token_list[0] assert _update_token.status == 2 _update_token = _update_token_list[1] @@ -657,7 +674,9 @@ def test_error_2(self, processor, db): _update_token = _update_token_list[3] assert _update_token.status == 2 - _notification_list = db.query(Notification).order_by(Notification.id).all() + _notification_list = db.scalars( + select(Notification).order_by(Notification.id) + ).all() _notification = _notification_list[0] assert _notification.id == 1 assert _notification.notice_id is not None @@ -850,19 +869,23 @@ def test_error_3(self, processor, db): processor.process() # assertion(DB) - _idx_position_list = db.query(IDXPosition).order_by(IDXPosition.id).all() + _idx_position_list = db.scalars( + select(IDXPosition).order_by(IDXPosition.id) + ).all() assert len(_idx_position_list) == 0 - _utxo_list = db.query(UTXO).order_by(UTXO.transaction_hash).all() + _utxo_list = db.scalars(select(UTXO).order_by(UTXO.transaction_hash)).all() assert len(_utxo_list) == 0 - _token_list = db.query(Token).order_by(Token.id).all() + _token_list = db.scalars(select(Token).order_by(Token.id)).all() _token = _token_list[0] assert _token.token_status == 2 _token = _token_list[1] assert _token.token_status == 2 - _update_token_list = db.query(UpdateToken).order_by(UpdateToken.id).all() + _update_token_list = db.scalars( + select(UpdateToken).order_by(UpdateToken.id) + ).all() _update_token = _update_token_list[0] assert _update_token.status == 2 _update_token = _update_token_list[1] @@ -872,7 +895,9 @@ def test_error_3(self, processor, db): _update_token = _update_token_list[3] assert _update_token.status == 2 - _notification_list = db.query(Notification).order_by(Notification.id).all() + _notification_list = db.scalars( + select(Notification).order_by(Notification.id) + ).all() _notification = _notification_list[0] assert _notification.id == 1 assert _notification.notice_id is not None @@ -1107,19 +1132,23 @@ def test_error_4(self, processor, db): ) # assertion(DB) - _idx_position_list = db.query(IDXPosition).order_by(IDXPosition.id).all() + _idx_position_list = db.scalars( + select(IDXPosition).order_by(IDXPosition.id) + ).all() assert len(_idx_position_list) == 0 - _utxo_list = db.query(UTXO).order_by(UTXO.transaction_hash).all() + _utxo_list = db.scalars(select(UTXO).order_by(UTXO.transaction_hash)).all() assert len(_utxo_list) == 0 - _token_list = db.query(Token).order_by(Token.id).all() + _token_list = db.scalars(select(Token).order_by(Token.id)).all() _token = _token_list[0] assert _token.token_status == 2 _token = _token_list[1] assert _token.token_status == 2 - _update_token_list = db.query(UpdateToken).order_by(UpdateToken.id).all() + _update_token_list = db.scalars( + select(UpdateToken).order_by(UpdateToken.id) + ).all() _update_token = _update_token_list[0] assert _update_token.status == 2 _update_token = _update_token_list[1] @@ -1129,7 +1158,9 @@ def test_error_4(self, processor, db): _update_token = _update_token_list[3] assert _update_token.status == 2 - _notification_list = db.query(Notification).order_by(Notification.id).all() + _notification_list = db.scalars( + select(Notification).order_by(Notification.id) + ).all() _notification = _notification_list[0] assert _notification.id == 1 assert _notification.notice_id is not None diff --git a/tests/test_utils_contract_utils.py b/tests/test_utils_contract_utils.py index 39559be8..99c79f75 100755 --- a/tests/test_utils_contract_utils.py +++ b/tests/test_utils_contract_utils.py @@ -21,6 +21,7 @@ import pytest from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from sqlalchemy.orm import Session from web3 import Web3 from web3.exceptions import ContractLogicError, Web3Exception @@ -369,9 +370,12 @@ def test_error_3(self, db: Session): ) # Transaction Lock - db.query(TransactionLock).filter( - TransactionLock.tx_from == self.test_account["address"] - ).populate_existing().with_for_update().first() + db.scalars( + select(TransactionLock) + .where(TransactionLock.tx_from == self.test_account["address"]) + .limit(1) + .with_for_update() + ).first() with pytest.raises(SendTransactionError) as ex_info: ContractUtils.send_transaction(transaction=tx, private_key=self.private_key) diff --git a/tests/test_utils_ledger_utils.py b/tests/test_utils_ledger_utils.py index a5bc11b7..f3fbf4ad 100644 --- a/tests/test_utils_ledger_utils.py +++ b/tests/test_utils_ledger_utils.py @@ -20,6 +20,7 @@ import pytz from eth_keyfile import decode_keyfile_json +from sqlalchemy import select from web3 import Web3 from web3.middleware import geth_poa_middleware @@ -430,9 +431,9 @@ def test_normal_1(self, db): ledger_utils.create_ledger(token_address_1, db) # assertion - _notifications = db.query(Notification).all() + _notifications = db.scalars(select(Notification)).all() assert len(_notifications) == 1 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == issuer_address @@ -445,7 +446,7 @@ def test_normal_1(self, db): "ledger_id": 1, } - _ledger = db.query(Ledger).first() + _ledger = db.scalars(select(Ledger).limit(1)).first() assert _ledger.id == 1 assert _ledger.token_address == token_address_1 assert _ledger.token_type == TokenType.IBET_SHARE.value @@ -897,9 +898,9 @@ def test_normal_2(self, db): ledger_utils.create_ledger(token_address_1, db) # assertion - _notifications = db.query(Notification).all() + _notifications = db.scalars(select(Notification)).all() assert len(_notifications) == 1 - _notification = db.query(Notification).first() + _notification = db.scalars(select(Notification).limit(1)).first() assert _notification.id == 1 assert _notification.notice_id is not None assert _notification.issuer_address == issuer_address @@ -911,7 +912,7 @@ def test_normal_2(self, db): "token_type": TokenType.IBET_STRAIGHT_BOND.value, "ledger_id": 1, } - _ledger = db.query(Ledger).first() + _ledger = db.scalars(select(Ledger).limit(1)).first() assert _ledger.id == 1 assert _ledger.token_address == token_address_1 assert _ledger.token_type == TokenType.IBET_STRAIGHT_BOND.value @@ -1091,9 +1092,9 @@ def test_normal_3(self, db): ledger_utils.create_ledger(token_address_1, db) # assertion - _notifications = db.query(Notification).all() + _notifications = db.scalars(select(Notification)).all() assert len(_notifications) == 0 - _ledger = db.query(Ledger).first() + _ledger = db.scalars(select(Ledger).limit(1)).first() assert _ledger is None # @@ -1122,9 +1123,9 @@ def test_normal_4(self, db): ledger_utils.create_ledger(token_address_1, db) # assertion - _notifications = db.query(Notification).all() + _notifications = db.scalars(select(Notification)).all() assert len(_notifications) == 0 - _ledger = db.query(Ledger).first() + _ledger = db.scalars(select(Ledger).limit(1)).first() assert _ledger is None ###########################################################################