]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_migration.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / tools / test_migration.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for migration functions
9 """
10 import pytest
11
12 from nominatim_db.tools import migration
13 from nominatim_db.errors import UsageError
14 import nominatim_db.version
15
16
17 class DummyTokenizer:
18
19     def update_sql_functions(self, config):
20         pass
21
22
23 @pytest.fixture
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())
28
29
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')
32
33     with pytest.raises(UsageError, match='Migration not possible'):
34         migration.migrate(def_config, {})
35
36
37 def test_already_at_version(temp_db_with_extensions, def_config, property_table):
38
39     property_table.set('database_version',
40                        str(nominatim_db.version.NOMINATIM_VERSION))
41
42     assert migration.migrate(def_config, {}) == 0
43
44
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)))
50
51     done = {'old': False, 'new': False}
52
53     def _migration(**_):
54         """ Dummy migration"""
55         done['new'] = True
56
57     def _old_migration(**_):
58         """ Dummy migration"""
59         done['old'] = True
60
61     oldversion[1] = 0
62     monkeypatch.setattr(migration, '_MIGRATION_FUNCTIONS',
63                         [(tuple(oldversion), _old_migration),
64                          (nominatim_db.version.NOMINATIM_VERSION, _migration)])
65
66     assert migration.migrate(def_config, {}) == 0
67
68     assert done['new']
69     assert not done['old']
70     assert property_table.get('database_version') == str(nominatim_db.version.NOMINATIM_VERSION)
71
72
73 # Tests for specific migrations
74 #
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.