def test_create_resource(db_path): Db(db_path).execute_script('tests/sql/basic.sql') client = create_app(db_path).test_client() response = client.get('/api/articles') assert len(response.get_json(force=True)['data']) == 2 response = client.post('/api/articles', json={ 'data': { 'type': 'articles', 'attributes': { 'title': 'Article 3', 'body': 'Body 3', } } }) assert response.headers['Content-Type'] == 'application/vnd.api+json' assert response.status_code == 201 assert response.get_json(force=True) == { 'data': { 'type': 'articles', 'id': 3, 'attributes': { 'title': 'Article 3', 'body': 'Body 3', }, 'links': { 'self': 'http://localhost/api/articles/3' } } } response = client.get('/api/articles') assert len(response.get_json(force=True)['data']) == 3
def test_get_all(db_path): Db(db_path).execute_script('tests/sql/basic.sql') client = create_app(db_path).test_client() response = client.get('/api/articles') assert response.headers['Content-Type'] == 'application/vnd.api+json' assert response.status_code == 200 assert response.get_json(force=True) == { 'data': [ { 'type': 'articles', 'id': 1, 'attributes': { 'title': 'Article 1', 'body': 'Body 1', }, 'links': { 'self': 'http://localhost/api/articles/1' } }, { 'type': 'articles', 'id': 2, 'attributes': { 'title': 'Article 2', 'body': 'Body 2', }, 'links': { 'self': 'http://localhost/api/articles/2' } }, ] }
def test_update_resource(db_path): Db(db_path).execute_script('tests/sql/basic.sql') client = create_app(db_path).test_client() response = client.patch('/api/articles/1', json={ 'data': { 'type': 'articles', 'id': 1, 'attributes': { 'body': 'changed', } } }) assert response.status_code == 204 # Make sure that we've changed only the desired record response = client.get('/api/articles/1') data = response.get_json(force=True)['data'] assert data['attributes']['title'] == 'Article 1' assert data['attributes']['body'] == 'changed' response = client.get('/api/articles/2') data = response.get_json(force=True)['data'] assert data['attributes']['title'] == 'Article 2' assert data['attributes']['body'] == 'Body 2'
def test_create_resource_with_relationship_has_many(db_path): Db(db_path).execute_script('tests/sql/relationships.sql') client = create_app(db_path).test_client() response = client.get('/api/authors') assert len(response.get_json(force=True)['data']) == 1 response = client.post('/api/authors', json={ 'data': { 'type': 'authors', 'attributes': { 'name': 'Author 2', }, 'relationships': { 'articles': { 'data': [ { 'type': 'articles', 'id': 1 }, { 'type': 'articles', 'id': 2 }, ] } } } }) assert response.headers['Content-Type'] == 'application/vnd.api+json' assert response.status_code == 201 assert response.get_json(force=True) == { 'data': { 'type': 'authors', 'id': 2, 'attributes': { 'name': 'Author 2', }, 'links': { 'self': 'http://localhost/api/authors/2' }, 'relationships': { 'articles': { 'links': { 'related': 'http://localhost/api/authors/2/articles' } } } } } response = client.get('/api/articles') assert len(response.get_json(force=True)['data']) == 2 # Make sure author sees both articles response = client.get('http://localhost/api/authors/2/articles') assert response.headers['Content-Type'] == 'application/vnd.api+json' assert response.status_code == 200 assert len(response.get_json(force=True)['data']) == 2
def test_relationships_inference(db_path): Db(db_path).execute_script('tests/sql/relationships.sql') app = create_app(db_path) assert app.config['RELATIONSHIPS']['articles'] == [ BelongsTo('articles', 'author') ] assert app.config['RELATIONSHIPS']['authors'] == [ HasMany('authors', 'articles') ]
def test_get_one_not_found(db_path): Db(db_path).execute_script('tests/sql/basic.sql') client = create_app(db_path).test_client() response = client.get('/api/articles/3') assert response.headers['Content-Type'] == 'application/vnd.api+json' assert response.status_code == 404 assert response.get_json() == { 'errors': [{ 'title': 'Resource not found', 'detail': 'Resource "articles" with id "3" not found' }] }
def test_delete_resource(db_path): Db(db_path).execute_script('tests/sql/basic.sql') client = create_app(db_path).test_client() response = client.get('/api/articles') assert len(response.get_json(force=True)['data']) == 2 response = client.delete('/api/articles/1') assert response.status_code == 204 response = client.get('/api/articles') assert len(response.get_json(force=True)['data']) == 1 assert response.get_json(force=True)['data'][0]['id'] == 2
def test_get_belongs_to(db_path): Db(db_path).execute_script('tests/sql/relationships.sql') client = create_app(db_path).test_client() response = client.get('/api/articles/1') assert response.status_code == 200 assert response.headers['Content-Type'] == 'application/vnd.api+json' assert response.get_json()['data']['relationships'] == { 'author': { 'links': { 'related': 'http://localhost/api/articles/1/author' } } } response = client.get('api/articles/1/author') assert response.status_code == 200 assert response.headers['Content-Type'] == 'application/vnd.api+json' assert response.get_json() == { 'data': { 'type': 'authors', 'id': 1, 'attributes': { 'name': 'Author 1', }, 'links': { 'self': 'http://localhost/api/authors/1' }, 'relationships': { 'articles': { 'links': { 'related': 'http://localhost/api/authors/1/articles' } } } } } response = client.get('api/articles/2/author') assert response.status_code == 200 assert response.headers['Content-Type'] == 'application/vnd.api+json' assert response.get_json() == {'data': None}
def test_empty_db(db_path): client = create_app(db_path).test_client() response = client.get('/') assert response.status_code == 200