本文整理汇总了Python中marshmallow.fields.Integer方法的典型用法代码示例。如果您正苦于以下问题:Python fields.Integer方法的具体用法?Python fields.Integer怎么用?Python fields.Integer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类marshmallow.fields
的用法示例。
在下文中一共展示了fields.Integer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _validate_marshmallow_type
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def _validate_marshmallow_type(self, field_cfg):
"""Make sure the Marshmallow type is one we support."""
def validate_basic_marshmallow_type(_type):
allowed_types = [
Bool, DateString, Integer, SanitizedUnicode
]
assert any([
isinstance(_type, allowed_type) for allowed_type
in allowed_types
])
marshmallow_type = field_cfg["marshmallow"]
if isinstance(marshmallow_type, List):
validate_basic_marshmallow_type(marshmallow_type.inner)
else:
validate_basic_marshmallow_type(marshmallow_type)
示例2: test_metadata
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_metadata():
"""Metadata should be available in the field definition."""
class TestSchema(Schema):
myfield = fields.String(metadata={"foo": "Bar"})
yourfield = fields.Integer(required=True, baz="waz")
schema = TestSchema()
dumped = validate_and_dump(schema)
props = dumped["definitions"]["TestSchema"]["properties"]
assert props["myfield"]["foo"] == "Bar"
assert props["yourfield"]["baz"] == "waz"
assert "metadata" not in props["myfield"]
assert "metadata" not in props["yourfield"]
# repeat process to assert idempotency
dumped = validate_and_dump(schema)
props = dumped["definitions"]["TestSchema"]["properties"]
assert props["myfield"]["foo"] == "Bar"
assert props["yourfield"]["baz"] == "waz"
示例3: test_nested_descriptions
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_nested_descriptions():
class TestNestedSchema(Schema):
myfield = fields.String(metadata={"description": "Brown Cow"})
yourfield = fields.Integer(required=True)
class TestSchema(Schema):
nested = fields.Nested(
TestNestedSchema, metadata={"description": "Nested 1", "title": "Title1"}
)
yourfield_nested = fields.Integer(required=True)
schema = TestSchema()
dumped = validate_and_dump(schema)
nested_def = dumped["definitions"]["TestNestedSchema"]
nested_dmp = dumped["definitions"]["TestSchema"]["properties"]["nested"]
assert nested_def["properties"]["myfield"]["description"] == "Brown Cow"
assert nested_dmp["$ref"] == "#/definitions/TestNestedSchema"
assert nested_dmp["description"] == "Nested 1"
assert nested_dmp["title"] == "Title1"
示例4: test_nested_string_to_cls
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_nested_string_to_cls():
class TestNamedNestedSchema(Schema):
foo = fields.Integer(required=True)
class TestSchema(Schema):
foo2 = fields.Integer(required=True)
nested = fields.Nested("TestNamedNestedSchema")
schema = TestSchema()
dumped = validate_and_dump(schema)
nested_def = dumped["definitions"]["TestNamedNestedSchema"]
nested_dmp = dumped["definitions"]["TestSchema"]["properties"]["nested"]
assert nested_dmp["type"] == "object"
assert nested_def["properties"]["foo"]["format"] == "integer"
示例5: test_list_nested
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_list_nested():
"""Test that a list field will work with an inner nested field."""
class InnerSchema(Schema):
foo = fields.Integer(required=True)
class ListSchema(Schema):
bar = fields.List(fields.Nested(InnerSchema), required=True)
schema = ListSchema()
dumped = validate_and_dump(schema)
nested_json = dumped["definitions"]["ListSchema"]["properties"]["bar"]
assert nested_json["type"] == "array"
assert "items" in nested_json
item_schema = nested_json["items"]
assert "InnerSchema" in item_schema["$ref"]
示例6: test_respect_only_for_nested_schema
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_respect_only_for_nested_schema():
"""Should ignore fields not in 'only' metadata for nested schemas."""
class InnerRecursiveSchema(Schema):
id = fields.Integer(required=True)
baz = fields.String()
recursive = fields.Nested("InnerRecursiveSchema")
class MiddleSchema(Schema):
id = fields.Integer(required=True)
bar = fields.String()
inner = fields.Nested("InnerRecursiveSchema", only=("id", "baz"))
class OuterSchema(Schema):
foo2 = fields.Integer(required=True)
nested = fields.Nested("MiddleSchema")
schema = OuterSchema()
dumped = validate_and_dump(schema)
inner_props = dumped["definitions"]["InnerRecursiveSchema"]["properties"]
assert "recursive" not in inner_props
示例7: test_respect_exclude_for_nested_schema
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_respect_exclude_for_nested_schema():
"""Should ignore fields in 'exclude' metadata for nested schemas."""
class InnerRecursiveSchema(Schema):
id = fields.Integer(required=True)
baz = fields.String()
recursive = fields.Nested("InnerRecursiveSchema")
class MiddleSchema(Schema):
id = fields.Integer(required=True)
bar = fields.String()
inner = fields.Nested("InnerRecursiveSchema", exclude=("recursive",))
class OuterSchema(Schema):
foo2 = fields.Integer(required=True)
nested = fields.Nested("MiddleSchema")
schema = OuterSchema()
dumped = validate_and_dump(schema)
inner_props = dumped["definitions"]["InnerRecursiveSchema"]["properties"]
assert "recursive" not in inner_props
示例8: test_respect_dotted_exclude_for_nested_schema
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_respect_dotted_exclude_for_nested_schema():
"""Should ignore dotted fields in 'exclude' metadata for nested schemas."""
class InnerRecursiveSchema(Schema):
id = fields.Integer(required=True)
baz = fields.String()
recursive = fields.Nested("InnerRecursiveSchema")
class MiddleSchema(Schema):
id = fields.Integer(required=True)
bar = fields.String()
inner = fields.Nested("InnerRecursiveSchema")
class OuterSchema(Schema):
foo2 = fields.Integer(required=True)
nested = fields.Nested("MiddleSchema", exclude=("inner.recursive",))
schema = OuterSchema()
dumped = validate_and_dump(schema)
inner_props = dumped["definitions"]["InnerRecursiveSchema"]["properties"]
assert "recursive" not in inner_props
示例9: test_nested_instance
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_nested_instance():
"""Should also work with nested schema instances"""
class TestNestedSchema(Schema):
baz = fields.Integer()
class TestSchema(Schema):
foo = fields.String()
bar = fields.Nested(TestNestedSchema())
schema = TestSchema()
dumped = validate_and_dump(schema)
nested_def = dumped["definitions"]["TestNestedSchema"]
nested_obj = dumped["definitions"]["TestSchema"]["properties"]["bar"]
assert "baz" in nested_def["properties"]
assert nested_obj["$ref"] == "#/definitions/TestNestedSchema"
示例10: test_metadata_direct_from_field
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_metadata_direct_from_field():
"""Should be able to get metadata without accessing metadata kwarg."""
class TestSchema(Schema):
id = fields.Integer(required=True)
metadata_field = fields.String(description="Directly on the field!")
schema = TestSchema()
dumped = validate_and_dump(schema)
assert dumped["definitions"]["TestSchema"]["properties"]["metadata_field"] == {
"title": "metadata_field",
"type": "string",
"description": "Directly on the field!",
}
示例11: test_dumps_iterable_enums
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_dumps_iterable_enums():
mapping = {"a": 0, "b": 1, "c": 2}
class TestSchema(Schema):
foo = fields.Integer(
validate=validate.OneOf(mapping.values(), labels=mapping.keys())
)
schema = TestSchema()
dumped = validate_and_dump(schema)
assert dumped["definitions"]["TestSchema"]["properties"]["foo"] == {
"enum": [v for v in mapping.values()],
"enumNames": [k for k in mapping.keys()],
"format": "integer",
"title": "foo",
"type": "number",
}
示例12: test_additional_properties_from_nested_meta
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_additional_properties_from_nested_meta(additional_properties_value):
class TestNestedSchema(Schema):
class Meta:
additional_properties = additional_properties_value
foo = fields.Integer()
class TestSchema(Schema):
nested = fields.Nested(TestNestedSchema())
schema = TestSchema()
dumped = validate_and_dump(schema)
assert (
dumped["definitions"]["TestNestedSchema"]["additionalProperties"]
== additional_properties_value
)
示例13: test_range_marshmallow_3
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_range_marshmallow_3():
class TestSchema(Schema):
foo = fields.Integer(
validate=Range(min=1, min_inclusive=False, max=3, max_inclusive=False)
)
bar = fields.Integer(validate=Range(min=2, max=4))
schema = TestSchema()
dumped = validate_and_dump(schema)
props = dumped["definitions"]["TestSchema"]["properties"]
assert props["foo"]["exclusiveMinimum"] == 1
assert props["foo"]["exclusiveMaximum"] == 3
assert props["bar"]["minimum"] == 2
assert props["bar"]["maximum"] == 4
示例14: test_arguments_are_added_to_request_with_Resource_and_schema
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_arguments_are_added_to_request_with_Resource_and_schema(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@accepts(
"Foo",
dict(name="foo", type=int, help="An important foo"),
schema=TestSchema,
api=api,
)
def post(self):
assert request.parsed_obj
assert request.parsed_obj["_id"] == 42
assert request.parsed_obj["name"] == "test name"
return "success"
with client as cl:
resp = cl.post("/test?foo=3", json={"_id": 42, "name": "test name"})
assert resp.status_code == 200
示例15: test_accepts_with_postional_args_query_params_schema_and_header_schema
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import Integer [as 别名]
def test_accepts_with_postional_args_query_params_schema_and_header_schema(app, client): # noqa
class QueryParamsSchema(Schema):
query_param = fields.List(fields.String(), required=True)
class HeadersSchema(Schema):
Header = fields.Integer(required=True)
@app.route("/test")
@accepts(
dict(name="foo", type=int, help="An important foo"),
query_params_schema=QueryParamsSchema,
headers_schema=HeadersSchema)
def test():
assert request.parsed_args["foo"] == 3
assert request.parsed_query_params["query_param"] == ["baz", "qux"]
assert request.parsed_headers["Header"] == 3
return "success"
with client as cl:
resp = cl.get("/test?foo=3&query_param=baz&query_param=qux", headers={"Header": "3"})
assert resp.status_code == 200