session.close() return {'kittens': [kitten.serialize() for kitten in kittens]} def create_kitten(db: SQLAlchemy, name: http.QueryParam): session = db.session_class() add_kitten = Kitten(name=name) session.add(add_kitten) session.commit() created_kitten = add_kitten.serialize() session.close() return created_kitten app = App(routes=[ routing.Route('/kittens/create/', 'GET', create_kitten), routing.Route('/kittens/', 'GET', list_kittens), ], settings={ "DATABASE": { "URL": environ.get('DB_URL', 'sqlite:///test.db'), "METADATA": Base.metadata } }) client = test.TestClient(app) runner = CommandLineRunner(app) @pytest.fixture def clear_db(scope="function"):
def validate_length(value: MinMaxLength): return {'value': value} def validate_not_blank(value: NotBlank): return {'value': value} def validate_pattern(value: ValidPattern): return {'value': value} app = app.App(routes=[ routing.Route('/length/', 'GET', validate_length), routing.Route('/not_blank/', 'GET', validate_not_blank), routing.Route('/pattern/', 'GET', validate_pattern), ]) client = test.TestClient(app) def test_valid_length(): response = client.get('/length/?value=abcde') assert response.status_code == 200 assert response.json() == {'value': 'abcde'} response = client.get('/length/?value=abcdefghij') assert response.status_code == 200 assert response.json() == {'value': 'abcdefghij'}
from apistar import app, routing, schema, test def get_boolean(value: schema.Boolean): return {'value': value} app = app.App(routes=[ routing.Route('/boolean/', 'GET', get_boolean), ]) client = test.TestClient(app) def test_boolean(): assert schema.Boolean(1) is True assert schema.Boolean(0) is False assert schema.Boolean('1') is True assert schema.Boolean('0') is False assert schema.Boolean(True) is True assert schema.Boolean(False) is False assert schema.Boolean('TRUE') is True assert schema.Boolean('FALSE') is False def test_valid_boolean(): response = client.get('/boolean/?value=true') assert response.status_code == 200 assert response.json() == {'value': True}
errors = { 'exact': 'You must agree to the terms and conditions to proceed.' } enum = ['yes'] def validate_color(value: Color): return {'value': value} def validate_terms(value: TermsAndConditions): return {'value': value} app = app.App(routes=[ routing.Route('/color/', 'GET', validate_color), routing.Route('/terms/', 'GET', validate_terms), ]) client = test.TestClient(app) def test_valid_enum(): response = client.get('/color/?value=red') assert response.status_code == 200 assert response.json() == {'value': 'red'} def test_valid_literal(): response = client.get('/terms/?value=yes') assert response.status_code == 200
class TypedList(schema.Array): items = schema.Integer def basic_untyped_list(things: UntypedList): return things def basic_typed_list(things: TypedList): return things routes = [ routing.Route('/basic_typed_list/', 'POST', basic_typed_list), routing.Route('/basic_untyped_list/', 'POST', basic_untyped_list), ] app = app.App(routes=routes) client = test.TestClient(app) def test_valid_untyped_list(): payload = [ 'foo', 'bar', 1, 3.1415927, 'baz', True,
'completed': schema.Boolean, 'difficulty': schema.Enum(enum=['easy', 'medium', 'hard']), 'location': Location(default={ 'latitude': 0.0, 'longitude': 0.0 }) } required = ['name'] def basic_object(score: HighScore): return score routes = [ routing.Route('/basic_object/', 'POST', basic_object), ] app = app.App(routes=routes) client = test.TestClient(app) def test_valid_object(): response = client.post('/basic_object/', json={ 'name': 'tom', 'score': 87, 'completed': True, 'location': { 'latitude': 51.477, 'longitude': 0.0
class MultipleNumber(schema.Number): multiple_of = 0.1 def get_min_max(value: MinMaxNumber): return {'value': value} def get_multiple(value: MultipleNumber): return {'value': value} app = app.App(routes=[ routing.Route('/min_max/', 'GET', get_min_max), routing.Route('/multiple/', 'GET', get_multiple), ]) client = test.TestClient(app) def test_invalid_numberr(): response = client.get('/min_max/?value=a') assert response.status_code == 400 assert response.json() == {'value': 'Must be a valid number.'} def test_valid_min_max(): response = client.get('/min_max/?value=2.9') assert response.status_code == 200
return { 'stars': list(Star.objects.values('name', 'id')) } def create_star(orm: DjangoBackend, name: http.QueryParam): star = orm.Star(name=name) star.save() return { 'star': {'name': star.name, 'id': star.id} } app = App( routes=[ routing.Route('/api/stars', 'GET', list_stars), routing.Route('/api/stars/create', 'GET', create_star), ], settings={ 'DATABASES': { 'default': dj_database_url.config( default=environ.get('DB_URL', 'sqlite:///test.db') ) }, 'INSTALLED_APPS': ['django_project'] }, commands=[django_makemigrations, django_migrate, django_showmigrations] ) client = test.TestClient(app)