2 Legacy word table for testing with functions to prefil and test contents
6 class MockLegacyWordTable:
7 """ A word table for testing using legacy word table structure.
9 def __init__(self, conn):
11 with conn.cursor() as cur:
12 cur.execute("""CREATE TABLE word (word_id INTEGER,
17 country_code varchar(2),
18 search_name_count INTEGER,
23 def add_full_word(self, word_id, word, word_token=None):
24 with self.conn.cursor() as cur:
25 cur.execute("""INSERT INTO word (word_id, word_token, word)
27 """, (word_id, ' ' + (word_token or word), word))
31 def add_special(self, word_token, word, cls, typ, oper):
32 with self.conn.cursor() as cur:
33 cur.execute("""INSERT INTO word (word_token, word, class, type, operator)
34 VALUES (%s, %s, %s, %s, %s)
35 """, (word_token, word, cls, typ, oper))
39 def add_country(self, country_code, word_token):
40 with self.conn.cursor() as cur:
41 cur.execute("INSERT INTO word (word_token, country_code) VALUES(%s, %s)",
42 (word_token, country_code))
46 def add_postcode(self, word_token, postcode):
47 with self.conn.cursor() as cur:
48 cur.execute("""INSERT INTO word (word_token, word, class, type)
49 VALUES (%s, %s, 'place', 'postcode')
50 """, (word_token, postcode))
55 with self.conn.cursor() as cur:
56 return cur.scalar("SELECT count(*) FROM word")
59 def count_special(self):
60 with self.conn.cursor() as cur:
61 return cur.scalar("SELECT count(*) FROM word WHERE class != 'place'")
64 def get_special(self):
65 with self.conn.cursor() as cur:
66 cur.execute("""SELECT word_token, word, class, type, operator
67 FROM word WHERE class != 'place'""")
68 result = set((tuple(row) for row in cur))
69 assert len(result) == cur.rowcount, "Word table has duplicates."
73 def get_country(self):
74 with self.conn.cursor() as cur:
75 cur.execute("""SELECT country_code, word_token
76 FROM word WHERE country_code is not null""")
77 result = set((tuple(row) for row in cur))
78 assert len(result) == cur.rowcount, "Word table has duplicates."
82 def get_postcodes(self):
83 with self.conn.cursor() as cur:
84 cur.execute("""SELECT word FROM word
85 WHERE class = 'place' and type = 'postcode'""")
86 return set((row[0] for row in cur))
88 def get_partial_words(self):
89 with self.conn.cursor() as cur:
90 cur.execute("""SELECT word_token, search_name_count FROM word
91 WHERE class is null and country_code is null
92 and not word_token like ' %'""")
93 return set((tuple(row) for row in cur))