]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_migration.py
Merge pull request #3588 from lonvia/optional-reverse-api
[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) 2024 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 from nominatim_db.db.connection import server_version_tuple
15 import nominatim_db.version
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     def _migration(**_):
53         """ Dummy migration"""
54         done['new'] = True
55
56     def _old_migration(**_):
57         """ Dummy migration"""
58         done['old'] = True
59
60     oldversion[1] = 0
61     monkeypatch.setattr(migration, '_MIGRATION_FUNCTIONS',
62                         [(tuple(oldversion), _old_migration),
63                          (nominatim_db.version.NOMINATIM_VERSION, _migration)])
64
65     assert migration.migrate(def_config, {}) == 0
66
67     assert done['new']
68     assert not done['old']
69     assert property_table.get('database_version') == str(nominatim_db.version.NOMINATIM_VERSION)
70
71
72 ###### Tests for specific migrations
73 #
74 # Each migration should come with two tests:
75 #  1. Test that migration from old to new state works as expected.
76 #  2. Test that the migration can be rerun on the new state without side effects.