示例#1
0
def get_default_serializer(class_attribute=None, format=None, value=None):
    """Retrieve the default ``on_serialize`` function that applies to the data
    type of ``class_attribute``.

    .. note::

      If ``class_attribute`` does not have a SQLAlchemy data type, then
      determines the data type based on ``value``.

    :param class_attribute: The class attribute whose default serializer will be
      returned. Defaults to :obj:`None <python:None>`.

    :param format: The format to which the value should be serialized. Accepts
      either: ``csv``, ``json``, ``yaml``, or ``dict``. Defaults to :obj:`None <python:None>`.
    :type format: :class:`str <python:str>`

    :param value: The class attribute's value.

    :returns: The default :term:`serializer function` to apply or :obj:`None <python:None>`
    :rtype: callable / :obj:`None <python:None>`

    :raises InvalidFormatError: if ``format`` is not a valid format type
    """
    format_to_tuple(format)
    format = format.lower()

    class_type_key = get_class_type_key(class_attribute, value)

    serializer_dict = DEFAULT_SERIALIZERS.get(class_type_key, None)

    if serializer_dict is None:
        return None

    return serializer_dict.get(format, None)
示例#2
0
def test_get_class_type_key(request, model_complex_postgresql, attribute,
                            value, expected_result):
    target = model_complex_postgresql[0]
    if attribute is not None:
        attribute = getattr(target, attribute)

    result = get_class_type_key(attribute, value=value)

    assert result == expected_result