]> git.openstreetmap.org Git - nominatim.git/blobdiff - test/python/tools/test_migration.py
Merge pull request #3492 from lonvia/drop-waste-disposal
[nominatim.git] / test / python / tools / test_migration.py
index 79ec8a0eaa8dbad01db66acca5e1272e43c7b3e8..2c7b2d5664cbf16591a848934e87ac9bf72a680e 100644 (file)
@@ -1,12 +1,20 @@
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+# This file is part of Nominatim. (https://nominatim.org)
+#
+# Copyright (C) 2024 by the Nominatim developer community.
+# For a full list of authors see the git log.
 """
 Tests for migration functions
 """
 import pytest
 """
 Tests for migration functions
 """
 import pytest
-import psycopg2.extras
 
 
-from nominatim.tools import migration
-from nominatim.errors import UsageError
-import nominatim.version
+from nominatim_db.tools import migration
+from nominatim_db.errors import UsageError
+from nominatim_db.db.connection import server_version_tuple
+import nominatim_db.version
+
+from mock_legacy_word_table import MockLegacyWordTable
 
 class DummyTokenizer:
 
 
 class DummyTokenizer:
 
@@ -20,6 +28,10 @@ def postprocess_mock(monkeypatch):
     monkeypatch.setattr(migration.tokenizer_factory, 'get_tokenizer_for_db',
                         lambda *args: DummyTokenizer())
 
     monkeypatch.setattr(migration.tokenizer_factory, 'get_tokenizer_for_db',
                         lambda *args: DummyTokenizer())
 
+@pytest.fixture
+def legacy_word_table(temp_db_conn):
+    return MockLegacyWordTable(temp_db_conn)
+
 
 def test_no_migration_old_versions(temp_db_with_extensions, table_factory, def_config):
     table_factory('country_name', 'name HSTORE, country_code TEXT')
 
 def test_no_migration_old_versions(temp_db_with_extensions, table_factory, def_config):
     table_factory('country_name', 'name HSTORE, country_code TEXT')
@@ -31,7 +43,6 @@ def test_no_migration_old_versions(temp_db_with_extensions, table_factory, def_c
 def test_set_up_migration_for_36(temp_db_with_extensions, temp_db_cursor,
                                  table_factory, def_config, monkeypatch,
                                  postprocess_mock):
 def test_set_up_migration_for_36(temp_db_with_extensions, temp_db_cursor,
                                  table_factory, def_config, monkeypatch,
                                  postprocess_mock):
-    psycopg2.extras.register_hstore(temp_db_cursor)
     # don't actually run any migration, except the property table creation
     monkeypatch.setattr(migration, '_MIGRATION_FUNCTIONS',
                         [((3, 5, 0, 99), migration.add_nominatim_property_table)])
     # don't actually run any migration, except the property table creation
     monkeypatch.setattr(migration, '_MIGRATION_FUNCTIONS',
                         [((3, 5, 0, 99), migration.add_nominatim_property_table)])
@@ -51,34 +62,20 @@ def test_set_up_migration_for_36(temp_db_with_extensions, temp_db_cursor,
                                           WHERE property = 'database_version'""")
 
 
                                           WHERE property = 'database_version'""")
 
 
-def test_already_at_version(def_config, property_table):
-
-    property_table.set('database_version',
-                       '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(nominatim.version.NOMINATIM_VERSION))
-
-    assert migration.migrate(def_config, {}) == 0
-
+def test_already_at_version(temp_db_with_extensions, def_config, property_table):
 
 
-def test_no_migrations_necessary(def_config, temp_db_cursor, property_table,
-                                 monkeypatch):
-    oldversion = [x for x in nominatim.version.NOMINATIM_VERSION]
-    oldversion[0] -= 1
     property_table.set('database_version',
     property_table.set('database_version',
-                       '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(oldversion))
-
-    oldversion[0] = 0
-    monkeypatch.setattr(migration, '_MIGRATION_FUNCTIONS',
-                        [(tuple(oldversion), lambda **attr: True)])
+                       str(nominatim_db.version.NOMINATIM_VERSION))
 
     assert migration.migrate(def_config, {}) == 0
 
 
 
     assert migration.migrate(def_config, {}) == 0
 
 
-def test_run_single_migration(def_config, temp_db_cursor, property_table,
-                              monkeypatch, postprocess_mock):
-    oldversion = [x for x in nominatim.version.NOMINATIM_VERSION]
+def test_run_single_migration(temp_db_with_extensions, def_config, temp_db_cursor,
+                              property_table, monkeypatch, postprocess_mock):
+    oldversion = [x for x in nominatim_db.version.NOMINATIM_VERSION]
     oldversion[0] -= 1
     property_table.set('database_version',
     oldversion[0] -= 1
     property_table.set('database_version',
-                       '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(oldversion))
+                       str(nominatim_db.version.NominatimVersion(*oldversion)))
 
     done = {'old': False, 'new': False}
     def _migration(**_):
 
     done = {'old': False, 'new': False}
     def _migration(**_):
@@ -92,14 +89,13 @@ def test_run_single_migration(def_config, temp_db_cursor, property_table,
     oldversion[0] = 0
     monkeypatch.setattr(migration, '_MIGRATION_FUNCTIONS',
                         [(tuple(oldversion), _old_migration),
     oldversion[0] = 0
     monkeypatch.setattr(migration, '_MIGRATION_FUNCTIONS',
                         [(tuple(oldversion), _old_migration),
-                         (nominatim.version.NOMINATIM_VERSION, _migration)])
+                         (nominatim_db.version.NOMINATIM_VERSION, _migration)])
 
     assert migration.migrate(def_config, {}) == 0
 
     assert done['new']
     assert not done['old']
 
     assert migration.migrate(def_config, {}) == 0
 
     assert done['new']
     assert not done['old']
-    assert property_table.get('database_version') == \
-           '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(nominatim.version.NOMINATIM_VERSION)
+    assert property_table.get('database_version') == str(nominatim_db.version.NOMINATIM_VERSION)
 
 
 ###### Tests for specific migrations
 
 
 ###### Tests for specific migrations
@@ -150,7 +146,7 @@ def test_add_nominatim_property_table_repeat(temp_db_conn, temp_db_cursor,
 
 
 def test_change_housenumber_transliteration(temp_db_conn, temp_db_cursor,
 
 
 def test_change_housenumber_transliteration(temp_db_conn, temp_db_cursor,
-                                            word_table, placex_table):
+                                            legacy_word_table, placex_table):
     placex_table.add(housenumber='3A')
 
     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION make_standard_name(name TEXT)
     placex_table.add(housenumber='3A')
 
     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION make_standard_name(name TEXT)
@@ -229,7 +225,7 @@ def test_create_tiger_housenumber_index(temp_db_conn, temp_db_cursor, table_fact
     migration.create_tiger_housenumber_index(temp_db_conn)
     temp_db_conn.commit()
 
     migration.create_tiger_housenumber_index(temp_db_conn)
     temp_db_conn.commit()
 
-    if temp_db_conn.server_version_tuple() >= (11, 0, 0):
+    if server_version_tuple(temp_db_conn) >= (11, 0, 0):
         assert temp_db_cursor.index_exists('location_property_tiger',
                                            'idx_location_property_tiger_housenumber_migrated')
 
         assert temp_db_cursor.index_exists('location_property_tiger',
                                            'idx_location_property_tiger_housenumber_migrated')