"""Module for generic helper methods.""" import random import string from abacus_common_logic.connectors.database import db from tests.integration.conftest import ows_abacus_contract_api_client from tests.integration.consts.headers import ADMIN_HEADERS def generate_random_string(length): """Generate a random alphanumeric string.""" return ''.join( random.choices( string.ascii_letters + string.digits, k=length ) ) def create_account(account_id=None): """Create account and payment_term_template entities.""" account_id = random.randint(100, 10**6) if account_id is None else account_id db.engine.execute(f""" INSERT IGNORE INTO account( account_id, account_name, created_by, created_at, last_modified_by, last_modified) VALUES( {account_id}, 'Test account', 'default_user_id', NOW(), 'default_user_id', NOW() )""") return {'account_id': account_id} def create_runcontroller(runcontroller_id=None): """Create runcontroller entity.""" runcontroller_id = random.randint(100, 10**6) if runcontroller_id is None else runcontroller_id # noqa: 501 db.engine.execute(f""" INSERT IGNORE INTO run_controller ( run_controller_id, run_controller_name, contract_type, created_by, created_at, last_modified_by, last_modified, deleted_by, deleted_at ) VALUES ( {runcontroller_id}, 'integration_tests', 'distribution', 'integration_tests', '2025-08-04 11:02:09', 'integration_tests', '2025-08-04 11:02:13', null, null )""") return {'runcontroller_id': runcontroller_id} def create_runcontroller_contract(runcontroller_id, contract_id): """Create runcontroller_contract entity.""" db.engine.execute(f""" INSERT INTO run_controller_contract ( run_controller_id, contract_id ) VALUES ( {runcontroller_id}, {contract_id}) """) def create_account_and_contract( account_id=None, contract_name=None ) -> tuple[int, dict, int]: """Create an account and a contract.""" admin_client = ows_abacus_contract_api_client(ADMIN_HEADERS) account = create_account(account_id=account_id) contract_body = admin_client.generate_contract_body( contract_name=contract_name or 'orcd_autotest_{}'.format( generate_random_string(16)), account_id=account['account_id'] ) res = admin_client.post_contract(contract_body) assert res.status_code == 201 contract_id = res.json()['contract_id'] return contract_id, contract_body, account['account_id']