2 Legacy word table for testing with functions to prefil and test contents
6 class MockIcuWordTable:
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,
13 word_token text NOT NULL,
20 def add_full_word(self, word_id, word, word_token=None):
21 with self.conn.cursor() as cur:
22 cur.execute("""INSERT INTO word (word_id, word_token, type, word, info)
23 VALUES(%s, %s, 'W', %s, '{}'::jsonb)""",
24 (word_id, word or word_token, word))
28 def add_special(self, word_token, word, cls, typ, oper):
29 with self.conn.cursor() as cur:
30 cur.execute("""INSERT INTO word (word_token, type, word, info)
32 json_build_object('class', %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, type, word)
42 VALUES(%s, 'C', %s)""",
43 (word_token, country_code))
47 def add_postcode(self, word_token, postcode):
48 with self.conn.cursor() as cur:
49 cur.execute("""INSERT INTO word (word_token, type, word)
51 """, (word_token, postcode))
56 with self.conn.cursor() as cur:
57 return cur.scalar("SELECT count(*) FROM word")
60 def count_special(self):
61 with self.conn.cursor() as cur:
62 return cur.scalar("SELECT count(*) FROM word WHERE type = 'S'")
65 def get_special(self):
66 with self.conn.cursor() as cur:
67 cur.execute("SELECT word_token, info, word FROM word WHERE type = 'S'")
68 result = set(((row[0], row[2], row[1]['class'],
69 row[1]['type'], row[1]['op']) for row in cur))
70 assert len(result) == cur.rowcount, "Word table has duplicates."
74 def get_country(self):
75 with self.conn.cursor() as cur:
76 cur.execute("SELECT word, word_token FROM word WHERE type = 'C'")
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 WHERE type = 'P'")
85 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, info FROM word WHERE type ='w'")
91 return set(((row[0], row[1]['count']) for row in cur))