Skip to content

Commit

Permalink
feat(tests): refactor submission handling to use pyodk client for ODK…
Browse files Browse the repository at this point in the history
… submissions
  • Loading branch information
Anuj-Gupta4 committed Nov 27, 2024
1 parent 198c03f commit f1b1831
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
34 changes: 19 additions & 15 deletions src/backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from httpx import ASGITransport, AsyncClient
from loguru import logger as log
from psycopg import AsyncConnection
from pyodk.client import Client

from app.auth.auth_routes import get_or_create_user
from app.auth.auth_schemas import AuthUser, FMTMUser
Expand All @@ -53,6 +54,7 @@
odk_central_url = os.getenv("ODK_CENTRAL_URL")
odk_central_user = os.getenv("ODK_CENTRAL_USER")
odk_central_password = encrypt_value(os.getenv("ODK_CENTRAL_PASSWD", ""))
odk_config_file = str(Path(__file__).parent / ".pyodk_config.toml")


def pytest_configure(config):
Expand Down Expand Up @@ -300,11 +302,12 @@ def forms(base_url, auth, pid):
odk_form_id = forms[0]["xmlFormId"]
odk_form_version = forms[0]["version"]

submission_id = uuid.uuid4()
submission_id = str(uuid.uuid4())

submission_xml = f"""
<data id="{odk_form_id}" version="{odk_form_version}">
<meta>
<instanceID>uuid:{submission_id}</instanceID>
<instanceID>{submission_id}</instanceID>
</meta>
<start>2024-11-15T12:28:23.641Z</start>
<end>2024-11-15T12:29:00.876Z</end>
Expand Down Expand Up @@ -346,24 +349,25 @@ def forms(base_url, auth, pid):
</survey_questions>
</data>
"""
submission_url = (
f"{base_url}/v1/projects/{odk_project_id}/forms/{odk_form_id}/submissions"
)

submission_response = requests.post(
submission_url,
data=submission_xml,
headers={"Content-Type": "application/xml"},
auth=auth,
)
assert (
submission_response.status_code == 200
), "Failed to create a submission in ODK Central"
with Client(config_path=odk_config_file) as client:
client.submissions.create(
project_id=odk_project_id,
form_id=odk_form_id,
xml=submission_xml,
device_id=None,
encoding="utf-8",
)
submission_data = client.submissions.get(
project_id=odk_project_id,
form_id=odk_form_id,
instance_id=submission_id,
)

yield {
"project": odk_project,
"odk_form_id": odk_form_id,
"submission_data": submission_response.json(),
"submission_data": submission_data,
}


Expand Down
4 changes: 2 additions & 2 deletions src/backend/tests/test_submission_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ async def test_read_submissions(client, submission):
assert isinstance(submission_list, list), "Expected a list of submissions"

first_submission = submission_list[0]
test_instance_id = submission_data["instanceId"]
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"]
submission_data.submitterId
), "Submitter ID mismatch"


Expand Down

0 comments on commit f1b1831

Please sign in to comment.