def test_max_pages(environment_variables): max_pages, _, _ = environment_variables class TestSchema(base.BaseModelSchema): a = fields.Int() b = fields.String() @TestSchema(many=True) def business_logic(*args, **kwargs): return [{'a': 1, 'b': 'One'}, {'a': 2, 'b': 'Two'}] with app.test_request_context('/?page=5'): ctx_mgr = contextlib.suppress() if max_pages == 50 \ else pytest.raises(flask_exceptions.BadRequest) with ctx_mgr: result = business_logic() if max_pages == 2: assert ctx_mgr.excinfo.value.message == 'page: Not a valid page.' else: assert json.loads(result) == { 'data': [{ 'a': 1, 'b': 'One' }, { 'a': 2, 'b': 'Two' }], 'next_page': False, 'page_size': 25 }
def ctx(request): with app.test_request_context() as req_ctx: yield req_ctx if TESTING_SESSION_KEY in session: del session[TESTING_SESSION_KEY] if 'MyCoolForm' in session: del session['MyCoolForm']
def test_show_errors(environment_variables): _, _, show_errors = environment_variables class TestSchema(base.BaseModelSchema): a = fields.Int() b = fields.String() @TestSchema() def business_logic(*args, **kwargs): return {'a': 2, 'b': 'Two'} # LUCKYCHARMS_SHOW_ERRORS set to True (see top of test file); msg set on exceptions with app.test_request_context('/', method='POST', data=json.dumps({ 'a': 2, 'b': 2 }), headers={'Content-Type': 'application/json'}): with pytest.raises(flask_exceptions.BadRequest) as excinfo: business_logic() expected_message = 'b: Not a valid string.' if show_errors else '' assert excinfo.value.message == expected_message
def test_without_proto(): class TestSchema(base.BaseModelSchema): a = fields.Integer() b = fields.String() @TestSchema() def business_logic(*args, **kwargs): return {'a': 1, 'b': 'One'} with app.test_request_context('/'): result = business_logic() assert json.loads(result) == {'a': 1, 'b': 'One'} class TestSchema(base.BaseModelSchema): a = fields.Integer() b = fields.String() config = { 'protobuffers': { 'load': proto.Test(), 'dump': proto.Test(), 'load_many': proto.Test(), 'dump_many': proto.TestCollection() } } with pytest.raises(Exception) as excinfo: @TestSchema() def business_logic(*args, **kwargs): return {'a': 1, 'b': 'One'} assert excinfo.exconly() == "Exception: protobuffer libraries not installed; please install " \ "luckycharms with extra 'proto' (for example, pip install luckycharms[proto])"
def test_max_page_size(environment_variables): _, max_page_size, _ = environment_variables class TestSchema(base.BaseModelSchema): a = fields.Int() b = fields.String() @TestSchema(many=True) def business_logic(*args, **kwargs): return [{'a': 1, 'b': 'One'}, {'a': 2, 'b': 'Two'}] with app.test_request_context('/'): result = business_logic() # default for when max_page_size is None if max_page_size == 25: assert json.loads(result) == { 'data': [{ 'a': 1, 'b': 'One' }, { 'a': 2, 'b': 'Two' }], 'next_page': False, 'page_size': 25 } else: assert json.loads(result) == { 'data': [ { 'a': 1, 'b': 'One' }, ], 'next_page': True, 'page_size': 1 }
def test_form_response(self): '''Test for api.form_response Case 1 : An error is present and no products are given. Case 2 : Products are given and no error is present. ''' #Case1 with app.test_request_context('/search'): products1 = None error1 = "Test error message" response1 = form_response(products1, error1) response1Json = json.loads(response1.get_data(as_text=True)) assert response1Json["errorMessage"] == error1 assert response1Json["products"] == [] #Case2 with app.test_request_context('/search'): products2 = [ { "title": "product3", "popularity": "3", "shop": { "lat": "50.01", "lng": "50.01" } }, { "title": "product2", "popularity": "2", "shop": { "lat": "50.005", "lng": "50.005" } }, { "title": "product1", "popularity": "1", "shop": { "lat": "50.002", "lng": "50.002" } }, ] error2 = None response2 = form_response(products2, error2) response2Json = json.loads(response2.get_data(as_text=True)) assert response2Json["errorMessage"] == '' assert response2Json["products"] == [ { "title": "product3", "popularity": "3", "shop": { "lat": "50.01", "lng": "50.01" } }, { "title": "product2", "popularity": "2", "shop": { "lat": "50.005", "lng": "50.005" } }, { "title": "product1", "popularity": "1", "shop": { "lat": "50.002", "lng": "50.002" } }, ]