From 167e0b4902c01d34ba6d4716cecdc4189c7e0e2e Mon Sep 17 00:00:00 2001 From: Rob Savoye Date: Sun, 25 Feb 2024 18:22:03 -0700 Subject: [PATCH] fix: Add Makefile target check, minor updates to get a clean test run --- tests/Makefile | 5 +++ tests/test_messages.py | 4 +- tests/test_notifications.py | 31 ++++++++++----- tests/test_stats.py | 76 +++++++++++++++++++++++++++++++++++++ tests/test_tasks.py | 11 ++++-- tests/test_teams.py | 1 - tests/test_users.py | 2 +- 7 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 tests/test_stats.py diff --git a/tests/Makefile b/tests/Makefile index a0cc95c0..d195e22c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -22,6 +22,7 @@ NAME := tm-admin VERSION := 0.1.0 SQL := $(wildcard *.sql) +TESTS := $(wildcard test_*.py) # Developer only! This requires a Tasking Manager database running on your local machine. # This is used to update the test data if any changes to the TM database schema are made. @@ -56,4 +57,8 @@ import: psql -d testdata -f $${i}; \ done +check: + for test in $(TESTS); do \ + ./$${test}; \ + done force: diff --git a/tests/test_messages.py b/tests/test_messages.py index 16e39259..dde66425 100755 --- a/tests/test_messages.py +++ b/tests/test_messages.py @@ -208,8 +208,8 @@ async def main(): stream=sys.stdout, ) - user.connect(args.uri) - msg.connect(args.uri) + await user.connect(args.uri) + await msg.connect(args.uri) await send_welcome_message() await send_message_after_validation() diff --git a/tests/test_notifications.py b/tests/test_notifications.py index c8586d2b..899f4c1c 100755 --- a/tests/test_notifications.py +++ b/tests/test_notifications.py @@ -31,6 +31,8 @@ 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 # Instantiate logger log = logging.getLogger(__name__) @@ -41,20 +43,19 @@ # FIXME: For now these tests assume you have a local postgres installed. One has the TM # database, the other for tm_admin. -dbname = os.getenv("TMDB", default="localhost/testdata") -user = UsersDB(dbname) -project = ProjectsDB(dbname) -task = TasksDB(dbname) +# user = UsersDB() +# project = ProjectsDB() +# task = TasksDB() -def update(): +async def update(): # user_id: int): log.debug(f"--- update() unimplemented!") -def get_unread_message_count(): +async def get_unread_message_count(): # user_id: int): log.debug(f"--- get_unread_message_count() unimplemented!") -if __name__ == "__main__": +async def main(): parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbose", nargs="?", const="0", help="verbose output") parser.add_argument("-u", "--uri", default='localhost/tm_admin', help="Database URI") @@ -72,5 +73,17 @@ def get_unread_message_count(): stream=sys.stdout, ) - update() - get_unread_message_count() + # user = UsersDB() + # project = ProjectsDB() + # task = TasksDB() + + await update() + await get_unread_message_count() + + +if __name__ == "__main__": + """This is just a hook so this file can be run standalone during development.""" + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + loop.run_until_complete(main()) + diff --git a/tests/test_stats.py b/tests/test_stats.py new file mode 100644 index 00000000..6b6636da --- /dev/null +++ b/tests/test_stats.py @@ -0,0 +1,76 @@ +#!/usr/bin/python3 + +# Copyright (c) 2022, 2023 Humanitarian OpenStreetMap Team +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program 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 Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +# Humanitarian OpenStreetmap Team +# 1100 13th Street NW Suite 800 Washington, D.C. 20005 +# + +import argparse +import logging +import sys +import os +from sys import argv +# from tm_admin.organizations.organizations_proto import OrganizationsMessage +#from tm_admin.yamlfile import YamlFile +from tm_admin.organizations.organizations import OrganizationsDB +from tm_admin.types_tm import Organizationtype, Mappinglevel +from datetime import datetime +from tm_admin.teams.teams import TeamsDB + +# Instantiate logger +log = logging.getLogger(__name__) + +import tm_admin as tma +rootdir = tma.__path__[0] + +# average mapping time per task +# average validation ti8me per task +# Estimated time to finish mapping +# Estimated time to finish validation +# mappers per project +# validators per project +# total contributors +# users by level + +## task Status +# tasks locked for validation +# finished tasks +# ready for validation +# available for mapping +# locked for mapping +# more mapping needed + + +# FIXME: For now these tests assume you have a local postgres installed + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("-v", "--verbose", nargs="?", const="0", help="verbose output") + parser.add_argument("-u", "--uri", default='localhost/tm_admin', help="Database URI") + args = parser.parse_args() + # if verbose, dump to the terminal. + log_level = os.getenv("LOG_LEVEL", default="INFO") + if args.verbose is not None: + log_level = logging.DEBUG + + logging.basicConfig( + level=log_level, + format=("%(asctime)s.%(msecs)03d [%(levelname)s] " "%(name)s | %(funcName)s:%(lineno)d | %(message)s"), + datefmt="%y-%m-%d %H:%M:%S", + stream=sys.stdout, + ) + diff --git a/tests/test_tasks.py b/tests/test_tasks.py index a1ef47a6..40b45a76 100755 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -31,6 +31,7 @@ from tm_admin.types_tm import Userrole, Mappinglevel from datetime import datetime from tm_admin.users.users_class import UsersTable +from tm_admin.tasks.api import TasksAPI # Instantiate logger log = logging.getLogger(__name__) @@ -38,10 +39,7 @@ import tm_admin as tma rootdir = tma.__path__[0] -dbname = os.getenv("TMDB", default="localhost/testdata") -user = UsersDB(dbname) -project = ProjectsDB(dbname) -task = TasksDB(dbname) +task = TasksAPI() # FIXME: For now these tests assume you have a local postgres installed. One has the TM # database, the other for tm_admin. @@ -185,6 +183,11 @@ def edit_task_boundary(): stream=sys.stdout, ) + # user = UsersDB(args.uri) + # task = TasksDB(args.uri) + await task.connect(args.uri) + # project = ProjectsDB(args.uri) + get_task() get_task_as_dto() _is_task_undoable() diff --git a/tests/test_teams.py b/tests/test_teams.py index 78aaa6eb..a408928b 100755 --- a/tests/test_teams.py +++ b/tests/test_teams.py @@ -46,7 +46,6 @@ dbname = os.getenv("TMDB", default="localhost/testdata") teams = TeamsAPI() -teams.connect("localhost/tm_admin") async def get_team_by_id(): log.debug(f"get_team_by_id() unimplemented!") diff --git a/tests/test_users.py b/tests/test_users.py index 282f70de..e5abcc89 100755 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -203,7 +203,7 @@ async def register_user(): # osm_id, username, changeset_count, picture_url, email # The id is generated by postgres, so we don't supply it. ut = UsersTable(username='foobar', name='barfoo', picture_url='URI', email_address="bar@foo.com", mapping_level='INTERMEDIATE', role='VALIDATOR') - new = user.createTable(ut) + # new = user.createTable(ut) entry = await user.getByName(ut.data['name']) # assert entry['id'] > 0