Пример #1
0
 def test_enforce_array_schema(self):
     """
     Enforcing array schema with min array length.
     """
     with self.subTest("enforcing array schema"):
         schema = get_schema([{"id": ["null", "integer"]}], enforce=True)
         expected = {
             "type": "array",
             "minItems": 1,
             "items": {
                 "type": "object",
                 "properties": {
                     "id": {
                         "type": ["integer"]
                     }
                 },
                 "required": ["id"],
                 "additionalProperties": False
             }
         }
         self.assertDictEqual(schema, expected)
     with self.subTest("skip enforcing array schema"):
         schema = get_schema([{"id": ["null", "integer"]}], enforce=False)
         expected = {
             "type": "array",
             "items": {
                 "type": "object",
                 "properties": {
                     "id": {
                         "type": ["integer", "null"]
                     }
                 },
             }
         }
         self.assertDictEqual(schema, expected)
Пример #2
0
 def test_enforce_object_schema(self):
     """
     Enforcing object schema with restricting a set of properties and marking
     all of them as required.
     """
     with self.subTest("enforcing object schema"):
         schema = get_schema({"id": ["null", "integer"]}, enforce=True)
         expected = {
             "type": "object",
             "properties": {
                 "id": {
                     "type": ["integer"]
                 }
             },
             "required": ["id"],
             "additionalProperties": False
         }
         self.assertDictEqual(schema, expected)
     with self.subTest("skip enforcing object schema"):
         schema = get_schema({"id": ["null", "integer"]}, enforce=False)
         expected = {
             "type": "object",
             "properties": {
                 "id": {
                     "type": ["integer", "null"]
                 }
             }
         }
         self.assertDictEqual(schema, expected)
Пример #3
0
 def test_enforce_null_type_variant(self):
     """
     Enforcing schema removing null type variant from all types.
     """
     with self.subTest("remove null variant"):
         schema = get_schema([None, int], enforce=True)
         expected = {"type": ["integer"]}
         self.assertDictEqual(schema, expected)
     with self.subTest("skip enforce"):
         schema = get_schema([None, int], enforce=False)
         expected = {"type": ["integer", "null"]}
         self.assertDictEqual(schema, expected)
     with self.subTest("leave single null"):
         schema = get_schema([None], enforce=True)
         expected = {"type": ["null"]}
         self.assertDictEqual(schema, expected)
Пример #4
0
 def test_get_schema_for_an_object(self):
     """
     Generating a schema for a json object.
     """
     schema = get_schema(
         {
             "id": int,
             "name": "string",
             "value": [None, float]
         },
         enforce=False)
     expected = {
         "type": "object",
         "properties": {
             "id": {
                 "type": "integer"
             },
             "name": {
                 "type": "string"
             },
             "value": {
                 "type": ["null", "number"]
             }
         }
     }
     self.assertDictEqual(schema, expected)
Пример #5
0
 def test_get_schema_for_list_of_types(self):
     """
     Generating a schema for a set of type variants.
     """
     schema = get_schema([None, int, "boolean"], enforce=False)
     expected = {"type": ["boolean", "integer", "null"]}
     self.assertDictEqual(schema, expected)
Пример #6
0
 def test_get_schema_from_json_schema_type_name(self):
     """
     Generating jsonschema type definition from type name
     """
     schema = get_schema("integer", enforce=False)
     expected = {"type": "integer"}
     self.assertEqual(schema, expected)
Пример #7
0
 def test_merge_string_and_format(self):
     """
     When any string is allowed, types described by string with format are
     omitted.
     """
     schema = get_schema([str, datetime])
     expected = {"type": ["string"]}
     self.assertDictEqual(schema, expected)
Пример #8
0
 def test_compat_get_schema_for_an_array_of_types(self):
     """
     For backward compatibility schema for [bool] is not an array of boolean,
     but an element of single boolean type.
     """
     schema = get_schema([bool], enforce=False)
     expected = {"type": ["boolean"]}
     self.assertDictEqual(schema, expected)
Пример #9
0
    def test_pass_full_schema_to_an_array(self):
        """
        A complete object schema can be passed to array schema and it will not
        be modified.
        """
        schema = {"type": "object"}

        result = get_schema([schema], enforce=False)
        self.assertIs(result["items"], schema)
Пример #10
0
 def test_pass_format_for_string(self):
     """
     When a type described via string with format is passed as single item
     in type list, format is passed to complete schema.
     """
     schema = get_schema([datetime])
     expected = {
         "type": ["string"],
         "format": "date-time",
     }
     self.assertDictEqual(schema, expected)
Пример #11
0
 def test_enforce_schema_recurse(self):
     """
     Enforcing schema for nested arrays and objects.
     """
     self.maxDiff = None
     schema = get_schema(
         {
             "id": [None, int],
             "nested": {
                 "name": "string",
             },
             "array": [{
                 "slug": "string"
             }]
         },
         enforce=True)
     expected = {
         "type": "object",
         "properties": {
             "id": {
                 "type": ["integer"]
             },
             "nested": {
                 "type": "object",
                 "properties": {
                     "name": {
                         "type": "string"
                     }
                 },
                 "required": ["name"],
                 "additionalProperties": False
             },
             "array": {
                 "type": "array",
                 "minItems": 1,
                 "items": {
                     "type": "object",
                     "properties": {
                         "slug": {
                             "type": "string"
                         }
                     },
                     "required": ["slug"],
                     "additionalProperties": False
                 }
             }
         },
         "required": ["id", "nested", "array"],
         "additionalProperties": False,
     }
     self.assertDictEqual(schema, expected)
Пример #12
0
 def test_get_schema_for_an_array_of_objects(self):
     """
     Generating a schema for an array of json objects.
     """
     schema = get_schema([{"id": int}], enforce=False)
     expected = {
         "type": "array",
         "items": {
             "type": "object",
             "properties": {
                 "id": {
                     "type": "integer"
                 }
             }
         }
     }
     self.assertDictEqual(schema, expected)
Пример #13
0
 def test_get_schema_for_nested_object(self):
     """
     Generating schema for an object that contains nested object.
     """
     schema = get_schema(
         {
             "id": int,
             "nested": {
                 "name": "string"
             },
             "array": [{
                 "slug": "string"
             }]
         },
         enforce=False)
     expected = {
         "type": "object",
         "properties": {
             "id": {
                 "type": "integer"
             },
             "nested": {
                 "type": "object",
                 "properties": {
                     "name": {
                         "type": "string"
                     }
                 }
             },
             "array": {
                 "type": "array",
                 "items": {
                     "type": "object",
                     "properties": {
                         "slug": {
                             "type": "string"
                         }
                     }
                 }
             }
         }
     }
     self.assertDictEqual(schema, expected)
Пример #14
0
 def test_get_schema_from_python_type(self):
     """
     Generating jsonschema type definition from python type
     """
     cases = (
         (None, 'null', None),
         (int, 'integer', None),
         (float, 'number', None),
         (str, 'string', None),
         (bool, 'boolean', None),
         (datetime, 'string', 'date-time'),
         (date, 'string', 'date'),
         (time, 'string', 'time'),
     )
     for t, name, fmt in cases:
         with self.subTest(name):
             schema = get_schema(t, enforce=False)
             expected = {"type": name}
             if fmt is not None:
                 expected["format"] = fmt
             self.assertDictEqual(schema, expected)
Пример #15
0
 def test_bypass_elem_schema(self):
     """
     A complete schema can be passed and will not be modified.
     """
     schema = {"type": "string"}
     self.assertIs(schema, get_schema(schema, enforce=False))