Skip to content

Commit 4b90759

Browse files
chg ! tests & rebase
1 parent 16dc101 commit 4b90759

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

src/country_workspace/migrations/0005_alter_individual_options.py renamed to src/country_workspace/migrations/0006_alter_individual_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class Migration(migrations.Migration):
77
dependencies = [
8-
("country_workspace", "0004_kobosubmission_delete_koboasset"),
8+
("country_workspace", "0005_alter_asyncjob_file"),
99
]
1010

1111
operations = [

tests/admin/test_admin_cleaners.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def household():
1515

1616

1717
def test_validate_program(household):
18-
assert validate_program(Mock(program=household.program)) == {"invalid": 0, "total": 0, "valid": 1}
18+
assert validate_program(Mock(program=household.program)) == {"invalid": 0, "valid": 1}
1919
with mock.patch(
2020
"country_workspace.workspaces.admin.cleaners.validate.Household.validate_with_checker", return_value=False
2121
):
22-
assert validate_program(Mock(program=household.program)) == {"invalid": 1, "total": 0, "valid": 0}
22+
assert validate_program(Mock(program=household.program)) == {"invalid": 1, "valid": 0}

tests/conftest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import pytest
88
import responses
99

10+
from django.core.files.storage.base import Storage
11+
from django.core.files.base import ContentFile
12+
1013
if TYPE_CHECKING:
1114
from country_workspace.models import User
1215

@@ -34,6 +37,37 @@ def pytest_addoption(parser):
3437
)
3538

3639

40+
class MockStorage(Storage):
41+
def __init__(self):
42+
self._file_content = None
43+
44+
def save(self, name, content, max_length=None):
45+
self._file_content = content.read() if hasattr(content, "read") else content
46+
return "mocked/path/to/file"
47+
48+
def get_available_name(self, name, max_length=None):
49+
return "mocked/path/to/file"
50+
51+
def open(self, name, mode="rb"):
52+
if self._file_content is not None:
53+
return ContentFile(self._file_content)
54+
return ContentFile(b"id,name\n1,Test")
55+
56+
57+
@pytest.fixture
58+
def mock_storage():
59+
return MockStorage()
60+
61+
62+
@pytest.fixture(autouse=True)
63+
def patch_asyncjob(mock_storage):
64+
from django.db import models
65+
from country_workspace.models.jobs import AsyncJob
66+
67+
AsyncJob._meta.get_field("file").storage = mock_storage
68+
AsyncJob._meta.get_field("file").__class__ = models.FileField
69+
70+
3771
def pytest_configure(config):
3872
if not config.option.enable_selenium and ("selenium" not in getattr(config.option, "markexpr", None)):
3973
if config.option.markexpr:

tests/workspace/actions/test_ws_bulk.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from country_workspace.state import state
1414
from country_workspace.workspaces.admin.cleaners.bulk_update import TYPES, create_xls_importer
1515
from tests.workspace.actions import stub
16+
from webtest import Upload
17+
1618

1719
if TYPE_CHECKING:
1820
from django_webtest import DjangoTestApp
@@ -157,12 +159,12 @@ def test_bulk_update_export(
157159
force_migrated_records,
158160
settings: "SettingsWrapper",
159161
household: "CountryHousehold",
160-
mock_media_storage,
162+
mock_storage,
161163
) -> None:
162-
with mock.patch("country_workspace.workspaces.admin.cleaners.bulk_update.MEDIA_STORAGE", mock_media_storage):
163-
url = reverse("workspace:workspaces_countryindividual_changelist")
164-
settings.CELERY_TASK_ALWAYS_EAGER = True
165-
selected_fields = stub.header_add["ind"]
164+
url = reverse("workspace:workspaces_countryindividual_changelist")
165+
settings.CELERY_TASK_ALWAYS_EAGER = True
166+
selected_fields = stub.header_add["ind"]
167+
with mock.patch("country_workspace.workspaces.admin.cleaners.bulk_update.MEDIA_STORAGE", mock_storage):
166168
with select_office(app, household.country_office, household.program):
167169
res = app.get(url)
168170
form = res.forms["changelist-form"]
@@ -180,3 +182,35 @@ def test_bulk_update_export(
180182
assert res.status_code == 302
181183
job: AsyncJob = household.program.jobs.first()
182184
job.queue()
185+
186+
187+
def test_bulk_update_import(
188+
app: "DjangoTestApp",
189+
force_migrated_records,
190+
settings: "SettingsWrapper",
191+
household: "CountryHousehold",
192+
data: tuple[io.BytesIO, "CountryHousehold", str],
193+
) -> None:
194+
buff, household, target = data
195+
url = reverse("workspace:workspaces_countryprogram_change", args=[household.program.pk])
196+
settings.CELERY_TASK_ALWAYS_EAGER = True
197+
198+
with select_office(app, household.country_office, household.program):
199+
res = app.get(url)
200+
res = res.click("Update Records")
201+
res.forms["bulk-update-form"]["description"] = f"Bulk update from {target}"
202+
res.forms["bulk-update-form"]["target"] = target
203+
res.forms["bulk-update-form"]["file"] = Upload(f"{target}.xlsx", buff.read())
204+
res = res.forms["bulk-update-form"].submit("_import")
205+
household.refresh_from_db()
206+
job: AsyncJob = household.program.jobs.first()
207+
208+
assert res.status_code == 302
209+
assert job
210+
211+
if target == "hh":
212+
admin1_v = f"admin1_{stub.header_add['hh'].index('admin1') + 2}"
213+
assert household.flex_fields.get("admin1") == admin1_v
214+
elif target == "ind":
215+
given_name = f"given_name_{stub.header_add['ind'].index('given_name') + 2}"
216+
assert household.members.filter(flex_fields__given_name=given_name).exists()

0 commit comments

Comments
 (0)