From e314d7978f3a81ec2eeba6430d9ad931b51f0c9d Mon Sep 17 00:00:00 2001 From: Stefano Apostolico Date: Fri, 29 Nov 2024 20:33:12 +0100 Subject: [PATCH] updates actions --- tests/workspace/test_ws_batch.py | 115 ++++++++++++++++++++++++++ tests/workspace/test_ws_household.py | 100 ++++++++++++++++++++++ tests/workspace/test_ws_individual.py | 79 ++++++++++++++++++ tests/workspace/test_ws_program.py | 94 +++++++++++++++++++++ 4 files changed, 388 insertions(+) create mode 100644 tests/workspace/test_ws_batch.py create mode 100644 tests/workspace/test_ws_household.py create mode 100644 tests/workspace/test_ws_individual.py create mode 100644 tests/workspace/test_ws_program.py diff --git a/tests/workspace/test_ws_batch.py b/tests/workspace/test_ws_batch.py new file mode 100644 index 0000000..4d04f21 --- /dev/null +++ b/tests/workspace/test_ws_batch.py @@ -0,0 +1,115 @@ +from typing import TYPE_CHECKING + +from django.urls import reverse + +import pytest +from responses import RequestsMock +from testutils.utils import select_office + +from country_workspace.state import state + +if TYPE_CHECKING: + from django_webtest.pytest_plugin import MixinWithInstanceVariables + from testutils.types import CWTestApp + + from country_workspace.workspaces.models import CountryBatch + +pytestmark = [pytest.mark.admin, pytest.mark.smoke, pytest.mark.django_db] + + +@pytest.fixture() +def office(): + from testutils.factories import OfficeFactory + + co = OfficeFactory() + state.tenant = co + yield co + + +@pytest.fixture() +def program(office, household_checker, individual_checker): + from testutils.factories import CountryProgramFactory + + return CountryProgramFactory( + household_checker=household_checker, + individual_checker=individual_checker, + household_columns="name\nid\nxx", + individual_columns="name\nid\nxx", + ) + + +@pytest.fixture() +def batch(program): + from testutils.factories import CountryHouseholdFactory + + hh = CountryHouseholdFactory(batch__program=program, batch__country_office=program.country_office) + return hh.batch + + +@pytest.fixture() +def app(django_app_factory: "MixinWithInstanceVariables", mocked_responses: "RequestsMock") -> "CWTestApp": + from testutils.factories import SuperUserFactory + + django_app = django_app_factory(csrf_checks=False) + admin_user = SuperUserFactory(username="superuser") + django_app.set_user(admin_user) + django_app._user = admin_user + yield django_app + + +def test_batch_changelist(app: "CWTestApp", batch: "CountryBatch") -> None: + url = reverse("workspace:workspaces_countrybatch_changelist") + with select_office(app, batch.program.country_office, batch.program): + res = app.get(url) + assert res.status_code == 200, res.location + assert f"Add {batch._meta.verbose_name}" not in res.text + res = app.get(url) + assert res.status_code == 200, res.location + + +def test_batch_change(app: "CWTestApp", batch: "CountryBatch") -> None: + url = reverse("workspace:workspaces_countrybatch_change", args=[batch.pk]) + with select_office(app, batch.program.country_office, batch.program): + res = app.get(url) + assert res.status_code == 200, res.location + assert f"Change {batch._meta.verbose_name}" in res.text + res = res.forms["countrybatch_form"].submit() + assert res.status_code == 302, res.location + + +# def test_hh_delete(app: "CWTestApp", household: "CountryHousehold") -> None: +# url = reverse("workspace:workspaces_countryhousehold_change", args=[household.pk]) +# res = app.get(url).follow() +# res.forms["select-tenant"]["tenant"] = household.country_office.pk +# res.forms["select-tenant"].submit() +# res = app.get(f"{url}?batch__program__exact={household.program.pk}") +# assert res.status_code == 200, res.location +# res = res.click("Delete") +# res = res.forms[1].submit().follow() +# assert res.status_code == 200 +# with pytest.raises(ObjectDoesNotExist): +# household.refresh_from_db() +# +# +# def test_hh_validate_single(app: "CWTestApp", household: "CountryHousehold") -> None: +# res = app.get("/").follow() +# res.forms["select-tenant"]["tenant"] = household.country_office.pk +# res.forms["select-tenant"].submit() +# with user_grant_permissions(app._user, ["workspaces.change_countryhousehold"], household.program): +# url = reverse("workspace:workspaces_countryhousehold_change", args=[household.pk]) +# res = app.get(f"{url}?batch__program__exact={household.program.pk}") +# res = res.click("Validate") +# res = res.follow() +# assert res.status_code == 200 +# +# +# def test_hh_validate_program(app: "CWTestApp", household: "CountryHousehold") -> None: +# res = app.get("/").follow() +# res.forms["select-tenant"]["tenant"] = household.country_office.pk +# res.forms["select-tenant"].submit() +# with user_grant_permissions(app._user, ["workspaces.change_countryhousehold"], household.program): +# url = reverse("workspace:workspaces_countryhousehold_changelist") +# res = app.get(f"{url}?batch__program__exact={household.program.pk}") +# res.click("Validate Programme").follow() +# household.refresh_from_db() +# assert household.last_checked diff --git a/tests/workspace/test_ws_household.py b/tests/workspace/test_ws_household.py new file mode 100644 index 0000000..1161fc4 --- /dev/null +++ b/tests/workspace/test_ws_household.py @@ -0,0 +1,100 @@ +from typing import TYPE_CHECKING + +from django.urls import reverse + +import pytest +from responses import RequestsMock +from testutils.perms import user_grant_permissions +from testutils.utils import select_office + +from country_workspace.state import state + +if TYPE_CHECKING: + from django_webtest.pytest_plugin import MixinWithInstanceVariables + from testutils.types import CWTestApp + + from country_workspace.workspaces.models import CountryHousehold, CountryProgram + +pytestmark = [pytest.mark.admin, pytest.mark.smoke, pytest.mark.django_db] + + +@pytest.fixture() +def office(): + from testutils.factories import OfficeFactory + + co = OfficeFactory() + state.tenant = co + yield co + + +@pytest.fixture() +def program(office, household_checker, individual_checker): + from testutils.factories import CountryProgramFactory + + return CountryProgramFactory( + household_checker=household_checker, + individual_checker=individual_checker, + household_columns="name\nid\nxx", + individual_columns="name\nid\nxx", + ) + + +@pytest.fixture() +def household(program): + from testutils.factories import CountryHouseholdFactory + + return CountryHouseholdFactory(batch__program=program, batch__country_office=program.country_office) + + +@pytest.fixture() +def app(django_app_factory: "MixinWithInstanceVariables", mocked_responses: "RequestsMock") -> "CWTestApp": + from testutils.factories import SuperUserFactory + + django_app = django_app_factory(csrf_checks=False) + admin_user = SuperUserFactory(username="superuser") + django_app.set_user(admin_user) + django_app._user = admin_user + yield django_app + + +def test_hh_changelist(app: "CWTestApp", household: "CountryHousehold") -> None: + url = reverse("workspace:workspaces_countryhousehold_changelist") + program: "CountryProgram" = household.program + with select_office(app, program.country_office, program): + + res = app.get(url) + assert res.status_code == 200, res.location + assert f"Add {household._meta.verbose_name}" not in res.text + # filter by program + res = app.get(url) + assert res.status_code == 200, res.location + + +def test_hh_change(app: "CWTestApp", household: "CountryHousehold") -> None: + url = reverse("workspace:workspaces_countryhousehold_change", args=[household.pk]) + program: "CountryProgram" = household.program + with select_office(app, program.country_office, program): + res = app.get(url) + assert res.status_code == 200, res.location + res = res.forms["countryhousehold_form"].submit() + assert res.status_code == 302, res.location + + +def test_hh_validate_single(app: "CWTestApp", household: "CountryHousehold") -> None: + with select_office(app, household.country_office, household.program): + with user_grant_permissions(app._user, ["workspaces.change_countryhousehold"], household.program): + url = reverse("workspace:workspaces_countryhousehold_change", args=[household.pk]) + res = app.get(url) + res = res.click("Validate") + res = res.follow() + assert res.status_code == 200 + + +def test_hh_validate_program(app: "CWTestApp", household: "CountryHousehold") -> None: + with select_office(app, household.country_office, household.program): + with user_grant_permissions(app._user, ["workspaces.change_countryhousehold"], household.program): + url = reverse("workspace:workspaces_countryhousehold_changelist") + res = app.get(url) + res.click("Validate Programme").follow() + household.refresh_from_db() + assert household.last_checked diff --git a/tests/workspace/test_ws_individual.py b/tests/workspace/test_ws_individual.py new file mode 100644 index 0000000..71aa012 --- /dev/null +++ b/tests/workspace/test_ws_individual.py @@ -0,0 +1,79 @@ +from typing import TYPE_CHECKING + +from django.urls import reverse + +import pytest +from responses import RequestsMock +from testutils.utils import select_office + +from country_workspace.state import state + +if TYPE_CHECKING: + from django_webtest import DjangoTestApp + from django_webtest.pytest_plugin import MixinWithInstanceVariables + + from country_workspace.workspaces.models import CountryIndividual + +pytestmark = [pytest.mark.admin, pytest.mark.smoke, pytest.mark.django_db] + + +@pytest.fixture() +def office(): + from testutils.factories import OfficeFactory + + co = OfficeFactory() + state.tenant = co + yield co + + +@pytest.fixture() +def program(office, household_checker, individual_checker): + from testutils.factories import CountryProgramFactory + + return CountryProgramFactory( + household_checker=household_checker, + individual_checker=individual_checker, + household_columns="name\nid\nxx", + individual_columns="name\nid\nxx", + ) + + +@pytest.fixture() +def individual(program): + from testutils.factories import CountryIndividualFactory + + return CountryIndividualFactory( + household__batch__program=program, household__batch__country_office=program.country_office + ) + + +@pytest.fixture() +def app(django_app_factory: "MixinWithInstanceVariables", mocked_responses: "RequestsMock") -> "DjangoTestApp": + from testutils.factories import SuperUserFactory + + django_app = django_app_factory(csrf_checks=False) + admin_user = SuperUserFactory(username="superuser") + django_app.set_user(admin_user) + django_app._user = admin_user + yield django_app + + +def test_ind_changelist(app: "DjangoTestApp", individual: "CountryIndividual") -> None: + url = reverse("workspace:workspaces_countryindividual_changelist") + with select_office(app, individual.country_office, individual.program): + res = app.get(url) + assert res.status_code == 200, res.location + assert f"Add {individual._meta.verbose_name}" not in res.text + # filter by program + res = app.get(url) + assert res.status_code == 200, res.location + + +def test_ind_change(app: "DjangoTestApp", individual: "CountryIndividual") -> None: + url = reverse("workspace:workspaces_countryindividual_changelist") + with select_office(app, individual.country_office, individual.program): + res = app.get(url) + res = res.click(individual.name) + assert res.status_code == 200, res.location + res = res.forms["countryindividual_form"].submit() + assert res.status_code == 302, res.location diff --git a/tests/workspace/test_ws_program.py b/tests/workspace/test_ws_program.py new file mode 100644 index 0000000..bc2937b --- /dev/null +++ b/tests/workspace/test_ws_program.py @@ -0,0 +1,94 @@ +from typing import TYPE_CHECKING + +from django.urls import reverse + +import pytest +from pytest_django.fixtures import SettingsWrapper +from responses import RequestsMock +from testutils.utils import select_office + +from country_workspace.state import state +from country_workspace.workspaces.models import CountryHousehold, CountryProgram + +if TYPE_CHECKING: + from django_webtest import DjangoTestApp + from django_webtest.pytest_plugin import MixinWithInstanceVariables + + +pytestmark = [pytest.mark.admin, pytest.mark.smoke, pytest.mark.django_db] + + +@pytest.fixture() +def office(): + from testutils.factories import OfficeFactory + + co = OfficeFactory() + state.tenant = co + yield co + + +@pytest.fixture() +def program(office): + from testutils.factories import CountryProgramFactory, DataCheckerFactory + + return CountryProgramFactory( + country_office=office, + household_checker=DataCheckerFactory(fields=["collect_individual_data"]), + individual_checker=DataCheckerFactory(fields=["gender"]), + household_columns="__str__\nid\nxx", + individual_columns="__str__\nid\nxx", + ) + + +@pytest.fixture() +def household(program): + from testutils.factories import CountryHouseholdFactory + + return CountryHouseholdFactory(batch__program=program, batch__country_office=program.country_office) + + +@pytest.fixture() +def app( + django_app_factory: "MixinWithInstanceVariables", + mocked_responses: "RequestsMock", + settings: SettingsWrapper, +) -> "DjangoTestApp": + from testutils.factories import SuperUserFactory + + django_app = django_app_factory(csrf_checks=False) + admin_user = SuperUserFactory(username="superuser") + django_app.set_user(admin_user) + django_app._user = admin_user + yield django_app + + +def test_configure_hh_columns(app, household: "CountryHousehold"): + program: "CountryProgram" = household.program + with select_office(app, program.country_office, program): + res = app.get(program.get_change_url()) + res = res.click("Household Columns") + form = res.forms["configure-columns"] + form["columns"] = ["name", "flex_fields__collect_individual_data"] + form.submit().follow() + program.refresh_from_db() + assert program.household_columns == "name\nflex_fields__collect_individual_data" + hh_list = reverse("workspace:workspaces_countryhousehold_changelist") + res = app.get(hh_list) + assert not res.pyquery("div.text a:contains('flex_fields__collect_individual_data')") + assert res.pyquery("div.text a:contains('Collect_individual_data')") + # assert "collect_individual_data" in res.text + + +def test_configure_ind_columns(app, household: "CountryHousehold"): + program: "CountryProgram" = household.program + with select_office(app, program.country_office, program): + res = app.get(program.get_change_url()) + res = res.click("Individual Columns") + form = res.forms["configure-columns"] + form["columns"] = ["name", "flex_fields__gender"] + form.submit().follow() + program.refresh_from_db() + assert program.individual_columns == "name\nflex_fields__gender" + hh_list = reverse("workspace:workspaces_countryindividual_changelist") + res = app.get(hh_list) + assert "gender" in res.text