def test_validate_dict_without_constraints(self): msg = attributes._validate_dict({}) self.assertIsNone(msg) # Validate a dictionary without constraints. msg = attributes._validate_dict({"key": "value"}) self.assertIsNone(msg)
def test_subdictionary(self): dictionary, constraints = self._construct_dict_and_constraints() del dictionary["key3"]["k4"] dictionary["key3"]["k5"] = "a string value" msg = attributes._validate_dict(dictionary, constraints) self.assertIn("Expected keys:", msg)
def test_validate_dict_convert_boolean(self): dictionary, constraints = self._construct_dict_and_constraints() constraints["key_bool"] = {"type:boolean": None, "required": False, "convert_to": attributes.convert_to_boolean} dictionary["key_bool"] = "true" msg = attributes._validate_dict(dictionary, constraints) self.assertIsNone(msg) # Explicitly comparing with literal 'True' as assertTrue # succeeds also for 'true' self.assertIs(True, dictionary["key_bool"])
def test_validate_dict_wrong_values(self): dictionary, constraints = self._construct_dict_and_constraints() dictionary["key1"] = "UNSUPPORTED" msg = attributes._validate_dict(dictionary, constraints) self.assertIsNotNone(msg)
def test_validate_dict_required_keys(self): dictionary, constraints = self._construct_dict_and_constraints() del dictionary["key1"] msg = attributes._validate_dict(dictionary, constraints) self.assertIn("Expected keys:", msg)
def test_validate_dict_not_required_keys(self): dictionary, constraints = self._construct_dict_and_constraints() del dictionary["key2"] msg = attributes._validate_dict(dictionary, constraints) self.assertIsNone(msg, "Field that was not required by the specs was" "required by the validator.")
def test_validate_dict_with_invalid_validator(self): dictionary, constraints = self._construct_dict_and_constraints() constraints["key1"] = {"type:unsupported": None, "required": True} msg = attributes._validate_dict(dictionary, constraints) self.assertEqual("Validator 'type:unsupported' does not exist.", msg)
def test_validate_a_valid_dict_with_constraints(self): dictionary, constraints = self._construct_dict_and_constraints() msg = attributes._validate_dict(dictionary, constraints) self.assertIsNone(msg, "Validation of a valid dictionary failed.")
def test_validate_dict_type(self): for value in (None, True, "1", []): self.assertEqual("'%s' is not a dictionary" % value, attributes._validate_dict(value))