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, phrase_class, phrase_type):
11 _process_amenity(temp_db_conn, phrase_label, phrase_class, phrase_type, '')
13 with temp_db_conn.cursor() as temp_db_cursor:
14 temp_db_cursor.execute(f"""
16 WHERE word_token=' {phrase_label}'
17 AND word='{phrase_label}'
18 AND class='{phrase_class}'
19 AND type='{phrase_type}'""")
20 return temp_db_cursor.fetchone()
22 def execute_and_verify_add_word_with_operator(temp_db_conn, phrase_label, phrase_class, phrase_type, phrase_operator):
23 _process_amenity(temp_db_conn, phrase_label, phrase_class, phrase_type, phrase_operator)
25 with temp_db_conn.cursor() as temp_db_cursor:
26 temp_db_cursor.execute(f"""
28 WHERE word_token=' {phrase_label}'
29 AND word='{phrase_label}'
30 AND class='{phrase_class}'
31 AND type='{phrase_type}'
32 AND operator='{phrase_operator}'""")
33 return temp_db_cursor.fetchone()
35 def test_process_amenity_with_near_operator(temp_db_conn, word_table, amenity_operator_funcs):
36 phrase_label = 'label'
37 phrase_class = 'class'
40 assert execute_and_verify_add_word(temp_db_conn, phrase_label, phrase_class, phrase_type)
41 assert execute_and_verify_add_word_with_operator(temp_db_conn, phrase_label, phrase_class, phrase_type, 'near')
42 assert execute_and_verify_add_word_with_operator(temp_db_conn, phrase_label, phrase_class, phrase_type, 'in')
44 def index_exists(db_connect, index):
45 """ Check that an index with the given name exists in the database.
47 with db_connect.cursor() as cur:
48 cur.execute("""SELECT tablename FROM pg_indexes
49 WHERE indexname = %s and schemaname = 'public'""", (index, ))
54 def test_create_place_classtype_indexes(temp_db_conn):
55 phrase_class = 'class'
57 table_name = f'place_classtype_{phrase_class}_{phrase_type}'
59 with temp_db_conn.cursor() as temp_db_cursor:
60 temp_db_cursor.execute("CREATE EXTENSION postgis;")
61 temp_db_cursor.execute(f'CREATE TABLE {table_name}(place_id BIGINT, centroid GEOMETRY)')
63 _create_place_classtype_indexes(temp_db_conn, '', phrase_class, phrase_type)
65 centroid_index_exists = index_exists(temp_db_conn, f'idx_place_classtype_{phrase_class}_{phrase_type}_centroid')
66 place_id_index_exists = index_exists(temp_db_conn, f'idx_place_classtype_{phrase_class}_{phrase_type}_place_id')
68 assert centroid_index_exists and place_id_index_exists
70 def test_create_place_classtype_table(temp_db_conn, placex_table):
71 phrase_class = 'class'
73 _create_place_classtype_table(temp_db_conn, '', phrase_class, phrase_type)
75 with temp_db_conn.cursor() as temp_db_cursor:
76 temp_db_cursor.execute(f"""
78 FROM information_schema.tables
79 WHERE table_type='BASE TABLE'
80 AND table_name='place_classtype_{phrase_class}_{phrase_type}'""")
81 result = temp_db_cursor.fetchone()
84 def test_grant_access_to_web_user(temp_db_conn, def_config):
85 phrase_class = 'class'
87 table_name = f'place_classtype_{phrase_class}_{phrase_type}'
89 with temp_db_conn.cursor() as temp_db_cursor:
90 temp_db_cursor.execute(f'CREATE TABLE {table_name}()')
92 _grant_access_to_webuser(temp_db_conn, def_config, phrase_class, phrase_type)
94 with temp_db_conn.cursor() as temp_db_cursor:
95 temp_db_cursor.execute(f"""
96 SELECT * FROM information_schema.role_table_grants
97 WHERE table_name='{table_name}'
98 AND grantee='{def_config.DATABASE_WEBUSER}'
99 AND privilege_type='SELECT'""")
100 result = temp_db_cursor.fetchone()
104 def amenity_operator_funcs(temp_db_cursor):
105 temp_db_cursor.execute(f"""
106 CREATE OR REPLACE FUNCTION make_standard_name(name TEXT) RETURNS TEXT
111 RETURN name; --Basically return the same name for the tests
114 LANGUAGE plpgsql IMMUTABLE;
116 CREATE SEQUENCE seq_word start 1;
118 CREATE OR REPLACE FUNCTION getorcreate_amenity(lookup_word TEXT,
119 lookup_class text, lookup_type text)
124 return_word_id INTEGER;
126 lookup_token := ' '||trim(lookup_word);
127 SELECT min(word_id) FROM word
128 WHERE word_token = lookup_token and word = lookup_word
129 and class = lookup_class and type = lookup_type
131 IF return_word_id IS NULL THEN
132 return_word_id := nextval('seq_word');
133 INSERT INTO word VALUES (return_word_id, lookup_token, lookup_word,
134 lookup_class, lookup_type, null, 0);
136 RETURN return_word_id;
141 CREATE OR REPLACE FUNCTION getorcreate_amenityoperator(lookup_word TEXT,
149 return_word_id INTEGER;
151 lookup_token := ' '||trim(lookup_word);
152 SELECT min(word_id) FROM word
153 WHERE word_token = lookup_token and word = lookup_word
154 and class = lookup_class and type = lookup_type and operator = op
156 IF return_word_id IS NULL THEN
157 return_word_id := nextval('seq_word');
158 INSERT INTO word VALUES (return_word_id, lookup_token, lookup_word,
159 lookup_class, lookup_type, null, 0, op);
161 RETURN return_word_id;
164 LANGUAGE plpgsql;""")