2 Custom mocks for testing.
8 class MockParamCapture:
9 """ Mock that records the parameters with which a function was called
10 as well as the number of calls.
12 def __init__(self, retval=0):
14 self.return_value = retval
16 def __call__(self, *args, **kwargs):
19 self.last_kwargs = kwargs
20 return self.return_value
24 """ A word table for testing.
26 def __init__(self, conn):
28 with conn.cursor() as cur:
29 cur.execute("""CREATE TABLE word (word_id INTEGER,
34 country_code varchar(2),
35 search_name_count INTEGER,
40 def add_special(self, word_token, word, cls, typ, op):
41 with self.conn.cursor() as cur:
42 cur.execute("""INSERT INTO word (word_token, word, class, type, operator)
43 VALUES (%s, %s, %s, %s, %s)
44 """, (word_token, word, cls, typ, op))
48 def add_postcode(self, word_token, postcode):
49 with self.conn.cursor() as cur:
50 cur.execute("""INSERT INTO word (word_token, word, class, type)
51 VALUES (%s, %s, 'place', 'postcode')
52 """, (word_token, postcode))
57 with self.conn.cursor() as cur:
58 return cur.scalar("SELECT count(*) FROM word")
61 def count_special(self):
62 with self.conn.cursor() as cur:
63 return cur.scalar("SELECT count(*) FROM word WHERE class != 'place'")
66 def get_special(self):
67 with self.conn.cursor() as cur:
68 cur.execute("""SELECT word_token, word, class, type, operator
69 FROM word WHERE class != 'place'""")
70 return set((tuple(row) for row in cur))
73 def get_postcodes(self):
74 with self.conn.cursor() as cur:
75 cur.execute("""SELECT word FROM word
76 WHERE class = 'place' and type = 'postcode'""")
77 return set((row[0] for row in cur))
80 class MockPlacexTable:
81 """ A placex table for testing.
83 def __init__(self, conn):
84 self.idseq = itertools.count(10000)
86 with conn.cursor() as cur:
87 cur.execute("""CREATE TABLE placex (
89 parent_place_id BIGINT,
90 linked_place_id BIGINT,
92 indexed_date TIMESTAMP,
93 geometry_sector INTEGER,
94 rank_address SMALLINT,
97 indexed_status SMALLINT,
103 admin_level smallint,
106 geometry Geometry(Geometry,4326),
108 country_code varchar(2),
111 centroid GEOMETRY(Geometry, 4326))""")
112 cur.execute("CREATE SEQUENCE IF NOT EXISTS seq_place")
115 def add(self, osm_type='N', osm_id=None, cls='amenity', typ='cafe', names=None,
116 admin_level=None, address=None, extratags=None, geom='POINT(10 4)',
118 with self.conn.cursor() as cur:
119 psycopg2.extras.register_hstore(cur)
120 cur.execute("""INSERT INTO placex (place_id, osm_type, osm_id, class,
121 type, name, admin_level, address,
122 extratags, geometry, country_code)
123 VALUES(nextval('seq_place'), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
124 (osm_type, osm_id or next(self.idseq), cls, typ, names,
125 admin_level, address, extratags, 'SRID=4326;' + geom,