示例#1
0
def test_getitem():
    """Test accessing an invalid index raises an error."""
    d = FhirDateTime(**random.choice(cases["success"]))
    min_ = 0
    max_ = 6
    for _ in range(100):
        with pytest.raises(IndexError):
            _ = d[random.randrange(min_ - 1, -2000, -1)]
        with pytest.raises(IndexError):
            _ = d[random.randrange(max_ + 1, 2000)]
示例#2
0
def make_and_assert(params: dict):
    """Create and run tests on a FhirDateTime object."""
    dt = FhirDateTime(**params)
    if isinstance(params["year"], (date, datetime)):
        return compare_native(dt, params["year"])

    if isinstance(params["year"], str):
        # There's not really a good way to test string parsing without writing a second
        # string parser, in which case you have two things to make sure are correct...
        return

    params_set = set(params.keys())
    none_params = {"year", "month", "day", "hour", "minute"} - params_set
    for p in none_params:
        assert getattr(dt, p) is None

    zero_params = {"second", "microsecond"} - params_set
    for p in zero_params:
        assert getattr(dt, p) == 0

    for p in params_set - none_params - zero_params:
        assert getattr(dt, p) == params.get(p)
示例#3
0
def test_other_methods():
    """Test other methods, mostly for coverage."""
    dt = FhirDateTime(2020, 5, 4, 13, 42, 54, 295815, tzinfo=timezone.utc)
    assert dt.date() == date(2020, 5, 4)
    assert dt.time() == time(13, 42, 54, 295815)
    assert (dt - timedelta(5)) == FhirDateTime(
        2020, 4, 29, 13, 42, 54, 295815, tzinfo=timezone.utc
    )

    dt = dt.replace(tzinfo=timezone(timedelta(hours=3)))
    assert dt.timetz() == time(13, 42, 54, 295815, tzinfo=timezone(timedelta(hours=3)))
    assert dt.timetuple() == _time.struct_time((2020, 5, 4, 13, 42, 54, 0, 125, -1))

    assert dt.isoformat() == "2020-05-04T13:42:54.295815+03:00"
    assert dt.isoformat(timespec="milliseconds") == "2020-05-04T13:42:54.295+03:00"
    with pytest.raises(ValueError):
        dt.isoformat(timespec="doesn't exist")
    with pytest.raises(ValueError):
        FhirDateTime.fromisoformat("2020*02*13")

    assert dt.weekday() == 0
    assert dt.isoweekday() == 1
    assert dt.isocalendar() == (2020, 19, 1)

    assert dt.asdatetime == datetime(
        2020, 5, 4, 13, 42, 54, 295815, timezone(timedelta(hours=3))
    )
    assert dt.timestamp() == 1588588974.295815

    assert str(FhirDateTime("2020")) == "2020"
    assert str(FhirDateTime("2020-05")) == "2020-05"
    assert str(FhirDateTime("2020-05-04")) == "2020-05-04"
示例#4
0
def test_from_native_xfail(param):
    """Test creation of a FhirDateTime from a native object, should fail."""
    FhirDateTime.from_native(param)
示例#5
0
def test_sorting_embedded(pre_sort, post_sort, obj_path):
    """Test sorting of a list of objects that contain FhirDateTime objects."""
    assert sorted(pre_sort, key=FhirDateTime.sort_key(obj_path)) == post_sort
示例#6
0
def test_sorting_top_level(pre_sort, post_sort):
    """Test sorting of a list of just FhirDateTime objects."""
    assert sorted(pre_sort, key=FhirDateTime.sort_key()) == post_sort
示例#7
0
from collections import namedtuple

import pytest

from fhirdatetime import FhirDateTime

CarePlan = namedtuple("CarePlan", ["period"])
Period = namedtuple("Period", ["start", "end"])


@pytest.mark.parametrize(
    "pre_sort, post_sort",
    [
        (
            [FhirDateTime(2021, 4), FhirDateTime(2021), FhirDateTime(2021, 4, 12)],
            [FhirDateTime(2021), FhirDateTime(2021, 4), FhirDateTime(2021, 4, 12)],
        ),
    ],
)
def test_sorting_top_level(pre_sort, post_sort):
    """Test sorting of a list of just FhirDateTime objects."""
    assert sorted(pre_sort, key=FhirDateTime.sort_key()) == post_sort


@pytest.mark.parametrize(
    "pre_sort, post_sort, obj_path",
    [
        (
            [  # Pre-sort
                CarePlan(Period(start=FhirDateTime(2021, 4), end=None)),
示例#8
0
from fhirdatetime import FhirDateTime


def idfn(val):
    """Create a string ID for the test values."""
    return f" {val} "


# Handy timezones
utc = timezone.utc
ten_behind = timezone(timedelta(hours=-10))

eq = [
    (
        FhirDateTime(year=2020),
        FhirDateTime(year=2020),
    ),
    (
        FhirDateTime(year=2020, month=1),
        FhirDateTime(year=2020, month=1),
    ),
    (
        FhirDateTime(year=2020, month=1, day=6),
        FhirDateTime(year=2020, month=1, day=6),
    ),
    (
        FhirDateTime(year=2020, month=1, day=6, hour=12, minute=30),
        FhirDateTime(year=2020, month=1, day=6, hour=12, minute=30),
    ),
    (