]> git.openstreetmap.org Git - nominatim.git/blob - test/python/mocks.py
Merge pull request #2341 from lonvia/cleanup-python-tests
[nominatim.git] / test / python / mocks.py
1 """
2 Custom mocks for testing.
3 """
4 import itertools
5
6 import psycopg2.extras
7
8 from nominatim.db import properties
9
10 class MockParamCapture:
11     """ Mock that records the parameters with which a function was called
12         as well as the number of calls.
13     """
14     def __init__(self, retval=0):
15         self.called = 0
16         self.return_value = retval
17         self.last_args = None
18         self.last_kwargs = None
19
20     def __call__(self, *args, **kwargs):
21         self.called += 1
22         self.last_args = args
23         self.last_kwargs = kwargs
24         return self.return_value
25
26
27 class MockWordTable:
28     """ A word table for testing.
29     """
30     def __init__(self, conn):
31         self.conn = conn
32         with conn.cursor() as cur:
33             cur.execute("""CREATE TABLE word (word_id INTEGER,
34                                               word_token text,
35                                               word text,
36                                               class text,
37                                               type text,
38                                               country_code varchar(2),
39                                               search_name_count INTEGER,
40                                               operator TEXT)""")
41
42         conn.commit()
43
44     def add_special(self, word_token, word, cls, typ, oper):
45         with self.conn.cursor() as cur:
46             cur.execute("""INSERT INTO word (word_token, word, class, type, operator)
47                               VALUES (%s, %s, %s, %s, %s)
48                         """, (word_token, word, cls, typ, oper))
49         self.conn.commit()
50
51
52     def add_postcode(self, word_token, postcode):
53         with self.conn.cursor() as cur:
54             cur.execute("""INSERT INTO word (word_token, word, class, type)
55                               VALUES (%s, %s, 'place', 'postcode')
56                         """, (word_token, postcode))
57         self.conn.commit()
58
59
60     def count(self):
61         with self.conn.cursor() as cur:
62             return cur.scalar("SELECT count(*) FROM word")
63
64
65     def count_special(self):
66         with self.conn.cursor() as cur:
67             return cur.scalar("SELECT count(*) FROM word WHERE class != 'place'")
68
69
70     def get_special(self):
71         with self.conn.cursor() as cur:
72             cur.execute("""SELECT word_token, word, class, type, operator
73                            FROM word WHERE class != 'place'""")
74             return set((tuple(row) for row in cur))
75
76
77     def get_postcodes(self):
78         with self.conn.cursor() as cur:
79             cur.execute("""SELECT word FROM word
80                            WHERE class = 'place' and type = 'postcode'""")
81             return set((row[0] for row in cur))
82
83
84 class MockPlacexTable:
85     """ A placex table for testing.
86     """
87     def __init__(self, conn):
88         self.idseq = itertools.count(10000)
89         self.conn = conn
90         with conn.cursor() as cur:
91             cur.execute("""CREATE TABLE placex (
92                                place_id BIGINT,
93                                parent_place_id BIGINT,
94                                linked_place_id BIGINT,
95                                importance FLOAT,
96                                indexed_date TIMESTAMP,
97                                geometry_sector INTEGER,
98                                rank_address SMALLINT,
99                                rank_search SMALLINT,
100                                partition SMALLINT,
101                                indexed_status SMALLINT,
102                                osm_id int8,
103                                osm_type char(1),
104                                class text,
105                                type text,
106                                name hstore,
107                                admin_level smallint,
108                                address hstore,
109                                extratags hstore,
110                                geometry Geometry(Geometry,4326),
111                                wikipedia TEXT,
112                                country_code varchar(2),
113                                housenumber TEXT,
114                                postcode TEXT,
115                                centroid GEOMETRY(Geometry, 4326))""")
116             cur.execute("CREATE SEQUENCE IF NOT EXISTS seq_place")
117         conn.commit()
118
119     def add(self, osm_type='N', osm_id=None, cls='amenity', typ='cafe', names=None,
120             admin_level=None, address=None, extratags=None, geom='POINT(10 4)',
121             country=None):
122         with self.conn.cursor() as cur:
123             psycopg2.extras.register_hstore(cur)
124             cur.execute("""INSERT INTO placex (place_id, osm_type, osm_id, class,
125                                                type, name, admin_level, address,
126                                                extratags, geometry, country_code)
127                             VALUES(nextval('seq_place'), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
128                         (osm_type, osm_id or next(self.idseq), cls, typ, names,
129                          admin_level, address, extratags, 'SRID=4326;' + geom,
130                          country))
131         self.conn.commit()
132
133
134 class MockPropertyTable:
135     """ A property table for testing.
136     """
137     def __init__(self, conn):
138         self.conn = conn
139
140
141     def set(self, name, value):
142         """ Set a property in the table to the given value.
143         """
144         properties.set_property(self.conn, name, value)