2 Tests for import special phrases functions
5 from nominatim.tools.special_phrases import _create_place_classtype_indexes, _create_place_classtype_table, _get_wiki_content, _grant_access_to_webuser, _process_amenity
7 def test_get_wiki_content():
8 assert _get_wiki_content('fr')
10 def execute_and_verify_add_word(temp_db_conn, phrase_label, normalized_label,
11 phrase_class, phrase_type):
12 _process_amenity(temp_db_conn, phrase_label, normalized_label,
13 phrase_class, phrase_type, '')
15 with temp_db_conn.cursor() as temp_db_cursor:
16 temp_db_cursor.execute(f"""
18 WHERE word_token=' {normalized_label}'
19 AND word='{normalized_label}'
20 AND class='{phrase_class}'
21 AND type='{phrase_type}'
22 AND type='{phrase_type}'""")
23 return temp_db_cursor.fetchone()
25 def execute_and_verify_add_word_with_operator(temp_db_conn, phrase_label, normalized_label,
26 phrase_class, phrase_type, phrase_operator):
27 _process_amenity(temp_db_conn, phrase_label, normalized_label,
28 phrase_class, phrase_type, phrase_operator)
30 with temp_db_conn.cursor() as temp_db_cursor:
31 temp_db_cursor.execute(f"""
33 WHERE word_token=' {normalized_label}'
34 AND word='{normalized_label}'
35 AND class='{phrase_class}'
36 AND type='{phrase_type}'
37 AND operator='{phrase_operator}'""")
38 return temp_db_cursor.fetchone()
40 def test_process_amenity_with_near_operator(temp_db_conn, word_table, amenity_operator_funcs):
41 phrase_label = ' label '
42 normalized_label = 'label'
43 phrase_class = 'class'
46 assert execute_and_verify_add_word(temp_db_conn, phrase_label, normalized_label,
47 phrase_class, phrase_type)
48 assert execute_and_verify_add_word_with_operator(temp_db_conn, phrase_label, normalized_label,
49 phrase_class, phrase_type, 'near')
50 assert execute_and_verify_add_word_with_operator(temp_db_conn, phrase_label, normalized_label,
51 phrase_class, phrase_type, 'in')
53 def index_exists(db_connect, index):
54 """ Check that an index with the given name exists in the database.
56 with db_connect.cursor() as cur:
57 cur.execute("""SELECT tablename FROM pg_indexes
58 WHERE indexname = %s and schemaname = 'public'""", (index, ))
63 def test_create_place_classtype_indexes(temp_db_conn):
64 phrase_class = 'class'
66 table_name = f'place_classtype_{phrase_class}_{phrase_type}'
68 with temp_db_conn.cursor() as temp_db_cursor:
69 temp_db_cursor.execute("CREATE EXTENSION postgis;")
70 temp_db_cursor.execute(f'CREATE TABLE {table_name}(place_id BIGINT, centroid GEOMETRY)')
72 _create_place_classtype_indexes(temp_db_conn, '', phrase_class, phrase_type)
74 centroid_index_exists = index_exists(temp_db_conn, f'idx_place_classtype_{phrase_class}_{phrase_type}_centroid')
75 place_id_index_exists = index_exists(temp_db_conn, f'idx_place_classtype_{phrase_class}_{phrase_type}_place_id')
77 assert centroid_index_exists and place_id_index_exists
79 def test_create_place_classtype_table(temp_db_conn, placex_table):
80 phrase_class = 'class'
82 _create_place_classtype_table(temp_db_conn, '', phrase_class, phrase_type)
84 with temp_db_conn.cursor() as temp_db_cursor:
85 temp_db_cursor.execute(f"""
87 FROM information_schema.tables
88 WHERE table_type='BASE TABLE'
89 AND table_name='place_classtype_{phrase_class}_{phrase_type}'""")
90 result = temp_db_cursor.fetchone()
93 def test_grant_access_to_web_user(temp_db_conn, def_config):
94 phrase_class = 'class'
96 table_name = f'place_classtype_{phrase_class}_{phrase_type}'
98 with temp_db_conn.cursor() as temp_db_cursor:
99 temp_db_cursor.execute(f'CREATE TABLE {table_name}()')
101 _grant_access_to_webuser(temp_db_conn, def_config, phrase_class, phrase_type)
103 with temp_db_conn.cursor() as temp_db_cursor:
104 temp_db_cursor.execute(f"""
105 SELECT * FROM information_schema.role_table_grants
106 WHERE table_name='{table_name}'
107 AND grantee='{def_config.DATABASE_WEBUSER}'
108 AND privilege_type='SELECT'""")
109 result = temp_db_cursor.fetchone()
113 def amenity_operator_funcs(temp_db_cursor):
114 temp_db_cursor.execute(f"""
115 CREATE OR REPLACE FUNCTION make_standard_name(name TEXT) RETURNS TEXT
120 RETURN trim(name); --Basically return only the trimed name for the tests
123 LANGUAGE plpgsql IMMUTABLE;
125 CREATE SEQUENCE seq_word start 1;
127 CREATE OR REPLACE FUNCTION getorcreate_amenity(lookup_word TEXT, normalized_word TEXT,
128 lookup_class text, lookup_type text)
133 return_word_id INTEGER;
135 lookup_token := ' '||trim(lookup_word);
136 SELECT min(word_id) FROM word
137 WHERE word_token = lookup_token and word = normalized_word
138 and class = lookup_class and type = lookup_type
140 IF return_word_id IS NULL THEN
141 return_word_id := nextval('seq_word');
142 INSERT INTO word VALUES (return_word_id, lookup_token, normalized_word,
143 lookup_class, lookup_type, null, 0);
145 RETURN return_word_id;
150 CREATE OR REPLACE FUNCTION getorcreate_amenityoperator(lookup_word TEXT,
151 normalized_word TEXT,
159 return_word_id INTEGER;
161 lookup_token := ' '||trim(lookup_word);
162 SELECT min(word_id) FROM word
163 WHERE word_token = lookup_token and word = normalized_word
164 and class = lookup_class and type = lookup_type and operator = op
166 IF return_word_id IS NULL THEN
167 return_word_id := nextval('seq_word');
168 INSERT INTO word VALUES (return_word_id, lookup_token, normalized_word,
169 lookup_class, lookup_type, null, 0, op);
171 RETURN return_word_id;
174 LANGUAGE plpgsql;""")