Example #1
0
 def test_missing_info(self):
     for ctx, expected in [(Ctx({}, False), Const.ERROR),
                           (Ctx({}, True), Const.UNKNOWN)]:
         btw_ = Between(10, "price", 20)
         (ans, err) = btw_(ctx)
         self.assertEqual(ans, expected)
         self.assertIsInstance(err, KeyError)
Example #2
0
    def test_ser_not_eq(self):
        with acura_midsize as acura:
            eq = Eq("type", "Midsize")
            (ans, err) = eq(Ctx(acura))
            self.assertTrue(ans)
            self.assertIsNone(err)
            text = str(eq)
            self.assertEqual(text, "type == 'Midsize'")

            not_eq = NotEq("type", "Midsize")
            (ans, err) = not_eq(Ctx(acura))
            self.assertFalse(ans)
            self.assertIsNone(err)
            text = str(not_eq)
            self.assertEqual(text, "type != 'Midsize'")

            not_eq = NotEq("maxprice", 19.9)
            (ans, err) = not_eq(Ctx(acura))
            self.assertTrue(ans)
            self.assertIsNone(err)
            text = str(not_eq)
            self.assertEqual(text, "maxprice != 19.9")

            not_eq = NotEq("pass", True)
            text = str(not_eq)
            self.assertEqual(text, "pass != True")
Example #3
0
    def test_in_simple(self):
        with acura_small as acura:
            in_ = In("make", "Acura")
            (ans, err) = in_(Ctx(acura))
            self.assertTrue(ans)
            self.assertIsNone(err)

            in_ = In("make", "Ford", "Chrysler", "Eagle", "Honda", "Acura",
                     "Mazda")
            (ans, err) = in_(Ctx(acura))
            self.assertTrue(ans)
            self.assertIsNone(err)

            in_ = In(
                "make",
                "Ford",
            )
            (ans, err) = in_(Ctx(acura))
            self.assertFalse(ans)
            self.assertIsNone(err)

            in_ = In("make", "Ford", "Chrysler", "Eagle", "Honda")
            (ans, err) = in_(Ctx(acura, True))
            self.assertFalse(ans)
            self.assertIsNone(err)
Example #4
0
    def test_basic_compare(self):
        values = ['True', True, 'true', 'tRUE', 1]
        active = Bool("active")
        for value in values:
            ctx = Ctx({"active": value})
            (obj, err) = active(ctx)
            self.assertTrue(obj)
            self.assertIsNone(err)

            not_active = Not(active)
            (obj, err) = not_active(ctx)
            self.assertFalse(obj)
            self.assertIsNone(err)

        values = [False, 'False', 'false', 'fALSE', 0]
        active = Bool("active")
        for value in values:
            ctx = Ctx({"active": value})
            (obj, err) = active(ctx)
            self.assertFalse(obj)
            self.assertIsNone(err)

            not_active = Not(active)
            (obj, err) = not_active(ctx)
            self.assertTrue(obj)
            self.assertIsNone(err)
Example #5
0
    def test_ser_simple_math(self):
        expected = "1 in (4,3,2,1,0,)"
        in_ = to_criteria(expected)
        self.assertIsInstance(in_, In)
        self.assertEqual(in_.key, 1)
        self.assertEqual(in_.right, (
            4,
            3,
            2,
            1,
            0,
        ))
        (ans, err) = in_(Ctx({}))
        self.assertTrue(ans)
        self.assertIsNone(err)
        text = str(in_)
        self.assertEqual(expected, text)

        expected = "1 in (4,3,2,0,-1,)"
        in_ = to_criteria(expected)
        self.assertIsInstance(in_, In)
        self.assertEqual(in_.key, 1)
        self.assertEqual(in_.right, (
            4,
            3,
            2,
            0,
            -1,
        ))
        (ans, err) = in_(Ctx({}))
        self.assertFalse(ans)
        self.assertIsNone(err)
        text = str(in_)
        self.assertEqual(expected, text)
Example #6
0
    def test_between(self):
        expected = "50 <= price < 100"
        c = to_criteria(expected)
        self.assertIsInstance(c, Between)
        self.assertEqual(c.lower, 50)
        self.assertEqual(c.lower_op, operator.le)
        self.assertEqual(c.key, "price")
        self.assertEqual(c.upper_op, operator.lt)
        self.assertEqual(c.upper, 100)
        (ans, err) = c(Ctx({"price": 75}))
        self.assertTrue(ans)
        self.assertIsNone(err)
        text = str(c)
        self.assertEqual(expected, text)

        expected = "44.1 < score <= 66.2"
        c = to_criteria(expected)
        self.assertIsInstance(c, Between)
        self.assertEqual(c.lower, 44.1)
        self.assertEqual(c.lower_op, operator.lt)
        self.assertEqual(c.key, "score")
        self.assertEqual(c.upper_op, operator.le)
        self.assertEqual(c.upper, 66.2)
        (ans, err) = c(Ctx({"score": 55.6}))
        self.assertTrue(ans)
        self.assertIsNone(err)
        text = str(c)
        self.assertEqual(expected, text)
Example #7
0
    def test_all_unknown_fuzzy_on_off(self):
        ctx = Ctx(acura, True)
        all_ = Criteria().Eq("x", "x").Eq("y",
                                          "y").Eq("z",
                                                  "z").Eq("w",
                                                          "w").All().Done()
        (ans, err) = all_(ctx)
        self.assertEqual(ans, Const.UNKNOWN)
        self.assertIsInstance(err, KeyError)

        and_ = Criteria().Eq("x", "x").Eq("y",
                                          "y").And().Eq("z", "z").And().Eq(
                                              "w", "w").And().Done()
        (ans_, err_) = and_(ctx)
        self.assertEqual(ans, ans_)
        self.assertEqual(err.message, err_.message)

        ctx = Ctx(acura, False)
        all_ = Criteria().Eq("x", "x").Eq("y",
                                          "y").Eq("z",
                                                  "z").Eq("w",
                                                          "w").All().Done()
        (ans, err) = all_(ctx)
        self.assertEqual(ans, Const.ERROR)
        self.assertIsInstance(err, KeyError)

        and_ = Criteria().Eq("x", "x").Eq("y",
                                          "y").And().Eq("z", "z").And().Eq(
                                              "w", "w").And().Done()
        (ans_, err_) = and_(ctx)
        self.assertEqual(ans, ans_)
        self.assertEqual(err.message, err_.message)
Example #8
0
    def test_get(self):
        ctx = Ctx(acura_small)
        make = ctx["make"]
        self.assertEqual(make, "Acura")

        with self.assertRaises(KeyError):
            ctx["cpu"]

        cpu = ctx.get("cpu", default="Intel")
        self.assertEqual(cpu, "Intel")
Example #9
0
    def test_not_simple(self):
        not_ = Not(cTrue)
        (ans, err) = not_(Ctx({}))
        self.assertFalse(ans)
        self.assertIsNone(err)

        not_ = Not(cFalse)
        (ans, err) = not_(Ctx({}))
        self.assertTrue(ans)
        self.assertIsNone(err)
Example #10
0
    def test_not_and(self):
        and_ = And(cTrue, cFalse)
        not_ = Not(and_)
        (ans, err) = not_(Ctx({}))
        self.assertTrue(ans)
        self.assertIsNone(err)

        not__ = Not(not_)
        (ans, err) = not__(Ctx({}))
        self.assertFalse(ans)
        self.assertIsNone(err)
Example #11
0
    def test_basic_init(self):
        for ctx in [Ctx({}, False), Ctx({}, True)]:
            for c in [cTrue, Bool(True), Bool('True'), Bool(1)]:
                (obj, err) = c(ctx)
                self.assertTrue(obj)
                self.assertIsNone(err)

            for c in [cFalse, Bool(False), Bool('False'), Bool(0)]:
                (obj, err) = c(ctx)
                self.assertFalse(obj)
                self.assertIsNone(err)
Example #12
0
    def test_eq_missing_attribute(self):
        with acura_small as acura:
            eq = Eq("cpu", "Intel")
            ctx = Ctx(acura, True)
            (ans, error) = eq(ctx)
            self.assertTrue(ctx.fuzzy)
            self.assertEqual(ans, Const.UNKNOWN)
            self.assertIsInstance(error, KeyError)

            ctx = Ctx(acura, False)
            (ans, error) = eq(ctx)
            self.assertFalse(ctx.fuzzy)
            self.assertEqual(ans, Const.ERROR)
            self.assertIsInstance(error, KeyError)
Example #13
0
 def test_targets(self):
     with acura_small as acura:
         for fuzzy in (True, False):
             ctx = Ctx(acura, fuzzy)
             self.assertEqual(ctx["make"], "Acura")
             self.assertEqual(ctx["type"], "Small")
             self.assertEqual(ctx.fuzzy, fuzzy)
Example #14
0
    def test_ser_not_bool(self):
        expected = "not (active)"
        not_ = to_criteria(expected)
        text = str(not_)
        self.assertEqual(expected, text)

        text2 = "not active"
        not2_ = to_criteria(text2)
        text3 = str(not2_)
        self.assertNotEqual(text2, text3)
        self.assertEqual(text3, expected)

        expected = "not (True)"
        not_ = to_criteria(expected)
        text = str(not_)
        self.assertEqual(expected, text)

        text2 = "not True"
        not2_ = to_criteria(text2)
        text3 = str(not2_)
        self.assertNotEqual(text2, text3)
        self.assertEqual(text3, expected)

        expected = "not (1)"
        text2 = "not 1"
        not2_ = to_criteria(text2)
        text3 = str(not2_)
        self.assertNotEqual(text2, text3)
        self.assertEqual(text3, expected)
        (ans, err) = not2_(Ctx({}))
        self.assertFalse(ans)
        self.assertIsNone(err)
Example #15
0
    def test_simple_bool_eq(self):
        ctx = Ctx({"active": True})

        expected = "active == True"
        c = to_criteria(expected)
        self.assertIsInstance(c, Eq)
        self.assertEqual(c.key, "active")
        self.assertEqual(c.right, True)

        (ans, err) = c(ctx)
        self.assertTrue(ans)
        self.assertIsNone(err)
        text = str(c)
        self.assertEqual(expected, text)

        expected = "active"
        c = to_criteria(expected)
        self.assertIsInstance(c, Bool)
        self.assertEqual(c.key, "active")

        (ans, err) = c(ctx)
        self.assertTrue(ans)
        self.assertIsNone(err)
        text = str(c)
        self.assertEqual(expected, text)
Example #16
0
    def test_le_positive(self):
        with acura_small as acura:
            le_ = LtE("maxprice", 18.8)
            (ans, err) = le_(Ctx(acura))
            self.assertTrue(ans)
            self.assertIsNone(err)

            le_ = LtE("maxprice", 18.81)
            (ans, err) = le_(Ctx(acura))
            self.assertTrue(ans)
            self.assertIsNone(err)

            le_ = LtE("maxprice", 18.79)
            (ans, err) = le_(Ctx(acura))
            self.assertFalse(ans)
            self.assertIsNone(err)
Example #17
0
    def test_bool_op_all(self):
        ctx = Ctx({"active": True, "score": 92, "valid": False})

        original = "active and score >= 90 and valid != True"
        expected = original
        all_ = to_criteria(original)
        self.assertIsInstance(all_, All)
        active, score, valid = all_.many
        self.assertIsInstance(active, Bool)
        self.assertIsInstance(score, GtE)
        self.assertIsInstance(valid, NotEq)

        (ans, err) = all_(ctx)
        self.assertTrue(ans)
        self.assertIsNone(err)
        text = str(all_)
        self.assertEqual(expected, text)

        original = "active and (score >= 90 and valid != True)"
        expected = "(active and (score >= 90 and valid != True))"
        and_ = to_criteria(original)
        self.assertIsInstance(and_, And)
        active = and_.left
        and_2nd = and_.right
        self.assertIsInstance(and_2nd, And)
        score, valid = and_2nd.left, and_2nd.right
        self.assertIsInstance(active, Bool)
        self.assertIsInstance(score, GtE)
        self.assertIsInstance(valid, NotEq)

        (ans, err) = and_(ctx)
        self.assertTrue(ans)
        self.assertIsNone(err)
        text = str(and_)
        self.assertEqual(expected, text)
Example #18
0
    def test_and_and_nesting(self):
        with acura_small as acura:
            ctx = Ctx(acura)

            expected = "(source == 'USA' or ((make == 'Mazda' or type == 'Midsize') or maxprice == 18.8))"
            or_ = Or(
                Eq("source", "USA"),
                Or(Or(Eq("make", "Mazda"), Eq("type", "Midsize")),
                   Eq("maxprice", 18.8)))
            (ans, err) = or_(ctx)
            text = str(or_)
            self.assertEqual(expected, text)
            self.assertTrue(ans)
            self.assertIsNone(err)

            or2_ = to_criteria(expected)
            self.assertIsInstance(or2_, Or)
            self.assertEqual(expected, str(or2_))

            expected = "source == 'USA' or make == 'Mazda' or type == 'Midsize' or maxprice == 18.8"
            any_ = Any(Eq("source", "USA"), Eq("make", "Mazda"),
                       Eq("type", "Midsize"), Eq("maxprice", 18.8))
            text = str(any_)
            self.assertEqual(expected, text)
            (ans_, err_) = any_(ctx)
            self.assertTrue(ans)
            self.assertEqual(ans, ans_)
            self.assertEqual(err, err_)
            self.assertNotEqual(str(or_), str(any_))

            any2_ = to_criteria(expected)
            self.assertIsInstance(any2_, Any)
            self.assertEqual(expected, str(any2_))
Example #19
0
    def test_ser_not_in(self):

        with acura_small as acura:
            ctx = Ctx(acura)

            for expected in (
                    "make not in ('Acura',)",
                    "make not in ('Ford','Chrysler','Eagle','Honda','Acura',)"
            ):
                not_in_ = to_criteria(expected)
                text = str(not_in_)
                self.assertEqual(expected, text)
                (ans, err) = not_in_(ctx)
                self.assertFalse(ans)
                self.assertIsNone(err)

            for expected in (
                    "make not in ('Ford',)",
                    "make not in ('Ford','Chrysler','Eagle','Honda','Mazda',)"
            ):
                in_ = to_criteria(expected)
                text = str(in_)
                self.assertEqual(expected, text)
                (ans, err) = in_(ctx)
                self.assertTrue(ans)
                self.assertIsNone(err)
Example #20
0
    def test_criteria_call(self):
        car_search_criteria = to_criteria("cpu == 'Intel' and make == 'Acura' and type == 'Midsize' and drivetrain == 'Front'")
        (ans, err) = car_search_criteria(acura)
        self.assertEqual(ans, Const.ERROR)
        self.assertIsInstance(err, KeyError)

        (ans, err) = car_search_criteria(Ctx(acura))
        self.assertEqual(ans, Const.ERROR)
        self.assertIsInstance(err, KeyError)

        (ans, err) = car_search_criteria(acura, True)
        self.assertTrue(ans)
        self.assertIsInstance(err, KeyError)

        (ans, err) = car_search_criteria(Ctx(acura, True))
        self.assertTrue(ans)
        self.assertIsInstance(err, KeyError)
Example #21
0
    def test_and_left_cmp_error(self):
        with acura:
            ctx = Ctx(acura, True)
            acura.set_compare_error("make", CompareError(Exception("left first")))
            acura.set_compare_error("source", CompareError(KeyError("right first")))
            make = ctx["make"]
            source = ctx["source"]

            (obj, err) = Eq("make", "xxx")(ctx)
            self.assertEqual(obj, Const.UNKNOWN)
            self.assertIsInstance(err, Exception)
            self.assertEqual(err.message, "left first")

            (obj, err) = Eq("source", "xxx")(ctx)
            self.assertEqual(obj, Const.UNKNOWN)
            self.assertIsInstance(err, KeyError)
            self.assertEqual(err.message, "right first")

            and_ = Criteria().Eq("make", "xxx").Eq("source", "yyy").And().Done()
            (obj, err) = and_(ctx)
            self.assertEqual(obj, Const.UNKNOWN)
            self.assertIsInstance(err, Exception)
            self.assertEqual(err.message, "left first")

        with acura:
            ctx = Ctx(acura, True)
            acura.set_compare_error("source", CompareError(KeyError("right first")))
            make = ctx["make"]
            source = ctx["source"]

            and_ = And(cTrue, Eq("source", "xxx"))
            (obj, err) = and_(ctx)
            self.assertTrue(obj)
            self.assertIsInstance(err, KeyError)
            self.assertEqual(err.message, "right first")

            ctx = Ctx(acura, False)
            and_ = And(cTrue, Eq("source", "xxx"))
            (obj, err) = and_(ctx)
            self.assertEqual(obj, Const.ERROR)
            self.assertIsInstance(err, KeyError)
            self.assertEqual(err.message, "right first")
Example #22
0
 def test_in(self):
     ctx = Ctx({"score": 92})
     expected = "score in (90,91,92,)"
     in_ = to_criteria(expected)
     self.assertIsInstance(in_, In)
     self.assertEqual(in_.key, "score")
     self.assertEqual(len(in_.right), 3)
     (ans, err) = in_(ctx)
     self.assertTrue(ans)
     self.assertIsNone(err)
     text = str(in_)
     self.assertEqual(expected, text)
Example #23
0
    def test_car_attributes(self):
        with acura_small as acura:
            self.assertEqual(acura.make, 'Acura')
            self.assertEqual(acura.type, 'Small')
            self.assertEqual(acura.maxprice, 18.8)
            with self.assertRaises(AttributeError):
                acura.not_such_attribute

        ctx = Ctx(acura, fuzzy=False)
        self.assertEqual(acura.make, ctx['make'])
        self.assertEqual(acura.type, ctx['type'])
        self.assertEqual(acura.maxprice, ctx['maxprice'], 18.8)
        with self.assertRaises(KeyError):
            ctx['not_such_attribute']

        ctx = Ctx(acura, fuzzy=True)
        self.assertEqual(acura.make, ctx['make'])
        self.assertEqual(acura.type, ctx['type'])
        self.assertEqual(acura.maxprice, ctx['maxprice'], 18.8)
        with self.assertRaises(KeyError):
            ctx['not_such_attribute']
Example #24
0
    def test_not_eq(self):
        with acura_small as acura:
            ctx = Ctx(acura)
            eq_ = Eq("make", "Acura")
            not_ = Not(eq_)

            (ans, err) = eq_(ctx)
            self.assertTrue(ans)
            self.assertIsNone(err)

            (ans, err) = not_(ctx)
            self.assertFalse(ans)
            self.assertIsNone(err)
Example #25
0
    def test_ne_simple(self):

        with acura_small as acura:
            c = NotEq("make", "Acura")
            (ans, err) = c(Ctx(acura))
            self.assertFalse(ans)
            self.assertIsNone(err)

            c = NotEq("make", "Mazda")
            (ans, err) = c(Ctx(acura))
            self.assertTrue(ans)
            self.assertIsNone(err)

            c = NotEq("cpu", "Intel")
            (ans, err) = c(Ctx(acura))
            self.assertEqual(ans, Const.ERROR)
            self.assertIsInstance(err, KeyError)

            c = NotEq("cpu", "Intel")
            (ans, err) = c(Ctx(acura, True))
            self.assertEqual(ans, Const.UNKNOWN)
            self.assertIsInstance(err, KeyError)
Example #26
0
    def test_ser_in(self):
        with acura_small as acura:
            expected = "make in ('Acura',)"
            in_ = to_criteria(expected)
            text = str(in_)
            self.assertEqual(expected, text)

            expected = "make in ('Ford','Chrysler','Eagle','Honda','Acura','Mazda',)"
            in_ = to_criteria(expected)
            text = str(in_)
            self.assertEqual(expected, text)
            (ans, err) = in_(Ctx(acura))
            self.assertTrue(ans)
            self.assertIsNone(err)
Example #27
0
    def test_ser(self):

        with acura_small as acura:
            expected = "make != 'Acura'"
            not_eq = to_criteria(expected)
            text = str(not_eq)
            self.assertEqual(expected, text)
            self.assertIsInstance(not_eq, NotEq)
            self.assertEqual(not_eq.key, 'make')
            self.assertEqual(not_eq.right, 'Acura')
            self.assertEqual(not_eq.op, operator.ne)
            (ans, err) = not_eq(Ctx(acura))
            self.assertFalse(ans)
            self.assertIsNone(err)
Example #28
0
    def test_filter_cars(self):
        """ create criteria either through api or text """
        expected = "((17 <= maxprice < 21 and make == 'Chevrolet') and type == 'Compact')"
        car_search_criteria = Criteria().Between(17, "maxprice", 21).Eq("make", "Chevrolet").And().Eq("type", "Compact").And().Done()
        car_search_criteria2 = to_criteria(expected)
        self.assertEqual(str(car_search_criteria), expected)
        self.assertEqual(str(car_search_criteria2), expected)

        """ test that one of the cars below matches the criteria """
        ctx = Ctx(chevrolet_compact_e)
        (ans, err) = car_search_criteria(ctx)
        self.assertTrue(ans)
        self.assertIsNone(err)

        ctx = Ctx(chevrolet_compact_c)
        (ans, err) = car_search_criteria(ctx)
        self.assertFalse(ans)
        self.assertIsNone(err)

        """ to conform to built-in filter function's requirement """
        def predicate(car):
            (ans, _) = car_search_criteria(Ctx(car))
            return ans

        """ there should be only be one match """
        potential = filter(predicate, CARS)
        self.assertEqual(len(potential), 1)

        """ change search criteria a bit """
        car_search_criteria = Criteria().Eq("make", "Chevrolet").Eq("type", "Compact").Eq("source", "USA").All().Done()

        def predicate(car):
            (ans, _) = car_search_criteria(Ctx(car))
            return ans

        potential = filter(predicate, CARS)
        self.assertEqual(len(potential), 2)
Example #29
0
    def test_fuzzy_match(self):
        car_search_criteria = Criteria().Between(17, "maxprice", 21).Eq("make", "Chevrolet").And().Eq("type", "Compact").And().Done()
        ctx = Ctx(chevrolet_compact_e)
        (ans, err) = car_search_criteria(ctx)
        self.assertTrue(ans)
        self.assertIsNone(err)

        ctx = Ctx(chevrolet_compact_c)
        (ans, err) = car_search_criteria(ctx)
        self.assertFalse(ans)
        self.assertIsNone(err)

        with chevrolet_compact_c as chevrolet_compact:
            """ make maxprice missing for chevrolet_compact temporarily """
            chevrolet_compact.set_access_error("maxprice", KeyError)
            ctx = Ctx(chevrolet_compact)
            (ans, err) = car_search_criteria(ctx)
            self.assertEqual(ans, Const.ERROR)
            self.assertIsInstance(err, KeyError)

            ctx = Ctx(chevrolet_compact, True)
            (ans, err) = car_search_criteria(ctx)
            self.assertTrue(ans)
            self.assertIsInstance(err, KeyError)
Example #30
0
    def test_bool_op_any(self):
        ctx = Ctx({"active": True, "score": 92, "valid": False})
        expected = "active or score >= 90 or valid != True"
        any_ = to_criteria(expected)
        self.assertIsInstance(any_, Any)
        sunny, score, funny = any_.many
        self.assertIsInstance(sunny, Bool)
        self.assertIsInstance(score, GtE)
        self.assertIsInstance(funny, NotEq)
        (ans, err) = any_(ctx)
        self.assertTrue(ans)
        self.assertIsNone(err)
        text = str(any_)
        self.assertEqual(expected, text)

        original = "active or (score >= 90 or valid != True)"
        expected = "(active or (score >= 90 or valid != True))"
        or_ = to_criteria(original)
        self.assertIsInstance(or_, Or)
        active = or_.left
        or_2nd = or_.right
        self.assertIsInstance(or_2nd, Or)
        score, valid = or_2nd.left, or_2nd.right
        self.assertIsInstance(sunny, Bool)
        self.assertIsInstance(score, GtE)
        self.assertIsInstance(valid, NotEq)
        (ans, err) = or_(ctx)
        self.assertTrue(ans)
        self.assertIsNone(err)
        text = str(or_)
        self.assertEqual(expected, text)

        original = "(active or score >= 90) or valid != True"
        expected = "((active or score >= 90) or valid != True)"
        or_ = to_criteria(original)
        self.assertIsInstance(or_, Or)
        or_2nd = or_.left
        self.assertIsInstance(or_2nd, Or)
        active, score = or_2nd.left, or_2nd.right
        valid = or_.right
        self.assertIsInstance(active, Bool)
        self.assertIsInstance(score, GtE)
        self.assertIsInstance(valid, NotEq)
        (ans, err) = or_(ctx)
        self.assertTrue(ans)
        self.assertIsNone(err)
        text = str(or_)
        self.assertEqual(expected, text)