-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(backend): submission routes / different formats (#1917)
* feat(tests): add submission fixture for ODK Central project submissions * feat(tests): add tests for getting submissions * fix(tests): improve submission fixture * fix(tests): improve submission fixture * feat(tests): add tests for downloading submissions as JSON and ZIP file * feat(tests): get submission count test * feat(tests): add test for downloading submissions as GeoJSON * feat(tests): refactor submission handling to use pyodk client for ODK submissions * feat(tests): add ODK configuration file and refactor submission handling
- Loading branch information
1 parent
4351665
commit d5bae01
Showing
3 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[central] | ||
base_url = "https://proxy" | ||
username = "[email protected]" | ||
password = "Password1234" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# Copyright (c) 2022, 2023 Humanitarian OpenStreetMap Team | ||
# | ||
# This file is part of FMTM. | ||
# | ||
# FMTM is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# FMTM is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with FMTM. If not, see <https:#www.gnu.org/licenses/>. | ||
# | ||
"""Tests for submission routes.""" | ||
|
||
import json | ||
|
||
import pytest | ||
|
||
|
||
async def test_read_submissions(client, submission): | ||
"""Test get submissions with a single submission expected.""" | ||
odk_project = submission["project"] | ||
submission_data = submission["submission_data"] | ||
|
||
response = await client.get(f"/submission?project_id={odk_project.id}") | ||
assert response.status_code == 200, f"Failed to fetch submissions: {response.text}" | ||
|
||
submission_list = response.json() | ||
assert isinstance(submission_list, list), "Expected a list of submissions" | ||
|
||
first_submission = submission_list[0] | ||
test_instance_id = submission_data.instanceId | ||
assert first_submission["__id"] == test_instance_id, "Instance ID mismatch" | ||
assert ( | ||
first_submission["meta"]["instanceID"] == test_instance_id | ||
), "Meta instanceID mismatch" | ||
assert first_submission["__system"]["submitterId"] == str( | ||
submission_data.submitterId | ||
), "Submitter ID mismatch" | ||
|
||
|
||
async def test_download_submission_json(client, submission): | ||
"""Test downloading submissions as JSON.""" | ||
odk_project = submission["project"] | ||
|
||
response = await client.get( | ||
f"/submission/download?project_id={odk_project.id}&export_json=true" | ||
) | ||
|
||
assert response.status_code == 200, ( | ||
f"Failed to download JSON submissions. " f"Response: {response.text}" | ||
) | ||
assert ( | ||
"Content-Disposition" in response.headers | ||
), "Missing Content-Disposition header" | ||
|
||
expected_filename = f"{odk_project.slug}_submissions.json" | ||
|
||
assert response.headers["Content-Disposition"].endswith( | ||
expected_filename | ||
), f"Expected file name to end with {expected_filename}" | ||
|
||
submissions = response.json() | ||
assert isinstance(submissions, dict), "Expected JSON response to be a dictionary" | ||
assert "value" in submissions, "Missing 'value' key in JSON response" | ||
assert isinstance(submissions["value"], list), "Expected 'value' to be a list" | ||
assert len(submissions["value"]) > 0, "Expected at least one submission in 'value'" | ||
|
||
|
||
async def test_download_submission_file(client, submission): | ||
"""Test downloading submissions as a ZIP file.""" | ||
odk_project = submission["project"] | ||
|
||
response = await client.get( | ||
f"/submission/download?project_id={odk_project.id}&export_json=false" | ||
) | ||
|
||
assert response.status_code == 200, ( | ||
f"Failed to download submissions as file. " f"Response: {response.text}" | ||
) | ||
assert ( | ||
"Content-Disposition" in response.headers | ||
), "Missing Content-Disposition header" | ||
|
||
expected_filename = f"{odk_project.slug}.zip" | ||
|
||
assert response.headers["Content-Disposition"].endswith( | ||
expected_filename | ||
), f"Expected file name to end with {expected_filename}" | ||
assert len(response.content) > 0, "Expected non-empty ZIP file content" | ||
|
||
|
||
async def test_get_submission_count(client, submission): | ||
"""Test fetching the submission count for a project.""" | ||
odk_project = submission["project"] | ||
|
||
response = await client.get( | ||
f"/submission/get-submission-count?project_id={odk_project.id}" | ||
) | ||
assert ( | ||
response.status_code == 200 | ||
), f"Failed to fetch submission count. Response: {response.text}" | ||
|
||
submission_count = response.json() | ||
assert isinstance( | ||
submission_count, int | ||
), "Expected submission count to be an integer" | ||
assert submission_count > 0, "Submission count should be greater than zero" | ||
|
||
|
||
async def test_download_submission_geojson(client, submission): | ||
"""Test downloading submissions as a GeoJSON file.""" | ||
odk_project = submission["project"] | ||
|
||
response = await client.get( | ||
f"/submission/download-submission-geojson?project_id={odk_project.id}" | ||
) | ||
|
||
assert ( | ||
response.status_code == 200 | ||
), f"Failed to download GeoJSON submissions. Response: {response.text}" | ||
|
||
assert ( | ||
"Content-Disposition" in response.headers | ||
), "Missing Content-Disposition header" | ||
expected_filename = f"{odk_project.slug}.geojson" | ||
assert response.headers["Content-Disposition"].endswith( | ||
expected_filename | ||
), f"Expected file name to end with {expected_filename}" | ||
|
||
submission_geojson = json.loads(response.content) | ||
assert isinstance( | ||
submission_geojson, dict | ||
), "Expected GeoJSON content to be a dictionary" | ||
assert "type" in submission_geojson, "Missing 'type' key in GeoJSON" | ||
assert ( | ||
submission_geojson["type"] == "FeatureCollection" | ||
), "GeoJSON type must be 'FeatureCollection'" | ||
assert "features" in submission_geojson, "Missing 'features' key in GeoJSON" | ||
assert isinstance( | ||
submission_geojson["features"], list | ||
), "Expected 'features' to be a list" | ||
assert ( | ||
len(submission_geojson["features"]) > 0 | ||
), "Expected at least one feature in 'features'" | ||
|
||
|
||
if __name__ == "__main__": | ||
"""Main func if file invoked directly.""" | ||
pytest.main() |