Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IA-2986: Have "sub org unit types to display" by default #1346

Merged
merged 7 commits into from
Jun 13, 2024
Merged
45 changes: 45 additions & 0 deletions iaso/tests/gpkg/test_import_with_sub_org_unit_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from iaso.gpkg.import_gpkg import import_gpkg_file
from iaso.models import Account, Project
from iaso.test import APITestCase


class OrgUnitImportFromGPKG(APITestCase):
@classmethod
def setUpTestData(cls):
cls.account = Account.objects.create(name="a")
cls.user_test = cls.create_user_with_profile(username="test", account=cls.account)
cls.project = Project.objects.create(name="Project 1", account=cls.account, app_id="test_app_id")

def test_minimal_import_with_sub_org_unit_type(self):
import_gpkg_file(
"./iaso/tests/fixtures/gpkg/minimal.gpkg",
project_id=self.project.id,
source_name="test",
version_number=1,
validation_status="new",
description="",
)
self.client.force_authenticate(self.user_test)
response = self.client.get("/api/v2/orgunittypes/")
org_unit_types = response.json()["orgUnitTypes"]

for org_unit_type in org_unit_types:
current_level = org_unit_type["depth"]
current_id = org_unit_type["id"]
sub_unit_type_ids = [
org_unit_type["id"] for org_unit_type in org_unit_types if org_unit_type["depth"] == (current_level + 1)
]

if len(sub_unit_type_ids) > 0:
current_type = {
"name": org_unit_type["name"],
"short_name": org_unit_type["short_name"],
"project_ids": [self.project.id],
"sub_unit_type_ids": sub_unit_type_ids,
}
response = self.client.patch(f"/api/v2/orgunittypes/{current_id}/", data=current_type, format="json")
self.assertJSONResponse(response, 200)
self.assertHasField(response.json(), "sub_unit_types", list)
new_sub_units = response.json()["sub_unit_types"]
new_sub_unit_ids = [sub_unit_type["id"] for sub_unit_type in new_sub_units]
self.assertEqual(new_sub_unit_ids, sub_unit_type_ids)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test supposed to cover setuper.pyramid.update_org_unit_sub_type?

I'm asking because the logic seems replicated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, It's supposed to cover setuper.pyramid.update_org_unit_sub_type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

In fact, the test shouldn't re-implement the logic. Otherwise, if you change the function, the test will continue to work.

So, it would be better if setuper.pyramid.update_org_unit_sub_type could return something that you can test to ensure that it's doing what you want.

25 changes: 24 additions & 1 deletion setuper/pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def setup_orgunits(account_name, iaso_client):
test_file = "data/small_sample.gpkg"
geopackage_file = {"file": (test_file, open(test_file, "rb"), "application/octet-stream")}
task = iaso_client.post("/api/tasks/create/importgpkg/", files=geopackage_file, data=data)

print("-- Importing org units")

iaso_client.wait_task_completion(task)
Expand All @@ -26,5 +27,27 @@ def setup_orgunits(account_name, iaso_client):
"searches": [{"validation_status": "all", "color": "f4511e", "source": data_source_id}],
}
task = iaso_client.post("/api/tasks/create/orgunitsbulkupdate/", json=data)

update_org_unit_sub_type(iaso_client, project_id)
iaso_client.wait_task_completion(task)


def update_org_unit_sub_type(iaso_client, project_id):
print("-- Updating org unit sub type")
org_unit_types = iaso_client.get("/api/v2/orgunittypes/")["orgUnitTypes"]
for org_unit_type in org_unit_types:
org_unit_type_level = org_unit_type["depth"]
org_unit_type_id = org_unit_type["id"]
sub_unit_type_ids = [
org_unit_type["id"]
for org_unit_type in org_unit_types
if org_unit_type["depth"] == (org_unit_type_level + 1)
]
if len(sub_unit_type_ids) > 0:
current_type = {
"name": org_unit_type["name"],
"short_name": org_unit_type["short_name"],
"project_ids": [project_id],
"sub_unit_type_ids": sub_unit_type_ids,
}
# Updating default sub type
iaso_client.patch(f"/api/v2/orgunittypes/{org_unit_type_id}/", json=current_type)
18 changes: 10 additions & 8 deletions setuper/review_change_proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def setup_review_change_proposal(account_name, iaso_client):
"status": status,
}
validation = None
proposal_review = None
if len(groups) > 0:
data["new_groups"] = groups
approved_fields.append("new_groups")
Expand All @@ -64,11 +65,12 @@ def setup_review_change_proposal(account_name, iaso_client):
if len(new_reference_instances) > 0:
data["new_reference_instances"] = new_reference_instances
approved_fields.append("new_reference_instances")
proposal_review = iaso_client.post("/api/orgunits/changes/", json=data)
if status == "approved" or status == "rejected":
validation = {
"approved_fields": approved_fields,
"status": status,
"rejection_comment": status,
}
iaso_client.patch(f"/api/orgunits/changes/{proposal_review['id']}/", json=validation)
if len(approved_fields) > 0:
proposal_review = iaso_client.post("/api/orgunits/changes/", json=data)
if status == "approved" or status == "rejected":
validation = {
"approved_fields": approved_fields,
"status": status,
"rejection_comment": status,
}
iaso_client.patch(f"/api/orgunits/changes/{proposal_review['id']}/", json=validation)
Loading