|
1 | 1 | from jsonschema import ValidationError
|
2 | 2 | import pytest
|
3 | 3 |
|
4 |
| -from openapi_schema_validator import OAS30Validator, oas30_format_checker |
| 4 | +from openapi_schema_validator import OAS30Validator, oas30_format_checker, \ |
| 5 | + OAS31Validator, oas31_format_checker |
5 | 6 |
|
6 | 7 | try:
|
7 | 8 | from unittest import mock
|
@@ -237,3 +238,232 @@ def test_oneof_required(self):
|
237 | 238 | validator = OAS30Validator(schema, format_checker=oas30_format_checker)
|
238 | 239 | result = validator.validate(instance)
|
239 | 240 | assert result is None
|
| 241 | + |
| 242 | + |
| 243 | +class TestOAS31ValidatorValidate(object): |
| 244 | + @pytest.mark.parametrize('schema_type', [ |
| 245 | + 'boolean', 'array', 'integer', 'number', 'string', |
| 246 | + ]) |
| 247 | + def test_null(self, schema_type): |
| 248 | + schema = {"type": schema_type} |
| 249 | + validator = OAS31Validator(schema) |
| 250 | + value = None |
| 251 | + |
| 252 | + with pytest.raises(ValidationError): |
| 253 | + validator.validate(value) |
| 254 | + |
| 255 | + @pytest.mark.parametrize('schema_type', [ |
| 256 | + 'boolean', 'array', 'integer', 'number', 'string', |
| 257 | + ]) |
| 258 | + def test_nullable(self, schema_type): |
| 259 | + schema = {"type": [schema_type, 'null']} |
| 260 | + validator = OAS31Validator(schema) |
| 261 | + value = None |
| 262 | + |
| 263 | + result = validator.validate(value) |
| 264 | + |
| 265 | + assert result is None |
| 266 | + |
| 267 | + @pytest.mark.parametrize('value', [ |
| 268 | + '1989-01-02T00:00:00Z', |
| 269 | + '2018-01-02T23:59:59Z', |
| 270 | + ]) |
| 271 | + @mock.patch( |
| 272 | + 'openapi_schema_validator._format.' |
| 273 | + 'DATETIME_HAS_RFC3339_VALIDATOR', False |
| 274 | + ) |
| 275 | + @mock.patch( |
| 276 | + 'openapi_schema_validator._format.' |
| 277 | + 'DATETIME_HAS_STRICT_RFC3339', False |
| 278 | + ) |
| 279 | + @mock.patch( |
| 280 | + 'openapi_schema_validator._format.' |
| 281 | + 'DATETIME_HAS_ISODATE', False |
| 282 | + ) |
| 283 | + def test_string_format_no_datetime_validator(self, value): |
| 284 | + schema = {"type": 'string', "format": 'date-time'} |
| 285 | + validator = OAS31Validator( |
| 286 | + schema, |
| 287 | + format_checker=oas31_format_checker, |
| 288 | + ) |
| 289 | + |
| 290 | + result = validator.validate(value) |
| 291 | + |
| 292 | + assert result is None |
| 293 | + |
| 294 | + @pytest.mark.parametrize('value', [ |
| 295 | + '1989-01-02T00:00:00Z', |
| 296 | + '2018-01-02T23:59:59Z', |
| 297 | + ]) |
| 298 | + @mock.patch( |
| 299 | + 'openapi_schema_validator._format.' |
| 300 | + 'DATETIME_HAS_RFC3339_VALIDATOR', True |
| 301 | + ) |
| 302 | + @mock.patch( |
| 303 | + 'openapi_schema_validator._format.' |
| 304 | + 'DATETIME_HAS_STRICT_RFC3339', False |
| 305 | + ) |
| 306 | + @mock.patch( |
| 307 | + 'openapi_schema_validator._format.' |
| 308 | + 'DATETIME_HAS_ISODATE', False |
| 309 | + ) |
| 310 | + def test_string_format_datetime_rfc3339_validator(self, value): |
| 311 | + schema = {"type": 'string', "format": 'date-time'} |
| 312 | + validator = OAS31Validator( |
| 313 | + schema, |
| 314 | + format_checker=oas31_format_checker, |
| 315 | + ) |
| 316 | + |
| 317 | + result = validator.validate(value) |
| 318 | + |
| 319 | + assert result is None |
| 320 | + |
| 321 | + @pytest.mark.parametrize('value', [ |
| 322 | + '1989-01-02T00:00:00Z', |
| 323 | + '2018-01-02T23:59:59Z', |
| 324 | + ]) |
| 325 | + @mock.patch( |
| 326 | + 'openapi_schema_validator._format.' |
| 327 | + 'DATETIME_HAS_RFC3339_VALIDATOR', False |
| 328 | + ) |
| 329 | + @mock.patch( |
| 330 | + 'openapi_schema_validator._format.' |
| 331 | + 'DATETIME_HAS_STRICT_RFC3339', True |
| 332 | + ) |
| 333 | + @mock.patch( |
| 334 | + 'openapi_schema_validator._format.' |
| 335 | + 'DATETIME_HAS_ISODATE', False |
| 336 | + ) |
| 337 | + def test_string_format_datetime_strict_rfc3339(self, value): |
| 338 | + schema = {"type": 'string', "format": 'date-time'} |
| 339 | + validator = OAS31Validator( |
| 340 | + schema, |
| 341 | + format_checker=oas31_format_checker, |
| 342 | + ) |
| 343 | + |
| 344 | + result = validator.validate(value) |
| 345 | + |
| 346 | + assert result is None |
| 347 | + |
| 348 | + @pytest.mark.parametrize('value', [ |
| 349 | + '1989-01-02T00:00:00Z', |
| 350 | + '2018-01-02T23:59:59Z', |
| 351 | + ]) |
| 352 | + @mock.patch( |
| 353 | + 'openapi_schema_validator._format.' |
| 354 | + 'DATETIME_HAS_RFC3339_VALIDATOR', False |
| 355 | + ) |
| 356 | + @mock.patch( |
| 357 | + 'openapi_schema_validator._format.' |
| 358 | + 'DATETIME_HAS_STRICT_RFC3339', False |
| 359 | + ) |
| 360 | + @mock.patch( |
| 361 | + 'openapi_schema_validator._format.' |
| 362 | + 'DATETIME_HAS_ISODATE', True |
| 363 | + ) |
| 364 | + def test_string_format_datetime_isodate(self, value): |
| 365 | + schema = {"type": 'string', "format": 'date-time'} |
| 366 | + validator = OAS31Validator( |
| 367 | + schema, |
| 368 | + format_checker=oas31_format_checker, |
| 369 | + ) |
| 370 | + |
| 371 | + result = validator.validate(value) |
| 372 | + |
| 373 | + assert result is None |
| 374 | + |
| 375 | + @pytest.mark.parametrize('value', [ |
| 376 | + 'f50ec0b7-f960-400d-91f0-c42a6d44e3d0', |
| 377 | + 'F50EC0B7-F960-400D-91F0-C42A6D44E3D0', |
| 378 | + ]) |
| 379 | + def test_string_uuid(self, value): |
| 380 | + schema = {"type": 'string', "format": 'uuid'} |
| 381 | + validator = OAS31Validator( |
| 382 | + schema, |
| 383 | + format_checker=oas31_format_checker, |
| 384 | + ) |
| 385 | + |
| 386 | + result = validator.validate(value) |
| 387 | + |
| 388 | + assert result is None |
| 389 | + |
| 390 | + def test_schema_validation(self): |
| 391 | + schema = { |
| 392 | + "type": "object", |
| 393 | + "required": [ |
| 394 | + "name" |
| 395 | + ], |
| 396 | + "properties": { |
| 397 | + "name": { |
| 398 | + "type": "string" |
| 399 | + }, |
| 400 | + "age": { |
| 401 | + "type": "integer", |
| 402 | + "format": "int32", |
| 403 | + "minimum": 0, |
| 404 | + "nullable": True, |
| 405 | + }, |
| 406 | + "birth-date": { |
| 407 | + "type": "string", |
| 408 | + "format": "date", |
| 409 | + } |
| 410 | + }, |
| 411 | + "additionalProperties": False, |
| 412 | + } |
| 413 | + validator = OAS31Validator( |
| 414 | + schema, |
| 415 | + format_checker=oas31_format_checker, |
| 416 | + ) |
| 417 | + |
| 418 | + result = validator.validate({"name": "John", "age": 23}, schema) |
| 419 | + assert result is None |
| 420 | + |
| 421 | + with pytest.raises(ValidationError) as excinfo: |
| 422 | + validator.validate({"name": "John", "city": "London"}, schema) |
| 423 | + |
| 424 | + error = "Additional properties are not allowed ('city' was unexpected)" |
| 425 | + assert error in str(excinfo.value) |
| 426 | + |
| 427 | + with pytest.raises(ValidationError) as excinfo: |
| 428 | + validator.validate({"name": "John", "birth-date": "-12"}) |
| 429 | + |
| 430 | + error = "'-12' is not a 'date'" |
| 431 | + assert error in str(excinfo.value) |
| 432 | + |
| 433 | + def test_schema_ref(self): |
| 434 | + schema = { |
| 435 | + "$ref": "#/$defs/Pet", |
| 436 | + "$defs": { |
| 437 | + "Pet": { |
| 438 | + "required": [ |
| 439 | + "id", |
| 440 | + "name" |
| 441 | + ], |
| 442 | + "properties": { |
| 443 | + "id": { |
| 444 | + "type": "integer", |
| 445 | + "format": "int64" |
| 446 | + }, |
| 447 | + "name": { |
| 448 | + "type": "string" |
| 449 | + }, |
| 450 | + "tag": { |
| 451 | + "type": "string" |
| 452 | + } |
| 453 | + } |
| 454 | + } |
| 455 | + } |
| 456 | + } |
| 457 | + validator = OAS31Validator( |
| 458 | + schema, |
| 459 | + format_checker=oas31_format_checker, |
| 460 | + ) |
| 461 | + |
| 462 | + result = validator.validate({"id": 1, "name": "John"}, schema) |
| 463 | + assert result is None |
| 464 | + |
| 465 | + with pytest.raises(ValidationError) as excinfo: |
| 466 | + validator.validate({"name": "John"}, schema) |
| 467 | + |
| 468 | + error = "'id' is a required property" |
| 469 | + assert error in str(excinfo.value) |
0 commit comments