1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 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
12 class MockIcuWordTable:
13 """ A word table for testing using legacy word table structure.
15 def __init__(self, conn):
17 with conn.cursor() as cur:
18 cur.execute("""CREATE TABLE word (word_id INTEGER,
19 word_token text NOT NULL,
26 def add_full_word(self, word_id, word, word_token=None):
27 with self.conn.cursor() as cur:
28 cur.execute("""INSERT INTO word (word_id, word_token, type, word, info)
29 VALUES(%s, %s, 'W', %s, '{}'::jsonb)""",
30 (word_id, word or word_token, word))
34 def add_special(self, word_token, word, cls, typ, oper):
35 with self.conn.cursor() as cur:
36 cur.execute("""INSERT INTO word (word_token, type, word, info)
38 json_build_object('class', %s,
41 """, (word_token, word, cls, typ, oper))
45 def add_country(self, country_code, word_token):
46 with self.conn.cursor() as cur:
47 cur.execute("""INSERT INTO word (word_token, type, word)
48 VALUES(%s, 'C', %s)""",
49 (word_token, country_code))
53 def add_postcode(self, word_token, postcode):
54 with self.conn.cursor() as cur:
55 cur.execute("""INSERT INTO word (word_token, type, word)
57 """, (word_token, postcode))
61 def add_housenumber(self, word_id, word_token):
62 with self.conn.cursor() as cur:
63 cur.execute("""INSERT INTO word (word_id, word_token, type)
65 """, (word_id, word_token))
70 with self.conn.cursor() as cur:
71 return cur.scalar("SELECT count(*) FROM word")
74 def count_special(self):
75 with self.conn.cursor() as cur:
76 return cur.scalar("SELECT count(*) FROM word WHERE type = 'S'")
79 def count_housenumbers(self):
80 with self.conn.cursor() as cur:
81 return cur.scalar("SELECT count(*) FROM word WHERE type = 'H'")
84 def get_special(self):
85 with self.conn.cursor() as cur:
86 cur.execute("SELECT word_token, info, word FROM word WHERE type = 'S'")
87 result = set(((row[0], row[2], row[1]['class'],
88 row[1]['type'], row[1]['op']) for row in cur))
89 assert len(result) == cur.rowcount, "Word table has duplicates."
93 def get_country(self):
94 with self.conn.cursor() as cur:
95 cur.execute("SELECT word, word_token FROM word WHERE type = 'C'")
96 result = set((tuple(row) for row in cur))
97 assert len(result) == cur.rowcount, "Word table has duplicates."
101 def get_postcodes(self):
102 with self.conn.cursor() as cur:
103 cur.execute("SELECT word FROM word WHERE type = 'P'")
104 return set((row[0] for row in cur))
107 def get_partial_words(self):
108 with self.conn.cursor() as cur:
109 cur.execute("SELECT word_token, info FROM word WHERE type ='w'")
110 return set(((row[0], row[1]['count']) for row in cur))