diff --git a/tests/test_projects.py b/tests/test_projects.py index e0b30e1..fc1b555 100755 --- a/tests/test_projects.py +++ b/tests/test_projects.py @@ -32,13 +32,12 @@ 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__) @@ -46,12 +45,11 @@ 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): @@ -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(): @@ -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() diff --git a/tests/test_tasks.py b/tests/test_tasks.py index a14a532..84ab304 100755 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -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() @@ -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() @@ -81,7 +86,7 @@ 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", @@ -89,14 +94,14 @@ async def create_tasks(): 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 @@ -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 @@ -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 @@ -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: @@ -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() diff --git a/tests/test_users.py b/tests/test_users.py index 87251b0..8379d06 100755 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -23,6 +23,8 @@ Test users API """ +from __future__ import annotations + import tm_admin as tma rootdir = tma.__path__[0] @@ -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]) @@ -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() diff --git a/tm_admin/campaigns/api.py b/tm_admin/campaigns/api.py index 86084f7..6a130e3 100644 --- a/tm_admin/campaigns/api.py +++ b/tm_admin/campaigns/api.py @@ -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() diff --git a/tm_admin/projects/api.py b/tm_admin/projects/api.py index da5c116..6ebc0d0 100755 --- a/tm_admin/projects/api.py +++ b/tm_admin/projects/api.py @@ -19,6 +19,8 @@ # 1100 13th Street NW Suite 800 Washington, D.C. 20005 # +from __future__ import annotations + import argparse import logging import sys @@ -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 @@ -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. @@ -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, @@ -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) diff --git a/tm_admin/tasks/api.py b/tm_admin/tasks/api.py index baa6b90..c8b9c63 100755 --- a/tm_admin/tasks/api.py +++ b/tm_admin/tasks/api.py @@ -19,6 +19,8 @@ # 1100 13th Street NW Suite 800 Washington, D.C. 20005 # +from __future__ import annotations + import argparse import logging import sys @@ -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 @@ -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() @@ -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. @@ -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. diff --git a/tm_admin/teams/api.py b/tm_admin/teams/api.py index 5d4ef4f..cf4babc 100755 --- a/tm_admin/teams/api.py +++ b/tm_admin/teams/api.py @@ -31,7 +31,7 @@ from cpuinfo import get_cpu_info from shapely.geometry import shape from shapely import centroid -from tm_admin.types_tm import Mappingtypes, Projectstatus, Taskcreationmode, Editors, Permissions, Projectpriority, Projectdifficulty, Teamroles, Teammemberfunctions +from tm_admin.types_tm import Mappingtypes, Projectstatus, Taskcreationmode, Editors, Permissions, Projectpriority, Projectdifficulty, Teamrole, Teammemberfunctions # from osm_rawdata.pgasync import PostgresClient from tm_admin.teams.teams_class import TeamsTable from tm_admin.teams.team_members_class import Team_membersTable @@ -61,9 +61,9 @@ def __init__(self): (TeamsAPI): 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, ] super().__init__("teams") diff --git a/tm_admin/users/api.py b/tm_admin/users/api.py index d2f1c2b..7323a7d 100755 --- a/tm_admin/users/api.py +++ b/tm_admin/users/api.py @@ -40,8 +40,10 @@ from shapely import wkb, get_coordinates from tm_admin.pgsupport import PGSupport import typing -if typing.TYPE_CHECKING: - from tm_admin.projects.api import ProjectsAPI +#if typing.TYPE_CHECKING: +# from tm_admin.projects.api import ProjectsAPI +# from tm_admin.tasks.api import TasksAPI +import tm_admin import re # from progress import Bar, PixelBar from tqdm import tqdm @@ -57,7 +59,7 @@ class UsersAPI(PGSupport): def __init__(self): - self.projects = None #ProjectsAPI() + self.projects = None # self.projects = projects.api.ProjectsAPI() self.cursor = None super().__init__("users") @@ -65,6 +67,7 @@ def __init__(self): async def initialize(self, inuri: str, papi: ProjectsAPI, + tapi: TasksAPI, ) -> None: """ Connect to all tables for API endpoints that require @@ -76,8 +79,10 @@ async def initialize(self, await self.connect(inuri) await self.getTypes("users") # await self.projects.initialize(inuri) - await papi.initialize(inuri, self) + await papi.initialize(inuri, self, tapi) + await tapi.initialize(inuri, papi, self) self.projects = papi + self.tasks = tapi # self.cursor = "DECLARE user_cursor CURSOR FOR SELECT * FROM users;" async def getByID(self,