1from collections import namedtuple 2 3import pytest 4 5import marshmallow as ma 6 7from flask import Flask 8 9from .mocks import DatabaseMock 10 11 12class AppConfig: 13 """Base application configuration class 14 15 Overload this to add config parameters 16 """ 17 API_TITLE = 'API Test' 18 API_VERSION = '1' 19 OPENAPI_VERSION = '3.0.2' 20 21 22@pytest.fixture(params=[0]) 23def collection(request): 24 _collection = DatabaseMock() 25 for idx in range(request.param): 26 _collection.post({'db_field': idx}) 27 return _collection 28 29 30@pytest.fixture(params=[AppConfig]) 31def app(request): 32 _app = Flask(__name__) 33 _app.config.from_object(request.param) 34 return _app 35 36 37class CounterSchema(ma.Schema): 38 """Base Schema with load/dump counters""" 39 40 load_count = 0 41 dump_count = 0 42 43 @classmethod 44 def reset_load_count(cls): 45 cls.load_count = 0 46 47 @classmethod 48 def reset_dump_count(cls): 49 cls.dump_count = 0 50 51 @ma.post_load(pass_many=True) 52 def increment_load_count(self, data, many, **kwargs): 53 self.__class__.load_count += 1 54 return data 55 56 @ma.post_dump(pass_many=True) 57 def increment_dump_count(self, data, many, **kwargs): 58 self.__class__.dump_count += 1 59 return data 60 61 62@pytest.fixture 63def schemas(): 64 65 class DocSchema(CounterSchema): 66 item_id = ma.fields.Int(dump_only=True) 67 field = ma.fields.Int(attribute='db_field') 68 69 class DocEtagSchema(CounterSchema): 70 field = ma.fields.Int(attribute='db_field') 71 72 class QueryArgsSchema(ma.Schema): 73 class Meta: 74 ordered = True 75 unknown = ma.EXCLUDE 76 arg1 = ma.fields.String() 77 arg2 = ma.fields.Integer() 78 79 class ClientErrorSchema(ma.Schema): 80 error_id = ma.fields.Str() 81 text = ma.fields.Str() 82 83 return namedtuple( 84 'Model', 85 ('DocSchema', 'DocEtagSchema', 'QueryArgsSchema', 'ClientErrorSchema') 86 )(DocSchema, DocEtagSchema, QueryArgsSchema, ClientErrorSchema) 87