class Pagination(PartialSchema): get_total = fields.Boolean( required=False, description="Request the total number of elements" ) page = fields.Int( required=False, description="Current page number", validate=validate.Range(min=1), ) size = fields.Int( required=False, description="Number of elements to retrieve", validate=validate.Range(min=1, max=100), ) sort_order = fields.Str( validate=validate.OneOf(["asc", "desc"]), required=False, missing="asc" ) sort_by = fields.Str(required=False, missing=None) input_filter = fields.Str(required=False, missing=None) @post_load def verify_parameters(self, data, **kwargs): if "get_total" in data: data["page"] = None data["size"] = None else: data.setdefault("get_total", False) data.setdefault("page", 1) data.setdefault("size", 20) return data
class RAMSchema(Schema): total = fields.Int() used = fields.Int() active = fields.Int() inactive = fields.Int() buffer = fields.Int() free = fields.Int() cache = fields.Int()
class InputSchema(Schema): mystr = fields.Str(required=True, validate=validate.Length(min=1)) mydate = fields.DateTime( required=True, format=ISO8601UTC, # validate=validate.Range( # max=datetime.now(pytz.utc).replace(hour=23, minute=59, second=59), # min=datetime(1900, 1, 1, tzinfo=pytz.utc), # max_inclusive=True, # error="Invalid date", # ), ) myint_exclusive = fields.Int( required=True, validate=validate.Range( min=1, max=10, min_inclusive=False, max_inclusive=False ), ) myint_inclusive = fields.Int( required=True, validate=validate.Range(min=1, max=10), )
class CPUSchema(Schema): count = fields.Int() load_percentage = fields.Decimal(places=2) user = fields.Int() system = fields.Int() idle = fields.Int() wait = fields.Int() stolen = fields.Int()
class ChunkUpload(PartialSchema): name = fields.Str(required=True) mimeType = fields.Str(required=True) size = fields.Int(required=True, validate=validate.Range(min=1)) lastModified = fields.Int(required=True, validate=validate.Range(min=1))
class TotalSchema(Schema): total = fields.Int()
class IOSchema(Schema): blocks_received = fields.Int() blocks_sent = fields.Int()
class ProcSchema(Schema): waiting_for_run = fields.Int() uninterruptible_sleep = fields.Int()
class SwapSchema(Schema): from_disk = fields.Int() to_disk = fields.Int() total = fields.Int() used = fields.Int() free = fields.Int()
def test_responses(self, faker: Faker) -> None: class MySchema(Schema): name = fields.Str() f = "myfield" assert ( ResponseMaker.get_schema_type(f, fields.Str(metadata={"password": True})) == "password" ) assert ResponseMaker.get_schema_type(f, fields.Bool()) == "boolean" assert ResponseMaker.get_schema_type(f, fields.Boolean()) == "boolean" assert ResponseMaker.get_schema_type(f, fields.Date()) == "date" assert ResponseMaker.get_schema_type(f, fields.DateTime()) == "datetime" assert ResponseMaker.get_schema_type(f, fields.AwareDateTime()) == "datetime" assert ResponseMaker.get_schema_type(f, fields.NaiveDateTime()) == "datetime" assert ResponseMaker.get_schema_type(f, fields.Decimal()) == "number" # This is because Email is not typed on marshmallow assert ResponseMaker.get_schema_type(f, fields.Email()) == "email" # type: ignore assert ResponseMaker.get_schema_type(f, fields.Float()) == "number" assert ResponseMaker.get_schema_type(f, fields.Int()) == "int" assert ResponseMaker.get_schema_type(f, fields.Integer()) == "int" assert ResponseMaker.get_schema_type(f, fields.Number()) == "number" assert ResponseMaker.get_schema_type(f, fields.Str()) == "string" assert ResponseMaker.get_schema_type(f, fields.String()) == "string" assert ResponseMaker.get_schema_type(f, fields.Dict()) == "dictionary" assert ResponseMaker.get_schema_type(f, fields.List(fields.Str())) == "string[]" assert ResponseMaker.get_schema_type(f, fields.Nested(MySchema())) == "nested" # Unsupported types, fallback to string assert ResponseMaker.get_schema_type(f, fields.URL()) == "string" assert ResponseMaker.get_schema_type(f, fields.Url()) == "string" assert ResponseMaker.get_schema_type(f, fields.UUID()) == "string" # assert ResponseMaker.get_schema_type(f, fields.Constant("x")) == "string" assert ResponseMaker.get_schema_type(f, fields.Field()) == "string" # assert ResponseMaker.get_schema_type(f, fields.Function()) == "string" # assert ResponseMaker.get_schema_type(f, fields.Mapping()) == "string" # assert ResponseMaker.get_schema_type(f, fields.Method()) == "string" # assert ResponseMaker.get_schema_type(f, fields.Raw()) == "string" # assert ResponseMaker.get_schema_type(f, fields.TimeDelta()) == "string" assert not ResponseMaker.is_binary(None) assert not ResponseMaker.is_binary("") assert not ResponseMaker.is_binary("application/json") assert ResponseMaker.is_binary("application/octet-stream") assert ResponseMaker.is_binary("application/x-bzip") assert ResponseMaker.is_binary("application/x-bzip2") assert ResponseMaker.is_binary("application/pdf") assert ResponseMaker.is_binary("application/msword") assert ResponseMaker.is_binary("application/rtf") assert ResponseMaker.is_binary("application/x-tar") assert ResponseMaker.is_binary("application/gzip") assert ResponseMaker.is_binary("application/zip") assert ResponseMaker.is_binary("application/x-7z-compressed") assert not ResponseMaker.is_binary("text/plain") assert not ResponseMaker.is_binary("text/css") assert not ResponseMaker.is_binary("text/csv") assert not ResponseMaker.is_binary("text/html") assert not ResponseMaker.is_binary("text/javascript") assert not ResponseMaker.is_binary("text/xml") assert ResponseMaker.is_binary("image/gif") assert ResponseMaker.is_binary("image/jpeg") assert ResponseMaker.is_binary("image/png") assert ResponseMaker.is_binary("image/svg+xml") assert ResponseMaker.is_binary("image/tiff") assert ResponseMaker.is_binary("image/webp") assert ResponseMaker.is_binary("image/bmp") assert ResponseMaker.is_binary("image/aac") assert ResponseMaker.is_binary("audio/midi") assert ResponseMaker.is_binary("audio/mpeg") assert ResponseMaker.is_binary("audio/wav") assert ResponseMaker.is_binary("audio/anyother") assert ResponseMaker.is_binary("video/mpeg") assert ResponseMaker.is_binary("video/ogg") assert ResponseMaker.is_binary("video/webm") assert ResponseMaker.is_binary("video/anyother") assert ResponseMaker.is_binary("video/anyother") assert not ResponseMaker.is_binary(faker.pystr()) response = EndpointResource.response("", code=200) assert response[1] == 200 # type: ignore response = EndpointResource.response(None, code=200) assert response[1] == 204 # type: ignore response = EndpointResource.response(None, code=200, head_method=True) assert response[1] == 200 # type: ignore
class InputSchema(Schema): # lowercase key without label defined. label will be key.title() in schema mystr = fields.Str(required=True, validate=validate.Length(min=4)) # non-lowercase key without label defined. label will be == to key in schema MYDATE = fields.Date(required=True) MYDATETIME = fields.AwareDateTime( required=True, format=ISO8601UTC, default_timezone=pytz.utc, validate=validate.Range( max=datetime.now(pytz.utc).replace(hour=23, minute=59, second=59), min=datetime(1900, 1, 1, tzinfo=pytz.utc), max_inclusive=True, error="Invalid date", ), ) myint_exclusive = fields.Int( required=True, # Explicit label definition... but missing description validate=validate.Range( min=1, max=10, min_inclusive=False, max_inclusive=False ), metadata={ "label": "Int exclusive field", }, ) myint_inclusive = fields.Int( required=True, # Both label and description explicit definition validate=validate.Range(min=1, max=10), metadata={ "label": "Int inclusive field", "description": "This field accepts values in a defined range", }, ) myselect = fields.Str( required=True, validate=validate.OneOf(choices=["a", "b"], labels=["A", "B"]), ) myselect2 = fields.Str( required=True, # Wrong definition, number labels < number of choices # Labels will be ignored and replaced by choices validate=validate.OneOf(choices=["a", "b"], labels=["A"]), ) mymaxstr = fields.Str(required=True, validate=validate.Length(max=7)) myequalstr = fields.Str(required=True, validate=validate.Length(equal=6)) # Note: requests (from pytest) has to json-dump the arrays and objects, # but the normal Marshmallow fields does not json-load the inputs # fields.Nested is a replacement of the default Nested field with the ability # to receive json dumped data from requests or pytest mynested = fields.Nested(Nested, required=True) mynullablenested = fields.Nested(Nested, required=True, allow_none=True) # fields.List is a replacement of the default List field with the ability # to receive json dumped data from requests or pytest # In json model the type of this field will be resolved as string[] mylist = fields.List(fields.Str(), required=True) # In json model the type of this field will be resolved as int[] mylist2 = fields.List(CustomInt, required=True) # In json model the type of this field will be resolved as mylist3[] # The type is key[] ... should be something more explicative like FieldName[] mylist3 = fields.List(CustomGenericField, required=True)