def check_query(query: str, args: Dict[str, Any], expected: List[Dict[str, Any]]) -> bool: """ ConfDB Query result order is undefined, so we need additional helper to check if all results matched :param query: :param args: :param expected: :return: """ e = Engine() left: Dict[int, Dict[str, Any]] = {dict_hash_int(ctx): ctx for ctx in expected} not_found: Set[Dict[str, Any]] = set() for ctx in e.query(query, **args): ctx_hash = dict_hash_int(ctx) if ctx_hash in left: del left[ctx_hash] else: not_found.add(ctx) for ctx_hash in not_found: print("Unexpected return result: %s" % not_found[ctx_hash]) for ctx in left: print("Missed result: %s" % ctx) return not left and not not_found
def clean(self, data): data = super(ConfDBQueryApplication, self).clean(data) src = data.get("source", "") try: Engine().compile(src) except SyntaxError as e: raise ValueError("Syntax error: %s", e) return data
def test_match(conf, query, output): e = Engine() e.insert_bulk(conf) assert list(e.query(query)) == output
def test_collapse_join(conf, query, result): e = Engine() e.insert_bulk(CONF1) list(e.query(query)) assert e.dump("indent") == result
def test_not(input, query, output): e = Engine() assert list(e.query(query, **input)) == output
def test_match_any_vlan(input, query, output): e = Engine() assert list(e.query(query, **input)) == output
def test_prepared_query(query): e = Engine() q = e.compile(query) e.query(q)
def test_invalid(query): e = Engine() with pytest.raises(SyntaxError): e.compile(query)
def test_valid(query): e = Engine() e.compile(query)
def test_match_prefix(input, query, output): e = Engine() assert list(e.query(query, **input)) == output
def test_match(conf, query, output): db = ConfDB() db.insert_bulk(conf) e = Engine().with_db(db) assert list(e.query(query)) == output
def test_fact(conf, input, query, out_conf, output): db = ConfDB() db.insert_bulk(CONF1) e = Engine().with_db(db) assert list(e.query(query, **input)) == output assert db.marshall("indent") == out_conf