New python-fastapi generator (#9611)

* [python-fastapi] Added new generator

See https://fastapi.tiangolo.com/ for more details about FastAPI

Signed-off-by: Nikita Vakula <programmistov.programmist@gmail.com>

* [python-fastapi] Added samples

Signed-off-by: Nikita Vakula <programmistov.programmist@gmail.com>
This commit is contained in:
Nikita Vakula
2021-05-30 11:43:31 +02:00
committed by GitHub
parent dee2840b20
commit 0da4099868
52 changed files with 3483 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
import contextlib
from typing import Any
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from openapi_server.main import app as application
@pytest.fixture
def app() -> FastAPI:
application.dependency_overrides = {}
return application
@pytest.fixture
def client(app) -> TestClient:
return TestClient(app)

View File

@@ -0,0 +1,165 @@
# coding: utf-8
from fastapi.testclient import TestClient
import json
import pytest
from openapi_server.models.api_response import ApiResponse
from openapi_server.models.pet import Pet
def test_add_pet(client: TestClient):
"""Test case for add_pet
Add a new pet to the store
"""
pet = {"photo_urls":["photoUrls","photoUrls"],"name":"doggie","id":0,"category":{"name":"name","id":6},"tags":[{"name":"name","id":1},{"name":"name","id":1}],"status":"available"}
headers = {
'Authorization': 'Bearer special-key',
}
response = client.request(
'POST',
'/pet',
headers=headers,
json=pet,
)
assert response.status_code == 200
def test_delete_pet(client: TestClient):
"""Test case for delete_pet
Deletes a pet
"""
headers = {
'api_key': 'api_key_example',
'Authorization': 'Bearer special-key',
}
response = client.request(
'DELETE',
'/pet/{petId}'.format(petId=56),
headers=headers,
)
assert response.status_code == 200
def test_find_pets_by_status(client: TestClient):
"""Test case for find_pets_by_status
Finds Pets by status
"""
params = [("status", ['status_example'])]
headers = {
'Authorization': 'Bearer special-key',
}
response = client.request(
'GET',
'/pet/findByStatus',
headers=headers,
params=params,
)
assert response.status_code == 200
def test_find_pets_by_tags(client: TestClient):
"""Test case for find_pets_by_tags
Finds Pets by tags
"""
params = [("tags", ['tags_example'])]
headers = {
'Authorization': 'Bearer special-key',
}
response = client.request(
'GET',
'/pet/findByTags',
headers=headers,
params=params,
)
assert response.status_code == 200
def test_get_pet_by_id(client: TestClient):
"""Test case for get_pet_by_id
Find pet by ID
"""
headers = {
'api_key': 'special-key',
}
response = client.request(
'GET',
'/pet/{petId}'.format(petId=56),
headers=headers,
)
assert response.status_code == 200
def test_update_pet(client: TestClient):
"""Test case for update_pet
Update an existing pet
"""
pet = {"photo_urls":["photoUrls","photoUrls"],"name":"doggie","id":0,"category":{"name":"name","id":6},"tags":[{"name":"name","id":1},{"name":"name","id":1}],"status":"available"}
headers = {
'Authorization': 'Bearer special-key',
}
response = client.request(
'PUT',
'/pet',
headers=headers,
json=pet,
)
assert response.status_code == 200
def test_update_pet_with_form(client: TestClient):
"""Test case for update_pet_with_form
Updates a pet in the store with form data
"""
headers = {
'Authorization': 'Bearer special-key',
}
data = {
'name': 'name_example',
'status': 'status_example'
}
response = client.request(
'POST',
'/pet/{petId}'.format(petId=56),
headers=headers,
data=data,
)
assert response.status_code == 200
def test_upload_file(client: TestClient):
"""Test case for upload_file
uploads an image
"""
headers = {
'Authorization': 'Bearer special-key',
}
data = {
'additional_metadata': 'additional_metadata_example',
'file': '/path/to/file'
}
response = client.request(
'POST',
'/pet/{petId}/uploadImage'.format(petId=56),
headers=headers,
data=data,
)
assert response.status_code == 200

View File

@@ -0,0 +1,76 @@
# coding: utf-8
from fastapi.testclient import TestClient
import json
import pytest
from openapi_server.models.order import Order
def test_delete_order(client: TestClient):
"""Test case for delete_order
Delete purchase order by ID
"""
headers = {
}
response = client.request(
'DELETE',
'/store/order/{orderId}'.format(orderId='order_id_example'),
headers=headers,
)
assert response.status_code == 200
def test_get_inventory(client: TestClient):
"""Test case for get_inventory
Returns pet inventories by status
"""
headers = {
'api_key': 'special-key',
}
response = client.request(
'GET',
'/store/inventory',
headers=headers,
)
assert response.status_code == 200
def test_get_order_by_id(client: TestClient):
"""Test case for get_order_by_id
Find purchase order by ID
"""
headers = {
}
response = client.request(
'GET',
'/store/order/{orderId}'.format(orderId=56),
headers=headers,
)
assert response.status_code == 200
def test_place_order(client: TestClient):
"""Test case for place_order
Place an order for a pet
"""
order = {"pet_id":6,"quantity":1,"id":0,"ship_date":"2000-01-23T04:56:07.000+00:00","complete":0,"status":"placed"}
headers = {
}
response = client.request(
'POST',
'/store/order',
headers=headers,
json=order,
)
assert response.status_code == 200

View File

@@ -0,0 +1,154 @@
# coding: utf-8
from fastapi.testclient import TestClient
import json
import pytest
from openapi_server.models.user import User
def test_create_user(client: TestClient):
"""Test case for create_user
Create user
"""
user = {"first_name":"firstName","last_name":"lastName","password":"password","user_status":6,"phone":"phone","id":0,"email":"email","username":"username"}
headers = {
'api_key': 'special-key',
}
response = client.request(
'POST',
'/user',
headers=headers,
json=user,
)
assert response.status_code == 200
def test_create_users_with_array_input(client: TestClient):
"""Test case for create_users_with_array_input
Creates list of users with given input array
"""
user = [{"first_name":"firstName","last_name":"lastName","password":"password","user_status":6,"phone":"phone","id":0,"email":"email","username":"username"}]
headers = {
'api_key': 'special-key',
}
response = client.request(
'POST',
'/user/createWithArray',
headers=headers,
json=user,
)
assert response.status_code == 200
def test_create_users_with_list_input(client: TestClient):
"""Test case for create_users_with_list_input
Creates list of users with given input array
"""
user = [{"first_name":"firstName","last_name":"lastName","password":"password","user_status":6,"phone":"phone","id":0,"email":"email","username":"username"}]
headers = {
'api_key': 'special-key',
}
response = client.request(
'POST',
'/user/createWithList',
headers=headers,
json=user,
)
assert response.status_code == 200
def test_delete_user(client: TestClient):
"""Test case for delete_user
Delete user
"""
headers = {
'api_key': 'special-key',
}
response = client.request(
'DELETE',
'/user/{username}'.format(username='username_example'),
headers=headers,
)
assert response.status_code == 200
def test_get_user_by_name(client: TestClient):
"""Test case for get_user_by_name
Get user by user name
"""
headers = {
}
response = client.request(
'GET',
'/user/{username}'.format(username='username_example'),
headers=headers,
)
assert response.status_code == 200
def test_login_user(client: TestClient):
"""Test case for login_user
Logs user into the system
"""
params = [("username", 'username_example'),
("password", 'password_example')]
headers = {
}
response = client.request(
'GET',
'/user/login',
headers=headers,
params=params,
)
assert response.status_code == 200
def test_logout_user(client: TestClient):
"""Test case for logout_user
Logs out current logged in user session
"""
headers = {
'api_key': 'special-key',
}
response = client.request(
'GET',
'/user/logout',
headers=headers,
)
assert response.status_code == 200
def test_update_user(client: TestClient):
"""Test case for update_user
Updated user
"""
user = {"first_name":"firstName","last_name":"lastName","password":"password","user_status":6,"phone":"phone","id":0,"email":"email","username":"username"}
headers = {
'api_key': 'special-key',
}
response = client.request(
'PUT',
'/user/{username}'.format(username='username_example'),
headers=headers,
json=user,
)
assert response.status_code == 200