def test_compare(self):
     self.assertEqual(TimeValue(1), TimeValue(1))
     self.assertNotEqual(TimeValue(1), TimeValue(2))
     self.assertLess(TimeValue(1), TimeValue(2))
     self.assertLessEqual(TimeValue(1), TimeValue(1))
     self.assertGreater(TimeValue(2), TimeValue(1))
     self.assertGreaterEqual(TimeValue(2), TimeValue(2))
     self.assertNotEqual(TimeValue(2), TimeValue(3))
     self.assertEqual(TimeValue(TimeOffset(4)), TimeValue(TimeOffset(4)))
    def test_addsub(self):
        cases = [
            (TimeValue(50), '+', TimeValue(50), TimeValue(100)),
            (TimeValue(50, rate=Fraction(25)), '+', TimeValue(TimeOffset(2)),
             TimeValue(100, rate=Fraction(25))),
            (TimeValue(TimeOffset(2)), '+', TimeValue(TimeOffset(2)),
             TimeValue(TimeOffset(4))),
            (TimeValue(50), '-', TimeValue(50), TimeValue(0)),
            (TimeValue(50, rate=Fraction(25)), '-', TimeValue(TimeOffset(2)),
             TimeValue(0, rate=Fraction(25))),
            (TimeValue(TimeOffset(2)), '-', TimeValue(TimeOffset(2)),
             TimeValue(TimeOffset(0))),
        ]

        for case in cases:
            with self.subTest(case=case):
                if case[1] == '+':
                    result = case[0] + case[2]
                else:
                    result = case[0] - case[2]

                self.assertEqual(
                    result,
                    case[3],
                    msg="{!r} {} {!r} = {!r}, expected {!r}".format(
                        case[0], case[1], case[2], result, case[3]))
    def test_as_timeoffset(self):
        tv = TimeValue(TimeOffset(4), rate=Fraction(25))
        to = tv.as_timeoffset()
        self.assertIsInstance(to, TimeOffset)
        self.assertEqual(to, TimeOffset(4))

        tv = TimeValue(Timestamp(4), rate=Fraction(25))
        to = tv.as_timeoffset()
        self.assertIsInstance(to, TimeOffset)
        self.assertEqual(to, TimeOffset(4))

        tv = TimeValue(100, rate=Fraction(25))
        to = tv.as_timeoffset()
        self.assertIsInstance(to, TimeOffset)
        self.assertEqual(to, TimeOffset(4))
    def test_addsub_no_rate(self):
        cases = [
            (TimeValue(50), '+', TimeValue(TimeOffset(2))),
            (TimeValue(TimeOffset(2)), '+', TimeValue(50)),
            (TimeValue(50), '-', TimeValue(TimeOffset(2))),
            (TimeValue(TimeOffset(2)), '-', TimeValue(50)),
        ]

        for case in cases:
            with self.subTest(case=case):
                with self.assertRaises(ValueError):
                    if case[1] == '+':
                        case[0] + case[2]
                    else:
                        case[0] - case[2]
    def test_from_str(self):
        cases = [
            ("-100", TimeValue(-100)),
            ("0", TimeValue(0)),
            ("100", TimeValue(100)),
            ("100@25", TimeValue(100, rate=Fraction(25))),
            ("100@30000/1001", TimeValue(100, rate=Fraction(30000, 1001))),
            ("-4:0", TimeValue(TimeOffset(4, sign=-1))),
            ("0:0", TimeValue(TimeOffset(0))),
            ("4:0", TimeValue(TimeOffset(4))),
            ("4:0@25", TimeValue(100, rate=Fraction(25))),
            ("4:0@30000/1001", TimeValue(120, rate=Fraction(30000, 1001))),
        ]

        for case in cases:
            with self.subTest(case=case):
                self.assertEqual(TimeValue.from_str(case[0]), case[1])
    def test_as_but_no_rate(self):
        tv = TimeValue(TimeOffset(4))
        with self.assertRaises(ValueError):
            tv.as_count()

        tv = TimeValue(100)
        with self.assertRaises(ValueError):
            tv.as_timeoffset()
    def test_as_count(self):
        tv = TimeValue(100)
        ct = tv.as_count()
        self.assertEqual(ct, 100)

        tv = TimeValue(TimeOffset(4), rate=Fraction(25))
        ct = tv.as_count()
        self.assertEqual(ct, 100)
    def test_mediatimeoffset(self):
        tv = TimeValue(TimeOffset(4), rate=Fraction(25))
        self.assertIsInstance(tv, SupportsMediaTimeOffset)
        to = mediatimeoffset(tv)
        self.assertIsInstance(to, TimeOffset)
        self.assertEqual(to, TimeOffset(4))

        tv = TimeValue(Timestamp(4), rate=Fraction(25))
        self.assertIsInstance(tv, SupportsMediaTimeOffset)
        to = mediatimeoffset(tv)
        self.assertIsInstance(to, TimeOffset)
        self.assertEqual(to, TimeOffset(4))

        tv = TimeValue(100, rate=Fraction(25))
        self.assertIsInstance(tv, SupportsMediaTimeOffset)
        to = mediatimeoffset(tv)
        self.assertIsInstance(to, TimeOffset)
        self.assertEqual(to, TimeOffset(4))
    def test_to_str(self):
        cases = [
            ("-100", TimeValue(-100), True),
            ("0", TimeValue(0), True),
            ("100", TimeValue(100), True),
            ("100@25", TimeValue(100, rate=Fraction(25)), True),
            ("100", TimeValue(100, rate=Fraction(25)), False),
            ("100@30000/1001", TimeValue(100, rate=Fraction(30000,
                                                            1001)), True),
            ("-4:0", TimeValue(TimeOffset(4, sign=-1)), True),
            ("0:0", TimeValue(TimeOffset(0)), True),
            ("4:0", TimeValue(TimeOffset(4)), True),
        ]

        for case in cases:
            with self.subTest(case=case):
                self.assertEqual(case[0], case[1].to_str(include_rate=case[2]))
                self.assertEqual(case[1].to_str(), str(case[1]))
    def test_multdiv(self):
        cases = [
            (TimeValue(50), '*', 2, TimeValue(100)),
            (TimeValue(TimeOffset(2)), '*', 2, TimeValue(TimeOffset(4))),
            (2, '*', TimeValue(50), TimeValue(100)),
            (2, '*', TimeValue(TimeOffset(2)), TimeValue(TimeOffset(4))),
            (TimeValue(50), '/', 2, TimeValue(25)),
            (TimeValue(TimeOffset(2)), '/', 2, TimeValue(TimeOffset(1))),
            (TimeValue(25), '/', 2, TimeValue(12)),
            (TimeValue(25), '//', 2, TimeValue(12)),
        ]

        for case in cases:
            with self.subTest(case=case):
                if case[1] == '*':
                    result = case[0] * case[2]
                elif case[1] == '/':
                    result = case[0] / case[2]
                else:
                    result = case[0] // case[2]

                self.assertEqual(
                    result,
                    case[3],
                    msg="{!r} {} {!r} = {!r}, expected {!r}".format(
                        case[0], case[1], case[2], result, case[3]))
Ejemplo n.º 11
0
 def test_schedule_activation(self):
     """Test setting up a delayed activation -
     this test will block for two seconds"""
     testFunc = self.dut._scheduleRelative
     testTime = TimeOffset(1, 0)
     # monkey patch in test function
     self.api.activateStaged = self.mockApiCallback
     ret = testFunc(testTime)
     self.assertTrue(self.api.locked)
     startTime = time.time()
     print("Sleeping for 1.1 seconds")
     time.sleep(1.1)
     self.assertTrue(self.hadCallback)
     diff = self.callbackTime - startTime
     self.assertAlmostEqual(1, diff, 2)
     self.assertFalse(self.api.locked)
     self.assertEqual(ret[0], 202)
     self.verify_against_schema(ret[1])
     self.check_last_is_null()
 def test_from_timeoffset_to_count(self):
     tv = TimeValue(TimeOffset(4), rate=Fraction(25))
     self.assertIsInstance(tv.value, int)
     self.assertEqual(tv.value, 100)
     self.assertEqual(tv.rate, Fraction(25))
 def test_compare_no_rate(self):
     with self.assertRaises(ValueError):
         TimeValue(100) == TimeValue(TimeOffset(4))
 def test_compare_with_convert(self):
     self.assertEqual(TimeValue(100, rate=Fraction(25)),
                      TimeValue(TimeOffset(4)))
     self.assertEqual(TimeValue(TimeOffset(4)),
                      TimeValue(100, rate=Fraction(25)))
 def test_from_timeoffset(self):
     tv = TimeValue(TimeOffset(4), rate=None)
     self.assertEqual(tv.value, TimeOffset(4))
     self.assertIsNone(tv.rate)