Skip to content

Commit

Permalink
make whitespace tests more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-ai committed Oct 8, 2024
1 parent 4b34d2e commit b9eb4b8
Showing 1 changed file with 32 additions and 136 deletions.
168 changes: 32 additions & 136 deletions tests/unit/library/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

0 comments on commit b9eb4b8

Please sign in to comment.