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