示例#1
0
文件: utils.py 项目: nbashev/noc
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
示例#2
0
 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
示例#3
0
文件: test_match.py 项目: nbashev/noc
def test_match(conf, query, output):
    e = Engine()
    e.insert_bulk(conf)
    assert list(e.query(query)) == output
示例#4
0
def test_collapse_join(conf, query, result):
    e = Engine()
    e.insert_bulk(CONF1)
    list(e.query(query))
    assert e.dump("indent") == result
示例#5
0
def test_not(input, query, output):
    e = Engine()
    assert list(e.query(query, **input)) == output
示例#6
0
def test_match_any_vlan(input, query, output):
    e = Engine()
    assert list(e.query(query, **input)) == output
示例#7
0
def test_prepared_query(query):
    e = Engine()
    q = e.compile(query)
    e.query(q)
示例#8
0
def test_invalid(query):
    e = Engine()
    with pytest.raises(SyntaxError):
        e.compile(query)
示例#9
0
def test_valid(query):
    e = Engine()
    e.compile(query)
示例#10
0
def test_match_prefix(input, query, output):
    e = Engine()
    assert list(e.query(query, **input)) == output
示例#11
0
def test_match(conf, query, output):
    db = ConfDB()
    db.insert_bulk(conf)
    e = Engine().with_db(db)
    assert list(e.query(query)) == output
示例#12
0
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