-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2311,154 +2311,50 @@ 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": [ | ||
"[email protected]", | ||
"[email protected]", | ||
], | ||
"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 = [ | ||
(", ", ": "), | ||
(",", ":"), | ||
(",", ": "), | ||
(", ", ":"), | ||
] | ||
|
||
@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"<s>{prepared_json}".encode()) | ||
assert str(model + grammar) == prepared_json | ||
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"<s>{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"<s>{prepared_json}".encode()) | ||
assert str(model + grammar) == prepared_json |