def test_make_model_create_table(complex_type, complex_fields): """ Verifies that make_model generates the model's table. """ complex_type.enabled = 1 make_model(complex_type) assert complex_type.name in db.get_tables()
def test_make_model_disabled(complex_type, complex_fields): """ Verifies that make_model raises an exception when trying to generate a disabled type's model. """ with pytest.raises(ValueError): make_model(complex_type)
def test_types_post_delete(dummy_user): """ Tests the types post_delete signal. """ new_type = Types(name='somerandtype', enabled=1) new_type.save() make_model(new_type) new_type.delete_instance() assert 'somerandtype' not in db.get_tables()
def test_make_resource_serialization_get(client, app, admin_auth, complex_type, complex_item): model = make_model(complex_type) resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) response = client.get('/endpoint/%s' % (complex_item.id), headers={'authorization': admin_auth}) assert response.status == falcon.HTTP_OK
def test_make_model_ownership(complex_type, complex_fields): """ Verifies that the make_model generated model has an owner attribute. """ complex_type.enabled = 1 model = make_model(complex_type) assert hasattr(model, 'owner') assert isinstance(getattr(model, 'owner'), ForeignKeyField)
def test_make_resource_make_model(client, app, dummy_type, custom_field, method): """ Tests whether make_resource can correctly generate a resource using make_model. """ model = make_model(dummy_type) resource = make_resource(model) assert hasattr(resource, method)
def test_make_collection_serialization_get(client, app, admin_auth, complex_type, complex_fields, complex_item): complex_type.enabled = 1 complex_type.save() model = make_model(complex_type) collection = make_collection(model)() app.add_route('/endpoint', collection) response = client.get('/endpoint', headers={'authorization': admin_auth}) assert response.status == falcon.HTTP_OK
def test_types_pre_delete(dummy_user): new_type = Types(name='somerandtype2', enabled=1) new_type.save() model = make_model(new_type) new_item = model(owner=dummy_user.id) new_item.save() with pytest.raises(ValueError): new_type.delete_instance() # teardown new_item.delete_instance() new_type.delete_instance()
def test_make_collection_make_model_post(client, app, dummy_type, custom_field): """ Verifies make_collection's behaviour with a make_model generated model for non-authenticated POST requests. """ model = make_model(dummy_type) resource = make_collection(model)() app.add_route('/endpoint', resource) response = client.post('/endpoint', {'f': 'text'}) assert response.status == falcon.HTTP_UNAUTHORIZED
def test_make_collection_make_model(client, app, dummy_type, custom_field): """ Verifies that make_collection can use make_model's generated models and return a 401 to simple GET requests. """ model = make_model(dummy_type) resource = make_collection(model)() app.add_route('/endpoint', resource) response = client.get('/endpoint') assert response.status == falcon.HTTP_UNAUTHORIZED assert response.__dict__['headers']['www-authenticate'] != None
def test_make_model_foreign_column_io(complex_type, custom_type_two, dummy_admin): custom_type_two.enabled = 1 custom_type_two.save() complex_type.enabled = 1 complex_type.save() parent_model = make_model(complex_type) model = make_model(custom_type_two) parent_item_dict = { 'owner': dummy_admin.id, 'intfield': 10, 'strfield': 'blah', 'datefield': '2016-01-01', 'ufield': 'u' } parent_item = parent_model(**parent_item_dict) item = model(owner=dummy_admin.id, forfield=parent_item.id) item.save() assert getattr(item, 'id') != None item.delete_instance() parent_item.delete_instance()
def test_make_resource_serialization_patch(client, app, admin_auth, complex_type, complex_item): model = make_model(complex_type) resource = make_resource(model)() body = {'datefield': '2016-12-30', 'intfield': 5} app.add_route('/endpoint/{id}', resource) response = client.patch('/endpoint/%s' % (complex_item.id), body=body, headers={ 'authorization': admin_auth, 'Content-Type': 'application/json' }) assert response.status == falcon.HTTP_OK
def test_make_resource_make_model_delete(client, app, admin_auth, custom_field, dummy_type, dummy_admin): # setup model = make_model(dummy_type) item = model(owner=dummy_admin, f='someval') item.save() item_id = item.id # test resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) response = client.delete('/endpoint/%s' % (item_id), headers={'authorization': admin_auth}) assert response.status == falcon.HTTP_NO_CONTENT assert is_deleted(model, item_id) == True
def test_make_model_foreign_column(complex_type, custom_type_two, foreign_field): """ Tests whether make_model can generate models with foreign key fields. """ complex_type.enabled = 1 complex_type.save() custom_type_two.enabled = 1 model = make_model(custom_type_two) columns = Fields.select().where(Fields.type == custom_type_two.id, Fields.field_type == complex_type.name) for column in columns: field_object = getattr(model, column.name) assert isinstance(field_object, ForeignKeyField)
def complex_item(request, complex_type, complex_fields, dummy_admin): complex_type.enabled = 1 complex_type.save() item = make_model(complex_type)(strfield='test', intfield=10, floatfield=3.2, datefield='2016-04-20', ufield='val', owner=dummy_admin.id) item.save() def teardown(): item.delete_instance() request.addfinalizer(teardown) return item
def test_make_collection_make_model_get_auth(client, app, admin_auth, dummy_type, dummy_admin, custom_field): """ Verifies that make_collection can use make_model's generated models and return a 200 when auth is sent. """ model = make_model(dummy_type) item = model(owner=dummy_admin, f='someval') item.save() resource = make_collection(model)() app.add_route('/endpoint', resource) response = client.get('/endpoint', headers={'authorization': admin_auth}) items = json.loads(response.body)['entities'] assert response.status == falcon.HTTP_OK assert len(items) == model.select().limit(20).count() # teardown item.delete_instance()
def test_make_collection_make_model_post_ownership(client, app, dummy_user, admin_auth, dummy_type, custom_field): """ Verifies that is possible to overwrite an item's owner. """ model = make_model(dummy_type) resource = make_collection(model)() app.add_route('/endpoint', resource) data = {'owner': dummy_user.id} data[custom_field.name] = 'someval' response = client.post('/endpoint', data=data, headers={'authorization': admin_auth}) body = json.loads(response.body)['properties'] # teardown item = model.get(getattr(model, 'id') == int(body['id'])) item.delete_instance() assert int(body['owner']) == dummy_user.id
def test_make_model_io(complex_type, complex_fields, dummy_admin): """ Verifies that is possible to create and delete custom items. """ complex_type.enabled = 1 complex_type.save() model = make_model(complex_type) item_dict = { 'owner': dummy_admin.id, 'intfield': 10, 'floatfield': 4.5, 'strfield': 'blah', 'datefield': '2016-01-01', 'ufield': 'u' } item = model(**item_dict) item.save() assert getattr(item, 'id') != None item.delete_instance()
def test_make_resource_make_model_get(client, app, admin_auth, dummy_type, dummy_admin, custom_field): """ Verifies that make_resource can use make_model's generated models and return a 200 when auth is sent. """ model = make_model(dummy_type) item = model(owner=dummy_admin, f='someval') item.save() resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) response = client.get('/endpoint/%s' % (item.id), headers={'authorization': admin_auth}) assert response.status == falcon.HTTP_OK model_fields = model_columns(model) body = json.loads(response.body)['properties'] for i in model_fields: assert body[i] == getattr(item, i) # teardown item.delete_instance()
def test_make_collection_make_model_post_auth(client, app, dummy_admin, admin_auth, dummy_type, custom_field): """ Verifies make_collection's behaviour with a make_model generated model for authenticated POST requests. """ model = make_model(dummy_type) resource = make_collection(model)() app.add_route('/endpoint', resource) data = {} data[custom_field.name] = 'someval' response = client.post('/endpoint', data=data, headers={'authorization': admin_auth}) assert response.status == falcon.HTTP_CREATED body = json.loads(response.body)['properties'] assert 'id' in body for key in data: assert key in body # teardown item = model.get(getattr(model, 'id') == int(body['id'])) item.delete_instance()
def test_make_resource_make_model_patch(client, app, admin_auth, custom_field, dummy_type, dummy_admin): model = make_model(dummy_type) item = model(owner=dummy_admin, f='someval') item.save() resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) body = {} body[custom_field.name] = 'myval' check = {} check[custom_field.name] = 'myval' response = client.patch('/endpoint/%s' % (item.id), body=body, headers={ 'authorization': admin_auth, 'Content-Type': 'application/json' }) assert response.status == falcon.HTTP_OK response_body = json.loads(response.body)['properties'] for key in body: assert body[key] == response_body[key] # teardown item.delete_instance()
def test_make_model_columns(complex_type, complex_fields): """ Verifies that make_model can correctly generate a model. """ complex_type.enabled = 1 model = make_model(complex_type) fields_dict = { 'string': TextField, 'int': IntegerField, 'float': FloatField, 'bool': BooleanField, 'date': DateTimeField } columns = Fields.select().where(Fields.type == complex_type.id) for column in columns: field = fields_dict[column.field_type] field_object = getattr(model, column.name) assert isinstance(field_object, field) if column.unique: assert getattr(field_object, 'unique') == True if column.nullable: assert getattr(field_object, 'null') == True
def test_make_model_foreign_self(referencing_type, referencing_fields): referencing_type.enabled = 1 referencing_type.save() model = make_model(referencing_type) parent_field = getattr(model, 'parent') assert isinstance(parent_field, ForeignKeyField)