def test_and_or(self): s = SQLValidator("height IS NOT NULL and height > 20") self.assertTrue(s.valid) s = SQLValidator("height IS NOT NULL or height > 20") self.assertTrue(s.valid) s = SQLValidator("height IS NOT NULL or height > 20 and height < 30") self.assertTrue(s.valid)
def test_colons_etc(self): s = SQLValidator("addr:housenumber IS NOT NULL") self.assertFalse(s.valid) self.assertEquals(s.errors,['identifier with colon : must be in double quotes.']) s = SQLValidator("admin_level IS NOT NULL") self.assertTrue(s.valid) s = SQLValidator('"addr:housenumber" IS NOT NULL') self.assertTrue(s.valid) s = SQLValidator('"addr housenumber" IS NOT NULL') self.assertTrue(s.valid)
def test_invalid_sql(self): s = SQLValidator("drop table planet_osm_polygon") self.assertFalse(s.valid) self.assertEquals(s.errors, ['SQL could not be parsed.']) s = SQLValidator("(drop table planet_osm_polygon)") self.assertFalse(s.valid) self.assertEquals(s.errors, ['SQL could not be parsed.']) s = SQLValidator("") self.assertFalse(s.valid) self.assertEquals(s.errors, ['SQL could not be parsed.']) s = SQLValidator("name = 'a name'; blah") self.assertFalse(s.valid) self.assertEquals(s.errors, ['SQL could not be parsed.'])
def validate_schema(loaded_doc): if not loaded_doc: self._errors.append("YAML cannot be empty") return False if not isinstance(loaded_doc,dict): self._errors.append("YAML must be dict, not list") return False for theme, theme_dict in loaded_doc.items(): if theme in BANNED_THEME_NAMES or theme.startswith("gpkg_") or theme.startswith("rtree_"): self._errors.append("Theme name reserved: {0}".format(theme)) return False if not re.match('(?u)^[\w\s]+$', theme): self._errors.append("Each theme must be named using only characters, numbers, underscores and spaces") return False if not theme_dict or 'select' not in theme_dict: self._errors.append("Each theme must have a 'select' key") return False if 'types' in theme_dict: for typ in theme_dict['types']: if typ not in ['points','lines','polygons']: self._errors.append("types must be one or more of points, lines or polygons, got: {0}".format(typ)) return False seen_tags = [] if not theme_dict['select']: self._errors.append("'select' cannot be empty") return False for key in theme_dict['select']: if not key: self._errors.append("Missing OSM key") return False if key in seen_tags: self._errors.append("Duplicate tag: {0}".format(key)) return False seen_tags.append(key) if not isinstance(theme_dict['select'],list): self._errors.append("'select' children must be list elements (e.g. '- amenity')") return False self.keys_from_sql[theme] = set() if 'where' in theme_dict: if not theme_dict['where']: self._errors.append("if 'where' key is specified, it must not be empty") return False if not isinstance(theme_dict['where'],list): clauses = [theme_dict['where']] else: clauses = theme_dict['where'] for clause in clauses: s = SQLValidator(clause) if not s.valid: self._errors.append("SQL (" + clause + ') is invalid: ' + ' '.join(s.errors)) return False # also add the keys to keys_from_sql for k in s.column_names: self.keys_from_sql[theme].add(k) return True
def test_basic(self): s = SQLValidator("name = 'a name'") self.assertTrue(s.valid)
def test_column_names(self): s = SQLValidator( "(admin IS NOT NULL and level > 4) AND height is not null") self.assertTrue(s.valid) self.assertEquals(s.column_names, ['height', 'level', 'admin'])
def test_parens(self): s = SQLValidator("(admin IS NOT NULL and level > 4)") self.assertTrue(s.valid) s = SQLValidator( "(admin IS NOT NULL and level > 4) AND height is not null") self.assertTrue(s.valid)
def test_not_null(self): s = SQLValidator("height IS NOT NULL") self.assertTrue(s.valid)
def test_float_value(self): s = SQLValidator("height > 20") self.assertTrue(s.valid)
def test_identifier_list(self): s = SQLValidator("natural in ('water','cliff')") self.assertTrue(s.valid)