def test_exclusive_expression(self): from cocktail.schema import Schema, String, Boolean from cocktail.schema.exceptions import (ValueRequiredError, NoneRequiredError) test_schema = Schema() test_schema.add_member(Boolean("enabled")) test_schema.add_member( String("field", exclusive=test_schema["enabled"])) # Valid states assert test_schema.validate({"enabled": False, "field": None}) assert test_schema.validate({"enabled": True, "field": "foo"}) # None required error errors = list( test_schema.get_errors({ "enabled": False, "field": "foo" })) assert len(errors) == 1 error = errors[0] assert isinstance(error, NoneRequiredError) # Required error errors = list(test_schema.get_errors({"enabled": True, "field": None})) assert len(errors) == 1 error = errors[0] assert isinstance(error, ValueRequiredError)
def test_translated(self): from cocktail.schema import Schema, String, exceptions schema = Schema() schema.add_member(String("foo", translated=True, required=True)) validable = {"foo": {"ca": "Hola", "es": "Hola", "en": "Hello"}} assert not list(schema.get_errors(validable)) validable["foo"]["fr"] = None validable["foo"]["es"] = None errors = list(schema.get_errors(validable)) assert len(errors) == 2 assert set([error.language for error in errors]) == set(["fr", "es"])
def test_base_isolation(self): from cocktail.schema import Schema base = Schema() derived = Schema() self._add_error(derived, "derived_error") derived.inherit(base) assert not list(base.get_errors({}))
def test_single_inheritance(self): from cocktail.schema import Schema base = Schema("base") self._add_error(base, "base_error") derived = Schema("derived") derived.inherit(base) errors = list(derived.get_errors({})) assert len(errors) == 1 assert errors[0].error_id == "base_error"
def test_callable(self): from cocktail.schema import Schema, String, Boolean from cocktail.schema.exceptions import ValueRequiredError test_schema = Schema() test_schema.add_member(Boolean("enabled")) test_schema.add_member( String("field", required=lambda ctx: ctx.get_value("enabled"))) assert test_schema.validate({"enabled": False, "field": None}) assert test_schema.validate({"enabled": True, "field": "foo"}) errors = list(test_schema.get_errors({"enabled": True, "field": None})) assert len(errors) == 1 error = errors[0] assert isinstance(error, ValueRequiredError)
def test_multiple_inheritance(self): from cocktail.schema import Schema base1 = Schema("base1") self._add_error(base1, "base1_error") base2 = Schema("base2") self._add_error(base2, "base2_error") derived = Schema("derived") derived.inherit(base1) derived.inherit(base2) errors = list(derived.get_errors({})) assert len(errors) == 2 assert errors[0].error_id == "base1_error" assert errors[1].error_id == "base2_error"
def test_deep_inheritance(self): from cocktail.schema import Schema s1 = Schema() self._add_error(s1, "s1_error") s2 = Schema() s2.inherit(s1) self._add_error(s2, "s2_error") s3 = Schema() s3.inherit(s2) self._add_error(s3, "s3_error") s4 = Schema() s4.inherit(s3) errors = list(s4.get_errors({})) assert len(errors) == 3 assert errors[0].error_id == "s1_error" assert errors[1].error_id == "s2_error" assert errors[2].error_id == "s3_error"