-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconftest.py
92 lines (76 loc) · 2.28 KB
/
conftest.py
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
# -*- coding: utf-8 -*-
import functools
import json
import pytest
import yaml
from bravado_asyncio.http_client import AsyncioClient
from bravado_asyncio.http_client import RunMode
from mocket.mocket import Mocketizer
from mocket.plugins.httpretty import HTTPretty
from aiobravado.client import SwaggerClient
API_DOCS_URL = "http://localhost/api-docs"
# Convenience for httpretty.register_uri(httpretty.GET, **kwargs)
register_get = functools.partial(HTTPretty.register_uri, HTTPretty.GET, content_type='application/json')
def register_spec(swagger_dict, response_spec=None, spec_type='json'):
if response_spec is not None:
response_specs = swagger_dict['paths']['/test_http']['get']['responses']
response_specs['200']['schema'] = response_spec
if spec_type == 'yaml':
serialize_function = yaml.dump
content_type = 'application/yaml'
else:
serialize_function = json.dumps
content_type = 'application/json'
headers = 'Content-Type: {0}'.format(content_type)
register_get(
API_DOCS_URL,
body=serialize_function(swagger_dict),
headers=headers,
)
@pytest.fixture
def swagger_dict():
parameter = {
"in": "query",
"name": "test_param",
"type": "string"
}
responses = {
"200": {
"description": "Success"
}
}
operation = {
"operationId": "testHTTP",
"parameters": [parameter],
"responses": responses,
"tags": ["api_test"],
}
paths = {
"/test_http": {
"get": operation
}
}
return {
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Simple"
},
"basePath": "/",
"paths": paths
}
@pytest.fixture(autouse=True)
def httprettified():
with Mocketizer(instance=None):
yield
@pytest.fixture
def http_client(event_loop):
http_client = AsyncioClient(loop=event_loop, run_mode=RunMode.FULL_ASYNCIO)
yield http_client
http_client.client_session.close()
# This function can't be a fixture right now since we need to register the URLs before creating the client
async def swagger_client(http_client):
return await SwaggerClient.from_url(
API_DOCS_URL,
http_client=http_client,
)