def test_more_advanced_integration_test(self):
        def percents_add_to_100(data):
            total = 0
            for project in data:
                total += project["percentage"]
            if total != 100:
                raise Invalid("the percentages must add up to 100")
            return data

        def check_required_by(data):
            return "required_by_type" in data and data["required_by_type"] == "later"
        s = Schema(
            Chain(
                {
                    "description": All(str, when_empty(Invalid)),
                    "justification": All(str, when_empty(Invalid)),
                    "is_tech_purchase": All(bool, when_empty(False)),
                    "projects": All(
                        [{
                            "id": All(int, when_empty(Undefined)),
                            "project_id": All(int, when_empty(Invalid)),
                            "percentage": All(
                                int, in_range(1, 100), when_empty(Invalid))
                        }],
                        percents_add_to_100
                    ),
                    "vendor": All(str, when_empty(Invalid)),
                    "vendor_id": All(int, when_empty(Invalid)),
                    "required_by_type": All(str, in_list(["now", "later"])),
                    "date_required_by": All(int, when_empty(Invalid))
                },
                require_one("vendor_id", "vendor"),
                require_if("date_required_by", check_required_by)))
        data = {
            "description": "abc",
            "justification": "def",
            "projects": [
                {"project_id": 4, "percentage": 10},
                {"id": 2, "project_id": 5, "percentage": 90}
            ],
            "vendor": "ABC co.",
            "required_by_type": "later",
            "date_required_by": 1234
        }
        val = s(data)
        self.assertEqual(
            val, {
                "description": "abc",
                "justification": "def",
                "is_tech_purchase": False,
                "projects": [
                    {"project_id": 4, "percentage": 10},
                    {"id": 2, "project_id": 5, "percentage": 90}
                ],
                "vendor": "ABC co.",
                "required_by_type": "later",
                "date_required_by": 1234
            })
    def test_basic_integration_test(self):
        s = Schema({
            "abc": All(coerce_to(str), when_empty(Invalid)),
            "def": All(int, when_empty(Invalid))
        })
        with self.assertRaises(InvalidGroup) as cm:
            s({})
        self.assertEqual(
            map(str, cm.exception.errors),
            [
                "A value is required @ data[abc]",
                "A value is required @ data[def]"
            ],
        )
        a = s({"abc": 1, "def": 2})
        self.assertEqual(a, {"abc": "1", "def": 2})

        s = Schema(All({
            "abc": coerce_to(str),
            "def": int
        }))
        with self.assertRaises(InvalidGroup) as cm:
            s({"abc": 1, "def": "abc"})
        self.assertEqual(
            map(str, cm.exception.errors),
            [
                "Expected int @ data[def]"
            ]
        )

        s = Schema(All({
            "abc": All(coerce_to(str), when_empty(Invalid)),
            "def": All(int, when_empty(Invalid))
        }))
        with self.assertRaises(InvalidGroup) as cm:
            s({})
        self.assertEqual(
            map(str, cm.exception.errors),
            [
                "A value is required @ data[abc]",
                "A value is required @ data[def]"
            ],
        )
Ejemplo n.º 3
0
    def test_basic_integration_test(self):
        s = Schema({
            "abc": All(coerce_to(str), when_empty(Invalid)),
            "def": All(int, when_empty(Invalid))
        })
        with self.assertRaises(InvalidGroup) as cm:
            s({})
        self.assertEqual(
            map(str, cm.exception.errors),
            [
                "A value is required @ data[abc]",
                "A value is required @ data[def]"
            ],
        )
        a = s({"abc": 1, "def": 2})
        self.assertEqual(a, {"abc": "1", "def": 2})

        s = Schema(All({"abc": coerce_to(str), "def": int}))
        with self.assertRaises(InvalidGroup) as cm:
            s({"abc": 1, "def": "abc"})
        self.assertEqual(map(str, cm.exception.errors),
                         ["Expected int @ data[def]"])

        s = Schema(
            All({
                "abc": All(coerce_to(str), when_empty(Invalid)),
                "def": All(int, when_empty(Invalid))
            }))
        with self.assertRaises(InvalidGroup) as cm:
            s({})
        self.assertEqual(
            map(str, cm.exception.errors),
            [
                "A value is required @ data[abc]",
                "A value is required @ data[def]"
            ],
        )
 def test_when_empty_raises_stop_validation_on_falsy_values(self):
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")(0)
     self.assertEqual(cm.exception.data, "abc")
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")(0.0)
     self.assertEqual(cm.exception.data, "abc")
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")(False)
     self.assertEqual(cm.exception.data, "abc")
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")(None)
     self.assertEqual(cm.exception.data, "abc")
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")("")
     self.assertEqual(cm.exception.data, "abc")
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")(Undefined)
     self.assertEqual(cm.exception.data, "abc")
     self.assertEqual(when_empty("def")(1), 1)
     self.assertEqual(when_empty("def")(0.01), 0.01)
     self.assertEqual(when_empty("def")(True), True)
     self.assertEqual(when_empty("def")("abc"), "abc")
Ejemplo n.º 5
0
    def test_more_advanced_integration_test(self):
        def percents_add_to_100(data):
            total = 0
            for project in data:
                total += project["percentage"]
            if total != 100:
                raise Invalid("the percentages must add up to 100")
            return data

        def check_required_by(data):
            return "required_by_type" in data and data[
                "required_by_type"] == "later"

        s = Schema(
            Chain(
                {
                    "description":
                    All(str, when_empty(Invalid)),
                    "justification":
                    All(str, when_empty(Invalid)),
                    "is_tech_purchase":
                    All(bool, when_empty(False)),
                    "projects":
                    All([{
                        "id":
                        All(int, when_empty(Undefined)),
                        "project_id":
                        All(int, when_empty(Invalid)),
                        "percentage":
                        All(int, in_range(1, 100), when_empty(Invalid))
                    }], percents_add_to_100),
                    "vendor":
                    All(str, when_empty(Invalid)),
                    "vendor_id":
                    All(int, when_empty(Invalid)),
                    "required_by_type":
                    All(str, in_list(["now", "later"])),
                    "date_required_by":
                    All(int, when_empty(Invalid))
                }, require_one("vendor_id", "vendor"),
                require_if("date_required_by", check_required_by)))
        data = {
            "description":
            "abc",
            "justification":
            "def",
            "projects": [{
                "project_id": 4,
                "percentage": 10
            }, {
                "id": 2,
                "project_id": 5,
                "percentage": 90
            }],
            "vendor":
            "ABC co.",
            "required_by_type":
            "later",
            "date_required_by":
            1234
        }
        val = s(data)
        self.assertEqual(
            val, {
                "description":
                "abc",
                "justification":
                "def",
                "is_tech_purchase":
                False,
                "projects": [{
                    "project_id": 4,
                    "percentage": 10
                }, {
                    "id": 2,
                    "project_id": 5,
                    "percentage": 90
                }],
                "vendor":
                "ABC co.",
                "required_by_type":
                "later",
                "date_required_by":
                1234
            })
Ejemplo n.º 6
0
 def test_when_empty_raises_stop_validation_on_falsy_values(self):
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")(0)
     self.assertEqual(cm.exception.data, "abc")
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")(0.0)
     self.assertEqual(cm.exception.data, "abc")
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")(False)
     self.assertEqual(cm.exception.data, "abc")
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")(None)
     self.assertEqual(cm.exception.data, "abc")
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")("")
     self.assertEqual(cm.exception.data, "abc")
     with self.assertRaises(StopValidation) as cm:
         when_empty("abc")(Undefined)
     self.assertEqual(cm.exception.data, "abc")
     self.assertEqual(when_empty("def")(1), 1)
     self.assertEqual(when_empty("def")(0.01), 0.01)
     self.assertEqual(when_empty("def")(True), True)
     self.assertEqual(when_empty("def")("abc"), "abc")