1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2025 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Tests for migration functions
 
  12 from nominatim_db.tools import migration
 
  13 from nominatim_db.errors import UsageError
 
  14 import nominatim_db.version
 
  19     def update_sql_functions(self, config):
 
  24 def postprocess_mock(monkeypatch):
 
  25     monkeypatch.setattr(migration.refresh, 'create_functions', lambda *args: args)
 
  26     monkeypatch.setattr(migration.tokenizer_factory, 'get_tokenizer_for_db',
 
  27                         lambda *args: DummyTokenizer())
 
  30 def test_no_migration_old_versions(temp_db_with_extensions, def_config, property_table):
 
  31     property_table.set('database_version', '4.2.99-0')
 
  33     with pytest.raises(UsageError, match='Migration not possible'):
 
  34         migration.migrate(def_config, {})
 
  37 def test_already_at_version(temp_db_with_extensions, def_config, property_table):
 
  39     property_table.set('database_version',
 
  40                        str(nominatim_db.version.NOMINATIM_VERSION))
 
  42     assert migration.migrate(def_config, {}) == 0
 
  45 def test_run_single_migration(temp_db_with_extensions, def_config, temp_db_cursor,
 
  46                               property_table, monkeypatch, postprocess_mock):
 
  47     oldversion = [4, 4, 99, 0]
 
  48     property_table.set('database_version',
 
  49                        str(nominatim_db.version.NominatimVersion(*oldversion)))
 
  51     done = {'old': False, 'new': False}
 
  54         """ Dummy migration"""
 
  57     def _old_migration(**_):
 
  58         """ Dummy migration"""
 
  62     monkeypatch.setattr(migration, '_MIGRATION_FUNCTIONS',
 
  63                         [(tuple(oldversion), _old_migration),
 
  64                          (nominatim_db.version.NOMINATIM_VERSION, _migration)])
 
  66     assert migration.migrate(def_config, {}) == 0
 
  69     assert not done['old']
 
  70     assert property_table.get('database_version') == str(nominatim_db.version.NOMINATIM_VERSION)
 
  73 # Tests for specific migrations
 
  75 # Each migration should come with two tests:
 
  76 #  1. Test that migration from old to new state works as expected.
 
  77 #  2. Test that the migration can be rerun on the new state without side effects.