Skip to content

Commit 5c3038f

Browse files
committed
add tests
1 parent c20911e commit 5c3038f

File tree

16 files changed

+151
-72
lines changed

16 files changed

+151
-72
lines changed

src/country_workspace/models/household.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
from .program import Program
1717

1818

19-
@pghistory.track(
20-
pghistory.UpdateEvent("updates", condition=pghistory.AnyChange("flex_fields", "flex_files", "removed"))
21-
)
19+
@pghistory.track(pghistory.UpdateEvent(condition=pghistory.AnyChange("flex_fields", "flex_files", "removed")))
2220
class Household(Validable, BaseModel):
2321
system_fields = models.JSONField(default=dict, blank=True)
2422
members: "QuerySet[Individual]"

src/country_workspace/models/individual.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
from hope_flex_fields.models import DataChecker
1212

1313

14-
@pghistory.track(
15-
pghistory.UpdateEvent("updates", condition=pghistory.AnyChange("flex_fields", "flex_files", "removed"))
16-
)
14+
@pghistory.track(pghistory.UpdateEvent(condition=pghistory.AnyChange("flex_fields", "flex_files", "removed")))
1715
class Individual(Validable, BaseModel):
1816
household = models.ForeignKey(Household, on_delete=models.CASCADE, null=True, blank=True, related_name="members")
1917
system_fields = models.JSONField(default=dict, blank=True)

src/country_workspace/workspaces/admin/hh_ind.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -234,29 +234,30 @@ def history_view(
234234
self, request: HttpRequest, object_id: str, extra_context: dict[str, Any] | None = None
235235
) -> TemplateResponse:
236236
obj = self.get_object(request, unquote(object_id))
237-
etag = cache_manager.build_key_from_request(request, "history", obj.last_modified)
238-
if response := cache_manager.retrieve(etag):
239-
return response
240-
history = []
241-
prev = {}
242-
field_names = [f.name for __, f in obj.checker.get_fields()]
243-
for entry in obj.events.select_related("pgh_context").all():
244-
changes = {}
245-
for field_name in field_names:
246-
old_value = prev.get(field_name, "")
247-
new_value = entry.flex_fields.get(field_name, "")
248-
if old_value != new_value:
249-
changes[field_name] = {"from": old_value, "to": new_value}
250-
history.append(
251-
{
252-
"changes": changes,
253-
"date": entry.pgh_created_at,
254-
"pgh_label": entry.pgh_label,
255-
"user": entry.pgh_context.metadata["user"],
256-
}
257-
)
258-
prev = entry.flex_fields
259-
history.reverse()
237+
key = cache_manager.build_key_from_request(request, "history", obj.pk, obj.version)
238+
if not (history := cache_manager.retrieve(key)):
239+
history = []
240+
prev = {}
241+
field_names = [f.name for __, f in obj.checker.get_fields()]
242+
for entry in obj.events.select_related("pgh_context").all():
243+
changes = {}
244+
for field_name in field_names:
245+
old_value = prev.get(field_name, "")
246+
new_value = entry.flex_fields.get(field_name, "")
247+
if old_value != new_value:
248+
changes[field_name] = {"from": old_value, "to": new_value}
249+
history.append(
250+
{
251+
"changes": changes,
252+
"date": entry.pgh_created_at,
253+
"pgh_label": entry.pgh_label,
254+
"user": entry.pgh_context.metadata["user"],
255+
}
256+
)
257+
prev = entry.flex_fields
258+
history.reverse()
259+
cache_manager.store(key, history)
260+
260261
context = {
261262
**self.admin_site.each_context(request),
262263
"modeladmin": self,
@@ -273,9 +274,5 @@ def history_view(
273274
"preserved_filters": self.get_preserved_filters(request),
274275
**(extra_context or {}),
275276
}
276-
return TemplateResponse(
277-
request,
278-
self.object_history_template,
279-
context,
280-
headers={"Etag": etag},
281-
)
277+
278+
return TemplateResponse(request, self.object_history_template, context)

tests/cache/test_cache_base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pytest
44
from django.core.cache import cache
55
from django.urls import reverse
6-
76
from testutils.perms import user_grant_permissions
87
from testutils.utils import select_office
98

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
import factory
77
import pytest
88
import responses
9-
10-
from django.core.files.storage.base import Storage
119
from django.core.files.base import ContentFile
10+
from django.core.files.storage.base import Storage
1211

1312
if TYPE_CHECKING:
1413
from country_workspace.models import User
@@ -62,6 +61,7 @@ def mock_storage():
6261
@pytest.fixture(autouse=True)
6362
def patch_asyncjob(mock_storage):
6463
from django.db import models
64+
6565
from country_workspace.models.jobs import AsyncJob
6666

6767
AsyncJob._meta.get_field("file").storage = mock_storage

tests/contrib/aurora/test_aurora_client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import re
22
from collections.abc import Callable
33

4-
54
import pytest
65
import requests
76
import responses

tests/contrib/hope/test_hope_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
import re
33
from collections.abc import Callable
44
from unittest.mock import Mock
5-
from constance.test import override_config
65

76
import pytest
87
import requests
98
import responses
9+
from constance.test import override_config
1010

1111
from country_workspace.contrib.hope.client import HopeClient
1212
from country_workspace.exceptions import RemoteError
1313

1414

1515
@pytest.fixture
1616
def mock_signals():
17-
from country_workspace.contrib.hope.client import hope_request_start, hope_request_end
17+
from country_workspace.contrib.hope.client import hope_request_end, hope_request_start
1818

1919
start_mock = Mock()
2020
end_mock = Mock()

tests/contrib/hope/test_push_to_hope_core.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import pytest
1+
from json import JSONDecodeError
22

3-
from pytest_mock import MockerFixture
3+
import pytest
44
from django.db import DatabaseError
5+
from pytest_mock import MockerFixture
6+
from requests.exceptions import RequestException
57

8+
from country_workspace.contrib.hope.push import PushProcessor, map_fields, push_to_hope_core
9+
from country_workspace.exceptions import RemoteError
10+
from country_workspace.models import Household, Individual
611
from country_workspace.state import state
7-
from country_workspace.contrib.hope.push import PushProcessor, push_to_hope_core, map_fields
8-
from country_workspace.models import Individual, Household
9-
1012
from country_workspace.workspaces.models import CountryHousehold
11-
from requests.exceptions import RequestException
12-
from json import JSONDecodeError
13-
from country_workspace.exceptions import RemoteError
1413

1514

1615
@pytest.fixture

tests/datasources/test_rdi.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
from pytest_mock import MockerFixture
66

77
from country_workspace.datasources.rdi import (
8-
normalize_row,
9-
get_value,
108
ColumnConfigurationError,
11-
SheetProcessingError,
12-
MissingHouseholdError,
9+
Config,
1310
HouseholdValidationError,
11+
MissingHouseholdError,
12+
Row,
13+
Sheet,
14+
SheetProcessingError,
1415
filter_rows_with_household_pk,
16+
get_value,
17+
import_from_rdi,
18+
normalize_row,
1519
process_households,
1620
process_individuals,
1721
validate_households,
18-
import_from_rdi,
19-
Config,
20-
Sheet,
21-
Row,
2222
)
2323
from country_workspace.models import Household
2424

tests/extras/testutils/factories/program.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import factory
22

3-
from country_workspace.models import Program, BeneficiaryGroup
3+
from country_workspace.models import BeneficiaryGroup, Program
44
from country_workspace.workspaces.models import CountryProgram
55

66
from .base import AutoRegisterModelFactory

0 commit comments

Comments
 (0)