import math from datetime import timedelta from typing import List from server.constants.core import OS from tests.factories import * def get_accounts_data(*args, **kwargs): return [ dict( user_id=f"user{i}", app_slug=f"app{math.ceil(i / 2)}", is_active=kwargs.get("is_active", True), updated_at=datetime.utcnow() - timedelta(days=i), ) for i in args ] async def create_accounts( session, count: int = 4, apps_count: int or None = None, is_active: bool = True, with_apps: bool = True ): if with_apps: await create_applications(session, apps_count or math.ceil(count / 2)) accounts = [] for item in get_accounts_data(*range(1, count + 1), is_active=is_active): account = AccountFactory.create(**item) session.add(account) accounts.append(account) await session.commit() return accounts def get_applications_data(*args): return [ dict( slug=f"app{i}", name=f"App_{i}", settings={f"t{i}1": {f"s{i}1": f"v{i}1"}, f"t{i}2": {f"s{i}2": bool(i % 2)}}, types=[f"t{i}1", f"t{i}2"] ) for i in args ] async def create_applications(session, count: int = 2): for item in get_applications_data(*range(1, count + 1)): session.add(ApplicationFactory.create(**item)) await session.commit() def check_object(obj, _dict, exclude=None, **extra): exclude = set(exclude or []) for k, v in _dict.items(): if k not in exclude: assert getattr(obj, k) == v for k, v in extra.items(): assert getattr(obj, k) == v async def create_test_accounts(session, inactive_users: List[int] = None): app_data = ( (None, None, True), ({"mobile": {"set_1": "v_1", "set_2": False}}, ["mobile"], True), ({"mobile": {"s_1": "v_1"}, "portal": {"s_2": True}}, ["mobile", "portal"], True), ({"mobile": {"f1": "k1"}}, ["mobile", "fridge"], True), ) for index, item in enumerate(app_data, 1): session.add( ApplicationFactory.create( id=index, slug=f"app_{index}", name=f"App {index}", settings=item[0], types=item[1], is_active=item[2] ) ) await session.commit() account_data = ((1, True), (2, True), (2, True), (2, True), (2, False), (3, True), (3, True)) for index, item in enumerate(account_data, 1): session.add( AccountFactory.create( id=index, app_slug=f"app_{item[0]}", user_id=f"u_{index}", is_active=item[1] and (not inactive_users or index not in inactive_users), updated_at=datetime.utcnow() - timedelta(days=28 + index), ) ) await session.commit() async def _create_messages_one(session, table, limit: int = 10): messages_data = ( ("app_1", 1, None, 10, "ex_1"), ("app_1", 1, 1, None, None), ("app_1", 1, 2, 11, None), ("app_1", 2, 1, 10, None), ("app_1", 2, 3, 12, "ex_2"), ("app_2", 3, 4, 15, "ex_2"), ("app_2", 4, None, None, "ex_3"), ("app_3", 5, 5, 12, "ex_4"), ("app_4", 6, 6, 9, "ex_5"), ("app_4", 6, 2, 8, "ex_7"), ) field_list = ("app_slug", "account_id", "event_id", "message_id", "external_id") for index, item in enumerate(messages_data[:limit], 1): session.add( table.create( id=index, created_at=datetime.utcnow() - timedelta(days=index + 28), **{k: v for k, v in zip(field_list, item)}, ) ) await session.commit() async def create_test_messages(session): table_list = ((FeedMessageFactory, -2), (MessageFactory, 10), (PushMessageFactory, -1)) for table, limit in table_list: await _create_messages_one(session, table, limit) async def set_test_db( session, with_favorites: bool = True, with_messages: bool = False, inactive_users: List[int] = None, with_app_versions: bool = False ): await create_test_accounts(session, inactive_users) settings_data = ( (1, None, 3, {"f_1": "v_1"}), (2, None, 1, {"f_2": "v_2"}), (3, None, 1, {"f_3": "v_3"}), (4, "mobile", 1, {"s_1": False}), (4, "portal", 1, {"s_2": True}), (4, "portal", 2, {"s_2": "True"}), (5, "mobile", 1, {"s_1": False}), (6, "portal", 1, {"s_2": True}), ) for index, item in enumerate(settings_data, 1): session.add(SettingsFactory.create(id=index, account_id=item[0], type=item[1], version=item[2], data=item[3])) await session.commit() devices_data = ( (1, "android", True, "2.2.2"), (1, "ios", True, "4.4.4"), (1, "iron OS", True, "1.1.1"), (2, "android", True, "2.2.2"), (2, "kettle OS TM", False, "2.2.2"), (3, "android", True, None), (3, "ios", True, None), (3, "win", True, "3.3.3"), (4, "android", False, "4.4.4"), ) for index, item in enumerate(devices_data, 1): session.add( DeviceFactory.create( id=index, account_id=item[0], inner_id=f"in_id_{index}", os=item[1], token=f"token_{index}", status=item[2], app_version=item[3] if with_app_versions else None, ) ) await session.commit() if with_favorites: favorites_data = ( (1, "pl_1", "playlist", {"f1": "v1"}), (1, "pl_2", "playlist", {"f2": "v2"}), (1, "tr_1", "track", {"name": "3rd of september"}), (2, "pl_1", "playlist", {"f1": "v1"}), (2, "pl_3", "playlist", {"f3": "v3"}), (3, "pl_1", "playlist", {"f1": "v1"}), (4, "pl_3", "playlist", {"f3": "v3"}), (5, "tr_2", "track", {"id": "134"}), (6, "tr_3", "track", {"field": "value"}), ) for index, item in enumerate(favorites_data, 1): session.add( FavoritesFactory.create( id=index, account_id=item[0], entity_id=item[1], entity_type=item[2], data=item[3] ) ) await session.commit() if with_messages: await create_test_messages(session) def get_os(i): return (OS.IOS if i % 2 else OS.ANDROID).value def get_country_code(i): if i % 2: return f'c{i}' def get_device_record_data(i: int, *args, **kwargs) -> dict: return { "expo_token": kwargs.get("expo_token", None) or f"expo_token_{i}", "device_inner_id": kwargs.get("device_inner_id", None) or f"device_inner_id_{i}", "os": kwargs.get("os", None) or get_os(i), } def get_device(*args, **kwargs): return [ dict( account_id=math.ceil(i / 2), status=kwargs.get("is_active", True), token=f"expo_token_{i}", inner_id=f"device_inner_id_{i}", os=get_os(i), app_version=kwargs.get("app_version") ) for i in args ] async def create_devices( session, count: int = 4, apps_count: int or None = None, is_active: bool = True, app_version: str = None ): await create_accounts(session, apps_count or math.ceil(count / 2)) for item in get_device(*range(1, count + 1), status=is_active, app_version=app_version): session.add(DeviceFactory.create(**item)) await session.commit() def get_events(*args) -> List[dict]: return [ dict( app_slug=f"app{index % 3 + 1}", id=index, code=f"co{index % 3 + 1}", event_id=(index % 4 + 1) if index % 3 else None, external_id=f"ex{index % 5 + 1}" if index % 5 else None, created_at=datetime.utcnow() - timedelta(days=30 + index), ) for index in args ] async def create_events(session, count: int = 8): await create_applications(session, 3) for item in get_events(*range(1, count + 1)): session.add(EventFactory.create(**item)) await session.commit()