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_))
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")
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")
def test_none(self): eq = Eq("make", None) expected = "make == None" self.assertEqual(expected, str(eq)) eq2 = to_criteria(expected) self.assertEqual(eq2.key, "make") self.assertIsNone(eq2.right) car = {"make": None} (ans, err) = eq(car)
def test_criteria_class_override(self): eq = Eq("make", "Subaru") (ans, err) = eq({"make": "Subaru"}) self.assertTrue(ans) self.assertIsNone(err) """ create a diff ctx class """ class Ctx2(Ctx): def key(self, key): raise KeyError("no key %s" % key) """ back up the original ctx cls """ cls = criteria_class.lookup(Const.Ctx) """ override ctx impl cls temporarily """ criteria_class.override(Const.Ctx, Ctx2) eq = Eq("make", "Subaru") (ans, err) = eq({"make": "Subaru"}) self.assertEqual(ans, Const.ERROR) self.assertIsInstance(err, KeyError) """ restore the original ctx impl cls """ criteria_class.override(Const.Ctx, cls)
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)
def test_universal(self): eq = Eq("make", universal) expected = "make == '*'" self.assertEqual(expected, str(eq)) eq = to_criteria(expected) self.assertEqual(eq.right, Const.universal) self.assertEqual(expected, str(eq)) for value in ("xyz", 1, 0, 10.3, False, True, object(), "*"): car = {"make": value} (ans, err) = eq(car) self.assertTrue(ans) self.assertIsNone(err)
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)
def test_ser_not_eq(self): expected = "not (make == 'Acura')" not_ = Not(Eq("make", "Acura")) text = str(not_) self.assertEqual(text, expected) not2_ = to_criteria(text) self.assertIsInstance(not2_, Not) text2 = str(not2_) self.assertEqual(text, text2) text3 = "not make == 'Acura'" not3_ = to_criteria(text3) self.assertIsInstance(not3_, Not) text4 = str(not3_) self.assertNotEquals(text3, text4) self.assertEqual(text4, expected)
def test_ser_eq(self): eq = Eq("make", "Acura") text = str(eq) self.assertEqual(text, "make == 'Acura'") eq = Eq("price", 18.8) text = str(eq) self.assertEqual(text, "price == 18.8") eq = Eq("american", True) text = str(eq) self.assertEqual(text, "american == True") and_ = And(Eq("make", "Acura"), Eq("price", 18.8)) text = str(and_) self.assertEqual(text, "(make == 'Acura' and price == 18.8)") all_ = All(Eq("make", "Acura"), Eq("price", 18.8), Eq("american", True)) text = str(all_) self.assertEqual( text, "make == 'Acura' and price == 18.8 and american == True")
def test_nesting_conditions(self): ctx = Ctx(acura) and_ = And( Eq("type", "Small"), Eq("make", "Acura") ) and_ = And( and_, Eq("maxprice", 18.8) ) and_ = And( NotEq("source", "USA"), and_ ) (obj, err) = and_(ctx) self.assertTrue(obj) self.assertIsNone(err) all_ = All(NotEq("source", "USA"), Eq("type", "Small"), Eq("make", "Acura"), Eq("maxprice", 18.8)) obj_, err_ = all_(ctx) self.assertEqual(obj, obj_) self.assertEqual(err, err_)
def test_eq_positive(self): with acura_small as acura: eq = Eq("make", "Acura") (ans, err) = eq(Ctx(acura)) self.assertTrue(ans) self.assertIsNone(err)
def test_eq_negative(self): with acura_small as acura: eq = Eq("make", "acura") (ans, err) = eq(Ctx(acura)) self.assertFalse(ans) self.assertIsNone(err)