Skip to content

Commit

Permalink
FIx broken tests due to uncleared cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Guitlle committed Aug 29, 2024
1 parent 0360ca3 commit d0e8990
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
2 changes: 2 additions & 0 deletions kobo/apps/project_ownership/tests/api/v2/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from constance.test import override_config
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.utils import timezone
from mock import patch, MagicMock
from rest_framework import status
Expand Down Expand Up @@ -431,6 +432,7 @@ def test_account_usage_transferred_to_new_user(self):
assert response.status_code == status.HTTP_201_CREATED

# someuser should have no usage reported anymore
cache.clear()
response = self.client.get(service_usage_url)
assert response.data['total_nlp_usage'] == expected_empty_data['total_nlp_usage']
assert response.data['total_storage_bytes'] == expected_empty_data['total_storage_bytes']
Expand Down
9 changes: 2 additions & 7 deletions kpi/tests/api/v2/test_api_service_usage.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
from django.core.cache import cache
from django.urls import reverse
from rest_framework import status
from unittest.mock import patch

from kpi.models import Asset
from kpi.tests.test_usage_calculator import BaseServiceUsageTestCase


def clear_mock_cache(self):
self._clear_cache()


# We need to disable the cache system in some test cases
@patch('kpi.utils.cache.CachedClass._handle_cache_expiration', clear_mock_cache)
class ServiceUsageAPITestCase(BaseServiceUsageTestCase):
def test_anonymous_user(self):
"""
Expand Down Expand Up @@ -85,6 +79,7 @@ def test_service_usages_with_projects_in_trash_bin(self):
asset.deployment.xform.save(update_fields=['pending_delete'])

# Retry endpoint
cache.clear()
url = reverse(self._get_endpoint('service-usage-list'))
response = self.client.get(url)

Expand Down
2 changes: 2 additions & 0 deletions kpi/tests/test_usage_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.core.cache import cache
from django.test import override_settings
from django.urls import reverse
from django.utils import timezone
Expand Down Expand Up @@ -34,6 +35,7 @@ class BaseServiceUsageTestCase(BaseAssetTestCase):
def setUp(self):
super().setUp()
self.client.login(username='anotheruser', password='anotheruser')
cache.clear()

@classmethod
def setUpTestData(cls):
Expand Down
4 changes: 2 additions & 2 deletions kpi/utils/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
from functools import wraps

from django_redis import get_redis_connection
Expand Down Expand Up @@ -37,7 +36,8 @@ def wrapper(*args, **kwargs):

class CachedClass:
"""
Handles a mapping cache.
Handles a mapping cache for a class. It supports only getter methods that
receive no parameters but the self.
The inheriting class must define a function _get_cache_hash that creates a
unique name string for the underlying HSET.
Expand Down

0 comments on commit d0e8990

Please sign in to comment.