class ASerializer(DictSerializer): users = fields.ConstantField({ "foo": user_1, "bar": user_2 }, label="usersList", required=False)
def test_constant_field(): fn = fields.ConstantField(1.3).as_getter("whatever", Mock()) assert fn(Mock()) == 1.3 fn = fields.ConstantField().as_getter("whatever", Mock()) assert fn(Mock()) is None fn = fields.ConstantField(constant=[1.3, True]).as_getter( "whatever", Mock()) assert fn(Mock()) == [1.3, True] fn = fields.ConstantField(Mock(), required=False).as_getter("whatever", Mock()) with pytest.raises(TypeError): fn(Mock()) with pytest.raises(SerializationError): fields.ConstantField(Mock(), required=True).as_getter("whatever", Mock())
class ASerializer(Serializer): users = fields.ConstantField( { "foo": { "id": 12, "totalReactions": 19 }, "bar": { "id": 33, "totalReactions": 122 }, }, label="usersList", )
class ASerializer(Serializer): a = fields.ListField(fields.ConstantField(constant=3))
class ASerializer(DictSerializer): users = fields.ConstantField({"foo": user}, label="usersList")
class ASerializer(Serializer): http = fields.ConstantField(["GET", "POST", "PUT"])
class PISerializer(Serializer): pi = fields.ConstantField(constant=3.14)
class ASerializer(Serializer): a = fields.ConstantField(constant=None)