Beispiel #1
0
    def test_to_boolean(self):
        schema = {
            "boolean_1": {"type": "boolean", "coerce": "to_bool"},
            "boolean_2": {"type": "boolean", "coerce": "to_bool"},
            "boolean_3": {"type": "boolean", "coerce": "to_bool"},
            "boolean_4": {"type": "boolean", "coerce": "to_bool"},
            "boolean_5": {"type": "boolean", "coerce": "to_bool"},
            "boolean_6": {"type": "boolean", "coerce": "to_bool"},
            "boolean_7": {"type": "boolean", "coerce": "to_bool"},
        }
        data = {
            "boolean_1": True,
            "boolean_2": "True",
            "boolean_3": "",
            "boolean_4": "False",
            "boolean_5": False,
            "boolean_6": "None",
            "boolean_7": None,
        }
        result = {
            "boolean_1": True,
            "boolean_2": True,
            "boolean_3": False,
            "boolean_4": False,
            "boolean_5": False,
            "boolean_6": False,
            "boolean_7": False,
        }

        validator = Validator(schema=schema)
        assert validator.validate(data), validator.errors
        assert validator.document == result
Beispiel #2
0
    def test_uuids(self):
        schema = {
            "string_1": {"type": "string", "check_with": "uuid"},
        }
        data = {
            "string_1": "7c674878-e544-431c-8c11-f11565299cac",
        }
        result = {
            "string_1": "7c674878-e544-431c-8c11-f11565299cac",
        }

        validator = Validator(schema=schema)
        assert validator.validate(data), validator.errors
        assert validator.document == result
    def test_replacing_validator(self):
        DataSet.set_validator(Validator(allow_unknown=True))

        class Example(DataSet):
            schema = {
                "string_1": {
                    "type": "string",
                    "check_with": "uuid"
                },
            }

        assert Example._validator.allow_unknown
        assert not Example._validator.ignore_none_values
        assert not Example._validator.purge_readonly
        assert not Example._validator.purge_unknown
        assert not Example._validator.require_all

        data = {
            "string_2": "7c674878-e544-431c-8c11-f11565299cac",
        }
        result = {
            "string_2": "7c674878-e544-431c-8c11-f11565299cac",
        }

        validated = Example.validate_object(data)
        assert validated == result
Beispiel #4
0
    def test_to_datetime(self):
        schema = {
            "string_1": {"type": "datetime", "nullable": True, "coerce": "to_datetime"},
            "string_2": {"type": "datetime", "coerce": "to_datetime"},
        }
        data = {
            "string_1": "This is a normal string",
            "string_2": "1900-01-31T09:30:00.532649",
        }
        result = {
            "string_1": None,
            "string_2": datetime.fromisoformat("1900-01-31T09:30:00.532649"),
        }

        validator = Validator(schema=schema)
        assert validator.validate(data), validator.errors
        assert validator.document == result
Beispiel #5
0
    def test_to_integer(self):
        schema = {
            "integer_1": {"type": "integer", "coerce": "to_integer"},
            "integer_2": {"type": "integer", "coerce": "to_integer"},
            "integer_3": {"type": "integer", "coerce": "to_integer"},
        }
        data = {
            "integer_1": 12345,
            "integer_2": "None",
            "integer_3": None,
        }
        result = {
            "integer_1": 12345,
            "integer_2": 0,
            "integer_3": 0,
        }

        validator = Validator(schema=schema)
        assert validator.validate(data), validator.errors
        assert validator.document == result
Beispiel #6
0
    def test_to_string(self):
        schema = {
            "string_1": {"type": "string", "coerce": "to_string"},
            "string_2": {"type": "string", "coerce": "to_string"},
            "string_3": {"type": "string", "coerce": "to_string"},
        }
        data = {
            "string_1": "This is a normal string",
            "string_2": "None",
            "string_3": None,
        }
        result = {
            "string_1": "This is a normal string",
            "string_2": "",
            "string_3": "",
        }

        validator = Validator(schema=schema)
        assert validator.validate(data), validator.errors
        assert validator.document == result
    def test_a_validator_can_be_set_without_affecting_the_base_class(self):
        DataSet.set_validator(
            Validator())  # Revert the validator set in the test above.
        SanitisedDataSet.set_validator(Validator(require_all=True))

        class Example1(DataSet):
            schema = {
                "string_1": {
                    "type": "string",
                    "coerce": "to_string"
                },
                "string_2": {
                    "type": "string",
                    "coerce": "to_string"
                },
            }

        class Example2(SanitisedDataSet):
            schema = {
                "string_1": {
                    "type": "string",
                    "coerce": "to_string"
                },
                "string_2": {
                    "type": "string",
                    "coerce": "to_string"
                },
            }

        data = {
            "string_1": "Lorem ipsum",
        }
        result = {
            "string_1": "Lorem ipsum",
        }

        with pytest.raises(DocumentError):
            validated = Example2.validate(data)

        validated = Example1.validate_object(data)
        assert validated == result
Beispiel #8
0
    def test_to_nullable_float(self):
        schema = {
            "float_1": {
                "type": "float",
                "nullable": True,
                "coerce": "to_nullable_float",
            },
            "float_2": {
                "type": "float",
                "nullable": True,
                "coerce": "to_nullable_float",
            },
            "float_3": {
                "type": "float",
                "nullable": True,
                "coerce": "to_nullable_float",
            },
            "float_4": {
                "type": "float",
                "nullable": True,
                "coerce": "to_nullable_float",
            },
        }
        data = {
            "float_1": 0.0005,
            "float_2": "",
            "float_4": "None",
            "float_3": None,
        }
        result = {
            "float_1": 0.0005,
            "float_2": None,
            "float_3": None,
            "float_4": None,
        }

        validator = Validator(schema=schema)
        assert validator.validate(data), validator.errors
        assert validator.document == result