async def test_get_returns_correct_instance(endpoint: InstanceEndpoint, resource: RestResource, model_1: Model): response = await endpoint.get(model_1.id) assert json.loads( response.body.decode('utf-8')) == resource.render(model_1) assert response.status == 200 assert response.content_type == 'application/json'
async def test_get_should_return_all_instances(endpoint: CollectionEndpoint, resource: RestResource, model_1, model_2): response = await endpoint.get() people = json.loads(response.body.decode('utf-8')) assert len(people) == 2 assert resource.render(model_1) in people assert resource.render(model_2) in people assert response.status == 200 assert response.content_type == 'application/json'
async def test_post_should_create_new_model(endpoint: CollectionEndpoint, resource: RestResource, models): request = create_mock(method='POST', json=coroutine(lambda: {'name': 'henry', 'age': 469})) response = await endpoint.post(request) assert response.status == 201 assert response.content_type == 'application/json' person = json.loads(response.body.decode('utf-8')) assert response.headers['LOCATION'] == '/{name}/{id}'.format(name=resource.name, id=person['id']) assert person['name'] == 'henry' assert person['age'] == 469 assert person in [resource.render(model) for model in models.values()] assert len(models) == 3
async def test_post_should_create_new_model(endpoint: CollectionEndpoint, resource: RestResource, models): request = create_mock(method='POST', json=coroutine(lambda: { 'name': 'henry', 'age': 469 })) response = await endpoint.post(request) assert response.status == 201 assert response.content_type == 'application/json' person = json.loads(response.body.decode('utf-8')) assert response.headers['LOCATION'] == '/{name}/{id}'.format( name=resource.name, id=person['id']) assert person['name'] == 'henry' assert person['age'] == 469 assert person in [resource.render(model) for model in models.values()] assert len(models) == 3
def resource(models): return RestResource('people', Model, models, ('id', 'name', 'age'), 'id')
from aiohttp.web import Application, run_app from settings import config from aiohttp_rest import RestResource from models import Bid from sqlalchemy import engine_from_config app = Application() app['config'] = config bid_resource = RestResource(collection_name='bids', factory=Bid, properties=('code', 'created_at', 'created_by', 'quantity', 'price'), id_field= 'code') bid_resource.register(app.router) if __name__ == '__main__': run_app(app)
from aiohttp.web import Application, run_app from aiohttp_rest import RestResource from models import Note from sqlalchemy import engine_from_config notes = {} app = Application() person_resource = RestResource( 'notes', Note, notes, ('title', 'description', 'created_at', 'created_by', 'priority'), 'title') person_resource.register(app.router) if __name__ == '__main__': run_app(app)
def empty_endpoint(): return CollectionEndpoint( RestResource('people', None, {}, ('id', 'name', 'age'), 'id'))
from aiohttp.web import Application, run_app from aiohttp_rest import RestResource class Person: def __init__(self, name, age): self.name = name self.age = age people = {} app = Application() person_resource = RestResource('people', Person, people, ('name', 'age'), 'name') person_resource.register(app.router) if __name__ == '__main__': run_app(app)
async def test_get_returns_correct_instance(endpoint: InstanceEndpoint, resource: RestResource, model_1: Model): response = await endpoint.get(model_1.id) assert json.loads(response.body.decode('utf-8')) == resource.render(model_1) assert response.status == 200 assert response.content_type == 'application/json'