]> git.openstreetmap.org Git - nominatim.git/blob - test/python/mocks.py
Merge remote-tracking branch 'upstream/master'
[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 # This must always point to the mock word table for the default tokenizer.
11 from mock_legacy_word_table import MockLegacyWordTable as MockWordTable
12
13 class MockParamCapture:
14     """ Mock that records the parameters with which a function was called
15         as well as the number of calls.
16     """
17     def __init__(self, retval=0):
18         self.called = 0
19         self.return_value = retval
20         self.last_args = None
21         self.last_kwargs = None
22
23     def __call__(self, *args, **kwargs):
24         self.called += 1
25         self.last_args = args
26         self.last_kwargs = kwargs
27         return self.return_value
28
29
30 class MockPlacexTable:
31     """ A placex table for testing.
32     """
33     def __init__(self, conn):
34         self.idseq = itertools.count(10000)
35         self.conn = conn
36         with conn.cursor() as cur:
37             cur.execute("""CREATE TABLE placex (
38                                place_id BIGINT,
39                                parent_place_id BIGINT,
40                                linked_place_id BIGINT,
41                                importance FLOAT,
42                                indexed_date TIMESTAMP,
43                                geometry_sector INTEGER,
44                                rank_address SMALLINT,
45                                rank_search SMALLINT,
46                                partition SMALLINT,
47                                indexed_status SMALLINT,
48                                osm_id int8,
49                                osm_type char(1),
50                                class text,
51                                type text,
52                                name hstore,
53                                admin_level smallint,
54                                address hstore,
55                                extratags hstore,
56                                geometry Geometry(Geometry,4326),
57                                wikipedia TEXT,
58                                country_code varchar(2),
59                                housenumber TEXT,
60                                postcode TEXT,
61                                centroid GEOMETRY(Geometry, 4326))""")
62             cur.execute("CREATE SEQUENCE IF NOT EXISTS seq_place")
63         conn.commit()
64
65     def add(self, osm_type='N', osm_id=None, cls='amenity', typ='cafe', names=None,
66             admin_level=None, address=None, extratags=None, geom='POINT(10 4)',
67             country=None):
68         with self.conn.cursor() as cur:
69             psycopg2.extras.register_hstore(cur)
70             cur.execute("""INSERT INTO placex (place_id, osm_type, osm_id, class,
71                                                type, name, admin_level, address,
72                                                extratags, geometry, country_code)
73                             VALUES(nextval('seq_place'), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
74                         (osm_type, osm_id or next(self.idseq), cls, typ, names,
75                          admin_level, address, extratags, 'SRID=4326;' + geom,
76                          country))
77         self.conn.commit()
78
79
80 class MockPropertyTable:
81     """ A property table for testing.
82     """
83     def __init__(self, conn):
84         self.conn = conn
85
86
87     def set(self, name, value):
88         """ Set a property in the table to the given value.
89         """
90         properties.set_property(self.conn, name, value)