Skip to content

Commit

Permalink
test: hashtag input parsing into correct format
Browse files Browse the repository at this point in the history
  • Loading branch information
azharcodeit committed Aug 1, 2024
1 parent 2afc4d0 commit d5a3665
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 48 deletions.
6 changes: 3 additions & 3 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ def validate_hashtags(cls, hashtags: Optional[str]) -> List[str]:
return ["#FMTM"]

hashtags = hashtags.replace(",", " ").replace(";", " ")
hashtags_list = hashtags.split()
hashtags_list = hashtags.lower().split()

# Add '#' to hashtag strings if missing
hashtags_with_hash = [
f"#{hashtag}" if hashtag and not hashtag.startswith("#") else hashtag
for hashtag in hashtags_list
]

if "#FMTM" not in hashtags_with_hash:
hashtags_with_hash.append("#FMTM")
if "#fmtm" not in hashtags_with_hash:
hashtags_with_hash.append("#fmtm")

return hashtags_with_hash

Expand Down
40 changes: 39 additions & 1 deletion src/backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from io import BytesIO
from pathlib import Path
from typing import Any, Generator
from uuid import uuid4

import pytest
import requests
Expand All @@ -42,7 +43,7 @@
from app.db.db_models import DbOrganisation, DbTaskHistory
from app.main import get_application
from app.models.enums import TaskStatus, UserRole
from app.projects import project_crud
from app.projects import project_crud, project_schemas
from app.projects.project_schemas import ODKCentralDecrypted, ProjectInfo, ProjectUpload
from app.users.user_crud import get_user
from tests.test_data import test_data_path
Expand Down Expand Up @@ -311,6 +312,43 @@ async def entities(odk_project):
yield entities


@pytest.fixture(scope="function")
def project_data():
"""Sample data for creating a project."""
project_name = f"Test Project {uuid4()}"
data = {
"project_info": {
"name": project_name,
"short_description": "test",
"description": "test",
},
"xform_category": "buildings",
"hashtags": "#FMTM",
"outline_geojson": {
"coordinates": [
[
[85.317028828, 27.7052522097],
[85.317028828, 27.7041424888],
[85.318844411, 27.7041424888],
[85.318844411, 27.7052522097],
[85.317028828, 27.7052522097],
]
],
"type": "Polygon",
},
}
odk_credentials = {
"odk_central_url": odk_central_url,
"odk_central_user": odk_central_user,
"odk_central_password": odk_central_password,
}

odk_creds_models = project_schemas.ODKCentralDecrypted(**odk_credentials)

data.update(**odk_creds_models.model_dump())
return data


# @pytest.fixture(scope="function")
# def get_ids(db, project):
# user_id_query = text(f"SELECT id FROM {DbUser.__table__.name} LIMIT 1")
Expand Down
79 changes: 35 additions & 44 deletions src/backend/tests/test_projects_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,64 +42,55 @@
odk_central_password = encrypt_value(os.getenv("ODK_CENTRAL_PASSWD", ""))


async def test_create_project(client, admin_user, organisation):
"""Test project creation endpoint."""
odk_credentials = {
"odk_central_url": odk_central_url,
"odk_central_user": odk_central_user,
"odk_central_password": odk_central_password,
}
odk_creds_models = project_schemas.ODKCentralDecrypted(**odk_credentials)

project_name = f"Test Project {uuid4()}"
project_data = {
"project_info": {
"name": project_name,
"short_description": "test",
"description": "test",
},
"xform_category": "buildings",
"hashtags": "#FMTM",
"outline_geojson": {
"coordinates": [
[
[85.317028828, 27.7052522097],
[85.317028828, 27.7041424888],
[85.318844411, 27.7041424888],
[85.318844411, 27.7052522097],
[85.317028828, 27.7052522097],
]
],
"type": "Polygon",
},
}
project_data.update(**odk_creds_models.model_dump())

def create_project(client, organisation_id, project_data):
"""Create a new project."""
response = client.post(
f"/projects/create-project?org_id={organisation.id}", json=project_data
f"/projects/create-project?org_id={organisation_id}", json=project_data
)

if response.status_code != 200:
log.error(response.json())
assert response.status_code == 200
return response.json()

response_data = response.json()

def test_create_project(client, organisation, project_data):
"""Test project creation endpoint."""
response_data = create_project(client, organisation.id, project_data)
project_name = project_data["project_info"]["name"]
assert "id" in response_data

# Duplicate response to test error condition: project name already exists
response_duplicate = client.post(
f"/projects/create-project?org_id={organisation.id}", json=project_data
)

assert response_duplicate.status_code == 400
response_duplicate_data = response_duplicate.json()
assert "detail" in response_duplicate_data
assert (
response_duplicate_data["detail"]
response_duplicate.json()["detail"]
== f"Project already exists with the name {project_name}"
)


@pytest.mark.parametrize(
"hashtag_input,expected_output",
[
("tag1, tag2, tag3", ["#tag1", "#tag2", "#tag3", "#fmtm"]),
("tag1 tag2 tag3", ["#tag1", "#tag2", "#tag3", "#fmtm"]),
("tag1 tag2 tag3", ["#tag1", "#tag2", "#tag3", "#fmtm"]),
("tag1, tag2 tag3 tag4", ["#tag1", "#tag2", "#tag3", "#tag4", "#fmtm"]),
("TAG1, tag2 TAG3", ["#tag1", "#tag2", "#tag3", "#fmtm"]),
("TAG1, tag2 #TAG3", ["#tag1", "#tag2", "#tag3", "#fmtm"]),
("", ["#fmtm"]),
(" ", ["#fmtm"]),
("#tag1, #tag2, #tag3", ["#tag1", "#tag2", "#tag3", "#fmtm"]),
("tag1, #tag2, tag3", ["#tag1", "#tag2", "#tag3", "#fmtm"]),
],
)
def test_hashtags(client, organisation, project_data, hashtag_input, expected_output):
"""Test hashtag parsing."""
project_data["hashtags"] = hashtag_input
response_data = create_project(client, organisation.id, project_data)
assert "id" in response_data
assert response_data["hashtags"][:-1] == expected_output


async def test_delete_project(client, admin_user, project):
"""Test deleting a FMTM project, plus ODK Central project."""
response = client.delete(f"/projects/{project.id}")
Expand Down Expand Up @@ -334,8 +325,8 @@ async def test_update_project(client, admin_user, project):
== updated_project_data["project_info"]["description"]
)

assert response_data["xform_category"] == "healthcare"
assert response_data["hashtags"] == ["#FMTM", "#anothertag"]
assert response_data["xform_category"] == updated_project_data["xform_category"]
assert response_data["hashtags"] == ["#fmtm", "#anothertag"]


async def test_project_summaries(client, project):
Expand Down

0 comments on commit d5a3665

Please sign in to comment.