+class LazyFmt(object):
+
+ def __init__(self, fmtstr, *args):
+ self.fmt = fmtstr
+ self.args = args
+
+ def __str__(self):
+ return self.fmt % self.args
+
+class PlaceObjName(object):
+
+ def __init__(self, placeid, conn):
+ self.pid = placeid
+ self.conn = conn
+
+ def __str__(self):
+ if self.pid is None:
+ return "<null>"
+
+ cur = self.conn.cursor()
+ cur.execute("""SELECT osm_type, osm_id, class
+ FROM placex WHERE place_id = %s""",
+ (self.pid, ))
+ eq_(1, cur.rowcount, "No entry found for place id %s" % self.pid)
+
+ return "%s%s:%s" % cur.fetchone()
+
+def compare_place_id(expected, result, column, context):
+ if expected == '0':
+ eq_(0, result,
+ LazyFmt("Bad place id in column %s. Expected: 0, got: %s.",
+ column, PlaceObjName(result, context.db)))
+ elif expected == '-':
+ assert_is_none(result,
+ LazyFmt("bad place id in column %s: %s.",
+ column, PlaceObjName(result, context.db)))
+ else:
+ eq_(NominatimID(expected).get_place_id(context.db.cursor()), result,
+ LazyFmt("Bad place id in column %s. Expected: %s, got: %s.",
+ column, expected, PlaceObjName(result, context.db)))
+
+def check_database_integrity(context):
+ """ Check some generic constraints on the tables.
+ """
+ # place_addressline should not have duplicate (place_id, address_place_id)
+ cur = context.db.cursor()
+ cur.execute("""SELECT count(*) FROM
+ (SELECT place_id, address_place_id, count(*) as c
+ FROM place_addressline GROUP BY place_id, address_place_id) x
+ WHERE c > 1""")
+ eq_(0, cur.fetchone()[0], "Duplicates found in place_addressline")
+
+