Example #1
0
def test__get__():
    class HasField:
        foo = Field()

    has_field = HasField()
    with assert_raises(AttributeError, re.compile(r"HasField\.foo is unset")):
        getattr(has_field, 'foo')

    class HasFieldWithDefault:
        foo = Field(default='bar')

    has_field_w_default = HasFieldWithDefault()
    assert has_field_w_default.foo == 'bar'

    class HasFieldWithDefaultFactory:
        foo = Field(default_factory=list)

    has_field_w_default_factory = HasFieldWithDefaultFactory()
    assert has_field_w_default_factory.foo == list()

    class HasFieldWithDefaultValueAndFactory:
        foo = Field(default='hi', default_factory=list)

    has_field_w_default_val_and_factory = HasFieldWithDefaultValueAndFactory()
    assert has_field_w_default_val_and_factory.foo == ['h', 'i']
Example #2
0
def test__delete__():
    class HasField:
        foo = Field()

        def __init__(self, foo) -> None:
            self.foo = foo

    has_field = HasField("bar")
    assert has_field.foo == "bar"

    del has_field.foo
    with assert_raises(AttributeError, re.compile(r"HasField\.foo is unset")):
        getattr(has_field, 'foo')

    foo_field = inspect.getattr_static(has_field, 'foo')
    assert foo_field.cached_value is UNSET
Example #3
0
        def test_stop_when_not_ongoing(self):
            log.title(f"test_models.test_stop_when_not_ongoing()")
            work = default_work(TEST_START_ARROW)
            day: Day = work.__getitem__(TEST_START_ARROW.DDMMYY)
            got_to_office_activity: Activity = day.__getitem__("Got to office")
            assert got_to_office_activity.ongoing() is True

            now = XArrow.now()
            entry: Entry = got_to_office_activity.stop(now)
            assert entry is got_to_office_activity.safe_last_entry()
            assert entry.end == now

            assert got_to_office_activity.ongoing() is False

            with assert_raises(ValueError,
                               f'{got_to_office_activity} is not ongoing'):
                got_to_office_activity.stop()
Example #4
0
def test__get__getitem_in_instance():
    class HasDiktField(DictSubclass):
        foo = DiktField()

    has_diktfield = HasDiktField()
    has_diktfield['foo'] = 'bar'
    assert has_diktfield.foo == 'bar'
    assert has_diktfield['foo'] == 'bar'

    class DoesntSupportGetitem:
        foo = DiktField()

    doesnt_support_getitem = DoesntSupportGetitem()
    doesnt_support_getitem.foo = 'bar'
    with assert_raises(
            TypeError,
            "'DoesntSupportGetitem' object does not support item assignment"):
        # The line that fails is `instance[field.name] = rv` in cache()
        doesnt_support_getitem.foo == 'bar'
Example #5
0
        def test_stop_before_last_entry_started(self):
            log.title(f"test_models.test_stop_before_last_entry_started()")
            work = default_work(TEST_START_ARROW)

            day: Day = work.__getitem__(TEST_START_ARROW.DDMMYY)
            assert work[TEST_START_ARROW.DDMMYY] is day

            got_to_office_activity: Activity = day.__getitem__("Got to office")
            assert got_to_office_activity.ongoing() is True
            assert day['Got to office'] is got_to_office_activity

            last_entry: Entry = got_to_office_activity[-1]
            assert got_to_office_activity.safe_last_entry() is last_entry

            yesterday = XArrow.dehumanize('yesterday')
            with assert_raises(
                    ValueError,
                    f'Cannot stop {got_to_office_activity.shortrepr()} before start time (tried to stop at {yesterday!r})'
            ):
                got_to_office_activity.stop(yesterday)
Example #6
0
def test__init__annotation_mismatch_but_not_buildable():
    foo = Foo(bar={'baz': 'NOT A NUMBER'})
    bar = foo.bar
    assert isinstance(bar, Foo.Bar)
    with assert_raises(ValueError):
        baz = bar.baz
Example #7
0
def test__setattr_setitem_symmetry__annotation_mismatch_but_not_buildable():
    foo = Foo()
    foo.bar = Foo.Bar()
    foo.bar.baz = ['hello', 'world']
    with assert_raises(TypeError, re.compile("argument must be.*not 'list'")):
        foo.bar.baz
Example #8
0
    def test_empty_sheet(self):
        work = Work()

        with assert_raises(ValueError):
            work.ongoing_activity()
Example #9
0
            def test_sanity(self, work=None):
                log.title(f"test_sanity({work = })")
                if not work:
                    work = default_work(TEST_START_ARROW)

                log.debug('work (Work)')
                assert isinstance(work, Work)
                assert work
                assert len(work) == 1
                assert TEST_START_ARROW.DDMMYY in work
                log.debug('Work["30/12/99"] -> Day')
                day: Day = work[TEST_START_ARROW.DDMMYY]
                assert isinstance(day, Day)
                assert day
                assert len(day) == 1
                assert "Got to office" in day

                log.debug('Day["Got to office"] -> Activity (ongoing)')
                got_to_office_activity: Activity = day["Got to office"]
                assert isinstance(got_to_office_activity, Activity)
                assert got_to_office_activity
                assert len(got_to_office_activity) == 1
                assert isinstance(got_to_office_activity.name, Colored)
                got_to_office_activity_is_ongoing = got_to_office_activity.ongoing(
                )
                assert got_to_office_activity_is_ongoing is True
                assert got_to_office_activity.name == "Got to office"

                log.debug(
                    'Day["On Device Validation"] -> Activity (not ongoing)')
                device_validation_activity: Activity = day[
                    "On Device Validation"]

                assert got_to_office_activity.name == "Got to office"
                assert isinstance(device_validation_activity, Activity)
                assert device_validation_activity.name == "On Device Validation"
                assert not device_validation_activity
                assert len(device_validation_activity) == 0
                assert isinstance(device_validation_activity.name, Colored)
                device_validation_activity_is_ongoing = device_validation_activity.ongoing(
                )
                assert device_validation_activity_is_ongoing is False

                log.debug('Work.ongoing_activity() -> Activity')
                ongoing_activity: Activity = work.ongoing_activity()
                assert ongoing_activity
                assert isinstance(ongoing_activity, Activity)
                assert ongoing_activity.name == "Got to office"
                assert isinstance(ongoing_activity.name, Colored)
                assert len(ongoing_activity) == 1
                assert ongoing_activity.ongoing() is True
                assert ongoing_activity != device_validation_activity
                assert ongoing_activity == got_to_office_activity
                assert ongoing_activity is ongoing_activity
                assert got_to_office_activity is got_to_office_activity
                assert ongoing_activity is got_to_office_activity

                assert device_validation_activity.name == "On Device Validation"
                assert ongoing_activity.name == "Got to office"

                ongoing_activity_copy = work.ongoing_activity()
                assert ongoing_activity_copy is ongoing_activity

                log.debug('Activity.stop() -> Entry')
                got_to_office_last_entry: Entry = ongoing_activity.stop()
                assert isinstance(got_to_office_last_entry, Entry)
                assert got_to_office_last_entry
                assert got_to_office_last_entry.end

                assert ongoing_activity.ongoing() is False

                log.debug('Activity.stop() -> ValueError (not ongoing)')
                with assert_raises(
                        ValueError,
                        match=f"{ongoing_activity!r} is not ongoing"):
                    ongoing_activity.stop()

                log.debug(
                    'Work.ongoing_activity() -> ValueError (no ongoing activity)'
                )
                with assert_raises(ValueError, match="No ongoing activity"):
                    work.ongoing_activity()

                log.debug('Work.on("Something New") -> Activity')
                something_new_activity: Activity = work.on("Something New")
                assert isinstance(something_new_activity, Activity)
                assert something_new_activity
                assert len(something_new_activity) == 1
                assert something_new_activity.name == "Something New"
                something_new_activity_is_ongoing = something_new_activity.ongoing(
                )
                assert something_new_activity_is_ongoing is True
                assert device_validation_activity.name == "On Device Validation"
                assert ongoing_activity.name == "Got to office"
                assert device_validation_activity.ongoing() is False
                assert ongoing_activity.ongoing() is False

                log.debug('Activity.start() -> ValueError (already ongoing)')
                with assert_raises(
                        ValueError,
                        match=f"{something_new_activity!r} is already ongoing"
                ):
                    something_new_activity.start()

                log.debug(
                    'Work.on("Something New") -> ValueError (already ongoing)')
                with assert_raises(
                        ValueError,
                        match=f"{something_new_activity!r} is already ongoing"
                ):
                    work.on(something_new_activity.name)

                log.debug('Work.on("Something New2") -> Activity')
                something_new2_activity: Activity = work.on("Something New2")
                assert isinstance(something_new2_activity, Activity)
                assert something_new2_activity
                assert len(something_new2_activity) == 1
                assert something_new2_activity.name == "Something New2"
                assert something_new2_activity.ongoing() is True
                assert device_validation_activity.name == "On Device Validation"
                assert ongoing_activity.name == "Got to office"
                assert device_validation_activity.ongoing() is False
                assert ongoing_activity.ongoing() is False
                assert something_new_activity.ongoing() is False

                log.debug(
                    'Work.on("something-new 2") -> ValueError (has similar name)'
                )
                assert something_new2_activity.has_similar_name(
                    "something-new 2") is True
                with assert_raises(
                        ValueError,
                        match=
                        f"{something_new2_activity!r} is ongoing, and has a similar name to 'something-new 2"
                ):
                    work.on("something-new 2")

                log.debug('Work.stop() -> Optional[Activity]')
                stop_time = XArrow.now()
                stopped_activity: Activity = work.stop(stop_time)
                assert stopped_activity.name == "Something New2"
                assert stopped_activity[-1].end == stop_time
                assert stopped_activity.ongoing() is False
                with assert_raises(ValueError, match=f"No ongoing activity"):
                    work.ongoing_activity()
                with assert_raises(ValueError, match=f"No ongoing activity"):
                    work.stop()