Skip to content

Commit 48b33c5

Browse files
committed
separate spec files test
1 parent 7d3bc1d commit 48b33c5

File tree

7 files changed

+140
-6
lines changed

7 files changed

+140
-6
lines changed

openapi_spec_validator/shortcuts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44

55
def validate_spec_factory(validator_callable):
6-
def validate(spec):
7-
return validator_callable(spec)
6+
def validate(spec, spec_url=''):
7+
return validator_callable(spec, spec_url=spec_url)
88
return validate
99

1010

tests/integration/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
import pytest
44
from six.moves.urllib import request
5+
from six.moves.urllib.parse import urlunparse
56
from yaml import safe_load
67

78
from openapi_spec_validator import openapi_v3_spec_validator
89

910

11+
def spec_url(spec_file, schema='file'):
12+
directory = path.abspath(path.dirname(__file__))
13+
full_path = path.join(directory, spec_file)
14+
return urlunparse((schema, None, full_path, None, None, None))
15+
16+
1017
def spec_from_file(spec_file):
1118
directory = path.abspath(path.dirname(__file__))
1219
path_full = path.join(directory, spec_file)
@@ -27,6 +34,7 @@ class Factory(dict):
2734
@pytest.fixture
2835
def factory():
2936
return Factory(
37+
spec_url=spec_url,
3038
spec_from_file=spec_from_file,
3139
spec_from_url=spec_from_url,
3240
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type: object
2+
required:
3+
- code
4+
- message
5+
properties:
6+
code:
7+
type: integer
8+
format: int32
9+
message:
10+
type: string
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: How many items to return at one time (max 100)
20+
required: false
21+
schema:
22+
type: integer
23+
format: int32
24+
responses:
25+
'200':
26+
description: An paged array of pets
27+
headers:
28+
x-next:
29+
description: A link to the next page of responses
30+
schema:
31+
type: string
32+
content:
33+
application/json:
34+
schema:
35+
$ref: "schemas/Pets.yaml"
36+
default:
37+
description: unexpected error
38+
content:
39+
application/json:
40+
schema:
41+
$ref: "../common/schemas/Error.yaml"
42+
post:
43+
summary: Create a pet
44+
operationId: createPets
45+
tags:
46+
- pets
47+
responses:
48+
'201':
49+
description: Null response
50+
default:
51+
description: unexpected error
52+
content:
53+
application/json:
54+
schema:
55+
$ref: "../common/schemas/Error.yaml"
56+
/pets/{petId}:
57+
get:
58+
summary: Info for a specific pet
59+
operationId: showPetById
60+
tags:
61+
- pets
62+
parameters:
63+
- name: petId
64+
in: path
65+
required: true
66+
description: The id of the pet to retrieve
67+
schema:
68+
type: string
69+
responses:
70+
'200':
71+
description: Expected response to a valid request
72+
content:
73+
application/json:
74+
schema:
75+
$ref: "schemas/Pets.yaml"
76+
default:
77+
description: unexpected error
78+
content:
79+
application/json:
80+
schema:
81+
$ref: "../common/schemas/Error.yaml"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
required:
2+
- id
3+
- name
4+
properties:
5+
id:
6+
type: integer
7+
format: int64
8+
name:
9+
type: string
10+
tag:
11+
type: string
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type: array
2+
items:
3+
$ref: "schemas/Pet.yaml"

tests/integration/test_validate.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55

66
class BaseTestValidOpeAPIv3Validator(object):
77

8-
def test_valid(self, validator, spec):
9-
return validator.validate(spec)
8+
@pytest.fixture
9+
def spec_url(self):
10+
return ''
11+
12+
def test_valid(self, validator, spec, spec_url):
13+
return validator.validate(spec, spec_url=spec_url)
1014

1115

1216
class BaseTestFailedOpeAPIv3Validator(object):
1317

14-
def test_failed(self, validator, spec):
18+
@pytest.fixture
19+
def spec_url(self):
20+
return ''
21+
22+
def test_failed(self, validator, spec, spec_url):
1523
with pytest.raises(ValidationError):
16-
validator.validate(spec)
24+
validator.validate(spec, spec_url=spec_url)
1725

1826

1927
class TestLocalEmptyExample(BaseTestFailedOpeAPIv3Validator):
@@ -30,6 +38,19 @@ def spec(self, factory):
3038
return factory.spec_from_file("data/v3.0/petstore.yaml")
3139

3240

41+
class TestLocalPetstoreSeparateExample(BaseTestValidOpeAPIv3Validator):
42+
43+
spec_file = "data/v3.0/petstore-separate/spec/openapi.yaml"
44+
45+
@pytest.fixture
46+
def spec_url(self, factory):
47+
return factory.spec_url(self.spec_file)
48+
49+
@pytest.fixture
50+
def spec(self, factory):
51+
return factory.spec_from_file(self.spec_file)
52+
53+
3354
class TestPetstoreExample(BaseTestValidOpeAPIv3Validator):
3455

3556
@pytest.fixture

0 commit comments

Comments
 (0)