Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
fix: Fix circular dependencies for APIs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robsavoye committed Mar 18, 2024
1 parent 90718ad commit ec0e95e
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 68 deletions.
36 changes: 20 additions & 16 deletions tests/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,24 @@
from tm_admin.projects.projects_class import ProjectsTable
import asyncio
from codetiming import Timer
from tm_admin.teams.api import TeamsAPI
from tm_admin.tasks.api import TasksAPI
from tm_admin.users.api import UsersAPI
from tm_admin.projects.api import ProjectsAPI
from test_users import create_users
import typing
import tm_admin
from shapely.geometry import Polygon, Point, shape
from test_users import create_users
import test_tasks
import test_users

# Instantiate logger
log = logging.getLogger(__name__)

import tm_admin as tma
rootdir = tma.__path__[0]

# FIXME: For now these tests assume you have a local postgres installed.

users = UsersAPI()
projects = ProjectsAPI()
tasks = TasksAPI()
teams = TeamsAPI()
users = tm_admin.users.api.UsersAPI()
tasks = tm_admin.tasks.api.TasksAPI()

# FIXME: For now these tests assume you have a local postgres installed.

# These tests are for the API endpoints
async def create_projects(papi):
Expand Down Expand Up @@ -118,8 +116,8 @@ async def get_team_role():
project_id = 1
team_id = 1
result = await projects.getTeamRole(project_id, team_id)
print(result)
# assert result == Teamrole.TEAM_MAPPER
# print(result)
assert result == Teamrole.TEAM_MAPPER

# These endpoint tests come from the TM backend
async def get_project_by_id():
Expand Down Expand Up @@ -511,12 +509,18 @@ async def main():
# await users.getTypes("users")
# await tasks.connectDBs(args.uri)
# await tasks.getTypes("tasks")
await projects.initialize(args.uri, users)
await users.initialize(args.uri, projects)
# teams = tm_admin.teams.api.TeamsAPI()

await projects.initialize(args.uri, users, tasks)
await users.initialize(args.uri, projects, tasks)
await tasks.initialize(args.uri, projects, users)
# await teams.initialize(args.uri, projects)

# The API for this class also requires access to the Users table
# The API for this class also requires access to other tables
await create_projects(projects)
await create_users(users)
await test_users.create_users(users)
await test_tasks.create_tasks(tasks)
# await create_teams(teams)

await get_team_role()

Expand Down
41 changes: 25 additions & 16 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,26 @@
from tm_admin.tasks.task_history_class import Task_historyTable
from tm_admin.tasks.task_issues_class import Task_issuesTable
from tm_admin.tasks.task_invalidation_history_class import Task_invalidation_historyTable
from tm_admin.tasks.api import TasksAPI
import asyncio
from codetiming import Timer
import tm_admin
from tm_admin.tasks.api import TasksAPI
import test_projects
import test_users

# Instantiate logger
log = logging.getLogger(__name__)

tasks = TasksAPI()
projects = tm_admin.projects.api.ProjectsAPI()
users = tm_admin.users.api.UsersAPI()

# FIXME: For now these tests assume you have a local postgres installed. One has the TM
# database, the other for tm_admin.

async def create_tasks():
await tasks.deleteRecords([1, 2, 3])
await tasks.resetSequence()
async def create_tasks(tapi):
await tapi.deleteRecords([1, 2, 3])
await tapi.resetSequence()
project_id = 1
user_id = 1
tl = list()
Expand All @@ -63,7 +68,7 @@ async def create_tasks():
"action_text": "validated task",
"action_date": "2024-01-25 10:50:56.140958",
"user_id": user_id})
await tasks.updateHistory(history, task_id, project_id)
await tapi.updateHistory(history, task_id, project_id)

task_id = 1
history = list()
Expand All @@ -81,22 +86,22 @@ async def create_tasks():
"action_text": "marked mapped",
"action_date": "2024-01-25 10:50:56.140958",
"user_id": user_id})
await tasks.appendHistory(history, task_id, project_id)
await tapi.appendHistory(history, task_id, project_id)

tl.append(TasksTable(id = 2, project_id = 1,
project_task_name = "testing, 1,2,3",
is_square = True,
task_status = Taskstatus.READY))

# Insert all the tables
result = await tasks.insertRecords(tl)
result = await tapi.insertRecords(tl)

# Now add some data to the jsonb columns
history.append({"action": Taskaction.RELEASED_FOR_MAPPING,
"action_text": "validated task",
"action_date": "2024-01-25 10:50:56.140958",
"user_id": user_id})
await tasks.updateHistory(history, task_id, project_id)
await tapi.updateHistory(history, task_id, project_id)

task_id = 1
project_id = 1
Expand All @@ -105,7 +110,7 @@ async def create_tasks():
"action_text": "validated task",
"action_date": "2024-01-25 10:50:56.140958",
"user_id": user_id})
await tasks.updateHistory(history, task_id, project_id)
await tapi.updateHistory(history, task_id, project_id)

task_id = 1
project_id = 1
Expand All @@ -124,13 +129,13 @@ async def create_tasks():
"action_text": "marked mapped",
"action_date": "2024-01-25 10:50:56.140958",
"user_id": user_id})
await tasks.appendHistory(history, task_id, project_id)
await tapi.appendHistory(history, task_id, project_id)

# Add some mapping issues
issuses = list()
issuses.append({"issue": Mapping_issue.FEATURE_GEOMETRY, "count": 3})
issuses.append({"issue": Mapping_issue.MISSED_FEATURE, "count": 10})
await tasks.updateIssues(issuses, task_id, project_id)
await tapi.updateIssues(issuses, task_id, project_id)

# Add some invalidation entries
mapper_id = 1
Expand All @@ -146,7 +151,7 @@ async def create_tasks():
"validator_id": validator_id,
"updated_date": updated_date,
}
await tasks.appendHistory([validation], task_id, project_id)
await tapi.appendHistory([validation], task_id, project_id)

async def get_task():
# task_id: int, project_id: int) -> Task:
Expand Down Expand Up @@ -305,10 +310,14 @@ async def main():
stream=sys.stdout,
)

# user = UsersDB(args.uri)
# task = TasksDB(args.uri)
await tasks.initialize(args.uri)
await create_tasks()
await projects.initialize(args.uri, users, tasks)
await users.initialize(args.uri, projects, tasks)
await tasks.initialize(args.uri, projects, users)

# These populate the database for testing
await create_tasks(tasks)
await test_users.create_users(users)
await test_projects.create_projects(projects)
# project = ProjectsDB(args.uri)

await get_tasks()
Expand Down
27 changes: 17 additions & 10 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
Test users API
"""

from __future__ import annotations

import tm_admin as tma
rootdir = tma.__path__[0]

Expand All @@ -33,23 +35,24 @@
from sys import argv
# from tm_admin.users.users_proto import UsersMessage
#from tm_admin.yamlfile import YamlFile
from tm_admin.users.users import UsersDB
from tm_admin.users.api import UsersAPI
from tm_admin.projects.projects import ProjectsDB
from tm_admin.projects.api import ProjectsAPI
from tm_admin.types_tm import Userrole, Mappinglevel
from datetime import datetime
from tm_admin.users.users_class import UsersTable
import asyncio
from codetiming import Timer

from tm_admin.users.api import UsersAPI
import typing
import tm_admin
import test_tasks
import test_projects
# Instantiate logger
log = logging.getLogger(__name__)

# FIXME: For now these tests assume you have a local postgres installed.

users = UsersAPI()
projects = ProjectsAPI()
tasks = tm_admin.tasks.api.TasksAPI()
projects = None # tm_admin.projects.api.ProjectsAPI()

# FIXME: For now these tests assume you have a local postgres installed.

async def create_users(uapi):
await uapi.deleteRecords([1, 2, 3, 4, 5])
Expand Down Expand Up @@ -548,11 +551,15 @@ async def main():
stream=sys.stdout,
)

await users.initialize(args.uri, projects)
await projects.initialize(args.uri, users)
tasks = tm_admin.tasks.api.TasksAPI()
projects = tm_admin.projects.api.ProjectsAPI()
await projects.initialize(args.uri, users, tasks)
await users.initialize(args.uri, projects, tasks)

# Populate the table with test data
await create_users(users)
# await create_tasks(tasks)
# await create_projects(projects)

# These tests are from the TM backend
await set_user_mapping_level()
Expand Down
6 changes: 3 additions & 3 deletions tm_admin/campaigns/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def __init__(self):
(CampaignsAPI): An instance of this class
"""
# self.allowed_roles = [
# Teamroles.TEAM_MAPPER,
# Teamroles.TEAM_VALIDATOR,
# Teamroles.TEAM_MANAGER,
# Teamrole.TEAM_MAPPER,
# Teamrole.TEAM_VALIDATOR,
# Teamrole.TEAM_MANAGER,
# ]
# self.messagesdb = MessagesDB()
# self.usersdb = UsersDB()
Expand Down
17 changes: 13 additions & 4 deletions tm_admin/projects/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# 1100 13th Street NW Suite 800 Washington, D.C. 20005
# <[email protected]>

from __future__ import annotations

import argparse
import logging
import sys
Expand All @@ -45,8 +47,11 @@
import asyncio
from shapely import wkb, wkt
import typing
import tm_admin
#if typing.TYPE_CHECKING:
from tm_admin.users.api import UsersAPI
# from tm_admin.users.api import UsersAPI
# from tm_admin.tasks.api import TasksAPI
# from tm_admin.projects.api import ProjectsAPI
# The number of threads is based on the CPU cores
info = get_cpu_info()
cores = info["count"] * 2
Expand All @@ -69,13 +74,15 @@ def __init__(self):
Teamrole.TEAM_MANAGER,
]
from tm_admin.users.api import UsersAPI
self.users = UsersAPI()
self.users = None
self.tasks = None
super().__init__("projects")

async def initialize(self,
inuri: str,
uapi: UsersAPI,
):
tapi: TasksAPI,
) -> None:
"""
Connect to all tables for API endpoints that require accessing multiple tables.
Expand All @@ -84,6 +91,8 @@ async def initialize(self,
"""
await self.connect(inuri)
await self.getTypes("projects")
self.users = uapi
self.tasks = tapi

async def getByID(self,
project_id: int,
Expand Down Expand Up @@ -114,7 +123,7 @@ async def getTeamRole(self,
Returns:
(Teamrole): The role of this team in this project
"""
log.warning(f"getProjectByTeam(): unimplemented!")
# log.warning(f"getProjectByTeam(): unimplemented!")
# sql = f"SELECT FROM projects WHERE project_id={project_id}"
# where = [{'teams': {"team_id": team_id, "project_id": project_id}}]
#data = await self.getColumns(['id', 'teams'], where)
Expand Down
26 changes: 15 additions & 11 deletions tm_admin/tasks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# 1100 13th Street NW Suite 800 Washington, D.C. 20005
# <[email protected]>

from __future__ import annotations

import argparse
import logging
import sys
Expand All @@ -33,15 +35,9 @@
from shapely import centroid
from tm_admin.types_tm import Taskcreationmode, Taskstatus, Teamrole, Taskaction
from tm_admin.projects.projects_class import ProjectsTable
from tm_admin.projects.projects_teams_class import Project_teamsTable
from tm_admin.projects.projects_teams_class import Projects_teamsTable
from tm_admin.tasks.tasks_class import TasksTable
# from tm_admin.messages.messages_class import MessagesAPI
from tm_admin.projects.api import ProjectsAPI
from tm_admin.users.users import UsersDB
from tm_admin.teams.teams import TeamsDB
from shapely import wkb, get_coordinates
from tm_admin.dbsupport import DBSupport
from tm_admin.generator import Generator
from tm_admin.tasks.task_history_class import Task_historyTable
import re
# from progress import Bar, PixelBar
Expand All @@ -50,6 +46,10 @@
from codetiming import Timer
import asyncio
from tm_admin.pgsupport import PGSupport
import typing
#if typing.TYPE_CHECKING:
# from tm_admin.projects.api import ProjectsAPI
# from tm_admin.users.api import UsersAPI

# The number of threads is based on the CPU cores
info = get_cpu_info()
Expand All @@ -68,11 +68,14 @@ def __init__(self):
(TasksAPI): An instance of this class
"""
super().__init__("tasks")
self.projects = ProjectsAPI()
self.projects = tm_admin.projects.api.ProjectsAPI()
self.users = tm_admin.users.api.UsersAPI()

async def initialize(self,
inuri: str,
):
papi: ProjectsAPI,
uapi: UsersAPI,
) -> None:
"""
Connect to all tables for API endpoints that require
accessing multiple tables.
Expand All @@ -82,12 +85,13 @@ async def initialize(self,
"""
await self.connect(inuri)
await self.getTypes("tasks")
await self.projects.initialize(inuri)
self.projects = papi
self.users = uapi

async def getStatus(self,
task_id: int,
project_id: int,
):
) -> Taskstatus:
"""
Get the current status for a task using it's project and task IDs.
Expand Down
Loading

0 comments on commit ec0e95e

Please sign in to comment.