def match_row(self, row): if 'ID' in row.headings: todo = [int(row['ID'])] else: todo = range(len(self.result)) for i in todo: res = self.result[i] for h in row.headings: if h == 'ID': pass elif h == 'osm': assert res['osm_type'] == row[h][0] assert res['osm_id'] == int(row[h][1:]) elif h == 'centroid': x, y = row[h].split(' ') assert Almost(float(y)) == float(res['lat']) assert Almost(float(x)) == float(res['lon']) elif row[h].startswith("^"): assert h in res assert re.fullmatch(row[h], res[h]) is not None, \ "attribute '%s': expected: '%s', got '%s'" % (h, row[h], res[h]) else: assert h in res assert str(res[h]) == str(row[h])
def assert_db_column(row, column, value, context): if column == 'object': return if column.startswith('centroid'): if value == 'in geometry': query = """SELECT ST_Within(ST_SetSRID(ST_Point({}, {}), 4326), ST_SetSRID('{}'::geometry, 4326))""".format( row['cx'], row['cy'], row['geomtxt']) cur = context.db.cursor() cur.execute(query) assert cur.fetchone()[0], "(Row %s failed: %s)" % (column, query) else: fac = float(column[9:]) if column.startswith('centroid*') else 1.0 x, y = value.split(' ') assert Almost(float(x) * fac) == row['cx'], "Bad x coordinate" assert Almost(float(y) * fac) == row['cy'], "Bad y coordinate" elif column == 'geometry': geom = context.osm.parse_geometry(value, context.scene) cur = context.db.cursor() query = "SELECT ST_Equals(ST_SnapToGrid(%s, 0.00001, 0.00001), ST_SnapToGrid(ST_SetSRID('%s'::geometry, 4326), 0.00001, 0.00001))" % ( geom, row['geomtxt'], ) cur.execute(query) assert cur.fetchone()[0], "(Row %s failed: %s)" % (column, query) elif value == '-': assert row[column] is None, "Row %s" % column else: assert value == str(row[column]), \ "Row '%s': expected: %s, got: %s" % (column, value, str(row[column]))
def assert_field(self, idx, field, value): """ Check that result row `idx` has a field `field` with value `value`. Float numbers are matched approximately. When the expected value starts with a carat, regular expression matching is used. """ assert field in self.result[idx], \ "Result row {} has no field '{}'.\nFull row: {}"\ .format(idx, field, json.dumps(self.result[idx], indent=4)) if isinstance(value, float): assert Almost(value) == float(self.result[idx][field]), \ BadRowValueAssert(self, idx, field, value) elif value.startswith("^"): assert re.fullmatch(value, self.result[idx][field]), \ BadRowValueAssert(self, idx, field, value) else: assert str(self.result[idx][field]) == str(value), \ BadRowValueAssert(self, idx, field, value)