diff options
Diffstat (limited to 'test_utils')
| -rw-r--r-- | test_utils/conftest.py | 42 | ||||
| -rw-r--r-- | test_utils/managers/cashout_methods.py | 85 | ||||
| -rw-r--r-- | test_utils/managers/conftest.py | 7 | ||||
| -rw-r--r-- | test_utils/managers/gr/conftest.py | 4 | ||||
| -rw-r--r-- | test_utils/models/thl/conftest.py | 12 |
5 files changed, 89 insertions, 61 deletions
diff --git a/test_utils/conftest.py b/test_utils/conftest.py index 6894450..757f141 100644 --- a/test_utils/conftest.py +++ b/test_utils/conftest.py @@ -23,6 +23,18 @@ from generalresearch.models.custom_types import InternalHostname, PostgresDict from generalresearch.pg_helper import PostgresConfig from generalresearch.sql_helper import SqlHelper +# -- redis notes from jenkins file +# sh "redis-cli -u ${env.THL_REDIS} FLUSHDB" +# sh "redis-cli -u ${env.GR_REDIS} FLUSHDB" + +# script { +# env.GR_REDIS_DB = new Random().nextInt(1024).toString() +# env.GR_REDIS = "redis://${env.REDIS}:6379/${env.GR_REDIS_DB}" +# echo "Using GR Redis: ${env.GR_REDIS}" +# if (sh(script: "redis-cli -u ${env.GR_REDIS} SET jenkins_lock 1 NX EX 3600", returnStdout: true).trim() != 'OK') +# error('Redis already locked... aborting.') +# } + @pytest.fixture(scope="session") def env_file_path(pytestconfig: Config) -> Path: @@ -178,18 +190,18 @@ def gr_repo( repo_url = "ssh://code.g-r-l.com/general-research/gr-carer.git" _ran = {} - if _ran.get(repo_url, False): - print(f"Already ran django_db_factory.{repo_url}") - return - - _ran[repo_url] = True fn = tmp_path_factory.mktemp("repos") repo_path = fn / "gr-carer" - repo_path.mkdir(parents=True, exist_ok=True) def _inner() -> Path: + if _ran.get(repo_url, False): + print(f"Already ran django_db_factory.{repo_url}") + return repo_path + + _ran[repo_url] = True + ssh_cmd = ( f"ssh -i {git_key_path} " "-o IdentitiesOnly=yes " @@ -206,11 +218,6 @@ def gr_repo( env=env, ) - result = subprocess.run( - ["cat", git_key_path], capture_output=True, text=True, check=False - ) - print(repr(result.stdout)) - return repo_path return _inner @@ -236,7 +243,8 @@ def django_db_factory( if _ran.get(django_project, False): print(f"Already ran django_db_factory.{django_project}") - return + return postgres_instance + _ran[django_project] = True if "gr" in django_project: @@ -246,6 +254,8 @@ def django_db_factory( # 1. Bootstrapping Django settings if not django_settings.configured: + print(postgres_instance_dict) + django_settings.configure( DATABASES={ "default": { @@ -265,11 +275,13 @@ def django_db_factory( ) django.setup() - for model in apps.get_models(): - print(f"Discovered model: {model._meta.label}") + # for model in apps.get_models(): + # print(f"Discovered model: {model._meta.label}") # 2. Run migrations directly during fixture activation - call_command("makemigrations", "gr", interactive=False) + if "gr" in django_project: + call_command("makemigrations", "common", interactive=False) + call_command("migrate") # 3. Return the Dsn so the factory gives a way to connect diff --git a/test_utils/managers/cashout_methods.py b/test_utils/managers/cashout_methods.py index b201e8c..238cdda 100644 --- a/test_utils/managers/cashout_methods.py +++ b/test_utils/managers/cashout_methods.py @@ -1,6 +1,11 @@ +from __future__ import annotations + import random +from collections.abc import Callable from uuid import uuid4 +import pytest + from generalresearch.models.thl.wallet import Currency, PayoutType from generalresearch.models.thl.wallet.cashout_method import ( CashoutMethod, @@ -8,45 +13,55 @@ from generalresearch.models.thl.wallet.cashout_method import ( ) -def random_ext_id(base: str = "U02"): - suffix = random.randint(0, 99999) - return f"{base}{suffix:05d}" +@pytest.fixture(scope="session") +def random_ext_id_factory(base: str = "U02") -> Callable[..., str]: + + def _inner() -> str: + suffix = random.randint(0, 99999) + return f"{base}{suffix:05d}" + return _inner -EXAMPLE_TANGO_CASHOUT_METHODS = [ - CashoutMethod( - id=uuid4().hex, - last_updated="2021-06-23T20:45:38.239182Z", - is_live=True, - type=PayoutType.TANGO, - ext_id=random_ext_id(), - name="Safeway eGift Card $25", - data=TangoCashoutMethodData( - value_type="fixed", countries=["US"], utid=random_ext_id() + +@pytest.fixture(scope="session") +def example_tango_cashout_methods( + random_ext_id_factory: Callable[..., str], +) -> list[CashoutMethod]: + return [ + CashoutMethod( + id=uuid4().hex, + last_updated="2021-06-23T20:45:38.239182Z", + is_live=True, + type=PayoutType.TANGO, + ext_id=random_ext_id_factory(), + name="Safeway eGift Card $25", + data=TangoCashoutMethodData( + value_type="fixed", countries=["US"], utid=random_ext_id_factory() + ), + user=None, + image_url="https://d30s7yzk2az89n.cloudfront.net/images/brands/b694446-1200w-326ppi.png", + original_currency=Currency.USD, + min_value=2500, + max_value=2500, ), - user=None, - image_url="https://d30s7yzk2az89n.cloudfront.net/images/brands/b694446-1200w-326ppi.png", - original_currency=Currency.USD, - min_value=2500, - max_value=2500, - ), - CashoutMethod( - id=uuid4().hex, - last_updated="2021-06-23T20:45:38.239182Z", - is_live=True, - type=PayoutType.TANGO, - ext_id=random_ext_id(), - name="Amazon.it Gift Certificate", - data=TangoCashoutMethodData( - value_type="variable", countries=["IT"], utid="U006961" + CashoutMethod( + id=uuid4().hex, + last_updated="2021-06-23T20:45:38.239182Z", + is_live=True, + type=PayoutType.TANGO, + ext_id=random_ext_id_factory(), + name="Amazon.it Gift Certificate", + data=TangoCashoutMethodData( + value_type="variable", countries=["IT"], utid="U006961" + ), + user=None, + image_url="https://d30s7yzk2az89n.cloudfront.net/images/brands/b405753-1200w-326ppi.png", + original_currency=Currency.EUR, + min_value=1, + max_value=10000, ), - user=None, - image_url="https://d30s7yzk2az89n.cloudfront.net/images/brands/b405753-1200w-326ppi.png", - original_currency=Currency.EUR, - min_value=1, - max_value=10000, - ), -] + ] + # AMT_ASSIGNMENT_CASHOUT_METHOD = CashoutMethod( # id=uuid4().hex, diff --git a/test_utils/managers/conftest.py b/test_utils/managers/conftest.py index b03a646..9c6a1a7 100644 --- a/test_utils/managers/conftest.py +++ b/test_utils/managers/conftest.py @@ -32,12 +32,10 @@ from generalresearch.managers.thl.userhealth import ( UserIpHistoryManager, ) from generalresearch.models import Source +from generalresearch.models.thl.wallet.cashout_method import CashoutMethod from generalresearch.pg_helper import PostgresConfig from generalresearch.redis_helper import RedisConfig from generalresearch.sql_helper import SqlHelper -from test_utils.managers.cashout_methods import ( - EXAMPLE_TANGO_CASHOUT_METHODS, -) # === THL === @@ -172,12 +170,13 @@ def delete_cashoutmethod_db(thl_web_rw: PostgresConfig) -> Callable[..., None]: def setup_cashoutmethod_db( cashout_method_manager: CashoutMethodManager, delete_cashoutmethod_db: Callable[..., None], + example_tango_cashout_methods: list[CashoutMethod], ) -> Callable[..., None]: def _inner(): delete_cashoutmethod_db() - for x in EXAMPLE_TANGO_CASHOUT_METHODS: + for x in example_tango_cashout_methods: cashout_method_manager.create(x) # TODO: convert these ids into instances to use. diff --git a/test_utils/managers/gr/conftest.py b/test_utils/managers/gr/conftest.py index 4da8fe3..69f3e9a 100644 --- a/test_utils/managers/gr/conftest.py +++ b/test_utils/managers/gr/conftest.py @@ -56,9 +56,11 @@ def gr_redis_config(settings: GRLBaseSettings) -> RedisConfig: @pytest.fixture(scope="session") def gr_db(django_db_factory: Callable[..., PostgresDsn]) -> PostgresConfig: + _dsn = django_db_factory("gr.common") + print("DDDD:", _dsn) return PostgresConfig( - dsn=django_db_factory("gr_carer"), + dsn=_dsn, connect_timeout=1, statement_timeout=5, ) diff --git a/test_utils/models/thl/conftest.py b/test_utils/models/thl/conftest.py index 907306d..fc57c73 100644 --- a/test_utils/models/thl/conftest.py +++ b/test_utils/models/thl/conftest.py @@ -4,13 +4,13 @@ from collections.abc import Callable from datetime import UTC, datetime from decimal import ROUND_DOWN, Decimal from random import choice as rand_choice -from random import choice as rchoice from random import randint, random from typing import Any from uuid import uuid4 import faker import pytest +from grip_client.enums import AccessType from pydantic import PositiveInt from generalresearch.managers.thl.ipinfo import IPGeonameManager, IPInformationManager @@ -30,7 +30,7 @@ from generalresearch.models.legacy.bucket import Bucket from generalresearch.models.thl.definitions import ( PayoutStatus, ) -from generalresearch.models.thl.ipinfo import IPGeoname, IPInformation, UserType +from generalresearch.models.thl.ipinfo import IPGeoname, IPInformation from generalresearch.models.thl.payout import UserPayoutEvent from generalresearch.models.thl.product import ( PayoutConfig, @@ -118,7 +118,7 @@ def wall_factory( session = session_factory() session_id = session.id - source = source or rchoice(list(Source)) + source = source or rand_choice(list(Source)) req_survey_id = req_survey_id or uuid4().hex req_cpi = req_cpi or Decimal(fake.random_int(min=1, max=150) / 100).quantize( Decimal(".01"), rounding=ROUND_DOWN @@ -284,7 +284,7 @@ def ipinformation_factory( network: str | None = None, organization: str | None = None, static_ip_score: float | None = None, - user_type: UserType | None = None, + user_type: AccessType | None = None, postal_code: str | None = None, latitude: Decimal | None = None, longitude: Decimal | None = None, @@ -426,8 +426,8 @@ def auditlog_factory(audit_log_manager: AuditLogManager): return audit_log_manager.create( user_id=user_id, - level=level or rchoice(list(AuditLogLevel)), - event_type=event_type or rchoice(list(event_types)), + level=level or rand_choice(list(AuditLogLevel)), + event_type=event_type or rand_choice(list(event_types)), event_msg=event_msg, event_value=event_value, ) |
