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_special(self, word_token, word, cls, typ, oper):
 
  24         with self.conn.cursor() as cur:
 
  25             cur.execute("""INSERT INTO word (word_token, word, class, type, operator)
 
  26                               VALUES (%s, %s, %s, %s, %s)
 
  27                         """, (word_token, word, cls, typ, oper))
 
  31     def add_country(self, country_code, word_token):
 
  32         with self.conn.cursor() as cur:
 
  33             cur.execute("INSERT INTO word (word_token, country_code) VALUES(%s, %s)",
 
  34                         (word_token, country_code))
 
  38     def add_postcode(self, word_token, postcode):
 
  39         with self.conn.cursor() as cur:
 
  40             cur.execute("""INSERT INTO word (word_token, word, class, type)
 
  41                               VALUES (%s, %s, 'place', 'postcode')
 
  42                         """, (word_token, postcode))
 
  47         with self.conn.cursor() as cur:
 
  48             return cur.scalar("SELECT count(*) FROM word")
 
  51     def count_special(self):
 
  52         with self.conn.cursor() as cur:
 
  53             return cur.scalar("SELECT count(*) FROM word WHERE class != 'place'")
 
  56     def get_special(self):
 
  57         with self.conn.cursor() as cur:
 
  58             cur.execute("""SELECT word_token, word, class, type, operator
 
  59                            FROM word WHERE class != 'place'""")
 
  60             result = set((tuple(row) for row in cur))
 
  61             assert len(result) == cur.rowcount, "Word table has duplicates."
 
  65     def get_country(self):
 
  66         with self.conn.cursor() as cur:
 
  67             cur.execute("""SELECT country_code, word_token
 
  68                            FROM word WHERE country_code is not null""")
 
  69             result = set((tuple(row) for row in cur))
 
  70             assert len(result) == cur.rowcount, "Word table has duplicates."
 
  74     def get_postcodes(self):
 
  75         with self.conn.cursor() as cur:
 
  76             cur.execute("""SELECT word FROM word
 
  77                            WHERE class = 'place' and type = 'postcode'""")
 
  78             return set((row[0] for row in cur))
 
  80     def get_partial_words(self):
 
  81         with self.conn.cursor() as cur:
 
  82             cur.execute("""SELECT word_token, search_name_count FROM word
 
  83                            WHERE class is null and country_code is null
 
  84                                  and not word_token like ' %'""")
 
  85             return set((tuple(row) for row in cur))