diff options
Diffstat (limited to 'test_utils/managers')
17 files changed, 532 insertions, 1816 deletions
diff --git a/test_utils/managers/conftest.py b/test_utils/managers/conftest.py index c8a6e2f..d2e5d20 100644 --- a/test_utils/managers/conftest.py +++ b/test_utils/managers/conftest.py @@ -1,9 +1,33 @@ -from typing import TYPE_CHECKING, Callable +from __future__ import annotations + +from typing import Callable import pytest -from generalresearch.managers.base import Permission +from generalresearch.managers.gr.business import ( + BusinessAddressManager, + BusinessBankAccountManager, + BusinessManager, +) +from generalresearch.managers.gr.team import ( + MembershipManager, + TeamManager, +) +from generalresearch.managers.spectrum.survey import SpectrumSurveyManager +from generalresearch.managers.thl.buyer import BuyerManager +from generalresearch.managers.thl.ipinfo import ( + GeoIpInfoManager, + IPGeonameManager, + IPInformationManager, +) +from generalresearch.managers.thl.profiling.uqa import UQAManager +from generalresearch.managers.thl.userhealth import ( + AuditLogManager, + IPRecordManager, + UserIpHistoryManager, +) from generalresearch.models import Source +from generalresearch.models.thl.user import User from generalresearch.pg_helper import PostgresConfig from generalresearch.redis_helper import RedisConfig from generalresearch.sql_helper import SqlHelper @@ -11,437 +35,11 @@ from test_utils.managers.cashout_methods import ( EXAMPLE_TANGO_CASHOUT_METHODS, ) -if TYPE_CHECKING: - from generalresearch.config import GRLBaseSettings - from generalresearch.grliq.managers.forensic_data import ( - GrlIqDataManager, - ) - from generalresearch.grliq.managers.forensic_events import ( - GrlIqEventManager, - ) - from generalresearch.grliq.managers.forensic_results import ( - GrlIqCategoryResultsReader, - ) - from generalresearch.managers.gr.authentication import ( - GRTokenManager, - GRUserManager, - ) - from generalresearch.managers.gr.business import ( - BusinessAddressManager, - BusinessBankAccountManager, - BusinessManager, - ) - from generalresearch.managers.gr.team import ( - MembershipManager, - TeamManager, - ) - from generalresearch.managers.thl.buyer import BuyerManager - from generalresearch.managers.thl.category import CategoryManager - from generalresearch.managers.thl.contest_manager import ContestManager - from generalresearch.managers.thl.ipinfo import ( - GeoIpInfoManager, - IPGeonameManager, - IPInformationManager, - ) - from generalresearch.managers.thl.ledger_manager.ledger import ( - LedgerAccountManager, - LedgerManager, - LedgerTransactionManager, - ) - from generalresearch.managers.thl.ledger_manager.thl_ledger import ( - ThlLedgerManager, - ) - from generalresearch.managers.thl.maxmind import MaxmindManager - from generalresearch.managers.thl.maxmind.basic import ( - MaxmindBasicManager, - ) - from generalresearch.managers.thl.payout import ( - BrokerageProductPayoutEventManager, - BusinessPayoutEventManager, - PayoutEventManager, - UserPayoutEventManager, - ) - from generalresearch.managers.thl.product import ProductManager - from generalresearch.managers.thl.session import SessionManager - from generalresearch.managers.thl.task_adjustment import ( - TaskAdjustmentManager, - ) - from generalresearch.managers.thl.user_manager.user_manager import ( - UserManager, - ) - from generalresearch.managers.thl.user_manager.user_metadata_manager import ( - UserMetadataManager, - ) - from generalresearch.managers.thl.userhealth import ( - AuditLogManager, - IPRecordManager, - UserIpHistoryManager, - ) - from generalresearch.managers.thl.wall import ( - WallCacheManager, - WallManager, - ) - from generalresearch.models.thl.user import User - - # === THL === @pytest.fixture(scope="session") -def ltxm( - thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "LedgerTransactionManager": - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.ledger_manager.ledger import ( - LedgerTransactionManager, - ) - - return LedgerTransactionManager( - pg_config=thl_web_rw, - permissions=[Permission.CREATE, Permission.READ], - testing=True, - redis_config=thl_redis_config, - ) - - -@pytest.fixture(scope="session") -def lam( - thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "LedgerAccountManager": - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.ledger_manager.ledger import ( - LedgerAccountManager, - ) - - return LedgerAccountManager( - pg_config=thl_web_rw, - permissions=[Permission.CREATE, Permission.READ], - testing=True, - redis_config=thl_redis_config, - ) - - -@pytest.fixture(scope="session") -def lm(thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig) -> "LedgerManager": - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.ledger_manager.ledger import ( - LedgerManager, - ) - - return LedgerManager( - pg_config=thl_web_rw, - permissions=[ - Permission.CREATE, - Permission.READ, - Permission.UPDATE, - Permission.DELETE, - ], - testing=True, - redis_config=thl_redis_config, - ) - - -@pytest.fixture(scope="session") -def thl_lm( - thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "ThlLedgerManager": - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.ledger_manager.thl_ledger import ( - ThlLedgerManager, - ) - - return ThlLedgerManager( - pg_config=thl_web_rw, - permissions=[ - Permission.CREATE, - Permission.READ, - Permission.UPDATE, - Permission.DELETE, - ], - testing=True, - redis_config=thl_redis_config, - ) - - -@pytest.fixture(scope="session") -def payout_event_manager( - thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "PayoutEventManager": - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.payout import PayoutEventManager - - return PayoutEventManager( - pg_config=thl_web_rw, - permissions=[Permission.CREATE, Permission.READ], - redis_config=thl_redis_config, - ) - - -@pytest.fixture(scope="session") -def user_payout_event_manager( - thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "UserPayoutEventManager": - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.payout import UserPayoutEventManager - - return UserPayoutEventManager( - pg_config=thl_web_rw, - permissions=[Permission.CREATE, Permission.READ], - redis_config=thl_redis_config, - ) - - -@pytest.fixture(scope="session") -def brokerage_product_payout_event_manager( - thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "BrokerageProductPayoutEventManager": - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.payout import ( - BrokerageProductPayoutEventManager, - ) - - return BrokerageProductPayoutEventManager( - pg_config=thl_web_rw, - permissions=[Permission.CREATE, Permission.READ], - redis_config=thl_redis_config, - ) - - -@pytest.fixture(scope="session") -def business_payout_event_manager( - thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "BusinessPayoutEventManager": - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.payout import ( - BusinessPayoutEventManager, - ) - - return BusinessPayoutEventManager( - pg_config=thl_web_rw, - permissions=[Permission.CREATE, Permission.READ], - redis_config=thl_redis_config, - ) - - -@pytest.fixture(scope="session") -def product_manager(thl_web_rw: PostgresConfig) -> "ProductManager": - assert thl_web_rw.dsn - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.product import ProductManager - - return ProductManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def user_manager( - settings: "GRLBaseSettings", thl_web_rw: PostgresConfig, thl_web_rr: PostgresConfig -) -> "UserManager": - assert thl_web_rw.dsn - assert thl_web_rw.dsn.path - assert thl_web_rr.dsn - assert thl_web_rr.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rr.dsn.path - - from generalresearch.managers.thl.user_manager.user_manager import ( - UserManager, - ) - - return UserManager( - pg_config=thl_web_rw, - pg_config_rr=thl_web_rr, - redis=settings.redis, - ) - - -@pytest.fixture(scope="session") -def user_metadata_manager(thl_web_rw: PostgresConfig) -> "UserMetadataManager": - assert thl_web_rw.dsn - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.user_manager.user_metadata_manager import ( - UserMetadataManager, - ) - - return UserMetadataManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def session_manager(thl_web_rw: PostgresConfig) -> "SessionManager": - assert thl_web_rw.dsn - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.session import SessionManager - - return SessionManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def wall_manager(thl_web_rw: PostgresConfig) -> "WallManager": - assert thl_web_rw.dsn - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.wall import WallManager - - return WallManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def wall_cache_manager( - thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "WallCacheManager": - # assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.wall import WallCacheManager - - return WallCacheManager(pg_config=thl_web_rw, redis_config=thl_redis_config) - - -@pytest.fixture(scope="session") -def task_adjustment_manager(thl_web_rw: PostgresConfig) -> "TaskAdjustmentManager": - # assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.task_adjustment import ( - TaskAdjustmentManager, - ) - - return TaskAdjustmentManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def contest_manager(thl_web_rw: PostgresConfig) -> "ContestManager": - assert thl_web_rw.dsn - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.contest_manager import ContestManager - - return ContestManager( - pg_config=thl_web_rw, - permissions=[ - Permission.CREATE, - Permission.READ, - Permission.UPDATE, - Permission.DELETE, - ], - ) - - -@pytest.fixture(scope="session") -def category_manager(thl_web_rw: PostgresConfig) -> "CategoryManager": - assert thl_web_rw.dsn - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - from generalresearch.managers.thl.category import CategoryManager - - return CategoryManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def buyer_manager(thl_web_rw: PostgresConfig) -> "BuyerManager": - # assert "/unittest-" in thl_web_rw.dsn.path - from generalresearch.managers.thl.buyer import BuyerManager - - return BuyerManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def survey_manager(thl_web_rw: PostgresConfig): - # assert "/unittest-" in thl_web_rw.dsn.path - from generalresearch.managers.thl.survey import SurveyManager - - return SurveyManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def surveystat_manager(thl_web_rw: PostgresConfig): - # assert "/unittest-" in thl_web_rw.dsn.path - from generalresearch.managers.thl.survey import SurveyStatManager - - return SurveyStatManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def surveypenalty_manager(thl_redis_config: RedisConfig): - from generalresearch.managers.thl.survey_penalty import SurveyPenaltyManager - - return SurveyPenaltyManager(redis_config=thl_redis_config) - - -@pytest.fixture(scope="session") -def upk_schema_manager(thl_web_rw: PostgresConfig): - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - from generalresearch.managers.thl.profiling.schema import ( - UpkSchemaManager, - ) - - return UpkSchemaManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def user_upk_manager(thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig): - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - from generalresearch.managers.thl.profiling.user_upk import ( - UserUpkManager, - ) - - return UserUpkManager(pg_config=thl_web_rw, redis_config=thl_redis_config) - - -@pytest.fixture(scope="session") -def question_manager(thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig): - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - from generalresearch.managers.thl.profiling.question import ( - QuestionManager, - ) - - return QuestionManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def uqa_manager(thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig): - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - from generalresearch.managers.thl.profiling.uqa import UQAManager - - return UQAManager(redis_config=thl_redis_config, pg_config=thl_web_rw) - - -@pytest.fixture(scope="function") -def uqa_manager_clear_cache(uqa_manager, user: "User"): - # On successive py-test/jenkins runs, the cache may contain - # the previous run's info (keyed under the same user_id) - uqa_manager.clear_cache(user) - yield - uqa_manager.clear_cache(user) - - -@pytest.fixture(scope="session") -def audit_log_manager(thl_web_rw: PostgresConfig) -> "AuditLogManager": +def audit_log_manager(thl_web_rw: PostgresConfig) -> AuditLogManager: assert thl_web_rw.dsn.path assert "/unittest-" in thl_web_rw.dsn.path @@ -451,7 +49,7 @@ def audit_log_manager(thl_web_rw: PostgresConfig) -> "AuditLogManager": @pytest.fixture(scope="session") -def ip_geoname_manager(thl_web_rw: PostgresConfig) -> "IPGeonameManager": +def ip_geoname_manager(thl_web_rw: PostgresConfig) -> IPGeonameManager: assert thl_web_rw.dsn.path assert "/unittest-" in thl_web_rw.dsn.path @@ -461,7 +59,7 @@ def ip_geoname_manager(thl_web_rw: PostgresConfig) -> "IPGeonameManager": @pytest.fixture(scope="session") -def ip_information_manager(thl_web_rw: PostgresConfig) -> "IPInformationManager": +def ip_information_manager(thl_web_rw: PostgresConfig) -> IPInformationManager: assert thl_web_rw.dsn.path assert "/unittest-" in thl_web_rw.dsn.path @@ -473,7 +71,7 @@ def ip_information_manager(thl_web_rw: PostgresConfig) -> "IPInformationManager" @pytest.fixture(scope="session") def ip_record_manager( thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "IPRecordManager": +) -> IPRecordManager: assert thl_web_rw.dsn.path assert "/unittest-" in thl_web_rw.dsn.path @@ -485,7 +83,7 @@ def ip_record_manager( @pytest.fixture(scope="session") def user_iphistory_manager( thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "UserIpHistoryManager": +) -> UserIpHistoryManager: assert thl_web_rw.dsn.path assert "/unittest-" in thl_web_rw.dsn.path @@ -508,7 +106,7 @@ def user_iphistory_manager_clear_cache(user_iphistory_manager, user): @pytest.fixture(scope="session") def geoipinfo_manager( thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig -) -> "GeoIpInfoManager": +) -> GeoIpInfoManager: assert thl_web_rw.dsn.path assert "/unittest-" in thl_web_rw.dsn.path @@ -518,38 +116,6 @@ def geoipinfo_manager( @pytest.fixture(scope="session") -def maxmind_basic_manager(settings: "GRLBaseSettings") -> "MaxmindBasicManager": - from generalresearch.managers.thl.maxmind.basic import ( - MaxmindBasicManager, - ) - - return MaxmindBasicManager( - data_dir="/tmp/", - maxmind_account_id=settings.maxmind_account_id, - maxmind_license_key=settings.maxmind_license_key, - ) - - -@pytest.fixture(scope="session") -def maxmind_manager( - settings: "GRLBaseSettings", - thl_web_rw: PostgresConfig, - thl_redis_config: RedisConfig, -) -> "MaxmindManager": - assert thl_web_rw.dsn.path - assert "/unittest-" in thl_web_rw.dsn.path - - from generalresearch.managers.thl.maxmind import MaxmindManager - - return MaxmindManager( - pg_config=thl_web_rw, - redis_config=thl_redis_config, - maxmind_account_id=settings.maxmind_account_id, - maxmind_license_key=settings.maxmind_license_key, - ) - - -@pytest.fixture(scope="session") def cashout_method_manager(thl_web_rw: PostgresConfig): assert thl_web_rw.dsn.path assert "/unittest-" in thl_web_rw.dsn.path @@ -592,7 +158,6 @@ def uqa_db_index(thl_web_rw: PostgresConfig): # except pymysql.OperationalError as e: # if "Duplicate key name 'idx_user_id'" not in str(e): # raise - return None @pytest.fixture(scope="session") @@ -606,9 +171,7 @@ def delete_cashoutmethod_db(thl_web_rw: PostgresConfig) -> Callable[..., None]: @pytest.fixture(scope="session") -def setup_cashoutmethod_db( - settings: "GRLBaseSettings", cashout_method_manager, delete_cashoutmethod_db -): +def setup_cashoutmethod_db(cashout_method_manager, delete_cashoutmethod_db): delete_cashoutmethod_db() for x in EXAMPLE_TANGO_CASHOUT_METHODS: cashout_method_manager.create(x) @@ -621,14 +184,12 @@ def setup_cashoutmethod_db( # cashout_method_manager.create(AMT_BONUS_CASHOUT_METHOD) raise NotImplementedError("Need to implement setup_cashoutmethod_db") - return None - # === THL: Marketplaces === @pytest.fixture(scope="session") -def spectrum_manager(spectrum_rw: SqlHelper) -> "SpectrumSurveyManager": +def spectrum_manager(spectrum_rw: SqlHelper) -> SpectrumSurveyManager: from generalresearch.managers.spectrum.survey import ( SpectrumSurveyManager, ) @@ -640,7 +201,7 @@ def spectrum_manager(spectrum_rw: SqlHelper) -> "SpectrumSurveyManager": @pytest.fixture(scope="session") def business_manager( gr_db: PostgresConfig, gr_redis_config: RedisConfig -) -> "BusinessManager": +) -> BusinessManager: from generalresearch.redis_helper import RedisConfig assert gr_db.dsn.path @@ -656,7 +217,7 @@ def business_manager( @pytest.fixture(scope="session") -def business_address_manager(gr_db: PostgresConfig) -> "BusinessAddressManager": +def business_address_manager(gr_db: PostgresConfig) -> BusinessAddressManager: assert gr_db.dsn.path assert "/unittest-" in gr_db.dsn.path @@ -668,7 +229,7 @@ def business_address_manager(gr_db: PostgresConfig) -> "BusinessAddressManager": @pytest.fixture(scope="session") def business_bank_account_manager( gr_db: PostgresConfig, -) -> "BusinessBankAccountManager": +) -> BusinessBankAccountManager: assert gr_db.dsn.path assert "/unittest-" in gr_db.dsn.path @@ -680,7 +241,7 @@ def business_bank_account_manager( @pytest.fixture(scope="session") -def team_manager(gr_db: PostgresConfig, gr_redis_config: RedisConfig) -> "TeamManager": +def team_manager(gr_db: PostgresConfig, gr_redis_config: RedisConfig) -> TeamManager: assert gr_db.dsn.path assert "/unittest-" in gr_db.dsn.path @@ -690,27 +251,7 @@ def team_manager(gr_db: PostgresConfig, gr_redis_config: RedisConfig) -> "TeamMa @pytest.fixture(scope="session") -def gr_um(gr_db: PostgresConfig, gr_redis_config: RedisConfig) -> "GRUserManager": - assert gr_db.dsn.path - assert "/unittest-" in gr_db.dsn.path - - from generalresearch.managers.gr.authentication import GRUserManager - - return GRUserManager(pg_config=gr_db, redis_config=gr_redis_config) - - -@pytest.fixture(scope="session") -def gr_tm(gr_db: PostgresConfig) -> "GRTokenManager": - assert gr_db.dsn.path - assert "/unittest-" in gr_db.dsn.path - - from generalresearch.managers.gr.authentication import GRTokenManager - - return GRTokenManager(pg_config=gr_db) - - -@pytest.fixture(scope="session") -def membership_manager(gr_db: PostgresConfig) -> "MembershipManager": +def membership_manager(gr_db: PostgresConfig) -> MembershipManager: assert gr_db.dsn.path assert "/unittest-" in gr_db.dsn.path @@ -719,47 +260,8 @@ def membership_manager(gr_db: PostgresConfig) -> "MembershipManager": return MembershipManager(pg_config=gr_db) -# === GRL IQ === - - -@pytest.fixture(scope="session") -def grliq_dm(grliq_db: PostgresConfig) -> "GrlIqDataManager": - assert grliq_db.dsn.path - assert "/unittest-" in grliq_db.dsn.path - - from generalresearch.grliq.managers.forensic_data import ( - GrlIqDataManager, - ) - - return GrlIqDataManager(postgres_config=grliq_db) - - -@pytest.fixture(scope="session") -def grliq_em(grliq_db: PostgresConfig) -> "GrlIqEventManager": - assert grliq_db.dsn.path - assert "/unittest-" in grliq_db.dsn.path - - from generalresearch.grliq.managers.forensic_events import ( - GrlIqEventManager, - ) - - return GrlIqEventManager(postgres_config=grliq_db) - - -@pytest.fixture(scope="session") -def grliq_crr(grliq_db: PostgresConfig) -> "GrlIqCategoryResultsReader": - assert grliq_db.dsn.path - assert "/unittest-" in grliq_db.dsn.path - - from generalresearch.grliq.managers.forensic_results import ( - GrlIqCategoryResultsReader, - ) - - return GrlIqCategoryResultsReader(postgres_config=grliq_db) - - @pytest.fixture(scope="session") -def delete_buyers_surveys(thl_web_rw: PostgresConfig, buyer_manager: "BuyerManager"): +def delete_buyers_surveys(thl_web_rw: PostgresConfig, buyer_manager: BuyerManager): # assert "/unittest-" in thl_web_rw.dsn.path thl_web_rw.execute_write( """ diff --git a/test_utils/managers/contest/conftest.py b/test_utils/managers/contest/conftest.py index fb0b44b..67935e7 100644 --- a/test_utils/managers/contest/conftest.py +++ b/test_utils/managers/contest/conftest.py @@ -1,286 +1,24 @@ -from datetime import datetime, timezone -from decimal import Decimal -from typing import TYPE_CHECKING, Callable -from uuid import uuid4 - import pytest -from generalresearch.currency import USDCent - -if TYPE_CHECKING: - from generalresearch.managers.thl.contest_manager import ContestManager - from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager - from generalresearch.models.thl.contest.contest import Contest - from generalresearch.models.thl.contest.leaderboard import ( - LeaderboardContestCreate, - ) - from generalresearch.models.thl.contest.milestone import ( - MilestoneContestCreate, - ) - from generalresearch.models.thl.contest.raffle import ( - RaffleContestCreate, - ) - from generalresearch.models.thl.product import Product - from generalresearch.models.thl.user import User - - -@pytest.fixture -def raffle_contest_create() -> "RaffleContestCreate": - from generalresearch.models.thl.contest import ( - ContestEndCondition, - ContestPrize, - ) - from generalresearch.models.thl.contest.definitions import ( - ContestPrizeKind, - ContestType, - ) - from generalresearch.models.thl.contest.raffle import ( - ContestEntryType, - RaffleContestCreate, - ) - - # This is what we'll get from the fastapi endpoint - return RaffleContestCreate( - name="test", - contest_type=ContestType.RAFFLE, - entry_type=ContestEntryType.CASH, - prizes=[ - ContestPrize( - name="iPod 64GB White", - kind=ContestPrizeKind.PHYSICAL, - estimated_cash_value=USDCent(100), - ) - ], - end_condition=ContestEndCondition(target_entry_amount=USDCent(100)), - ) - - -@pytest.fixture -def raffle_contest_in_db( - product_user_wallet_yes: "Product", - raffle_contest_create: "RaffleContestCreate", - contest_manager: "ContestManager", -) -> "Contest": - return contest_manager.create( - product_id=product_user_wallet_yes.uuid, contest_create=raffle_contest_create - ) - - -@pytest.fixture -def raffle_contest( - product_user_wallet_yes: "Product", raffle_contest_create: "RaffleContestCreate" -) -> "Contest": - from generalresearch.models.thl.contest.io import contest_create_to_contest - - return contest_create_to_contest( - product_id=product_user_wallet_yes.uuid, contest_create=raffle_contest_create - ) - - -@pytest.fixture(scope="function") -def raffle_contest_factory( - product_user_wallet_yes: "Product", - raffle_contest_create: "RaffleContestCreate", - contest_manager: "ContestManager", -) -> Callable[..., "Contest"]: - - def _inner(**kwargs): - raffle_contest_create.update(**kwargs) - return contest_manager.create( - product_id=product_user_wallet_yes.uuid, - contest_create=raffle_contest_create, - ) +from generalresearch.managers.base import Permission +from generalresearch.managers.thl.contest_manager import ContestManager +from generalresearch.pg_helper import PostgresConfig - return _inner - - -@pytest.fixture -def milestone_contest_create() -> "MilestoneContestCreate": - from generalresearch.models.thl.contest import ( - ContestPrize, - ) - from generalresearch.models.thl.contest.definitions import ( - ContestPrizeKind, - ContestType, - ) - from generalresearch.models.thl.contest.milestone import ( - ContestEntryTrigger, - MilestoneContestCreate, - MilestoneContestEndCondition, - ) - - # This is what we'll get from the fastapi endpoint - return MilestoneContestCreate( - name="Win a 50% bonus for 7 days and a $1 bonus after your first 3 completes!", - description="only valid for the first 5 users", - contest_type=ContestType.MILESTONE, - prizes=[ - ContestPrize( - name="50% for 7 days", - kind=ContestPrizeKind.PROMOTION, - estimated_cash_value=USDCent(0), - ), - ContestPrize( - name="$1 Bonus", - kind=ContestPrizeKind.CASH, - cash_amount=USDCent(1_00), - estimated_cash_value=USDCent(1_00), - ), - ], - end_condition=MilestoneContestEndCondition( - ends_at=datetime(year=2030, month=1, day=1, tzinfo=timezone.utc), - max_winners=5, - ), - entry_trigger=ContestEntryTrigger.TASK_COMPLETE, - target_amount=3, - ) +@pytest.fixture(scope="session") +def contest_manager(thl_web_rw: PostgresConfig) -> ContestManager: + assert thl_web_rw.dsn + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path -@pytest.fixture -def milestone_contest_in_db( - product_user_wallet_yes: "Product", - milestone_contest_create: "MilestoneContestCreate", - contest_manager: "ContestManager", -) -> "Contest": - return contest_manager.create( - product_id=product_user_wallet_yes.uuid, contest_create=milestone_contest_create - ) - - -@pytest.fixture -def milestone_contest( - product_user_wallet_yes: "Product", - milestone_contest_create: "MilestoneContestCreate", -) -> "Contest": - from generalresearch.models.thl.contest.io import contest_create_to_contest - - return contest_create_to_contest( - product_id=product_user_wallet_yes.uuid, contest_create=milestone_contest_create - ) - - -@pytest.fixture(scope="function") -def milestone_contest_factory( - product_user_wallet_yes: "Product", - milestone_contest_create: "MilestoneContestCreate", - contest_manager: "ContestManager", -) -> Callable[..., "Contest"]: - - def _inner(**kwargs): - milestone_contest_create.update(**kwargs) - return contest_manager.create( - product_id=product_user_wallet_yes.uuid, - contest_create=milestone_contest_create, - ) - - return _inner - - -@pytest.fixture -def leaderboard_contest_create( - product_user_wallet_yes: "Product", -) -> "LeaderboardContestCreate": - from generalresearch.models.thl.contest import ( - ContestPrize, - ) - from generalresearch.models.thl.contest.definitions import ( - ContestPrizeKind, - ContestType, - ) - from generalresearch.models.thl.contest.leaderboard import ( - LeaderboardContestCreate, - ) + from generalresearch.managers.thl.contest_manager import ContestManager - # This is what we'll get from the fastapi endpoint - return LeaderboardContestCreate( - name="test", - contest_type=ContestType.LEADERBOARD, - prizes=[ - ContestPrize( - name="$15 Cash", - estimated_cash_value=USDCent(15_00), - cash_amount=USDCent(15_00), - kind=ContestPrizeKind.CASH, - leaderboard_rank=1, - ), - ContestPrize( - name="$10 Cash", - estimated_cash_value=USDCent(10_00), - cash_amount=USDCent(10_00), - kind=ContestPrizeKind.CASH, - leaderboard_rank=2, - ), + return ContestManager( + pg_config=thl_web_rw, + permissions=[ + Permission.CREATE, + Permission.READ, + Permission.UPDATE, + Permission.DELETE, ], - leaderboard_key=f"leaderboard:{product_user_wallet_yes.uuid}:us:daily:2025-01-01:complete_count", ) - - -@pytest.fixture -def leaderboard_contest_in_db( - product_user_wallet_yes: "Product", - leaderboard_contest_create: "LeaderboardContestCreate", - contest_manager: "ContestManager", -) -> "Contest": - return contest_manager.create( - product_id=product_user_wallet_yes.uuid, - contest_create=leaderboard_contest_create, - ) - - -@pytest.fixture -def leaderboard_contest( - product_user_wallet_yes: "Product", - leaderboard_contest_create: "LeaderboardContestCreate", -): - from generalresearch.models.thl.contest.io import contest_create_to_contest - - return contest_create_to_contest( - product_id=product_user_wallet_yes.uuid, - contest_create=leaderboard_contest_create, - ) - - -@pytest.fixture(scope="function") -def leaderboard_contest_factory( - product_user_wallet_yes: "Product", - leaderboard_contest_create: "LeaderboardContestCreate", - contest_manager: "ContestManager", -) -> Callable[..., "Contest"]: - - def _inner(**kwargs): - leaderboard_contest_create.update(**kwargs) - return contest_manager.create( - product_id=product_user_wallet_yes.uuid, - contest_create=leaderboard_contest_create, - ) - - return _inner - - -@pytest.fixture -def user_with_money( - request, - user_factory: Callable[..., "User"], - product_user_wallet_yes: "Product", - thl_lm: "ThlLedgerManager", -) -> "User": - from generalresearch.models.thl.user import User - - params = getattr(request, "param", dict()) or {} - min_balance = int(params.get("min_balance", USDCent(1_00))) - - user: User = user_factory(product=product_user_wallet_yes) - wallet = thl_lm.get_account_or_create_user_wallet(user) - balance = thl_lm.get_account_balance(wallet) - todo = min_balance - balance - if todo > 0: - # # Put money in user's wallet - thl_lm.create_tx_user_bonus( - user=user, - ref_uuid=uuid4().hex, - description="bonus", - amount=Decimal(todo) / 100, - ) - print(f"wallet balance: {thl_lm.get_user_wallet_balance(user=user)}") - - return user diff --git a/test_utils/managers/gr/__init__.py b/test_utils/managers/gr/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test_utils/managers/gr/__init__.py diff --git a/test_utils/managers/gr/conftest.py b/test_utils/managers/gr/conftest.py new file mode 100644 index 0000000..37da164 --- /dev/null +++ b/test_utils/managers/gr/conftest.py @@ -0,0 +1,110 @@ +from __future__ import annotations + +from typing import Callable + +import pytest +import redis.asyncio as redis_async +from pydantic import PostgresDsn +from redis import Redis + +from generalresearch.config import GRLBaseSettings +from generalresearch.managers.gr.authentication import GRTokenManager, GRUserManager +from generalresearch.managers.gr.business import ( + BusinessAddressManager, + BusinessBankAccountManager, + BusinessManager, +) +from generalresearch.pg_helper import PostgresConfig +from generalresearch.redis_helper import RedisConfig + + +# === Msc === +@pytest.fixture(scope="session") +def gr_redis(settings: GRLBaseSettings) -> Redis: + assert "unittest" in str(settings.gr_redis) or "127.0.0.1" in str(settings.gr_redis) + return Redis.from_url( + url=str(settings.gr_redis), + decode_responses=True, + socket_timeout=settings.redis_timeout, + socket_connect_timeout=settings.redis_timeout, + ) + + +@pytest.fixture +def gr_redis_async(settings: GRLBaseSettings) -> redis_async.Redis: + assert "unittest" in str(settings.gr_redis) or "127.0.0.1" in str(settings.gr_redis) + + return redis_async.Redis.from_url( + str(settings.gr_redis), + decode_responses=True, + socket_timeout=0.20, + socket_connect_timeout=0.20, + ) + + +@pytest.fixture(scope="session") +def gr_redis_config(settings: GRLBaseSettings) -> RedisConfig: + assert "unittest" in str(settings.gr_redis) or "127.0.0.1" in str(settings.gr_redis) + + return RedisConfig( + dsn=settings.gr_redis, + decode_responses=True, + socket_timeout=settings.redis_timeout, + socket_connect_timeout=settings.redis_timeout, + ) + + +@pytest.fixture(scope="session") +def gr_db(django_db_factory: Callable[..., PostgresDsn]) -> PostgresConfig: + + return PostgresConfig( + dsn=django_db_factory("gr_carer"), + connect_timeout=1, + statement_timeout=5, + ) + + +# === Managers === + + +@pytest.fixture(scope="session") +def gr_user_manager( + gr_db: PostgresConfig, gr_redis_config: RedisConfig +) -> GRUserManager: + assert gr_db.dsn.path + assert "/unittest-" in gr_db.dsn.path + + from generalresearch.managers.gr.authentication import GRUserManager + + return GRUserManager(pg_config=gr_db, redis_config=gr_redis_config) + + +@pytest.fixture(scope="session") +def gr_team_manager(gr_db: PostgresConfig) -> GRTokenManager: + assert gr_db.dsn.path + assert "/unittest-" in gr_db.dsn.path + + from generalresearch.managers.gr.authentication import GRTokenManager + + return GRTokenManager(pg_config=gr_db) + + +@pytest.fixture(scope="session") +def gr_business_manager( + gr_db: PostgresConfig, gr_redis_config: RedisConfig +) -> BusinessManager: + return BusinessManager(pg_config=gr_db, redis_config=gr_redis_config) + + +@pytest.fixture(scope="session") +def gr_business_bank_account_manager( + gr_db: PostgresConfig, +) -> BusinessBankAccountManager: + return BusinessBankAccountManager(pg_config=gr_db) + + +@pytest.fixture(scope="session") +def gr_business_address_manager( + gr_db: PostgresConfig, +) -> BusinessAddressManager: + return BusinessAddressManager(pg_config=gr_db) diff --git a/test_utils/managers/ledger/conftest.py b/test_utils/managers/ledger/conftest.py index 105085d..ce8348e 100644 --- a/test_utils/managers/ledger/conftest.py +++ b/test_utils/managers/ledger/conftest.py @@ -1,739 +1,94 @@ -from datetime import datetime -from decimal import Decimal -from random import randint -from typing import TYPE_CHECKING, Callable, Dict, Optional -from uuid import uuid4 +from __future__ import annotations import pytest -from generalresearch.currency import USDCent -from generalresearch.managers.base import PostgresManager -from test_utils.models.conftest import ( - payout_config, - product_amt_true, - product_user_wallet_no, - product_user_wallet_yes, - session, - session_factory, - user_factory, - wall, - wall_factory, +from generalresearch.managers.base import Permission +from generalresearch.managers.thl.ledger_manager.ledger import ( + LedgerAccountManager, + LedgerManager, + LedgerTransactionManager, ) - -_ = ( - user_factory, - product_user_wallet_no, - wall, - product_amt_true, - product_user_wallet_yes, - session_factory, - session, - wall_factory, - payout_config, +from generalresearch.managers.thl.ledger_manager.thl_ledger import ( + ThlLedgerManager, ) +from generalresearch.pg_helper import PostgresConfig +from generalresearch.redis_helper import RedisConfig -if TYPE_CHECKING: - - from generalresearch.currency import LedgerCurrency - from generalresearch.managers.thl.ledger_manager.ledger import LedgerManager - from generalresearch.managers.thl.ledger_manager.thl_ledger import ( - ThlLedgerManager, - ) - from generalresearch.managers.thl.payout import ( - BrokerageProductPayoutEventManager, - BusinessPayoutEventManager, - ) - from generalresearch.managers.thl.session import SessionManager - from generalresearch.managers.thl.wall import WallManager - from generalresearch.models.thl.ledger import ( - LedgerAccount, - LedgerTransaction, - ) - from generalresearch.models.thl.payout import ( - BrokerageProductPayoutEvent, - ) - from generalresearch.models.thl.product import Product - from generalresearch.models.thl.session import Session - from generalresearch.models.thl.user import User - - -@pytest.fixture -def ledger_account( - request, lm: "LedgerManager", currency: "LedgerCurrency" -) -> "LedgerAccount": - from generalresearch.models.thl.ledger import ( - AccountType, - Direction, - LedgerAccount, - ) +# --- Ledger --- - account_type = getattr(request, "account_type", AccountType.CASH) - direction = getattr(request, "direction", Direction.CREDIT) - acct_uuid = uuid4().hex - qn = ":".join([currency, account_type, acct_uuid]) +@pytest.fixture(scope="session") +def ledger_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> LedgerManager: - acct_model = LedgerAccount( - uuid=acct_uuid, - display_name=f"test-{acct_uuid}", - currency=currency, - qualified_name=qn, - account_type=account_type, - normal_balance=direction, + return LedgerManager( + pg_config=thl_web_rw, + permissions=[ + Permission.CREATE, + Permission.READ, + Permission.UPDATE, + Permission.DELETE, + ], + testing=True, + redis_config=thl_redis_config, ) - return lm.create_account(account=acct_model) - - -@pytest.fixture -def ledger_account_factory( - request, thl_lm: "ThlLedgerManager", lm: "LedgerManager", currency: "LedgerCurrency" -) -> Callable[..., "LedgerAccount"]: - - from generalresearch.models.thl.ledger import ( - AccountType, - Direction, - LedgerAccount, - ) - - def _inner( - product: "Product", - account_type: AccountType = AccountType.CASH, - direction: Direction = Direction.CREDIT, - ) -> "LedgerAccount": - thl_lm.get_account_or_create_bp_wallet(product=product) - acct_uuid = uuid4().hex - qn = ":".join([currency, account_type, acct_uuid]) - - acct_model = LedgerAccount( - uuid=acct_uuid, - display_name=f"test-{acct_uuid}", - currency=currency, - qualified_name=qn, - account_type=account_type, - normal_balance=direction, - ) - return lm.create_account(account=acct_model) - - return _inner - -@pytest.fixture -def ledger_account_credit( - request, lm: "LedgerManager", currency: "LedgerCurrency" -) -> "LedgerAccount": - from generalresearch.models.thl.ledger import AccountType, Direction - account_type = AccountType.REVENUE - acct_uuid = uuid4().hex +@pytest.fixture(scope="session") +def ledger_tx_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> LedgerTransactionManager: + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path - qn = ":".join([currency, account_type, acct_uuid]) - from generalresearch.models.thl.ledger import LedgerAccount - - acct_model = LedgerAccount( - uuid=acct_uuid, - display_name=f"test-{acct_uuid}", - currency=currency, - qualified_name=qn, - account_type=account_type, - normal_balance=Direction.CREDIT, + from generalresearch.managers.thl.ledger_manager.ledger import ( + LedgerTransactionManager, ) - return lm.create_account(account=acct_model) - - -@pytest.fixture -def ledger_account_debit( - request, lm: "LedgerManager", currency: "LedgerCurrency" -) -> "LedgerAccount": - from generalresearch.models.thl.ledger import AccountType, Direction - - account_type = AccountType.EXPENSE - acct_uuid = uuid4().hex - - qn = ":".join([currency, account_type, acct_uuid]) - from generalresearch.models.thl.ledger import LedgerAccount - acct_model = LedgerAccount( - uuid=acct_uuid, - display_name=f"test-{acct_uuid}", - currency=currency, - qualified_name=qn, - account_type=account_type, - normal_balance=Direction.DEBIT, + return LedgerTransactionManager( + pg_config=thl_web_rw, + permissions=[Permission.CREATE, Permission.READ], + testing=True, + redis_config=thl_redis_config, ) - return lm.create_account(account=acct_model) -@pytest.fixture -def tag(request, lm: "LedgerManager") -> str: - from generalresearch.currency import LedgerCurrency +@pytest.fixture(scope="session") +def ledger_account_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> LedgerAccountManager: + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path - return ( - request.param - if hasattr(request, "tag") - else f"{LedgerCurrency.TEST}:{uuid4().hex}" + from generalresearch.managers.thl.ledger_manager.ledger import ( + LedgerAccountManager, ) - -@pytest.fixture -def usd_cent(request) -> USDCent: - amount = randint(99, 9_999) - return request.param if hasattr(request, "usd_cent") else USDCent(amount) - - -@pytest.fixture -def bp_payout_event( - product: "Product", - usd_cent: "USDCent", - business_payout_event_manager: "BusinessPayoutEventManager", - thl_lm: "ThlLedgerManager", -) -> "BrokerageProductPayoutEvent": - - return business_payout_event_manager.create_bp_payout_event( - thl_ledger_manager=thl_lm, - product=product, - amount=usd_cent, - ext_ref_id=uuid4().hex + return LedgerAccountManager( + pg_config=thl_web_rw, + permissions=[Permission.CREATE, Permission.READ], + testing=True, + redis_config=thl_redis_config, ) -@pytest.fixture -def bp_payout_event_factory( - brokerage_product_payout_event_manager: "BrokerageProductPayoutEventManager", - thl_lm: "ThlLedgerManager", -) -> Callable[..., "BrokerageProductPayoutEvent"]: +# --- THL Ledger --- - from generalresearch.currency import USDCent - from generalresearch.models.thl.product import Product - - def _inner( - product: Product, usd_cent: USDCent, ext_ref_id: Optional[str] = None - ) -> "BrokerageProductPayoutEvent": - - return brokerage_product_payout_event_manager.create_bp_payout_event( - thl_ledger_manager=thl_lm, - product=product, - amount=usd_cent, - ext_ref_id=ext_ref_id, - skip_wallet_balance_check=True, - skip_one_per_day_check=True, - ) - - return _inner - - -@pytest.fixture -def currency(lm: "LedgerManager") -> "LedgerCurrency": - # return request.param if hasattr(request, "currency") else LedgerCurrency.TEST - assert lm.currency, "LedgerManager must have a currency specified for these tests" - return lm.currency - - -@pytest.fixture -def tx_metadata(request) -> Optional[Dict[str, str]]: - return ( - request.param - if hasattr(request, "tx_metadata") - else {f"key-{uuid4().hex[:10]}": uuid4().hex} - ) - - -@pytest.fixture -def ledger_tx( - request, - ledger_account_credit: "LedgerAccount", - ledger_account_debit: "LedgerAccount", - tag: str, - currency: "LedgerCurrency", - tx_metadata: Optional[Dict[str, str]], - lm: "LedgerManager", -) -> "LedgerTransaction": - from generalresearch.models.thl.ledger import Direction, LedgerEntry - - amount = int(Decimal("1.00") * 100) - - entries = [ - LedgerEntry( - direction=Direction.CREDIT, - account_uuid=ledger_account_credit.uuid, - amount=amount, - ), - LedgerEntry( - direction=Direction.DEBIT, - account_uuid=ledger_account_debit.uuid, - amount=amount, - ), - ] - - return lm.create_tx(entries=entries, tag=tag, metadata=tx_metadata) - - -@pytest.fixture -def create_main_accounts( - lm: "LedgerManager", currency: "LedgerCurrency" -) -> Callable[..., None]: - - def _inner() -> None: - from generalresearch.models.thl.ledger import ( - AccountType, - Direction, - LedgerAccount, - ) - - account = LedgerAccount( - display_name="Cash flow task complete", - qualified_name=f"{currency.value}:revenue:task_complete", - normal_balance=Direction.CREDIT, - account_type=AccountType.REVENUE, - currency=lm.currency, - ) - lm.get_account_or_create(account=account) - - account = LedgerAccount( - display_name="Operating Cash Account", - qualified_name=f"{currency.value}:cash", - normal_balance=Direction.DEBIT, - account_type=AccountType.CASH, - currency=currency, - ) - - lm.get_account_or_create(account=account) - - return None - - return _inner - - -@pytest.fixture -def delete_ledger_db(thl_web_rw: "PostgresManager") -> Callable[..., None]: - - def _inner(): - for table in [ - "ledger_transactionmetadata", - "ledger_entry", - "ledger_transaction", - "ledger_account", - ]: - thl_web_rw.execute_write( - query=f"DELETE FROM {table};", - ) - - return _inner - - -@pytest.fixture -def wipe_main_accounts( - thl_web_rw: "PostgresManager", lm: "LedgerManager", currency: "LedgerCurrency" -) -> Callable[..., None]: - - def _inner() -> None: - db_table = thl_web_rw.db_name - qual_names = [ - f"{currency.value}:revenue:task_complete", - f"{currency.value}:cash", - ] - - res = thl_web_rw.execute_sql_query( - query=f""" - SELECT lt.id as ltid, le.id as leid, tmd.id as tmdid, la.uuid as lauuid - FROM `{db_table}`.`ledger_transaction` AS lt - LEFT JOIN `{db_table}`.ledger_entry le - ON lt.id = le.transaction_id - LEFT JOIN `{db_table}`.ledger_account la - ON la.uuid = le.account_id - LEFT JOIN `{db_table}`.ledger_transactionmetadata tmd - ON lt.id = tmd.transaction_id - WHERE la.qualified_name IN %s - """, - params=[qual_names], - ) - - lt = {x["ltid"] for x in res if x["ltid"]} - le = {x["leid"] for x in res if x["leid"]} - tmd = {x["tmdid"] for x in res if x["tmdid"]} - la = {x["lauuid"] for x in res if x["lauuid"]} - - thl_web_rw.execute_sql_query( - query=f""" - DELETE FROM `{db_table}`.`ledger_transactionmetadata` - WHERE id IN %s - """, - params=[tmd], - commit=True, - ) - - thl_web_rw.execute_sql_query( - query=f""" - DELETE FROM `{db_table}`.`ledger_entry` - WHERE id IN %s - """, - params=[le], - commit=True, - ) - - thl_web_rw.execute_sql_query( - query=f""" - DELETE FROM `{db_table}`.`ledger_transaction` - WHERE id IN %s - """, - params=[lt], - commit=True, - ) - - thl_web_rw.execute_sql_query( - query=f""" - DELETE FROM `{db_table}`.`ledger_account` - WHERE uuid IN %s - """, - params=[la], - commit=True, - ) - - return None - - return _inner - - -@pytest.fixture -def account_cash(lm: "LedgerManager", currency: "LedgerCurrency") -> "LedgerAccount": - from generalresearch.models.thl.ledger import ( - AccountType, - Direction, - LedgerAccount, - ) - - account = LedgerAccount( - display_name="Operating Cash Account", - qualified_name=f"{currency.value}:cash", - normal_balance=Direction.DEBIT, - account_type=AccountType.CASH, - currency=currency, - ) - return lm.get_account_or_create(account=account) +@pytest.fixture(scope="session") +def thl_ledger_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> ThlLedgerManager: -@pytest.fixture -def account_revenue_task_complete( - lm: "LedgerManager", currency: "LedgerCurrency" -) -> "LedgerAccount": - from generalresearch.models.thl.ledger import ( - AccountType, - Direction, - LedgerAccount, + return ThlLedgerManager( + pg_config=thl_web_rw, + permissions=[ + Permission.CREATE, + Permission.READ, + Permission.UPDATE, + Permission.DELETE, + ], + testing=True, + redis_config=thl_redis_config, ) - - account = LedgerAccount( - display_name="Cash flow task complete", - qualified_name=f"{currency.value}:revenue:task_complete", - normal_balance=Direction.CREDIT, - account_type=AccountType.REVENUE, - currency=currency, - ) - return lm.get_account_or_create(account=account) - - -@pytest.fixture -def account_expense_tango( - lm: "LedgerManager", currency: "LedgerCurrency" -) -> "LedgerAccount": - from generalresearch.models.thl.ledger import ( - AccountType, - Direction, - LedgerAccount, - ) - - account = LedgerAccount( - display_name="Tango Fee", - qualified_name=f"{currency.value}:expense:tango_fee", - normal_balance=Direction.DEBIT, - account_type=AccountType.EXPENSE, - currency=currency, - ) - return lm.get_account_or_create(account=account) - - -@pytest.fixture -def user_account_user_wallet( - lm: "LedgerManager", user: "User", currency: "LedgerCurrency" -) -> "LedgerAccount": - from generalresearch.models.thl.ledger import ( - AccountType, - Direction, - LedgerAccount, - ) - - account = LedgerAccount( - display_name=f"{user.uuid} Wallet", - qualified_name=f"{currency.value}:user_wallet:{user.uuid}", - normal_balance=Direction.CREDIT, - account_type=AccountType.USER_WALLET, - reference_type="user", - reference_uuid=user.uuid, - currency=currency, - ) - return lm.get_account_or_create(account=account) - - -@pytest.fixture -def product_account_bp_wallet( - lm: "LedgerManager", product: "Product", currency: "LedgerCurrency" -) -> "LedgerAccount": - from generalresearch.models.thl.ledger import ( - AccountType, - Direction, - LedgerAccount, - ) - - account = LedgerAccount.model_validate( - dict( - display_name=f"{product.name} Wallet", - qualified_name=f"{currency.value}:bp_wallet:{product.uuid}", - normal_balance=Direction.CREDIT, - account_type=AccountType.BP_WALLET, - reference_type="bp", - reference_uuid=product.uuid, - currency=currency, - ) - ) - return lm.get_account_or_create(account=account) - - -@pytest.fixture -def setup_accounts( - product_factory: Callable[..., "Product"], - lm: "LedgerManager", - user: "User", - currency: "LedgerCurrency", -) -> None: - from generalresearch.models.thl.ledger import ( - AccountType, - Direction, - LedgerAccount, - ) - - # BP's wallet and a revenue from their commissions account. - p1 = product_factory() - - account = LedgerAccount( - display_name=f"Revenue from {p1.name} commission", - qualified_name=f"{currency.value}:revenue:bp_commission:{p1.uuid}", - normal_balance=Direction.CREDIT, - account_type=AccountType.REVENUE, - reference_type="bp", - reference_uuid=p1.uuid, - currency=currency, - ) - lm.get_account_or_create(account=account) - - account = LedgerAccount.model_validate( - dict( - display_name=f"{p1.name} Wallet", - qualified_name=f"{currency.value}:bp_wallet:{p1.uuid}", - normal_balance=Direction.CREDIT, - account_type=AccountType.BP_WALLET, - reference_type="bp", - reference_uuid=p1.uuid, - currency=currency, - ) - ) - lm.get_account_or_create(account=account) - - # BP's wallet, user's wallet, and a revenue from their commissions account. - p2 = product_factory() - account = LedgerAccount( - display_name=f"Revenue from {p2.name} commission", - qualified_name=f"{currency.value}:revenue:bp_commission:{p2.uuid}", - normal_balance=Direction.CREDIT, - account_type=AccountType.REVENUE, - reference_type="bp", - reference_uuid=p2.uuid, - currency=currency, - ) - lm.get_account_or_create(account) - - account = LedgerAccount( - display_name=f"{p2.name} Wallet", - qualified_name=f"{currency.value}:bp_wallet:{p2.uuid}", - normal_balance=Direction.CREDIT, - account_type=AccountType.BP_WALLET, - reference_type="bp", - reference_uuid=p2.uuid, - currency=currency, - ) - lm.get_account_or_create(account) - - account = LedgerAccount( - display_name=f"{user.uuid} Wallet", - qualified_name=f"{currency.value}:user_wallet:{user.uuid}", - normal_balance=Direction.CREDIT, - account_type=AccountType.USER_WALLET, - reference_type="user", - reference_uuid=user.uuid, - currency="test", - ) - lm.get_account_or_create(account=account) - - -@pytest.fixture -def session_with_tx_factory( - user_factory: Callable[..., "User"], - product: "Product", - session_factory: Callable[..., "Session"], - session_manager: "SessionManager", - wall_manager: "WallManager", - utc_hour_ago: datetime, - thl_lm: "ThlLedgerManager", -) -> Callable[..., "Session"]: - - from generalresearch.models.thl.session import ( - Status, - StatusCode1, - ) - from generalresearch.models.thl.user import User - - def _inner( - user: User, - final_status: Status = Status.COMPLETE, - wall_req_cpi: Decimal = Decimal(".50"), - started: datetime = utc_hour_ago, - ) -> "Session": - s: "Session" = session_factory( - user=user, - wall_count=2, - final_status=final_status, - wall_req_cpi=wall_req_cpi, - started=started, - ) - last_wall = s.wall_events[-1] - - wall_manager.finish( - wall=last_wall, - status=Status.COMPLETE, - status_code_1=StatusCode1.COMPLETE, - finished=last_wall.finished, - ) - - status, status_code_1 = s.determine_session_status() - _, _, bp_pay, user_pay = s.determine_payments() - session_manager.finish_with_status( - session=s, - finished=last_wall.finished, - payout=bp_pay, - user_payout=user_pay, - status=status, - status_code_1=status_code_1, - ) - - thl_lm.create_tx_task_complete( - wall=last_wall, - user=user, - created=last_wall.finished, - force=True, - ) - - thl_lm.create_tx_bp_payment(session=s, created=last_wall.finished, force=True) - - return s - - return _inner - - -@pytest.fixture -def adj_to_fail_with_tx_factory( - session_manager: "SessionManager", - wall_manager: "WallManager", - thl_lm: "ThlLedgerManager", -) -> Callable[..., None]: - from datetime import datetime, timedelta - - from generalresearch.models.thl.definitions import WallAdjustedStatus - from generalresearch.models.thl.session import ( - Session, - ) - - def _inner( - session: Session, - created: datetime, - ) -> None: - w1 = wall_manager.get_wall_events(session_id=session.id)[-1] - - # This is defined in `thl-grpc/thl/user_quality_history/recons.py:150` - # so we can't use it as part of this test anyway to add rows to the - # thl_taskadjustment table anyway.. until we created a - # TaskAdjustment Manager to put into generalresearch! - - # create_task_adjustment_event( - # wall, - # user, - # adjusted_status, - # amount_usd=amount_usd, - # alert_time=alert_time, - # ext_status_code=ext_status_code, - # ) - - wall_manager.adjust_status( - wall=w1, - adjusted_status=WallAdjustedStatus.ADJUSTED_TO_FAIL, - adjusted_cpi=Decimal("0.00"), - adjusted_timestamp=created, - ) - - thl_lm.create_tx_task_adjustment( - wall=w1, - user=session.user, - created=created + timedelta(milliseconds=1), - ) - - session.wall_events = wall_manager.get_wall_events(session_id=session.id) - session_manager.adjust_status(session=session) - - thl_lm.create_tx_bp_adjustment( - session=session, created=created + timedelta(milliseconds=2) - ) - - return None - - return _inner - - -@pytest.fixture -def adj_to_complete_with_tx_factory( - session_manager: "SessionManager", - wall_manager: "WallManager", - thl_lm: "ThlLedgerManager", -) -> Callable[..., None]: - from datetime import timedelta - - from generalresearch.models.thl.definitions import WallAdjustedStatus - from generalresearch.models.thl.session import ( - Session, - ) - - def _inner( - session: Session, - created: datetime, - ) -> None: - w1 = wall_manager.get_wall_events(session_id=session.id)[-1] - - wall_manager.adjust_status( - wall=w1, - adjusted_status=WallAdjustedStatus.ADJUSTED_TO_COMPLETE, - adjusted_cpi=w1.req_cpi, - adjusted_timestamp=created, - ) - - thl_lm.create_tx_task_adjustment( - wall=w1, - user=session.user, - created=created + timedelta(milliseconds=1), - ) - - session.wall_events = wall_manager.get_wall_events(session_id=session.id) - session_manager.adjust_status(session=session) - - thl_lm.create_tx_bp_adjustment( - session=session, created=created + timedelta(milliseconds=2) - ) - - return None - - return _inner diff --git a/test_utils/managers/network/conftest.py b/test_utils/managers/network/conftest.py index 6c5ea23..e69de29 100644 --- a/test_utils/managers/network/conftest.py +++ b/test_utils/managers/network/conftest.py @@ -1,143 +0,0 @@ -import os -from datetime import datetime, timedelta, timezone -from uuid import uuid4 - -import pytest - -from generalresearch.managers.network.label import IPLabelManager -from generalresearch.managers.network.tool_run import ToolRunManager -from generalresearch.models.network.definitions import IPProtocol -from generalresearch.models.network.mtr.parser import parse_mtr_output -from generalresearch.models.network.nmap.parser import parse_nmap_xml -from generalresearch.models.network.rdns.parser import parse_rdns_output -from generalresearch.models.network.tool_run import MTRRun, NmapRun, RDNSRun, Status -from generalresearch.models.network.tool_run_command import ( - MTRRunCommand, - MTRRunCommandOptions, - NmapRunCommand, - NmapRunCommandOptions, - RDNSRunCommand, - RDNSRunCommandOptions, -) - - -@pytest.fixture(scope="session") -def scan_group_id(): - return uuid4().hex - - -@pytest.fixture(scope="session") -def iplabel_manager(thl_web_rw) -> IPLabelManager: - assert "/unittest-" in thl_web_rw.dsn.path - - return IPLabelManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def toolrun_manager(thl_web_rw) -> ToolRunManager: - assert "/unittest-" in thl_web_rw.dsn.path - - return ToolRunManager(pg_config=thl_web_rw) - - -@pytest.fixture(scope="session") -def nmap_raw_output(request) -> str: - fp = os.path.join(request.config.rootpath, "data/nmaprun1.xml") - with open(fp) as f: - data = f.read() - return data - - -@pytest.fixture(scope="session") -def nmap_result(nmap_raw_output): - return parse_nmap_xml(nmap_raw_output) - - -@pytest.fixture(scope="session") -def nmap_run(nmap_result, scan_group_id): - r = nmap_result - config = NmapRunCommand( - command="nmap", - options=NmapRunCommandOptions( - ip=r.target_ip, ports="22-1000,11000,1100,3389,61232", top_ports=None - ), - ) - return NmapRun( - tool_version=r.version, - status=Status.SUCCESS, - ip=r.target_ip, - started_at=r.started_at, - finished_at=r.finished_at, - raw_command=config.to_command_str(), - scan_group_id=scan_group_id, - config=config, - parsed=r, - ) - - -@pytest.fixture(scope="session") -def dig_raw_output(): - return "156.32.33.45.in-addr.arpa. 300 IN PTR scanme.nmap.org." - - -@pytest.fixture(scope="session") -def rdns_result(dig_raw_output): - return parse_rdns_output(ip="45.33.32.156", raw=dig_raw_output) - - -@pytest.fixture(scope="session") -def rdns_run(rdns_result, scan_group_id): - r = rdns_result - ip = "45.33.32.156" - utc_now = datetime.now(tz=timezone.utc) - config = RDNSRunCommand(command="dig", options=RDNSRunCommandOptions(ip=ip)) - return RDNSRun( - tool_version="1.2.3", - status=Status.SUCCESS, - ip=ip, - started_at=utc_now, - finished_at=utc_now + timedelta(seconds=1), - raw_command=config.to_command_str(), - scan_group_id=scan_group_id, - config=config, - parsed=r, - ) - - -@pytest.fixture(scope="session") -def mtr_raw_output(request): - fp = os.path.join(request.config.rootpath, "data/mtr_fatbeam.json") - with open(fp) as f: - data = f.read() - return data - - -@pytest.fixture(scope="session") -def mtr_result(mtr_raw_output): - return parse_mtr_output(mtr_raw_output, port=443, protocol=IPProtocol.TCP) - - -@pytest.fixture(scope="session") -def mtr_run(mtr_result, scan_group_id): - r = mtr_result - utc_now = datetime.now(tz=timezone.utc) - config = MTRRunCommand( - command="mtr", - options=MTRRunCommandOptions( - ip=r.destination, protocol=IPProtocol.TCP, port=443 - ), - ) - - return MTRRun( - tool_version="1.2.3", - status=Status.SUCCESS, - ip=r.destination, - started_at=utc_now, - finished_at=utc_now + timedelta(seconds=1), - raw_command=config.to_command_str(), - scan_group_id=scan_group_id, - config=config, - parsed=r, - facility_id=1, - source_ip="1.2.3.4", - ) diff --git a/test_utils/managers/thl/__init__.py b/test_utils/managers/thl/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test_utils/managers/thl/__init__.py diff --git a/test_utils/managers/thl/conftest.py b/test_utils/managers/thl/conftest.py new file mode 100644 index 0000000..5b70961 --- /dev/null +++ b/test_utils/managers/thl/conftest.py @@ -0,0 +1,258 @@ +from __future__ import annotations + +from typing import Callable + +import pytest +from pydantic import PostgresDsn + +from generalresearch.config import GRLBaseSettings +from generalresearch.managers.base import Permission +from generalresearch.managers.thl.buyer import BuyerManager +from generalresearch.managers.thl.category import CategoryManager +from generalresearch.managers.thl.payout import ( + BrokerageProductPayoutEventManager, + BusinessPayoutEventManager, + PayoutEventManager, + UserPayoutEventManager, +) +from generalresearch.managers.thl.product import ProductManager +from generalresearch.managers.thl.session import SessionManager +from generalresearch.managers.thl.task_adjustment import ( + TaskAdjustmentManager, +) +from generalresearch.managers.thl.user_manager.user_manager import ( + UserManager, +) +from generalresearch.managers.thl.user_manager.user_metadata_manager import ( + UserMetadataManager, +) +from generalresearch.managers.thl.wall import ( + WallCacheManager, + WallManager, +) +from generalresearch.pg_helper import PostgresConfig +from generalresearch.redis_helper import RedisConfig + + +@pytest.fixture(scope="session") +def thl_web_rr(django_db_factory: Callable[..., PostgresDsn]) -> PostgresConfig: + + return PostgresConfig( + dsn=django_db_factory("generalresearch.thl_django"), + connect_timeout=1, + statement_timeout=5, + ) + + +@pytest.fixture(scope="session") +def thl_web_rw(thl_web_rr: PostgresConfig) -> PostgresConfig: + return thl_web_rr + + +@pytest.fixture(scope="session") +def thl_redis_config(settings: GRLBaseSettings) -> RedisConfig: + return RedisConfig( + dsn=settings.thl_redis, + decode_responses=True, + socket_timeout=settings.redis_timeout, + socket_connect_timeout=settings.redis_timeout, + ) + + +@pytest.fixture(scope="session") +def payout_event_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> PayoutEventManager: + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path + + from generalresearch.managers.thl.payout import PayoutEventManager + + return PayoutEventManager( + pg_config=thl_web_rw, + permissions=[Permission.CREATE, Permission.READ], + redis_config=thl_redis_config, + ) + + +@pytest.fixture(scope="session") +def user_payout_event_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> UserPayoutEventManager: + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path + + from generalresearch.managers.thl.payout import UserPayoutEventManager + + return UserPayoutEventManager( + pg_config=thl_web_rw, + permissions=[Permission.CREATE, Permission.READ], + redis_config=thl_redis_config, + ) + + +@pytest.fixture(scope="session") +def brokerage_product_payout_event_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> BrokerageProductPayoutEventManager: + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path + + from generalresearch.managers.thl.payout import ( + BrokerageProductPayoutEventManager, + ) + + return BrokerageProductPayoutEventManager( + pg_config=thl_web_rw, + permissions=[Permission.CREATE, Permission.READ], + redis_config=thl_redis_config, + ) + + +@pytest.fixture(scope="session") +def business_payout_event_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> BusinessPayoutEventManager: + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path + + from generalresearch.managers.thl.payout import ( + BusinessPayoutEventManager, + ) + + return BusinessPayoutEventManager( + pg_config=thl_web_rw, + permissions=[Permission.CREATE, Permission.READ], + redis_config=thl_redis_config, + ) + + +@pytest.fixture(scope="session") +def product_manager(thl_web_rw: PostgresConfig) -> ProductManager: + assert thl_web_rw.dsn + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path + + from generalresearch.managers.thl.product import ProductManager + + return ProductManager(pg_config=thl_web_rw) + + +@pytest.fixture(scope="session") +def user_manager( + settings: GRLBaseSettings, thl_web_rw: PostgresConfig, thl_web_rr: PostgresConfig +) -> UserManager: + assert thl_web_rw.dsn + assert thl_web_rw.dsn.path + assert thl_web_rr.dsn + assert thl_web_rr.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rr.dsn.path + + from generalresearch.managers.thl.user_manager.user_manager import ( + UserManager, + ) + + return UserManager( + pg_config=thl_web_rw, + pg_config_rr=thl_web_rr, + redis=settings.redis, + ) + + +@pytest.fixture(scope="session") +def user_metadata_manager(thl_web_rw: PostgresConfig) -> UserMetadataManager: + assert thl_web_rw.dsn + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path + + from generalresearch.managers.thl.user_manager.user_metadata_manager import ( + UserMetadataManager, + ) + + return UserMetadataManager(pg_config=thl_web_rw) + + +@pytest.fixture(scope="session") +def session_manager(thl_web_rw: PostgresConfig) -> SessionManager: + assert thl_web_rw.dsn + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path + + from generalresearch.managers.thl.session import SessionManager + + return SessionManager(pg_config=thl_web_rw) + + +@pytest.fixture(scope="session") +def wall_manager(thl_web_rw: PostgresConfig) -> WallManager: + assert thl_web_rw.dsn + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path + + from generalresearch.managers.thl.wall import WallManager + + return WallManager(pg_config=thl_web_rw) + + +@pytest.fixture(scope="session") +def wall_cache_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> WallCacheManager: + # assert "/unittest-" in thl_web_rw.dsn.path + + from generalresearch.managers.thl.wall import WallCacheManager + + return WallCacheManager(pg_config=thl_web_rw, redis_config=thl_redis_config) + + +@pytest.fixture(scope="session") +def task_adjustment_manager(thl_web_rw: PostgresConfig) -> TaskAdjustmentManager: + # assert "/unittest-" in thl_web_rw.dsn.path + + from generalresearch.managers.thl.task_adjustment import ( + TaskAdjustmentManager, + ) + + return TaskAdjustmentManager(pg_config=thl_web_rw) + + +@pytest.fixture(scope="session") +def category_manager(thl_web_rw: PostgresConfig) -> CategoryManager: + assert thl_web_rw.dsn + assert thl_web_rw.dsn.path + assert "/unittest-" in thl_web_rw.dsn.path + from generalresearch.managers.thl.category import CategoryManager + + return CategoryManager(pg_config=thl_web_rw) + + +@pytest.fixture(scope="session") +def buyer_manager(thl_web_rw: PostgresConfig) -> BuyerManager: + # assert "/unittest-" in thl_web_rw.dsn.path + from generalresearch.managers.thl.buyer import BuyerManager + + return BuyerManager(pg_config=thl_web_rw) + + +@pytest.fixture(scope="session") +def survey_manager(thl_web_rw: PostgresConfig): + # assert "/unittest-" in thl_web_rw.dsn.path + from generalresearch.managers.thl.survey import SurveyManager + + return SurveyManager(pg_config=thl_web_rw) + + +@pytest.fixture(scope="session") +def surveystat_manager(thl_web_rw: PostgresConfig): + # assert "/unittest-" in thl_web_rw.dsn.path + from generalresearch.managers.thl.survey import SurveyStatManager + + return SurveyStatManager(pg_config=thl_web_rw) + + +@pytest.fixture(scope="session") +def surveypenalty_manager(thl_redis_config: RedisConfig): + from generalresearch.managers.thl.survey_penalty import SurveyPenaltyManager + + return SurveyPenaltyManager(redis_config=thl_redis_config) diff --git a/test_utils/managers/upk/conftest.py b/test_utils/managers/upk/conftest.py index e28d085..d8f956c 100644 --- a/test_utils/managers/upk/conftest.py +++ b/test_utils/managers/upk/conftest.py @@ -1,173 +1,69 @@ -import os -import time -from typing import TYPE_CHECKING, Optional -from uuid import UUID +from typing import Callable, Generator -import pandas as pd import pytest +from generalresearch.managers.thl.profiling.question import ( + QuestionManager, +) +from generalresearch.managers.thl.profiling.schema import ( + UpkSchemaManager, +) +from generalresearch.managers.thl.profiling.uqa import UQAManager +from generalresearch.managers.thl.profiling.user_upk import ( + UserUpkManager, +) +from generalresearch.models.thl.user import User from generalresearch.pg_helper import PostgresConfig - -if TYPE_CHECKING: - from generalresearch.managers.thl.category import CategoryManager - - -def insert_data_from_csv( - thl_web_rw: PostgresConfig, - table_name: str, - fp: Optional[str] = None, - disable_fk_checks: bool = False, - df: Optional[pd.DataFrame] = None, -): - assert fp is not None or df is not None and not (fp is not None and df is not None) - if fp: - df = pd.read_csv(fp, dtype=str) - df = df.where(pd.notnull(df), None) - cols = list(df.columns) - col_str = ", ".join(cols) - values_str = ", ".join(["%s"] * len(cols)) - if "id" in df.columns and len(df["id"].iloc[0]) == 36: - df["id"] = df["id"].map(lambda x: UUID(x).hex) - args = df.to_dict("tight")["data"] - - with thl_web_rw.make_connection() as conn: - with conn.cursor() as c: - if disable_fk_checks: - c.execute("SET CONSTRAINTS ALL DEFERRED") - c.executemany( - f"INSERT INTO {table_name} ({col_str}) VALUES ({values_str})", - params_seq=args, - ) - conn.commit() +from generalresearch.redis_helper import RedisConfig @pytest.fixture(scope="session") -def category_data( - thl_web_rw: PostgresConfig, category_manager: "CategoryManager" -) -> None: - fp = os.path.join(os.path.dirname(__file__), "marketplace_category.csv.gz") - insert_data_from_csv( - thl_web_rw, - fp=fp, - table_name="marketplace_category", - disable_fk_checks=True, - ) - # Don't strictly need to do this, but probably we should - category_manager.populate_caches() - cats = category_manager.categories.values() - path_id = {c.path: c.id for c in cats} - data = [ - {"id": c.id, "parent_id": path_id[c.parent_path]} for c in cats if c.parent_path - ] - query = """ - UPDATE marketplace_category - SET parent_id = %(parent_id)s - WHERE id = %(id)s; - """ - with thl_web_rw.make_connection() as conn: - with conn.cursor() as c: - c.executemany(query=query, params_seq=data) - conn.commit() +def upk_schema_manager(thl_web_rw: PostgresConfig) -> UpkSchemaManager: + return UpkSchemaManager(pg_config=thl_web_rw) @pytest.fixture(scope="session") -def property_data(thl_web_rw: PostgresConfig) -> None: - fp = os.path.join(os.path.dirname(__file__), "marketplace_property.csv.gz") - insert_data_from_csv(thl_web_rw, fp=fp, table_name="marketplace_property") - +def user_upk_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> UserUpkManager: -@pytest.fixture(scope="session") -def item_data(thl_web_rw: PostgresConfig) -> None: - fp = os.path.join(os.path.dirname(__file__), "marketplace_item.csv.gz") - insert_data_from_csv(thl_web_rw, fp=fp, table_name="marketplace_item") + return UserUpkManager(pg_config=thl_web_rw, redis_config=thl_redis_config) @pytest.fixture(scope="session") -def propertycategoryassociation_data( +def question_manager( thl_web_rw: PostgresConfig, - category_data, - property_data, - category_manager: "CategoryManager", -) -> None: - table_name = "marketplace_propertycategoryassociation" - fp = os.path.join(os.path.dirname(__file__), f"{table_name}.csv.gz") - # Need to lookup category pk from uuid - category_manager.populate_caches() - df = pd.read_csv(fp, dtype=str) - df["category_id"] = df["category_id"].map( - lambda x: category_manager.categories[x].id - ) - insert_data_from_csv(thl_web_rw, df=df, table_name=table_name) +) -> QuestionManager: + return QuestionManager(pg_config=thl_web_rw) @pytest.fixture(scope="session") -def propertycountry_data(thl_web_rw: PostgresConfig, property_data) -> None: - fp = os.path.join(os.path.dirname(__file__), "marketplace_propertycountry.csv.gz") - insert_data_from_csv(thl_web_rw, fp=fp, table_name="marketplace_propertycountry") +def uqa_manager( + thl_web_rw: PostgresConfig, thl_redis_config: RedisConfig +) -> UQAManager: + return UQAManager(redis_config=thl_redis_config, pg_config=thl_web_rw) -@pytest.fixture(scope="session") -def propertymarketplaceassociation_data( - thl_web_rw: PostgresConfig, property_data -) -> None: - table_name = "marketplace_propertymarketplaceassociation" - fp = os.path.join(os.path.dirname(__file__), f"{table_name}.csv.gz") - insert_data_from_csv(thl_web_rw, fp=fp, table_name=table_name) +@pytest.fixture(scope="function") +def uqa_manager_clear_cache_factory( + uqa_manager: UQAManager, +) -> Callable[..., Generator[None]]: -@pytest.fixture(scope="session") -def propertyitemrange_data( - thl_web_rw: PostgresConfig, property_data, item_data -) -> None: - table_name = "marketplace_propertyitemrange" - fp = os.path.join(os.path.dirname(__file__), f"{table_name}.csv.gz") - insert_data_from_csv(thl_web_rw, fp=fp, table_name=table_name) + def _inner(user: User) -> Generator[None]: + # On successive py-test/jenkins runs, the cache may contain + # the previous run's info (keyed under the same user_id) + uqa_manager.clear_cache(user) + yield -@pytest.fixture(scope="session") -def question_data(thl_web_rw: PostgresConfig) -> None: - table_name = "marketplace_question" - fp = os.path.join(os.path.dirname(__file__), f"{table_name}.csv.gz") - insert_data_from_csv( - thl_web_rw, fp=fp, table_name=table_name, disable_fk_checks=True - ) + uqa_manager.clear_cache(user) + return _inner -@pytest.fixture(scope="session") -def clear_upk_tables(thl_web_rw: PostgresConfig): - tables = [ - "marketplace_propertyitemrange", - "marketplace_propertymarketplaceassociation", - "marketplace_propertycategoryassociation", - "marketplace_category", - "marketplace_item", - "marketplace_property", - "marketplace_propertycountry", - "marketplace_question", - ] - table_str = ", ".join(tables) - - with thl_web_rw.make_connection() as conn: - with conn.cursor() as c: - c.execute(f"TRUNCATE {table_str} RESTART IDENTITY CASCADE;") - conn.commit() - -@pytest.fixture(scope="session") -def upk_data( - clear_upk_tables, - category_data, - property_data, - item_data, - propertycategoryassociation_data, - propertycountry_data, - propertymarketplaceassociation_data, - propertyitemrange_data, - question_data, -) -> None: - # Wait a second to make sure the HarmonizerCache refresh loop pulls these in - time.sleep(2) - - -def test_fixtures(upk_data): - pass +@pytest.fixture(scope="function") +def uqa_manager_clear_cache( + uqa_manager_clear_cache_factory: Callable[..., None], user: User +): + uqa_manager_clear_cache_factory(user=user) diff --git a/test_utils/managers/upk/marketplace_category.csv.gz b/test_utils/managers/upk/marketplace_category.csv.gz Binary files differdeleted file mode 100644 index 0f8ec1c..0000000 --- a/test_utils/managers/upk/marketplace_category.csv.gz +++ /dev/null diff --git a/test_utils/managers/upk/marketplace_item.csv.gz b/test_utils/managers/upk/marketplace_item.csv.gz Binary files differdeleted file mode 100644 index c12c5d8..0000000 --- a/test_utils/managers/upk/marketplace_item.csv.gz +++ /dev/null diff --git a/test_utils/managers/upk/marketplace_property.csv.gz b/test_utils/managers/upk/marketplace_property.csv.gz Binary files differdeleted file mode 100644 index a781d1d..0000000 --- a/test_utils/managers/upk/marketplace_property.csv.gz +++ /dev/null diff --git a/test_utils/managers/upk/marketplace_propertycategoryassociation.csv.gz b/test_utils/managers/upk/marketplace_propertycategoryassociation.csv.gz Binary files differdeleted file mode 100644 index 5b4ea19..0000000 --- a/test_utils/managers/upk/marketplace_propertycategoryassociation.csv.gz +++ /dev/null diff --git a/test_utils/managers/upk/marketplace_propertycountry.csv.gz b/test_utils/managers/upk/marketplace_propertycountry.csv.gz Binary files differdeleted file mode 100644 index 5d2a637..0000000 --- a/test_utils/managers/upk/marketplace_propertycountry.csv.gz +++ /dev/null diff --git a/test_utils/managers/upk/marketplace_propertyitemrange.csv.gz b/test_utils/managers/upk/marketplace_propertyitemrange.csv.gz Binary files differdeleted file mode 100644 index 84f4f0e..0000000 --- a/test_utils/managers/upk/marketplace_propertyitemrange.csv.gz +++ /dev/null diff --git a/test_utils/managers/upk/marketplace_propertymarketplaceassociation.csv.gz b/test_utils/managers/upk/marketplace_propertymarketplaceassociation.csv.gz Binary files differdeleted file mode 100644 index 6b9fd1c..0000000 --- a/test_utils/managers/upk/marketplace_propertymarketplaceassociation.csv.gz +++ /dev/null diff --git a/test_utils/managers/upk/marketplace_question.csv.gz b/test_utils/managers/upk/marketplace_question.csv.gz Binary files differdeleted file mode 100644 index bcfc3ad..0000000 --- a/test_utils/managers/upk/marketplace_question.csv.gz +++ /dev/null |
