]> git.openstreetmap.org Git - nominatim.git/blobdiff - test/python/mocks.py
switch to a more flexible variant description format
[nominatim.git] / test / python / mocks.py
index 53d84aace63b2783ac87c63f2be2bda389324696..f9faaa93c42e06fac7fdb11fe3fb6abeb132bd5e 100644 (file)
@@ -5,6 +5,8 @@ import itertools
 
 import psycopg2.extras
 
+from nominatim.db import properties
+
 class MockParamCapture:
     """ Mock that records the parameters with which a function was called
         as well as the number of calls.
@@ -12,6 +14,8 @@ class MockParamCapture:
     def __init__(self, retval=0):
         self.called = 0
         self.return_value = retval
+        self.last_args = None
+        self.last_kwargs = None
 
     def __call__(self, *args, **kwargs):
         self.called += 1
@@ -37,11 +41,18 @@ class MockWordTable:
 
         conn.commit()
 
-    def add_special(self, word_token, word, cls, typ, op):
+    def add_special(self, word_token, word, cls, typ, oper):
         with self.conn.cursor() as cur:
             cur.execute("""INSERT INTO word (word_token, word, class, type, operator)
                               VALUES (%s, %s, %s, %s, %s)
-                        """, (word_token, word, cls, typ, op))
+                        """, (word_token, word, cls, typ, oper))
+        self.conn.commit()
+
+
+    def add_country(self, country_code, word_token):
+        with self.conn.cursor() as cur:
+            cur.execute("INSERT INTO word (word_token, country_code) VALUES(%s, %s)",
+                        (word_token, country_code))
         self.conn.commit()
 
 
@@ -67,7 +78,18 @@ class MockWordTable:
         with self.conn.cursor() as cur:
             cur.execute("""SELECT word_token, word, class, type, operator
                            FROM word WHERE class != 'place'""")
-            return set((tuple(row) for row in cur))
+            result = set((tuple(row) for row in cur))
+            assert len(result) == cur.rowcount, "Word table has duplicates."
+            return result
+
+
+    def get_country(self):
+        with self.conn.cursor() as cur:
+            cur.execute("""SELECT country_code, word_token
+                           FROM word WHERE country_code is not null""")
+            result = set((tuple(row) for row in cur))
+            assert len(result) == cur.rowcount, "Word table has duplicates."
+            return result
 
 
     def get_postcodes(self):
@@ -76,6 +98,13 @@ class MockWordTable:
                            WHERE class = 'place' and type = 'postcode'""")
             return set((row[0] for row in cur))
 
+    def get_partial_words(self):
+        with self.conn.cursor() as cur:
+            cur.execute("""SELECT word_token, search_name_count FROM word
+                           WHERE class is null and country_code is null
+                                 and not word_token like ' %'""")
+            return set((tuple(row) for row in cur))
+
 
 class MockPlacexTable:
     """ A placex table for testing.
@@ -125,3 +154,16 @@ class MockPlacexTable:
                          admin_level, address, extratags, 'SRID=4326;' + geom,
                          country))
         self.conn.commit()
+
+
+class MockPropertyTable:
+    """ A property table for testing.
+    """
+    def __init__(self, conn):
+        self.conn = conn
+
+
+    def set(self, name, value):
+        """ Set a property in the table to the given value.
+        """
+        properties.set_property(self.conn, name, value)