Skip to content

Commit

Permalink
let's use responses' recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
saxix committed Dec 3, 2024
1 parent 1d9995d commit 6f6c93a
Show file tree
Hide file tree
Showing 22 changed files with 3,205 additions and 14,695 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ jobs:
-v "./tests:/app/tests" \
-v "./pytest.ini:/app/pytest.ini" \
-t ${{env.IMAGE}} \
pytest tests/ --selenium -v --maxfail=5 --migrations --cov-report xml:./output/coverage.xml
pytest tests/ --selenium -n 4 -v --maxfail=5 --migrations --cov-report xml:./output/coverage.xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
Expand Down
3 changes: 2 additions & 1 deletion src/country_workspace/cache/handlers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db.models import Model
from django.db.models.signals import post_save
from django.dispatch import receiver

Expand All @@ -7,7 +8,7 @@


@receiver(post_save)
def update_cache(sender, instance, **kwargs):
def update_cache(sender: "type[Model]", instance: Model, **kwargs):
program = None
if isinstance(instance, (Household, Individual, CountryHousehold, CountryIndividual)):
program = instance.program
Expand Down
9 changes: 7 additions & 2 deletions src/country_workspace/cache/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ def reset_cache_version(self, *, office: "Optional[Office]" = None, program: "Op

def get_cache_version(self, *, office: "Optional[Office]" = None, program: "Optional[Program]" = None):
key = self._get_version_key(office, program)
return self.cache.get(key) or 1
version = self.cache.get(key)
if not version:
version = 1
self.cache.set(key, version, timeout=self.cache_timeout)
return version

def incr_cache_version(self, *, office: "Optional[Office]" = None, program: "Optional[Program]" = None):

if office and program:
raise ValueError("Cannot use both office and program")
key = self._get_version_key(office, program)
try:
return self.cache.incr(key)
Expand Down
15 changes: 9 additions & 6 deletions tests/cache/test_cache_handlers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest import mock

from django.db.models import Model

import pytest
Expand Down Expand Up @@ -26,16 +28,17 @@ def pytest_generate_tests(metafunc: "Metafunc") -> None: # noqa

@pytest.fixture
def manager(worker_id):
m = CacheManager(f"cache-{worker_id}")
m = CacheManager(f"cache{worker_id}")
m.init()
return m


def test_handlers(manager, model):
fc = get_factory_for_model(model)
obj = fc()
program = getattr(obj, "program", obj)
v1 = manager.get_cache_version(program=program)
obj.save()
v2 = manager.get_cache_version(program=program)
assert v2 > v1
with mock.patch("country_workspace.cache.handlers.cache_manager", manager):
program = getattr(obj, "program", obj)
v1 = manager.get_cache_version(program=program)
obj.save()
v2 = manager.get_cache_version(program=program)
assert v2 > v1
6 changes: 3 additions & 3 deletions tests/cache/test_cache_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def program():

@pytest.fixture
def manager(worker_id):
m = CacheManager(f"cache-{worker_id}")
m = CacheManager(f"cache{worker_id}")
m.init()
return m

Expand All @@ -60,14 +60,14 @@ def test_cache_manager_init():

def test_cache_manager_build_key_from_request(app, manager, program, rf, worker_id):
request = rf.get("/")
assert manager.build_key_from_request(request) == f"cache-{worker_id}:entry:view:-:ts:v:t:p::"
assert manager.build_key_from_request(request) == f"cache{worker_id}:entry:view:-:ts:v:t:p::"

with user_grant_permissions(
app._user, "workspaces.view_countryhousehold", country_office_or_program=program.country_office
):
with state.set(tenant=program.country_office, program=program):
key = manager.build_key_from_request(request)
assert key.startswith(f"cache-{worker_id}:")
assert key.startswith(f"cache{worker_id}:")
assert program.country_office.slug in key


Expand Down
19 changes: 9 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import pytest
import responses
import vcr
from vcr.record_mode import RecordMode
from responses import _recorder

here = Path(__file__).parent
sys.path.insert(0, str(here / "../src"))
Expand Down Expand Up @@ -50,9 +49,10 @@ def pytest_configure(config):
os.environ["CELERY_TASK_ALWAYS_EAGER"] = "1"
os.environ["CELERY_TASK_STORE_EAGER_RESULT"] = "1"
os.environ["SECURE_HSTS_PRELOAD"] = "0"
os.environ["HOPE_API_URL"] = "https://dev-hope.unitst.org/api/rest/"
os.environ["HOPE_API_TOKEN"] = "kugiugiuygiuygiuygiuhgiuhgiuhgiugiu"
os.environ["AURORA_API_URL"] = "https://aurora.io/api/"
os.environ["HOPE_API_URL"] = "https://dev-hope.unitst.org/api/rest/"
# os.environ["HOPE_API_TOKEN"] = "kugiugiuygiuygiuygiuhgiuhgiuhgiugiu"
# os.environ["AURORA_API_URL"] = "https://aurora.io/api/"

os.environ["SECRET_KEY"] = "kugiugiuygiuygiuygiuhgiuhgiuhgiugiu"
os.environ["FILE_STORAGE_DEFAULT"] = "django.core.files.storage.FileSystemStorage?location=./~tests/storage/"
Expand All @@ -62,10 +62,10 @@ def pytest_configure(config):
import django
from django.conf import settings

settings.AURORA_API_URL = "https://aurora.io/api/"
settings.AURORA_API_TOKEN = "aurora_token"
settings.AURORA_API_URL = os.environ["AURORA_API_URL"]
# settings.AURORA_API_TOKEN = os.environ["HOPE_API_TOKEN"]
settings.HOPE_API_URL = "https://dev-hope.unitst.org/api/rest/"
settings.HOPE_API_TOKEN = "kugiugiuygiuygiuygiuhgiuhgiuhgiugiu"
settings.HOPE_API_TOKEN = os.environ["HOPE_API_TOKEN"]

settings.ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
settings.SIGNING_BACKEND = "testutils.signers.PlainSigner"
Expand All @@ -90,7 +90,7 @@ def setup(db, worker_id, settings):
settings.CELERY_BROKER_URL = f"redis://localhost:6379/1{worker_id}"
from country_workspace.cache.manager import cache_manager

cache_manager.prefix = f"cache-{worker_id}"
cache_manager.prefix = f"cache{worker_id}"
GroupFactory(name=config.NEW_USER_DEFAULT_GROUP)


Expand Down Expand Up @@ -152,8 +152,7 @@ def force_migrated_records(request, active_marks):
from country_workspace.versioning.management.manager import Manager

Manager().force_apply()
with vcr.VCR(record_mode=RecordMode.ONCE).use_cassette(Path(__file__).parent / "sync_all.yaml"):
SyncLog.objects.refresh()
_recorder.record(file_path=Path(__file__).parent / "r_sync_refresh.yaml")(lambda: SyncLog.objects.refresh())()


@pytest.fixture()
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/aurora/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def mock_vcr() -> vcr.VCR:
return vcr.VCR(
filter_headers=["authorization"],
cassette_library_dir=str(Path(__file__).parent.parent.parent / "extras/cassettes"),
record_mode=vcr.record_mode.RecordMode.ONCE,
record_mode=vcr.record_mode.RecordMode.NONE,
match_on=("path",),
)

Expand Down
Loading

0 comments on commit 6f6c93a

Please sign in to comment.