1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Legacy word table for testing with functions to prefil and test contents
11 from nominatim_db.db.connection import execute_scalar
13 class MockIcuWordTable:
14 """ A word table for testing using legacy word table structure.
16 def __init__(self, conn):
18 with conn.cursor() as cur:
19 cur.execute("""CREATE TABLE word (word_id INTEGER,
20 word_token text NOT NULL,
27 def add_full_word(self, word_id, word, word_token=None):
28 with self.conn.cursor() as cur:
29 cur.execute("""INSERT INTO word (word_id, word_token, type, word, info)
30 VALUES(%s, %s, 'W', %s, '{}'::jsonb)""",
31 (word_id, word or word_token, word))
35 def add_special(self, word_token, word, cls, typ, oper):
36 with self.conn.cursor() as cur:
37 cur.execute("""INSERT INTO word (word_token, type, word, info)
39 json_build_object('class', %s::text,
42 """, (word_token, word, cls, typ, oper))
46 def add_country(self, country_code, word_token):
47 with self.conn.cursor() as cur:
48 cur.execute("""INSERT INTO word (word_token, type, word)
49 VALUES(%s, 'C', %s)""",
50 (word_token, country_code))
54 def add_postcode(self, word_token, postcode):
55 with self.conn.cursor() as cur:
56 cur.execute("""INSERT INTO word (word_token, type, word)
58 """, (word_token, postcode))
62 def add_housenumber(self, word_id, word_tokens, word=None):
63 with self.conn.cursor() as cur:
64 if isinstance(word_tokens, str):
65 # old style without analyser
66 cur.execute("""INSERT INTO word (word_id, word_token, type)
68 """, (word_id, word_tokens))
72 for token in word_tokens:
73 cur.execute("""INSERT INTO word (word_id, word_token, type, word, info)
74 VALUES (%s, %s, 'H', %s, jsonb_build_object('lookup', %s::text))
75 """, (word_id, token, word, word_tokens[0]))
81 return execute_scalar(self.conn, "SELECT count(*) FROM word")
84 def count_special(self):
85 return execute_scalar(self.conn, "SELECT count(*) FROM word WHERE type = 'S'")
88 def count_housenumbers(self):
89 return execute_scalar(self.conn, "SELECT count(*) FROM word WHERE type = 'H'")
92 def get_special(self):
93 with self.conn.cursor() as cur:
94 cur.execute("SELECT word_token, info, word FROM word WHERE type = 'S'")
95 result = set(((row[0], row[2], row[1]['class'],
96 row[1]['type'], row[1]['op']) for row in cur))
97 assert len(result) == cur.rowcount, "Word table has duplicates."
101 def get_country(self):
102 with self.conn.cursor() as cur:
103 cur.execute("SELECT word, word_token FROM word WHERE type = 'C'")
104 result = set((tuple(row) for row in cur))
105 assert len(result) == cur.rowcount, "Word table has duplicates."
109 def get_postcodes(self):
110 with self.conn.cursor() as cur:
111 cur.execute("SELECT word FROM word WHERE type = 'P'")
112 return set((row[0] for row in cur))
115 def get_partial_words(self):
116 with self.conn.cursor() as cur:
117 cur.execute("SELECT word_token, info FROM word WHERE type ='w'")
118 return set(((row[0], row[1]['count']) for row in cur))