Beispiel #1
0
 def test_time_series_subset(self):
     """ Tests `time_series` for a subset of keys. """
     # start and end are equally weighted:
     timing = Timing({0: 1, 1: 1})
     result = timing.time_series(2, keys={1})
     # Spreading a value of 2 equally across _just end_ means
     # allocating 2 at end:
     self.assertEqual(result, {1: 2})
Beispiel #2
0
 def test_normalized_subset(self):
     """ Tests `normalized` for a subset of keys. """
     # start and end are equally weighted:
     timing = Timing({0: 1, 1: 1})
     result = timing.normalized(keys={1})
     # Normalizing just end should yield a value of 1 at that time:
     target = Timing({1: 1})
     self.assertEqual(result, target)
Beispiel #3
0
 def test_time_series_basic(self):
     """ Tests `time_series` with simple input. """
     # start and end are equally weighted:
     timing = Timing({0: 1, 1: 1})
     result = timing.time_series(2)
     # Spreading a value of 2 equally across start and end means
     # allocating 1 at start and 1 at end:
     self.assertEqual(result, {0: 1, 1: 1})
Beispiel #4
0
 def test_normalized_basic(self):
     """ Tests `normalized` with simple input. """
     # start and end are equally weighted:
     timing = Timing({0: 1, 1: 1})
     result = timing.normalized()
     # start and end are equally weighted, so each should get 0.5
     # when normalized:
     target = Timing({0: 0.5, 1: 0.5})
     self.assertEqual(result, target)
Beispiel #5
0
 def test_init_dict_money(self):
     """ Init `Timing` with a dict of `Money` values. """
     # It doesn't matter what this dict is; if we wrap its values as
     # `Money` objects, it should parse to the same result:
     timing_dict = {0: 0, 0.5: -2, 1: 1}
     money_dict = {key: value for key, value in timing_dict.items()}
     # Build timings based on the (otherwise-identical) Money and
     # non-Money inputs and confirm that the results are the same:
     timing1 = Timing(timing_dict)
     timing2 = Timing(money_dict)
     self.assertEqual(timing1, timing2)
Beispiel #6
0
    def test_init_dict_mixed_neg_2(self):
        """ Init `Timing` with a net-negative dict of mixed values.

        This tests basically the same behaviour as
        `test_init_dict_mixed_neg`, but was written much earlier
        (as `test_init_dict_accum`) and helped to track down some
        erroneous behaviour. It's retained for historical reasons.
        """
        available = {0: 1000, 0.25: -11000, 0.5: 1000, 0.75: -11000}
        timing = Timing(available)
        target = Timing({0.25: 10000, 0.75: 10000})
        self.assertEqual(timing, target)
Beispiel #7
0
    def test_init_dict_mixed_zero(self):
        """ Init `Timing` with a net-zero dict of mixed values.

        "Mixed" in this context means it has both positive and negative
        values. The sum of those values is zero in this test.
        """
        # Large outflow followed by equally large inflow:
        timing_dict = {0: 0, 0.5: -2, 1: 2}
        timing = Timing(timing_dict)
        # This should result in either an empty time-series or one with
        # all-zero values.
        self.assertTrue(all(value == 0 for value in timing.values()))
Beispiel #8
0
    def test_init_dict_mixed_pos(self):
        """ Init `Timing` with a net-positive dict of mixed values.

        "Mixed" in this context means it has both positive and negative
        values. The sum of those values is positive in this test.
        """
        # Large inflow followed by small outflow (for small net inflow):
        timing_dict = {0: 0, 0.5: 2, 1: -1}
        timing = Timing(timing_dict)
        # There should be only one key with non-zero weight:
        self.assertNotEqual(timing[0.5], 0)
        # All other keys must have zero weight, if they exist:
        self.assertTrue(
            all(value == 0 for key, value in timing.items() if key != 0.5))
Beispiel #9
0
 def test_init_dict_pos(self):
     """ Init `Timing` with a dict of all-positive values. """
     # Technically we use non-negative values:
     timing_dict = {0: 0, 0.5: 1, 1: 1}
     # Should convert directly to `Timing` (the 0 value is optional):
     timing = Timing(timing_dict)
     total_weight = sum(timing.values())
     for key, value in timing_dict.items():
         if value != 0:
             # Each non-0 value should be present...
             self.assertIn(key, timing)
             # ... and both non-0 values should be equally weighted.
             self.assertEqual(timing[key], total_weight / 2)
         elif key in timing:
             # The zero value, if it exists, should remain 0:
             self.assertEqual(timing[key], 0)
Beispiel #10
0
    def test_init_dict_mixed_neg(self):
        """ Init `Timing` with a net-negative dict of mixed values.

        "Mixed" in this context means it has both positive and negative
        values. The sum of those values is negative in this test.
        """
        # Large outflow followed by small inflow and then another large
        # outflow:
        timing_dict = {0: 0, 0.5: -2, 0.75: 1, 1: -2}
        timing = Timing(timing_dict)
        # This should result in a time-series of {0.5: 2, 1: 1} to
        # balance the net flows at each outflow.
        # NOTE: The behaviour is different than is provided for inflows!
        self.assertNotEqual(timing[0.5], 0)
        self.assertNotEqual(timing[1], 0)
        # Key 0.5 should have 2x the weight of key 1:
        self.assertEqual(timing[0.5], timing[1] * 2)
        # All other keys must have zero weight, if they exist:
        self.assertTrue(
            all(value == 0 for key, value in timing.items()
                if key not in (0.5, 1)))
Beispiel #11
0
    def test_init_dict_neg(self):
        """ Init `Timing` with a dict of all-negative values. """
        # This returns the same result as `test_init_dict_pos`;
        # the inputs have their sign flipped, but output is the same.

        # Technically we use non-positive values:
        timing_dict = {0: 0, 0.5: -1, 1: -1}
        # Should convert directly to `Timing`, except that all values
        # have their sign flipped (the 0 value is optional):
        timing = Timing(timing_dict)
        # Ensure that `total_weight` is positive; the later assertEqual
        # tests will thus also check that all values are non-negative:
        total_weight = abs(sum(timing.values()))
        for key, value in timing_dict.items():
            if value != 0:
                # Each non-0 value should be present...
                self.assertIn(key, timing)
                # ... and both non-0 values should be equally weighted.
                self.assertEqual(timing[key], total_weight / 2)
            elif key in timing:
                # The zero value, if it exists, should remain 0:
                self.assertEqual(timing[key], 0)
Beispiel #12
0
 def test_init_str_when(self):
     """ Init `Timing` with a single str parameter, `when`. """
     # 'start' should convert to 0:
     timing = Timing(when='start')
     self.assertEqual(set(timing.keys()), {0})
Beispiel #13
0
 def test_init_str_freq(self):
     """ Init `Timing` with a single str parameter, `frequency`. """
     # There are 12 occurances with a monthly frequency:
     timing = Timing(frequency="M")
     self.assertEqual(len(timing), 12)
Beispiel #14
0
 def test_init_when_freq(self):
     """ Init `Timing` with a single parameter, `when`. """
     timing = Timing(0.5)
     self.assertEqual(set(timing.keys()), {0.5})