]> git.openstreetmap.org Git - nominatim.git/blob - test/python/mocks.py
Merge pull request #2707 from lonvia/make-icu-tokenizer-the-default
[nominatim.git] / test / python / mocks.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Custom mocks for testing.
9 """
10 import itertools
11
12 import psycopg2.extras
13
14 from nominatim.db import properties
15
16 # This must always point to the mock word table for the default tokenizer.
17 from mock_icu_word_table import MockIcuWordTable as MockWordTable
18
19 class MockPlacexTable:
20     """ A placex table for testing.
21     """
22     def __init__(self, conn):
23         self.idseq = itertools.count(10000)
24         self.conn = conn
25         with conn.cursor() as cur:
26             cur.execute("""CREATE TABLE placex (
27                                place_id BIGINT,
28                                parent_place_id BIGINT,
29                                linked_place_id BIGINT,
30                                importance FLOAT,
31                                indexed_date TIMESTAMP,
32                                geometry_sector INTEGER,
33                                rank_address SMALLINT,
34                                rank_search SMALLINT,
35                                partition SMALLINT,
36                                indexed_status SMALLINT,
37                                osm_id int8,
38                                osm_type char(1),
39                                class text,
40                                type text,
41                                name hstore,
42                                admin_level smallint,
43                                address hstore,
44                                extratags hstore,
45                                geometry Geometry(Geometry,4326),
46                                wikipedia TEXT,
47                                country_code varchar(2),
48                                housenumber TEXT,
49                                postcode TEXT,
50                                centroid GEOMETRY(Geometry, 4326))""")
51             cur.execute("CREATE SEQUENCE IF NOT EXISTS seq_place")
52         conn.commit()
53
54     def add(self, osm_type='N', osm_id=None, cls='amenity', typ='cafe', names=None,
55             admin_level=None, address=None, extratags=None, geom='POINT(10 4)',
56             country=None, housenumber=None):
57         with self.conn.cursor() as cur:
58             psycopg2.extras.register_hstore(cur)
59             cur.execute("""INSERT INTO placex (place_id, osm_type, osm_id, class,
60                                                type, name, admin_level, address,
61                                                housenumber,
62                                                extratags, geometry, country_code)
63                             VALUES(nextval('seq_place'), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
64                         (osm_type, osm_id or next(self.idseq), cls, typ, names,
65                          admin_level, address, housenumber, extratags, 'SRID=4326;' + geom,
66                          country))
67         self.conn.commit()
68
69
70 class MockPropertyTable:
71     """ A property table for testing.
72     """
73     def __init__(self, conn):
74         self.conn = conn
75
76
77     def set(self, name, value):
78         """ Set a property in the table to the given value.
79         """
80         properties.set_property(self.conn, name, value)
81
82
83     def get(self, name):
84         """ Set a property in the table to the given value.
85         """
86         return properties.get_property(self.conn, name)