def test_samples(): p = os.path.dirname(os.path.abspath(__file__)) fh = open(os.path.join(p, "preds.txt")) s1 = PredicateSet() s2 = OptimizedPredicateSet() match = [] for line, pred in enumerate(fh): pred = pred.strip() obj = Predicate(pred) # Add to the set s1.add(obj) s2.add(obj) # Add to the list of matches if pred.endswith("true"): match.append(pred) # Run the sets s1_match = s1.evaluate(DOC) s2_match = s2.evaluate(DOC) # Ensure both match match.sort() s1_match = [p.predicate for p in s1_match] s1_match.sort() s2_match = [p.predicate for p in s2_match] s2_match.sort() assert s1_match == match assert s2_match == match
def test_invalidate(self): "AST is invalidated when set changes" p1 = Predicate("name is 'Jack'") p2 = Predicate("name is 'Jill'") s = OptimizedPredicateSet([p1, p2]) match = s.evaluate({'name': 'Jill'}) assert match == [p2] p3 = Predicate("name is 'Joe'") s.add(p3) assert s.ast == None match = s.evaluate({'name': 'Joe'}) assert match == [p3]
def test_finalize(self): p1 = Predicate("name is 'Jack'") p2 = Predicate("name is 'Jill'") s = OptimizedPredicateSet([p1, p2]) s.finalize() match = s.evaluate({'name': 'Jill'}) assert match == [p2] p3 = Predicate("name is 'Joe'") with pytest.raises(Exception): s.add(p3)
def test_dup(self): p1 = Predicate("name is 'Jill'") s = OptimizedPredicateSet([p1, p1]) match = s.evaluate({'name': 'Jill'}) assert match == [p1]
def test_two(self): p1 = Predicate("name is 'Jack'") p2 = Predicate("name is 'Jill'") s = OptimizedPredicateSet([p1, p2]) match = s.evaluate({'name': 'Jill'}) assert match == [p2]
def make_set_optimized(preds): s = OptimizedPredicateSet(preds) s.compile_ast() #print s.ast.description() return s