Skip to content

Commit

Permalink
updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saxix committed Oct 22, 2024
1 parent aa83385 commit c62c9d8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 52 deletions.
13 changes: 1 addition & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,7 @@ jobs:
-v "./tests:/app/tests" \
-v "./pytest.ini:/app/pytest.ini" \
-t "unicef/hope-country-workspace:${{env.BRANCH}}-test-${{env.checksum}}" \
pytest -n auto tests/ -v --maxfail=5 --migrations --cov --cov-report xml:output/coverage.xml
docker run --rm \
--network host \
-e PYTHONPATH=/app/src \
-e DATABASE_URL=${DATABASE_URL} \
-v "./output/:/app/output" \
-v "./src/:/app/src" \
-v "./tests:/app/tests" \
-v "./pytest.ini:/app/pytest.ini" \
-t "unicef/hope-country-workspace:${{env.BRANCH}}-test-${{env.checksum}}" \
pytest tests/ -v -m selenium --cov --cov-append --cov-report xml:output/coverage.xml
pytest tests/ --selenium -n auto -v --maxfail=5 --migrations --cov-report xml:./output/coverage.xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
Expand Down
1 change: 0 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ addopts =
--cov country_workspace
--cov-config=tests/.coveragerc
--cov-report html
--cov-report xml:coverage.xml


markers =
Expand Down
18 changes: 9 additions & 9 deletions src/country_workspace/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def configure(self, **kwargs: "Dict[str,Any]") -> "Iterator[None]":
for k, v in pre.items():
setattr(self, k, v)

@contextlib.contextmanager
def activate_tenant(self, country_office: "Office") -> "Iterator[None]":
_country_office = self.tenant
_tenant_cookie = self.tenant_cookie
self.tenant = country_office
self.tenant_cookie = country_office.slug
yield
self.tenant = _country_office
self.tenant_cookie = _tenant_cookie
# @contextlib.contextmanager
# def activate_tenant(self, country_office: "Office") -> "Iterator[None]":
# _country_office = self.tenant
# _tenant_cookie = self.tenant_cookie
# self.tenant = country_office
# self.tenant_cookie = country_office.slug
# yield
# self.tenant = _country_office
# self.tenant_cookie = _tenant_cookie

@contextlib.contextmanager
def set(self, **kwargs: "Dict[str,Any]") -> "Iterator[None]":
Expand Down
5 changes: 0 additions & 5 deletions src/country_workspace/utils/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ def debug(value: str, **kwargs: "Any") -> bool:
return settings.DEBUG == parse_bool(value)


@conditions.register("Server IP")
def server_ip(value: str, request: "HttpRequest|None", **kwargs: "Any") -> bool:
return request.META.get("REMOTE_ADDR", "-1") in value.split(",") #


@conditions.register("hostname")
def hostname(value: str, request: "HttpRequest|None", **kwargs: "Any") -> bool:
return request.get_host().split(":")[0] in value.split(",")
Expand Down
7 changes: 1 addition & 6 deletions src/country_workspace/workspaces/admin/batch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from django.db.models import QuerySet
from django.http import HttpRequest
Expand Down Expand Up @@ -42,11 +42,6 @@ def get_queryset(self, request: HttpRequest) -> "QuerySet[CountryBatch]":
else:
return qs.none()

def get_changelist(self, request: HttpRequest, **kwargs: Any) -> type:
from ..changelist import WorkspaceChangeList

return WorkspaceChangeList

def has_add_permission(self, request, obj=None):
return False

Expand Down
14 changes: 13 additions & 1 deletion tests/test_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from country_workspace.state import state
from country_workspace.utils.flags import client_ip, env_var, header_key
from country_workspace.utils.flags import client_ip, debug, env_var, header_key, hostname


@pytest.mark.parametrize(
Expand Down Expand Up @@ -55,3 +55,15 @@ def test_header_key(rf: "RequestFactory", value: str, result: str) -> None:
request = rf.get("/", HTTP_CUSTOM_KEY="123")
with state.configure(request=request):
assert header_key(value) == result


@pytest.mark.parametrize("value, result", [("localhost", True)])
def test_hostname(rf: "RequestFactory", value: str, result: str) -> None:

request = rf.get("/", HTTP_HOST=value)
with state.configure(request=request):
assert hostname(value, request=request)


def test_debug(rf: "RequestFactory") -> None:
assert debug(False)
27 changes: 9 additions & 18 deletions tests/workspace/test_ws_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,23 @@ def app(django_app_factory: "MixinWithInstanceVariables", mocked_responses: "Req
def test_batch_changelist(app: "CWTestApp", batch: "CountryBatch") -> None:
url = reverse("workspace:workspaces_countrybatch_changelist")
with select_office(app, batch.program.country_office):
# res = app.get(url).follow()
# res.forms["select-tenant"]["tenant"] = household.country_office.pk
# res.forms["select-tenant"].submit()
res = app.get(url)
assert res.status_code == 200, res.location
assert f"Add {batch._meta.verbose_name}" not in res.text
# filter by program
res = app.get(f"{url}?program__exact={batch.program.pk}")
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):
res = app.get(f"{url}?batch__program__exact={batch.program.pk}")
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_change(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
# assert f"Change {household._meta.verbose_name}" in res.text
# res = res.forms["countryhousehold_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()
Expand Down

0 comments on commit c62c9d8

Please sign in to comment.