1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2025 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Custom mocks for testing.
 
  12 from nominatim_db.db import properties
 
  15 class MockPlacexTable:
 
  16     """ A placex table for testing.
 
  18     def __init__(self, conn):
 
  19         self.idseq = itertools.count(10000)
 
  21         with conn.cursor() as cur:
 
  22             cur.execute("""CREATE TABLE placex (
 
  24                                parent_place_id BIGINT,
 
  25                                linked_place_id BIGINT,
 
  27                                indexed_date TIMESTAMP,
 
  28                                geometry_sector INTEGER,
 
  29                                rank_address SMALLINT,
 
  32                                indexed_status SMALLINT,
 
  42                                geometry Geometry(Geometry,4326),
 
  44                                country_code varchar(2),
 
  47                                centroid GEOMETRY(Geometry, 4326))""")
 
  48             cur.execute("CREATE SEQUENCE IF NOT EXISTS seq_place")
 
  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                             RETURNING place_id""",
 
  62                         (osm_type, osm_id or next(self.idseq), cls, typ, names,
 
  63                          admin_level, address, housenumber, rank_search,
 
  64                          extratags, 'SRID=4326;' + geom,
 
  66             place_id = cur.fetchone()[0]
 
  71 class MockPropertyTable:
 
  72     """ A property table for testing.
 
  74     def __init__(self, conn):
 
  77     def set(self, name, value):
 
  78         """ Set a property in the table to the given value.
 
  80         properties.set_property(self.conn, name, value)
 
  83         """ Set a property in the table to the given value.
 
  85         return properties.get_property(self.conn, name)