Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switched to use redis in e2e #4524

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
45 changes: 37 additions & 8 deletions tests/selenium/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -29,6 +32,7 @@
from hct_mis_api.apps.household.fixtures import DocumentTypeFactory
from hct_mis_api.apps.household.models import DocumentType
from hct_mis_api.apps.program.fixtures import BeneficiaryGroupFactory
from hct_mis_api.config.env import env
from tests.selenium.page_object.accountability.communication import (
AccountabilityCommunication,
)
Expand Down Expand Up @@ -114,11 +118,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"
Expand Down Expand Up @@ -147,12 +182,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(
{
Expand Down
Loading