From 06b8c7f53fad9de8e4ac8b3d1ecd4d0068b082e7 Mon Sep 17 00:00:00 2001 From: Jan Romaniak Date: Mon, 9 Dec 2024 16:13:47 +0100 Subject: [PATCH] switched to use redis in e2e --- src/hct_mis_api/migrations_script/__init__.py | 0 src/hct_mis_api/migrations_script/main.py | 60 ------------------- tests/selenium/conftest.py | 45 +++++++++++--- 3 files changed, 37 insertions(+), 68 deletions(-) delete mode 100644 src/hct_mis_api/migrations_script/__init__.py delete mode 100644 src/hct_mis_api/migrations_script/main.py diff --git a/src/hct_mis_api/migrations_script/__init__.py b/src/hct_mis_api/migrations_script/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/hct_mis_api/migrations_script/main.py b/src/hct_mis_api/migrations_script/main.py deleted file mode 100644 index 06f0572595..0000000000 --- a/src/hct_mis_api/migrations_script/main.py +++ /dev/null @@ -1,60 +0,0 @@ -import csv -import os -from datetime import datetime - -import django -from django.core.management import call_command -from django.db import connection - -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings") -django.setup() - - -def export_migration_info_to_csv(filename="migrations_info.csv"): - timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") - filename = f"migrations_info_{timestamp}.csv" - with connection.cursor() as cursor: - cursor.execute("SELECT * FROM django_migrations") - rows = cursor.fetchall() - column_names = [desc[0] for desc in cursor.description] - - with open(filename, mode="w", newline="") as file: - writer = csv.writer(file) - writer.writerow(column_names) - writer.writerows(rows) - print(f"Migration info exported to {filename}") - - -def clear_migration_table(): - with connection.cursor() as cursor: - cursor.execute("DELETE FROM django_migrations") - - -def fake_migrations(excluded_migrations): - call_command("migrate", "--fake") - with connection.cursor() as cursor: - for app, name in excluded_migrations: - cursor.execute("DELETE FROM django_migrations WHERE app = %s AND name = %s", [app, name]) - - -def apply_migrations(): - call_command("migrate") - print("Migrations applied.") - - -if __name__ == "__main__": - export_migration_info_to_csv() - clear_migration_table() - excluded_migrations = [ - ("targeting", "0002_migration"), - ("household", "0003_migration"), - ("household", "0004_migration"), - ("grievance", "0004_migration"), - ("payment", "0002_migration"), - ("payment", "0003_migration"), - ("payment", "0004_migration"), - ("payment", "0005_migration"), - ("aurora", "0003_migration"), - ] - fake_migrations(excluded_migrations) - apply_migrations() diff --git a/tests/selenium/conftest.py b/tests/selenium/conftest.py index e7da8c809e..9bbeff429e 100644 --- a/tests/selenium/conftest.py +++ b/tests/selenium/conftest.py @@ -1,11 +1,14 @@ import logging import os +import re from datetime import datetime +from typing import Any from django.conf import settings from django.core.management import call_command import pytest +import redis from _pytest.fixtures import FixtureRequest from _pytest.nodes import Item from _pytest.runner import CallInfo @@ -28,6 +31,7 @@ from hct_mis_api.apps.geo.models import Country from hct_mis_api.apps.household.fixtures import DocumentTypeFactory from hct_mis_api.apps.household.models import DocumentType +from hct_mis_api.config.env import env from tests.selenium.page_object.accountability.communication import ( AccountabilityCommunication, ) @@ -113,11 +117,42 @@ def pytest_addoption(parser) -> None: # type: ignore parser.addoption("--mapping", action="store_true", default=False, help="Enable mapping mode") +def get_redis_host() -> str: + regex = "\\/\\/(.*):" + redis_host = re.search(regex, env("CACHE_LOCATION")).group(1) + return redis_host + + +@pytest.fixture(autouse=True) +def configure_cache_for_tests(worker_id: str, settings: Any) -> None: + """ + Dynamically configure Django's cache backend for each pytest-xdist worker. + """ + redis_db = 0 if worker_id == "master" else int(worker_id.replace("gw", "")) + settings.CACHES = { + "default": { + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": f"redis://{get_redis_host()}:6379/{redis_db}", + "OPTIONS": { + "CLIENT_CLASS": "django_redis.client.DefaultClient", + }, + } + } + + +@pytest.fixture(autouse=True) +def flush_redis(worker_id: str) -> None: + redis_db = 0 if worker_id == "master" else int(worker_id.replace("gw", "")) + redis_client = redis.StrictRedis(host=get_redis_host(), port=6379, db=redis_db) + redis_client.flushdb() + + def pytest_configure(config) -> None: # type: ignore - env = Env() - settings.OUTPUT_DATA_ROOT = env("OUTPUT_DATA_ROOT", default="/tests/selenium/output_data") config.addinivalue_line("markers", "night: This marker is intended for e2e tests conducted during the night on CI") # delete all old screenshots + + env = Env() + settings.OUTPUT_DATA_ROOT = env("OUTPUT_DATA_ROOT", default="/tests/selenium/output_data") settings.REPORT_DIRECTORY = f"{settings.OUTPUT_DATA_ROOT}/report" settings.DOWNLOAD_DIRECTORY = f"{settings.OUTPUT_DATA_ROOT}/report/downloads" settings.SCREENSHOT_DIRECTORY = f"{settings.REPORT_DIRECTORY}/screenshot" @@ -146,12 +181,6 @@ def pytest_configure(config) -> None: # type: ignore settings.SECURE_CONTENT_TYPE_NOSNIFF = True settings.SECURE_REFERRER_POLICY = "same-origin" settings.CACHE_ENABLED = False - settings.CACHES = { - "default": { - "BACKEND": "hct_mis_api.apps.core.memcache.LocMemCache", - "TIMEOUT": 1800, - } - } settings.LOGGING["loggers"].update( {