This repository was archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtest_encode.py
More file actions
153 lines (135 loc) · 4.79 KB
/
test_encode.py
File metadata and controls
153 lines (135 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import coreapi
import coreschema
from openapi_codec.encode import generate_swagger_object, _get_parameters
from unittest import TestCase
class TestBasicInfo(TestCase):
def setUp(self):
self.document = coreapi.Document(
title='Example API',
url='https://www.example.com/',
description='Example description.',
)
self.swagger = generate_swagger_object(self.document)
def test_info(self):
self.assertIn('info', self.swagger)
expected = {
'title': self.document.title,
'version': '',
'description': self.document.description,
}
self.assertEquals(self.swagger['info'], expected)
def test_swagger_version(self):
self.assertIn('swagger', self.swagger)
expected = '2.0'
self.assertEquals(self.swagger['swagger'], expected)
def test_host(self):
self.assertIn('host', self.swagger)
expected = 'www.example.com'
self.assertEquals(self.swagger['host'], expected)
def test_schemes(self):
self.assertIn('schemes', self.swagger)
expected = ['https']
self.assertEquals(self.swagger['schemes'], expected)
class TestPaths(TestCase):
def setUp(self):
self.path = '/users/'
self.document = coreapi.Document(
content={
'users': {
'create': coreapi.Link(
action='post',
url=self.path
),
'list': coreapi.Link(
action='get',
url=self.path
)
}
}
)
self.swagger = generate_swagger_object(self.document)
def test_paths(self):
self.assertIn('paths', self.swagger)
self.assertIn(self.path, self.swagger['paths'])
self.assertIn('get', self.swagger['paths'][self.path])
self.assertIn('post', self.swagger['paths'][self.path])
expected = {
'responses': {
'200': {
'description': ''
}
},
'parameters': [],
'operationId': 'list',
'tags': ['users']
}
self.assertEquals(self.swagger['paths'][self.path]['get'], expected)
expected = {
'responses': {
'201': {
'description': ''
}
},
'parameters': [],
'operationId': 'create',
'tags': ['users']
}
self.assertEquals(self.swagger['paths'][self.path]['post'], expected)
class TestParameters(TestCase):
def setUp(self):
self.field = coreapi.Field(
name='email',
required=True,
location='query',
schema=coreschema.String(description='A valid email address.')
)
self.swagger = _get_parameters(coreapi.Link(fields=[self.field]), encoding='')
def test_expected_fields(self):
self.assertEquals(len(self.swagger), 1)
expected = {
'name': self.field.name,
'required': self.field.required,
'in': 'query',
'description': self.field.schema.description,
'type': 'string' # Everything is a string for now.
}
self.assertEquals(self.swagger[0], expected)
class TestFormat(TestCase):
def setUp(self):
self.field = coreapi.Field(
name='published_before',
required=True,
location='query',
schema=coreschema.String(description='Filter by published date.', format='date')
)
self.swagger = _get_parameters(coreapi.Link(fields=[self.field]), encoding='')
def test_expected_fields(self):
self.assertEquals(len(self.swagger), 1)
expected = {
'name': self.field.name,
'required': self.field.required,
'in': 'query',
'description': self.field.schema.description,
'type': 'string',
'format': 'date'
}
self.assertEquals(self.swagger[0], expected)
class TestIntegerField(TestCase):
def setUp(self):
self.field = coreapi.Field(
name='page',
required=True,
location='query',
schema=coreschema.Integer(description='A page number.')
)
self.swagger = _get_parameters(coreapi.Link(fields=[self.field]), encoding='')
def test_expected_fields(self):
self.assertEquals(len(self.swagger), 1)
expected = {
'name': self.field.name,
'required': self.field.required,
'in': 'query',
'description': self.field.schema.description,
'type': 'integer'
}
self.assertEquals(self.swagger[0], expected)