Skip to content

Commit

Permalink
test: get the detailed task history for a project (#1626)
Browse files Browse the repository at this point in the history
  • Loading branch information
azharcodeit authored Jul 8, 2024
1 parent d001cfe commit ec06348
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
64 changes: 62 additions & 2 deletions src/backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
from app.central import central_crud
from app.config import settings
from app.db.database import Base, get_db
from app.db.db_models import DbOrganisation
from app.db.db_models import DbOrganisation, DbTaskHistory
from app.main import get_application
from app.models.enums import CommunityType, UserRole
from app.models.enums import CommunityType, TaskStatus, UserRole
from app.projects import project_crud
from app.projects.project_schemas import ODKCentralDecrypted, ProjectInfo, ProjectUpload
from app.users.user_crud import get_user

engine = create_engine(settings.FMTM_DB_URL.unicode_string())
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Expand Down Expand Up @@ -188,6 +189,65 @@ async def project(db, admin_user, organisation):
return new_project


@pytest.fixture(scope="function")
async def task(project, db):
"""A test task, using the test project."""
boundaries = {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[85.3012091, 27.7122369],
[85.3012129, 27.7121403],
[85.3013408, 27.7121442],
[85.3013371, 27.7122408],
[85.3012441, 27.712238],
[85.3012091, 27.7122369],
]
],
},
"properties": {
"osm_id": 650958368,
"version": 2,
"tags": {"building": "yes"},
"changeset": 99124278,
"timestamp": "2021-02-11T17:21:06",
},
}
try:
tasks = await project_crud.create_tasks_from_geojson(
db=db, project_id=project.id, boundaries=boundaries
)

assert tasks is True

# Refresh the project to include the tasks
db.refresh(project)
except Exception as e:
log.exception(e)
pytest.fail(f"Test failed with exception: {str(e)}")
return project.tasks[0]


@pytest.fixture(scope="function")
async def task_history(db, project, task, admin_user):
"""A test task history using the test user, project and task."""
user = await get_user(db, admin_user.id)
task_history_entry = DbTaskHistory(
project_id=project.id,
task_id=task.id,
action=TaskStatus.READY,
action_text=f"Task created with action {TaskStatus.READY} by {user.username}",
actioned_by=user,
user_id=user.id,
)
db.add(task_history_entry)
db.commit()
db.refresh(task_history_entry)
return task_history_entry


# @pytest.fixture(scope="function")
# def get_ids(db, project):
# user_id_query = text(f"SELECT id FROM {DbUser.__table__.name} LIMIT 1")
Expand Down
39 changes: 39 additions & 0 deletions src/backend/tests/test_task_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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 task routes."""

import pytest


def test_read_task_history(client, task_history):
"""Test task history for a project."""
task_id = task_history.task_id

assert task_id is not None

response = client.get(f"/tasks/{task_id}/history/")
data = response.json()[0]

assert response.status_code == 200
assert data["id"] == task_history.id
assert data["username"] == task_history.actioned_by.username


if __name__ == "__main__":
"""Main func if file invoked directly."""
pytest.main()

0 comments on commit ec06348

Please sign in to comment.