Exemple #1
0
    def test_040_registered_attribute_simple_class(self, class_Time_resource):
        Time.register_new_time("test_1", ExampleTestTimeRepresentation)

        # Time attribute type and metaclass are correct
        assert Time.test_1.__name__ == "ExampleTestTimeRepresentationInTime"
        assert type(Time.test_1).__name__ == "ModifiedClass"
        assert issubclass(Time.test_1, ExampleTestTimeRepresentation)

        # constructed Time type and value are is correct
        t1a = Time.test_1(10, 8)
        assert type(t1a) == Time
        assert t1a.day_frac == Fraction(63, 625)

        # new attribute on Time instance, type and value are correct
        t1b = Time("963/1250")
        assert isinstance(t1b.test_1, ExampleTestTimeRepresentation)
        assert type(
            t1b.test_1).__name__ == "ExampleTestTimeRepresentationInTime"
        assert type(t1b.test_1.__class__).__name__ == "ModifiedClass"
        assert t1b.test_1.hour100 == 77
        assert t1b.test_1.minute100 == 4

        # new attribute on Time instance build by another calendar, type and value are correct
        t1c = Time.western(10, 35, 15)
        assert isinstance(t1c.test_1, ExampleTestTimeRepresentation)
        assert type(
            t1c.test_1).__name__ == "ExampleTestTimeRepresentationInTime"
        assert type(t1c.test_1.__class__).__name__ == "ModifiedClass"
        assert t1c.test_1.hour100 == 44
        assert t1c.test_1.minute100 == Fraction("275/24")
Exemple #2
0
    def test_110_naivety_is_preserved(self):
        class NaivetyCheck:
            def __init__(self, hour100, minute100, to_utc=None):
                self.hour100 = hour100
                self.minute100 = minute100
                self.to_utc = to_utc

            def to_time_pair(self):
                return Fraction(self.hour100 * 100 + self.minute100,
                                10000), None

            @classmethod
            def from_time_pair(cls, day_frac, to_utc=None):
                minutes_tot = day_frac * 10000
                hour100 = int(minutes_tot / 100)
                return cls(hour100, minutes_tot - hour100 * 100)

        Time.register_new_time("test_1", NaivetyCheck)

        t1 = Time("17/24")
        assert t1.test_1.to_utc is None

        t2 = Time("17/24", to_utc=0)
        assert t2.test_1.to_utc is not None

        t3 = Time.test_1(11, 12)
        assert t3.to_utc is None

        t4 = Time.test_1(11, 12, to_utc=0)
        assert t4.to_utc is not None
Exemple #3
0
 def test_000_register_new_time_repr(self, class_Time_resource):
     assert not hasattr(Time, "test_1")
     with pytest.raises(AttributeError):
         Time.test_1
     Time.register_new_time("test_1", ExampleTestTimeRepresentation)
     assert hasattr(Time, "test_1")
     Time.test_1
Exemple #4
0
    def test_030_register_new_time_repr_invalid_time_repr_class(self):
        class NoFromTimeRepr:  # without from_rata_die  TODO: shouldn't this comment be from_time_pair?
            def __init__(self, hour100, minute100):
                self.hour100 = hour100
                self.minute100 = minute100

            def to_time_pair(self):
                return Fraction(self.hour100 * 100 + self.minute100,
                                10000), None

        with pytest.raises(TypeError):
            Time.register_new_time("test_1", NoFromTimeRepr)

        class NoToTimeRepr:  # without to_rata_die
            def __init__(self, hour100, minute100):
                self.hour100 = hour100
                self.minute100 = minute100

            @classmethod
            def from_time_pair(cls, day_frac):
                minutes_tot = day_frac * 10000
                hour100 = int(minutes_tot / 100)
                return cls(hour100, minutes_tot - hour100 * 100)

        with pytest.raises(TypeError):
            Time.register_new_time("test_1", NoToTimeRepr)
Exemple #5
0
    def test_040_registered_attribute_simple_class(self, class_Time_resource):
        Time.register_new_time('test_1', ExampleTestTimeRepresentation)

        # Time attribute type and metaclass are correct
        assert Time.test_1.__name__ == 'ExampleTestTimeRepresentationInTime'
        assert type(Time.test_1).__name__ == 'ModifiedClass'
        assert issubclass(Time.test_1, ExampleTestTimeRepresentation)

        # constructed Time type and value are is correct
        t1a = Time.test_1(10, 8)
        assert type(t1a) == Time
        assert t1a.day_frac == Fraction(63, 625)

        # new attribute on Time instance, type and value are correct
        t1b = Time("963/1250")
        assert isinstance(t1b.test_1, ExampleTestTimeRepresentation)
        assert type(t1b.test_1).__name__ == 'ExampleTestTimeRepresentationInTime'
        assert type(t1b.test_1.__class__).__name__ == 'ModifiedClass'
        assert t1b.test_1.hour100 == 77
        assert t1b.test_1.minute100 == 4

        # new attribute on Time instance build by another calendar, type and value are correct
        t1c = Time.western(10, 35, 15)
        assert isinstance(t1c.test_1, ExampleTestTimeRepresentation)
        assert type(t1c.test_1).__name__ == 'ExampleTestTimeRepresentationInTime'
        assert type(t1c.test_1.__class__).__name__ == 'ModifiedClass'
        assert t1c.test_1.hour100 == 44
        assert t1c.test_1.minute100 == Fraction("275/24")
Exemple #6
0
    def test_030_register_new_time_repr_invalid_time_repr_class(self):
        class NoFromTimeRepr:  # without from_rata_die
            def __init__(self, hour100, minute100):
                self.hour100 = hour100
                self.minute100 = minute100

            def to_day_frac(self):
                return Fraction(self.hour100 * 100 + self.minute100, 10000)

        with pytest.raises(TypeError):
            Time.register_new_time('test_1', NoFromTimeRepr)

        class NoToTimeRepr:  # without to_rata_die
            def __init__(self, hour100, minute100):
                self.hour100 = hour100
                self.minute100 = minute100

            @classmethod
            def from_day_frac(cls, day_frac):
                minutes_tot = day_frac * 10000
                hour100 = int(minutes_tot / 100)
                return cls(hour100, minutes_tot - hour100 * 100)

        with pytest.raises(TypeError):
            Time.register_new_time('test_1', NoToTimeRepr)
Exemple #7
0
 def test_000_register_new_time_repr(self, class_Time_resource):
     assert not hasattr(Time, 'test_1')
     with pytest.raises(AttributeError):
         Time.test_1
     Time.register_new_time('test_1', ExampleTestTimeRepresentation)
     assert hasattr(Time, 'test_1')
     Time.test_1
Exemple #8
0
    def test_046_registered_attribute_class_with_static_methods(
            self, class_Time_resource):
        class ExampleTestTimeRepresentation3(ExampleTestTimeRepresentation):
            @staticmethod
            def is_odd(number):
                return (number % 2) == 1

        Time.register_new_time("test_3", ExampleTestTimeRepresentation3)

        # Time attribute type and metaclass are correct
        assert Time.test_3.__name__ == "ExampleTestTimeRepresentation3InTime"
        assert type(Time.test_3).__name__ == "ModifiedClass"
        assert issubclass(Time.test_3, ExampleTestTimeRepresentation)

        # constructed Time type and value are is correct
        t3a = Time.test_3(10, 8)
        assert type(t3a) == Time
        assert t3a.day_frac == Fraction(63, 625)

        # new attribute on Time instance, type and value are correct
        t3b = Time("963/1250")
        assert isinstance(t3b.test_3, ExampleTestTimeRepresentation3)
        assert type(
            t3b.test_3).__name__ == "ExampleTestTimeRepresentation3InTime"
        assert type(t3b.test_3.__class__).__name__ == "ModifiedClass"
        assert t3b.test_3.hour100 == 77
        assert t3b.test_3.minute100 == 4

        # new attribute on Time instance build by another calendar, type and value are correct
        t3c = Time.western(10, 35, 15)
        assert isinstance(t3c.test_3, ExampleTestTimeRepresentation3)
        assert type(
            t3c.test_3).__name__ == "ExampleTestTimeRepresentation3InTime"
        assert type(t3c.test_3.__class__).__name__ == "ModifiedClass"
        assert t3c.test_3.hour100 == 44
        assert t3c.test_3.minute100 == Fraction("275/24")

        # static method can be reached on the class and on all types of instance
        assert Time.test_3.is_odd(3)
        assert not Time.test_3.is_odd(4)
        assert t3a.test_3.is_odd(3)
        assert not t3a.test_3.is_odd(4)
        assert t3b.test_3.is_odd(3)
        assert not t3b.test_3.is_odd(4)
        assert t3c.test_3.is_odd(3)
        assert not t3c.test_3.is_odd(4)
Exemple #9
0
    def test_046_registered_attribute_class_with_static_methods(self, class_Time_resource):
        class ExampleTestTimeRepresentation3(ExampleTestTimeRepresentation):
            @staticmethod
            def is_odd(number):
                return (number % 2) == 1

        Time.register_new_time('test_3', ExampleTestTimeRepresentation3)

        # Time attribute type and metaclass are correct
        assert Time.test_3.__name__ == 'ExampleTestTimeRepresentation3InTime'
        assert type(Time.test_3).__name__ == 'ModifiedClass'
        assert issubclass(Time.test_3, ExampleTestTimeRepresentation)

        # constructed Time type and value are is correct
        t3a = Time.test_3(10, 8)
        assert type(t3a) == Time
        assert t3a.day_frac == Fraction(63, 625)

        # new attribute on Time instance, type and value are correct
        t3b = Time("963/1250")
        assert isinstance(t3b.test_3, ExampleTestTimeRepresentation3)
        assert type(t3b.test_3).__name__ == 'ExampleTestTimeRepresentation3InTime'
        assert type(t3b.test_3.__class__).__name__ == 'ModifiedClass'
        assert t3b.test_3.hour100 == 77
        assert t3b.test_3.minute100 == 4

        # new attribute on Time instance build by another calendar, type and value are correct
        t3c = Time.western(10, 35, 15)
        assert isinstance(t3c.test_3, ExampleTestTimeRepresentation3)
        assert type(t3c.test_3).__name__ == 'ExampleTestTimeRepresentation3InTime'
        assert type(t3c.test_3.__class__).__name__ == 'ModifiedClass'
        assert t3c.test_3.hour100 == 44
        assert t3c.test_3.minute100 == Fraction("275/24")

        # static method can be reached on the class and on all types of instance
        assert Time.test_3.is_odd(3)
        assert not Time.test_3.is_odd(4)
        assert t3a.test_3.is_odd(3)
        assert not t3a.test_3.is_odd(4)
        assert t3b.test_3.is_odd(3)
        assert not t3b.test_3.is_odd(4)
        assert t3c.test_3.is_odd(3)
        assert not t3c.test_3.is_odd(4)
Exemple #10
0
 def test_020_register_new_time_repr_invalid_attribute_name(self):
     with pytest.raises(ValueError):
         Time.register_new_time("", ExampleTestTimeRepresentation)
     with pytest.raises(ValueError):
         Time.register_new_time("123new", ExampleTestTimeRepresentation)
     with pytest.raises(ValueError):
         Time.register_new_time(123, ExampleTestTimeRepresentation)
Exemple #11
0
 def test_020_register_new_time_repr_invalid_attribute_name(self):
     with pytest.raises(ValueError):
         Time.register_new_time('', ExampleTestTimeRepresentation)
     with pytest.raises(ValueError):
         Time.register_new_time('123new', ExampleTestTimeRepresentation)
     with pytest.raises(ValueError):
         Time.register_new_time(123, ExampleTestTimeRepresentation)
Exemple #12
0
    def test_043_registered_attribute_class_with_other_constructors(
            self, class_Time_resource):
        class ExampleTestTimeRepresentation2(ExampleTestTimeRepresentation):
            @classmethod
            def with_seconds(cls, hour100, minute100, second100):
                return cls(hour100, minute100 + Fraction(second100, 100))

        Time.register_new_time("test_2", ExampleTestTimeRepresentation2)

        # Time attribute type and metaclass are correct
        assert Time.test_2.__name__ == "ExampleTestTimeRepresentation2InTime"
        assert type(Time.test_2).__name__ == "ModifiedClass"
        assert issubclass(Time.test_2, ExampleTestTimeRepresentation)

        # constructed Time type and value are is correct
        t2a = Time.test_2(10, 8)
        assert type(t2a) == Time
        assert t2a.day_frac == Fraction(63, 625)
        d2d = Time.test_2.with_seconds(40, 40, 40)
        assert type(d2d) == Time
        assert d2d.day_frac == Fraction("10101/25000")

        # new attribute on Time instance, type and value are correct
        t2b = Time("963/1250")
        assert isinstance(t2b.test_2, ExampleTestTimeRepresentation2)
        assert type(
            t2b.test_2).__name__ == "ExampleTestTimeRepresentation2InTime"
        assert type(t2b.test_2.__class__).__name__ == "ModifiedClass"
        assert t2b.test_2.hour100 == 77
        assert t2b.test_2.minute100 == 4

        # new attribute on Time instance build by another calendar, type and value are correct
        t2c = Time.western(10, 35, 15)
        assert isinstance(t2c.test_2, ExampleTestTimeRepresentation2)
        assert type(
            t2c.test_2).__name__ == "ExampleTestTimeRepresentation2InTime"
        assert type(t2c.test_2.__class__).__name__ == "ModifiedClass"
        assert t2c.test_2.hour100 == 44
        assert t2c.test_2.minute100 == Fraction("275/24")
Exemple #13
0
    def test_043_registered_attribute_class_with_other_constructors(self, class_Time_resource):
        class ExampleTestTimeRepresentation2(ExampleTestTimeRepresentation):
            @classmethod
            def with_seconds(cls, hour100, minute100, second100):
                return cls(hour100, minute100 + Fraction(second100, 100))

        Time.register_new_time('test_2', ExampleTestTimeRepresentation2)

        # Time attribute type and metaclass are correct
        assert Time.test_2.__name__ == 'ExampleTestTimeRepresentation2InTime'
        assert type(Time.test_2).__name__ == 'ModifiedClass'
        assert issubclass(Time.test_2, ExampleTestTimeRepresentation)

        # constructed Time type and value are is correct
        t2a = Time.test_2(10, 8)
        assert type(t2a) == Time
        assert t2a.day_frac == Fraction(63, 625)
        d2d = Time.test_2.with_seconds(40, 40, 40)
        assert type(d2d) == Time
        assert d2d.day_frac == Fraction("10101/25000")

        # new attribute on Time instance, type and value are correct
        t2b = Time("963/1250")
        assert isinstance(t2b.test_2, ExampleTestTimeRepresentation2)
        assert type(t2b.test_2).__name__ == 'ExampleTestTimeRepresentation2InTime'
        assert type(t2b.test_2.__class__).__name__ == 'ModifiedClass'
        assert t2b.test_2.hour100 == 77
        assert t2b.test_2.minute100 == 4

        # new attribute on Time instance build by another calendar, type and value are correct
        t2c = Time.western(10, 35, 15)
        assert isinstance(t2c.test_2, ExampleTestTimeRepresentation2)
        assert type(t2c.test_2).__name__ == 'ExampleTestTimeRepresentation2InTime'
        assert type(t2c.test_2.__class__).__name__ == 'ModifiedClass'
        assert t2c.test_2.hour100 == 44
        assert t2c.test_2.minute100 == Fraction("275/24")
Exemple #14
0
 def test_010_register_new_time_repr_existing_time_repr_or_attribute(self):
     with pytest.raises(AttributeError):
         Time.register_new_time("western", ExampleTestTimeRepresentation)
     with pytest.raises(AttributeError):
         Time.register_new_time("day_frac", ExampleTestTimeRepresentation)
Exemple #15
0
 def test_010_register_new_time_repr_existing_time_repr_or_attribute(self):
     with pytest.raises(AttributeError):
         Time.register_new_time('western', ExampleTestTimeRepresentation)
     with pytest.raises(AttributeError):
         Time.register_new_time('day_frac', ExampleTestTimeRepresentation)