diff --git a/tests/unit/library/test_json.py b/tests/unit/library/test_json.py index ab7592e93..da7d5459a 100644 --- a/tests/unit/library/test_json.py +++ b/tests/unit/library/test_json.py @@ -2311,132 +2311,6 @@ def test_false_schema(self, schema_obj): assert ve.value.args[0] == "No valid JSON can be generated from a schema of `False`" class TestWhitespace: - schema = { - "title": "Complex Example", - "type": "object", - "properties": { - "id": { - "type": "string", - "format": "uuid", - "description": "A unique identifier" - }, - "name": { - "type": "string", - "minLength": 3, - "maxLength": 50 - }, - "age": { - "type": "integer", - "minimum": 0, - "maximum": 120 - }, - "emails": { - "type": "array", - "items": { - "type": "string", - "format": "email" - }, - "minItems": 1, - "maxItems": 5 - }, - "address": { - "type": "object", - "properties": { - "street": { "type": "string" }, - "city": { "type": "string" }, - "zipcode": { "type": "string", "pattern": "^\\d{5}(-\\d{4})?$" } - }, - "required": ["street", "city"] - }, - "phoneNumbers": { - "type": "array", - "items": { - "type": "object", - "properties": { - "type": { "type": "string" }, - "number": { "type": "string", "pattern": "^\\+?[1-9]\\d{1,14}$" } - }, - "required": ["type", "number"] - }, - "minItems": 1, - }, - "preferences": { - "type": "object", - "properties": { - "newsletter": { "type": "boolean" }, - "theme": { - "enum": ["light", "dark"] - } - }, - "additionalProperties": False - }, - "notes": { - "type": "array", - "items": { "type": "string" }, - "minItems": 0, - "maxItems": 10, - }, - "metadata": { - "type": "object", - "additionalProperties": { "type": "string" } - }, - "createdAt": { - "type": "string", - "format": "date-time" - }, - "tags": { - "type": "array", - "items": { "type": "string" }, - "minItems": 1, - "maxItems": 10, - } - }, - "required": ["id", "name", "emails", "createdAt"], - "additionalProperties": False - } - - obj = { - "id": "d290f1ee-6c54-4b01-90e6-d701748f0851", - "name": "John Doe", - "age": 35, - "emails": [ - "john.doe@example.com", - "john.d@example.org", - ], - "address": { - "street": "123 Main St", - "city": "Metropolis", - "zipcode": "12345", - }, - "phoneNumbers": [ - { - "type": "mobile", - "number": "+1234567890", - }, - { - "type": "home", - "number": "+1987654321", - } - ], - "preferences": { - "newsletter": True, - "theme": "dark", - }, - "notes": [ - "This is the first note.", - "This is the second note.", - ], - "metadata": { - "source": "signup_form", - "referrer": "social_media", - }, - "createdAt": "2024-09-30T14:48:00Z", - "tags": [ - "new_user", - "vip", - ], - } - seps = [ (", ", ": "), (",", ":"), @@ -2444,14 +2318,25 @@ class TestWhitespace: (", ", ":"), ] + @pytest.mark.parametrize( + "schema, obj", + [ + # Dynamic object (both item and key seps) + ({"type": "object"}, {"a": 1, "b": 2, "c": [1, 2, 3]}), + # Static object: enum (both item and key seps) + ({"enum": [{"a": 1, "b": 2, "c": [1, 2, 3]}]}, {"a": 1, "b": 2, "c": [1, 2, 3]}), + # Static object: const (both item and key seps) + ({"const": {"a": 1, "b": 2, "c": [1, 2, 3]}}, {"a": 1, "b": 2, "c": [1, 2, 3]}), + ] + ) @pytest.mark.parametrize( "separators", seps, ) - def test_separators(self, separators): - grammar = gen_json(schema=self.schema, separators=separators) + def test_separators(self, separators, schema, obj): + grammar = gen_json(schema=schema, separators=separators) for seps in self.seps: - prepared_json = json.dumps(self.obj, separators=seps) + prepared_json = json.dumps(obj, separators=seps) if separators == seps: assert grammar.match(prepared_json) is not None model = models.Mock(f"{prepared_json}".encode()) @@ -2459,6 +2344,17 @@ def test_separators(self, separators): else: assert grammar.match(prepared_json) is None + @pytest.mark.parametrize( + "schema, obj", + [ + # Dynamic object (both item and key seps) + ({"type": "object"}, {"a": 1, "b": 2, "c": [1, 2, 3]}), + # Static object: enum (both item and key seps) + ({"enum": [{"a": 1, "b": 2, "c": [1, 2, 3]}]}, {"a": 1, "b": 2, "c": [1, 2, 3]}), + # Static object: const (both item and key seps) + ({"const": {"a": 1, "b": 2, "c": [1, 2, 3]}}, {"a": 1, "b": 2, "c": [1, 2, 3]}), + ] + ) @pytest.mark.parametrize( "separators", seps, @@ -2467,10 +2363,10 @@ def test_separators(self, separators): "indent", [None, 0, 2, 4], ) - def test_whitespace_flexibility(self, indent, separators): - grammar = gen_json(schema=self.schema, whitespace_flexible=True) - for seps in self.seps: - prepared_json = json.dumps(self.obj, separators=separators, indent=indent) - assert grammar.match(prepared_json, raise_exceptions=True) is not None - model = models.Mock(f"{prepared_json}".encode()) - assert str(model + grammar) == prepared_json + def test_whitespace_flexibility(self, indent, separators, schema, obj): + grammar = gen_json(schema=schema, whitespace_flexible=True) + prepared_json = json.dumps(obj, separators=separators, indent=indent) + + assert grammar.match(prepared_json, raise_exceptions=True) is not None + model = models.Mock(f"{prepared_json}".encode()) + assert str(model + grammar) == prepared_json