# Path: modernrpc/helpers.py
# def ensure_sequence(element: Any) -> Iterable:
#     """Ensure the given argument is a sequence object (tuple, list). If not, return a list containing its value."""
#     return element if isinstance(element, (list, tuple)) else [element]
# 
# def get_builtin_date(
#     date: Union[str, datetime.datetime, xmlrpc_client.DateTime],
#     date_format: str = "%Y-%m-%dT%H:%M:%S",
#     raise_exception: bool = False,
# ) -> Optional[datetime.datetime]:
#     """
#     Try to convert a date to a builtin instance of ``datetime.datetime``.
#     The input date can be a ``str``, a ``datetime.datetime``, a ``xmlrpc.client.Datetime`` or a ``xmlrpclib.Datetime``
#     instance. The returned object is a ``datetime.datetime``.
# 
#     :param date: The date object to convert.
#     :param date_format: If the given date is a str, format is passed to strptime to parse it
#     :param raise_exception: If set to True, an exception will be raised if the input string cannot be parsed
#     :return: A valid ``datetime.datetime`` instance
#     """
#     if isinstance(date, datetime.datetime):
#         # Default XML-RPC handler is configured to decode dateTime.iso8601 type
#         # to builtin datetime.datetim instance
#         return date
#     if isinstance(date, xmlrpc_client.DateTime):
#         # If constant settings.MODERNRPC_XMLRPC_USE_BUILTIN_TYPES has been set to True
#         # the date is decoded as DateTime object
#         return datetime.datetime.strptime(date.value, "%Y%m%dT%H:%M:%S")
# 
#     # If date is given as str. This is the normal behavior for JSON-RPC
#     try:
#         return datetime.datetime.strptime(date, date_format)
#     except ValueError:
#         if raise_exception:
#             raise
#         return None


import datetime
import xmlrpc.client
import pytest
from modernrpc.helpers import ensure_sequence, get_builtin_date




@pytest.mark.parametrize(
    "arg, expected_result",
    [
        ((1, 2, 3), (1, 2, 3)),
        (("a", "b", "c"), ("a", "b", "c")),
        ("azerty", ["azerty"]),
        ({"x": 42}, [{"x": 42}]),
    ],
)
def test_ensure_sequence_1(arg, expected_result):
    assert ensure_sequence(arg) == expected_result


@pytest.mark.parametrize(
    "date",
    [
        "2016-10-20T21:54:00",
        datetime.datetime(2016, 10, 20, 21, 54, 00),
        xmlrpc.client.DateTime(datetime.datetime(2016, 10, 20, 21, 54, 00)),
    ],
)
def test_get_builtin_date(date):
    assert get_builtin_date(date) == datetime.datetime(2016, 10, 20, 21, 54, 0)


def test_get_builtin_date_with_format():

# Path: modernrpc/helpers.py
# def ensure_sequence(element: Any) -> Iterable:
#     """Ensure the given argument is a sequence object (tuple, list). If not, return a list containing its value."""
#     return element if isinstance(element, (list, tuple)) else [element]
# 
# def get_builtin_date(
#     date: Union[str, datetime.datetime, xmlrpc_client.DateTime],
#     date_format: str = "%Y-%m-%dT%H:%M:%S",
#     raise_exception: bool = False,
# ) -> Optional[datetime.datetime]:
#     """
#     Try to convert a date to a builtin instance of ``datetime.datetime``.
#     The input date can be a ``str``, a ``datetime.datetime``, a ``xmlrpc.client.Datetime`` or a ``xmlrpclib.Datetime``
#     instance. The returned object is a ``datetime.datetime``.
# 
#     :param date: The date object to convert.
#     :param date_format: If the given date is a str, format is passed to strptime to parse it
#     :param raise_exception: If set to True, an exception will be raised if the input string cannot be parsed
#     :return: A valid ``datetime.datetime`` instance
#     """
#     if isinstance(date, datetime.datetime):
#         # Default XML-RPC handler is configured to decode dateTime.iso8601 type
#         # to builtin datetime.datetim instance
#         return date
#     if isinstance(date, xmlrpc_client.DateTime):
#         # If constant settings.MODERNRPC_XMLRPC_USE_BUILTIN_TYPES has been set to True
#         # the date is decoded as DateTime object
#         return datetime.datetime.strptime(date.value, "%Y%m%dT%H:%M:%S")
# 
#     # If date is given as str. This is the normal behavior for JSON-RPC
#     try:
#         return datetime.datetime.strptime(date, date_format)
#     except ValueError:
#         if raise_exception:
#             raise
#         return None


# Path: tests/unit/test_helpers.py
import datetime
import xmlrpc.client
import pytest
from modernrpc.helpers import ensure_sequence, get_builtin_date





@pytest.mark.parametrize(
    "arg, expected_result",
    [
        ((1, 2, 3), (1, 2, 3)),
        (("a", "b", "c"), ("a", "b", "c")),
        ("azerty", ["azerty"]),
        ({"x": 42}, [{"x": 42}]),
    ],
)
def test_ensure_sequence_1(arg, expected_result):
    assert ensure_sequence(arg) == expected_result


@pytest.mark.parametrize(
    "date",
    [
        "2016-10-20T21:54:00",
        datetime.datetime(2016, 10, 20, 21, 54, 00),
        xmlrpc.client.DateTime(datetime.datetime(2016, 10, 20, 21, 54, 00)),
    ],
)
def test_get_builtin_date(date):
    assert get_builtin_date(date) == datetime.datetime(2016, 10, 20, 21, 54, 0)


def test_get_builtin_date_with_format():

    res = get_builtin_date("20 10 2016 21h54m00", date_format="%d %m %Y %Hh%Mm%S")