|
1 | 1 | """Core API tests.""" |
| 2 | + |
2 | 3 | import uuid |
| 4 | +from unittest.mock import MagicMock, patch |
3 | 5 |
|
4 | 6 | from django.contrib.admin.models import LogEntry |
5 | 7 | from django.contrib.contenttypes.models import ContentType |
| 8 | +from django.core.cache import caches |
| 9 | +from django.test import TestCase, override_settings |
| 10 | +from django.urls import reverse |
6 | 11 |
|
7 | 12 | import pytest |
8 | 13 | from rest_framework import status |
| 14 | +from rest_framework.test import APIClient |
9 | 15 |
|
| 16 | +from tdpservice.conftest import UserFactory |
| 17 | +from tdpservice.core.models import FeatureFlag |
| 18 | +from tdpservice.core.views import FeatureFlagViewset |
10 | 19 | from tdpservice.data_files.models import DataFile |
11 | 20 |
|
12 | 21 |
|
@@ -78,3 +87,119 @@ def test_log_entry_creation(api_client, data_file_instance): |
78 | 87 | content_type_id=ContentType.objects.get_for_model(DataFile).pk, |
79 | 88 | object_id=data_file_instance.pk, |
80 | 89 | ).exists() |
| 90 | + |
| 91 | + |
| 92 | +@override_settings( |
| 93 | + CACHES={ |
| 94 | + "feature-flags": { |
| 95 | + "BACKEND": "django.core.cache.backends.locmem.LocMemCache", |
| 96 | + "LOCATION": "unique-test-cache-location", # Unique location to avoid conflicts |
| 97 | + "KEY_PREFIX": "test", |
| 98 | + }, |
| 99 | + } |
| 100 | +) |
| 101 | +class TestFeatureFlagViewset(TestCase): |
| 102 | + """Tests for the FeatureFlagViewset class.""" |
| 103 | + |
| 104 | + api_client = APIClient() |
| 105 | + |
| 106 | + def setUp(self): |
| 107 | + """Run before all tests in TestCase.""" |
| 108 | + super().setUp() |
| 109 | + cache = caches["feature-flags"] |
| 110 | + cache.clear() |
| 111 | + |
| 112 | + user = UserFactory.create() |
| 113 | + self.api_client.login(username=user.username, password="test_password") |
| 114 | + |
| 115 | + def test_existing_list_cache_avoids_lookup(self): |
| 116 | + """Test that no lookup is performed if flags exist in the cache.""" |
| 117 | + mock_queryset = MagicMock() |
| 118 | + with patch.object( |
| 119 | + FeatureFlagViewset, "get_queryset", return_value=mock_queryset |
| 120 | + ) as mock_method: |
| 121 | + # request and check the cache was cold |
| 122 | + response = self.api_client.get(reverse("feature-flag-list")) |
| 123 | + assert response.status_code == status.HTTP_200_OK |
| 124 | + assert mock_method.called |
| 125 | + |
| 126 | + mock_method.reset_mock() |
| 127 | + |
| 128 | + # the cache should be warm now, request again |
| 129 | + response = self.api_client.get(reverse("feature-flag-list")) |
| 130 | + assert response.status_code == status.HTTP_200_OK |
| 131 | + assert not mock_method.called |
| 132 | + |
| 133 | + def test_no_list_cache_forces_lookup(self): |
| 134 | + """Test that a lookup is performed if there are no flags in the cache.""" |
| 135 | + mock_queryset = MagicMock() |
| 136 | + with patch.object( |
| 137 | + FeatureFlagViewset, "get_queryset", return_value=mock_queryset |
| 138 | + ) as mock_method: |
| 139 | + # request and check the cache was cold |
| 140 | + response = self.api_client.get(reverse("feature-flag-list")) |
| 141 | + assert response.status_code == status.HTTP_200_OK |
| 142 | + assert mock_method.called |
| 143 | + |
| 144 | + def test_saving_flag_invalidates_cache(self): |
| 145 | + """Test saving a feature flag invalidates existing cache.""" |
| 146 | + mock_queryset = MagicMock() |
| 147 | + with patch.object( |
| 148 | + FeatureFlagViewset, "get_queryset", return_value=mock_queryset |
| 149 | + ) as mock_method: |
| 150 | + # request and check the cache was cold |
| 151 | + response = self.api_client.get(reverse("feature-flag-list")) |
| 152 | + assert response.status_code == status.HTTP_200_OK |
| 153 | + assert mock_method.called |
| 154 | + |
| 155 | + mock_method.reset_mock() |
| 156 | + |
| 157 | + # the cache should be warm now, request again |
| 158 | + response = self.api_client.get(reverse("feature-flag-list")) |
| 159 | + assert response.status_code == status.HTTP_200_OK |
| 160 | + assert not mock_method.called |
| 161 | + |
| 162 | + mock_method.reset_mock() |
| 163 | + |
| 164 | + # create a new feature flag |
| 165 | + FeatureFlag.objects.create(feature_name="unit-test") |
| 166 | + |
| 167 | + # check that the cache was invalidated |
| 168 | + response = self.api_client.get(reverse("feature-flag-list")) |
| 169 | + assert response.status_code == status.HTTP_200_OK |
| 170 | + assert mock_method.called |
| 171 | + |
| 172 | + def test_existing_single_cache_avoids_lookup(self): |
| 173 | + """Test that no lookup is performed if flags exist in the cache.""" |
| 174 | + FeatureFlag.objects.create(feature_name="test1") |
| 175 | + with patch.object( |
| 176 | + FeatureFlagViewset, "get_queryset", return_value=FeatureFlag.objects.all() |
| 177 | + ) as mock_method: |
| 178 | + # request and check the cache was cold |
| 179 | + response = self.api_client.get( |
| 180 | + reverse("feature-flag-detail", args=("test1",)) |
| 181 | + ) |
| 182 | + assert response.status_code == status.HTTP_200_OK |
| 183 | + assert mock_method.called |
| 184 | + |
| 185 | + mock_method.reset_mock() |
| 186 | + |
| 187 | + # the cache should be warm now, request again |
| 188 | + response = self.api_client.get( |
| 189 | + reverse("feature-flag-detail", args=("test1",)) |
| 190 | + ) |
| 191 | + assert response.status_code == status.HTTP_200_OK |
| 192 | + assert not mock_method.called |
| 193 | + |
| 194 | + def test_no_single_cache_forces_lookup(self): |
| 195 | + """Test that a lookup is performed if there are no flags in the cache.""" |
| 196 | + FeatureFlag.objects.create(feature_name="test2") |
| 197 | + with patch.object( |
| 198 | + FeatureFlagViewset, "get_queryset", return_value=FeatureFlag.objects.all() |
| 199 | + ) as mock_method: |
| 200 | + # request and check the cache was cold |
| 201 | + response = self.api_client.get( |
| 202 | + reverse("feature-flag-detail", args=("test2",)) |
| 203 | + ) |
| 204 | + assert response.status_code == status.HTTP_200_OK |
| 205 | + assert mock_method.called |
0 commit comments