adjusted tests for --clean-deleted-relations command

This commit is contained in:
lujoh
2023-10-17 23:03:37 -04:00
parent 06204dfcd8
commit 9ec26c60ff
4 changed files with 20 additions and 12 deletions

View File

@@ -29,6 +29,7 @@ class AdminFuncs:
""" """
def add_args(self, parser: argparse.ArgumentParser) -> None: def add_args(self, parser: argparse.ArgumentParser) -> None:
self.parser = parser
group = parser.add_argument_group('Admin tasks') group = parser.add_argument_group('Admin tasks')
objs = group.add_mutually_exclusive_group(required=True) objs = group.add_mutually_exclusive_group(required=True)
objs.add_argument('--warm', action='store_true', objs.add_argument('--warm', action='store_true',
@@ -89,6 +90,8 @@ class AdminFuncs:
return 0 return 0
if args.clean_deleted: if args.clean_deleted:
if not args.age:
self.parser.error('Age is required for --clean-deleted command')
LOG.warning('Cleaning up deleted relations') LOG.warning('Cleaning up deleted relations')
from ..tools import admin from ..tools import admin
admin.clean_deleted_relations(args.config, age=args.age) admin.clean_deleted_relations(args.config, age=args.age)

View File

@@ -90,12 +90,9 @@ def analyse_indexing(config: Configuration, osm_id: Optional[str] = None,
print(msg) print(msg)
def clean_deleted_relations(config: Configuration, age: Optional[str] = None) -> None: def clean_deleted_relations(config: Configuration, age: str) -> None:
""" Clean deleted relations older than a given age """ Clean deleted relations older than a given age
""" """
if not age:
LOG.fatal('No age given to delete relations')
raise UsageError('Age parameter not found')
with connect(config.get_libpq_dsn()) as conn: with connect(config.get_libpq_dsn()) as conn:
with conn.cursor() as cur: with conn.cursor() as cur:
try: try:

View File

@@ -36,9 +36,15 @@ def test_admin_migrate(cli_call, mock_func_factory):
def test_admin_clean_deleted_relations(cli_call, mock_func_factory): def test_admin_clean_deleted_relations(cli_call, mock_func_factory):
mock = mock_func_factory(nominatim.tools.admin, 'clean_deleted_relations') mock = mock_func_factory(nominatim.tools.admin, 'clean_deleted_relations')
assert cli_call('admin', '--clean-deleted') == 0 assert cli_call('admin', '--clean-deleted', '--age', '1 month') == 0
assert mock.called == 1 assert mock.called == 1
def test_admin_clean_deleted_relations_no_age(cli_call, mock_func_factory):
mock = mock_func_factory(nominatim.tools.admin, 'clean_deleted_relations')
with pytest.raises(SystemExit):
cli_call('admin', '--clean-deleted')
class TestCliAdminWithDb: class TestCliAdminWithDb:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)

View File

@@ -91,8 +91,8 @@ class TestAdminCleanDeleted:
(175, 'R', 'landcover', 'grass'))) (175, 'R', 'landcover', 'grass')))
temp_db_cursor.execute("""INSERT INTO placex (place_id, osm_id, osm_type, class, type, indexed_date, indexed_status) temp_db_cursor.execute("""INSERT INTO placex (place_id, osm_id, osm_type, class, type, indexed_date, indexed_status)
VALUES(1, 100, 'N', 'boundary', 'administrative', current_date - INTERVAL '1 month', 1), VALUES(1, 100, 'N', 'boundary', 'administrative', current_date - INTERVAL '1 month', 1),
(2, 145, 'N', 'boundary', 'administrative', current_date - INTERVAL '1 month', 1), (2, 145, 'N', 'boundary', 'administrative', current_date - INTERVAL '3 month', 1),
(3, 175, 'R', 'landcover', 'grass', current_date - INTERVAL '1 month', 1)""") (3, 175, 'R', 'landcover', 'grass', current_date - INTERVAL '3 months', 1)""")
# set up tables and triggers for utils function # set up tables and triggers for utils function
table_factory('place_to_be_deleted', table_factory('place_to_be_deleted',
"""osm_id BIGINT, """osm_id BIGINT,
@@ -126,17 +126,19 @@ class TestAdminCleanDeleted:
assert self.temp_db_cursor.table_rows('import_polygon_delete') == 3 assert self.temp_db_cursor.table_rows('import_polygon_delete') == 3
def test_admin_clean_deleted_no_age(self):
with pytest.raises(UsageError):
admin.clean_deleted_relations(self.project_env)
@pytest.mark.parametrize('test_age', ['T week', '1 welk', 'P1E']) @pytest.mark.parametrize('test_age', ['T week', '1 welk', 'P1E'])
def test_admin_clean_deleted_bad_age(self, test_age): def test_admin_clean_deleted_bad_age(self, test_age):
with pytest.raises(UsageError): with pytest.raises(UsageError):
admin.clean_deleted_relations(self.project_env, age = test_age) admin.clean_deleted_relations(self.project_env, age = test_age)
def test_admin_clean_deleted_partial(self):
admin.clean_deleted_relations(self.project_env, age = '2 months')
assert self.temp_db_cursor.row_set('SELECT osm_id, osm_type, class, type, indexed_status FROM placex') == {(100, 'N', 'boundary', 'administrative', 1),
(145, 'N', 'boundary', 'administrative', 100),
(175, 'R', 'landcover', 'grass', 100)}
assert self.temp_db_cursor.table_rows('import_polygon_delete') == 1
@pytest.mark.parametrize('test_age', ['1 week', 'P3D', '5 hours']) @pytest.mark.parametrize('test_age', ['1 week', 'P3D', '5 hours'])
def test_admin_clean_deleted(self, test_age): def test_admin_clean_deleted(self, test_age):
admin.clean_deleted_relations(self.project_env, age = test_age) admin.clean_deleted_relations(self.project_env, age = test_age)