Skip to content

Commit f8c252b

Browse files
committed
create tests for ValidateAdheresToSchema
1 parent 7881f9f commit f8c252b

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import pytest
2+
import jsonschema
3+
from unittest import TestCase
4+
5+
from airbyte_cdk.sources.declarative.validators.validate_adheres_to_schema import (
6+
ValidateAdheresToSchema,
7+
)
8+
9+
10+
class TestValidateAdheresToSchema(TestCase):
11+
def test_given_valid_input_matching_schema_when_validate_then_succeeds(self):
12+
schema = {
13+
"type": "object",
14+
"required": ["id", "name"],
15+
"properties": {
16+
"id": {"type": "integer"},
17+
"name": {"type": "string"},
18+
"email": {"type": "string", "format": "email"},
19+
"age": {"type": "integer", "minimum": 0},
20+
},
21+
}
22+
23+
validator = ValidateAdheresToSchema(schema=schema)
24+
25+
valid_data = {"id": 1, "name": "John Doe", "email": "[email protected]", "age": 30}
26+
27+
validator.validate(valid_data)
28+
29+
def test_given_missing_required_field_when_validate_then_raises_error(self):
30+
schema = {
31+
"type": "object",
32+
"required": ["id", "name"],
33+
"properties": {
34+
"id": {"type": "integer"},
35+
"name": {"type": "string"},
36+
"email": {"type": "string", "format": "email"},
37+
"age": {"type": "integer", "minimum": 0},
38+
},
39+
}
40+
41+
validator = ValidateAdheresToSchema(schema=schema)
42+
43+
# Data missing the required 'name' field
44+
invalid_data = {"id": 1, "email": "[email protected]", "age": 30}
45+
46+
with pytest.raises(ValueError) as exc_info:
47+
validator.validate(invalid_data)
48+
49+
assert "required" in str(exc_info.value)
50+
assert "name" in str(exc_info.value)
51+
52+
def test_given_incorrect_type_when_validate_then_raises_error(self):
53+
schema = {
54+
"type": "object",
55+
"required": ["id", "name"],
56+
"properties": {
57+
"id": {"type": "integer"},
58+
"name": {"type": "string"},
59+
"email": {"type": "string", "format": "email"},
60+
"age": {"type": "integer", "minimum": 0},
61+
},
62+
}
63+
64+
validator = ValidateAdheresToSchema(schema=schema)
65+
66+
invalid_data = {
67+
"id": "one", # Should be an integer
68+
"name": "John Doe",
69+
"email": "[email protected]",
70+
"age": 30,
71+
}
72+
73+
with pytest.raises(ValueError) as exc_info:
74+
validator.validate(invalid_data)
75+
76+
assert "type" in str(exc_info.value)
77+
assert "integer" in str(exc_info.value)
78+
79+
def test_given_invalid_schema_when_validate_then_raises_error(self):
80+
invalid_schema = {"type": "object", "properties": {"id": {"type": "invalid_type"}}}
81+
82+
validator = ValidateAdheresToSchema(schema=invalid_schema)
83+
data = {"id": 123}
84+
85+
with pytest.raises(jsonschema.exceptions.SchemaError) as exc_info:
86+
validator.validate(data)
87+
88+
assert "invalid_type" in str(exc_info.value)
89+
90+
def test_given_null_value_when_validate_then_succeeds_if_nullable(self):
91+
schema = {
92+
"type": "object",
93+
"properties": {
94+
"id": {"type": "integer"},
95+
"name": {"type": "string"},
96+
"optional_field": {"type": ["string", "null"]},
97+
},
98+
"required": ["id", "name"],
99+
}
100+
101+
validator = ValidateAdheresToSchema(schema=schema)
102+
103+
data_with_null = {"id": 1, "name": "Test User", "optional_field": None}
104+
105+
validator.validate(data_with_null)
106+
107+
data_without_field = {"id": 1, "name": "Test User"}
108+
109+
validator.validate(data_without_field)
110+
111+
def test_given_empty_schema_when_validate_then_succeeds(self):
112+
empty_schema = {}
113+
114+
validator = ValidateAdheresToSchema(schema=empty_schema)
115+
116+
validator.validate(123)
117+
validator.validate("string value")
118+
validator.validate({"complex": "object"})
119+
validator.validate([1, 2, 3])
120+
validator.validate(None)
121+
validator.validate(True)

0 commit comments

Comments
 (0)