]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_sp_importer.py
Removed magic mocking, using monkeypatch instead, and using a placex table to simulat...
[nominatim.git] / test / python / tools / test_sp_importer.py
1 import pytest
2 import tempfile
3 import json
4 import os
5
6 from nominatim_db.tools.special_phrases.sp_importer import SPImporter
7
8 # Testing Style Class Pair Retrival
9 @pytest.fixture
10 def sample_style_file():
11     sample_data = [
12         {
13             "keys" : ["emergency"],
14             "values" : {
15                 "fire_hydrant" : "skip",
16                 "yes" : "skip",
17                 "no" : "skip",
18                 "" : "main"
19             }
20         },
21         {
22             "keys" : ["historic", "military"],
23             "values" : {
24                 "no" : "skip",
25                 "yes" : "skip",
26                 "" : "main"
27             }
28         },
29         {
30             "keys" : ["name:prefix", "name:suffix", "name:prefix:*", "name:suffix:*",
31                     "name:botanical", "wikidata", "*:wikidata"],
32             "values" : {
33                 "" : "extra"
34             }
35         },
36         {
37             "keys" : ["addr:housename"],
38             "values" : {
39                 "" : "name,house"
40             }
41         },
42         {
43             "keys": ["highway"],
44             "values": {
45                 "motorway": "main",
46                 "": "skip"
47             }
48         }
49     ]
50     content = ",".join(json.dumps(entry) for entry in sample_data)
51
52     with tempfile.NamedTemporaryFile(mode='w+', delete=False) as tmp:
53         tmp.write(content)
54         tmp_path = tmp.name
55
56     yield tmp_path
57     os.remove(tmp_path)
58
59 def test_get_classtype_style(sample_style_file):
60     class Config:
61         def get_import_style_file(self):
62             return sample_style_file
63         
64         def load_sub_configuration(self, name):
65             return {'blackList': {}, 'whiteList': {}}
66
67     config = Config()
68     importer = SPImporter(config=config, conn=None, sp_loader=None)
69
70     result = importer.get_classtype_pairs_style()
71
72     expected = {
73         ("highway", "motorway"),
74     }
75
76     assert expected.issubset(result)
77
78 # Testing Database Class Pair Retrival using Mock Database
79 def test_get_classtype_pairs(monkeypatch):
80     class Config:
81         def load_sub_configuration(self, path, section=None):
82             return {"blackList": {}, "whiteList": {}}
83
84     class Cursor:
85         def execute(self, query): pass
86         def fetchall(self):
87             return [
88                 ("highway", "motorway"),  
89                 ("historic", "castle")    
90             ]
91         def __enter__(self): return self
92         def __exit__(self, exc_type, exc_val, exc_tb): pass
93
94     class Connection:
95         def cursor(self): return Cursor()
96
97     config = Config()
98     conn = Connection()
99     importer = SPImporter(config=config, conn=conn, sp_loader=None)
100
101     result = importer.get_classtype_pairs()
102
103     expected = {
104         ("highway", "motorway"),
105         ("historic", "castle")
106     }
107
108     assert result == expected
109
110 # Testing Database Class Pair Retrival using Conftest.py and placex
111 def test_get_classtype_pair_data(placex_table, temp_db_conn):
112     class Config:
113         def load_sub_configuration(self, *_):
114             return {'blackList': {}, 'whiteList': {}}
115         
116     for _ in range(101):
117         placex_table.add(cls='highway', typ='motorway') # edge case 101
118
119     for _ in range(99):
120         placex_table.add(cls='amenity', typ='prison') # edge case 99
121
122     for _ in range(150):
123         placex_table.add(cls='tourism', typ='hotel')
124
125     config = Config()
126     importer = SPImporter(config=config, conn=temp_db_conn, sp_loader=None)
127
128     result = importer.get_classtype_pairs()
129
130     expected = {
131         ("highway", "motorway"),
132         ("tourism", "hotel")
133     }
134
135     assert result == expected, f"Expected {expected}, got {result}"
136
137 def test_get_classtype_pair_data_more(placex_table, temp_db_conn):
138     class Config:
139         def load_sub_configuration(self, *_):
140             return {'blackList': {}, 'whiteList': {}}
141         
142     for _ in range(100):
143         placex_table.add(cls='emergency', typ='firehydrant') # edge case 100, not included
144
145     for _ in range(199):
146         placex_table.add(cls='amenity', typ='prison') 
147
148     for _ in range(3478):
149         placex_table.add(cls='tourism', typ='hotel')
150
151     config = Config()
152     importer = SPImporter(config=config, conn=temp_db_conn, sp_loader=None)
153
154     result = importer.get_classtype_pairs()
155
156     expected = {
157         ("amenity", "prison"),
158         ("tourism", "hotel")
159     }
160
161     assert result == expected, f"Expected {expected}, got {result}"