]> git.openstreetmap.org Git - nominatim.git/blob - test/python/dummy_tokenizer.py
Merge pull request #3670 from lonvia/flake-for-tests
[nominatim.git] / test / python / dummy_tokenizer.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 Tokenizer for testing.
9 """
10 from nominatim_db.data.place_info import PlaceInfo
11 from nominatim_db.config import Configuration
12
13
14 def create(dsn, data_dir):
15     """ Create a new instance of the tokenizer provided by this module.
16     """
17     return DummyTokenizer(dsn, data_dir)
18
19
20 class DummyTokenizer:
21
22     def __init__(self, dsn, data_dir):
23         self.dsn = dsn
24         self.data_dir = data_dir
25         self.init_state = None
26         self.analyser_cache = {}
27
28     def init_new_db(self, *args, **kwargs):
29         assert self.init_state is None
30         self.init_state = "new"
31
32     def init_from_project(self, config):
33         assert isinstance(config, Configuration)
34         assert self.init_state is None
35         self.init_state = "loaded"
36
37     @staticmethod
38     def finalize_import(_):
39         pass
40
41     def name_analyzer(self):
42         return DummyNameAnalyzer(self.analyser_cache)
43
44
45 class DummyNameAnalyzer:
46
47     def __enter__(self):
48         return self
49
50     def __exit__(self, exc_type, exc_value, traceback):
51         self.close()
52
53     def __init__(self, cache):
54         self.analyser_cache = cache
55         cache['countries'] = []
56
57     def close(self):
58         pass
59
60     @staticmethod
61     def normalize_postcode(postcode):
62         return postcode
63
64     @staticmethod
65     def update_postcodes_from_db():
66         pass
67
68     def update_special_phrases(self, phrases, should_replace):
69         self.analyser_cache['special_phrases'] = phrases
70
71     def add_country_names(self, code, names):
72         self.analyser_cache['countries'].append((code, names))
73
74     @staticmethod
75     def process_place(place):
76         assert isinstance(place, PlaceInfo)
77         return {}