Example #1
0
    def test_creating_loads_persist(self):
        """
        If C{persist_filename} exists, it is loaded by the constructor.
        """
        filename = self.makeFile()

        persist = Persist()
        persist.set("a", "Hi there!")
        persist.save(filename)

        monitor = Monitor(self.reactor,
                          self.config,
                          persist=Persist(),
                          persist_filename=filename)
        self.assertEqual(monitor.persist.get("a"), "Hi there!")
Example #2
0
    def test_accumulate_within_step_with_nonzero_start_accumulated_value(self):
        """
        step:    0              5
               --|--+--+--+--+--|--
        value:   0     3     4
        """
        persist = Persist()
        accumulate = Accumulator(persist, 5)

        # Persist data that would have been stored when
        # accumulate(2, 3, "key") was called.
        persist.set("key", (2, 6))
        step_data = accumulate(4, 4, "key")
        self.assertEqual(step_data, None)
        self.assertEqual(persist.get("key"), (4, 14))
Example #3
0
    def test_accumulate_non_zero_accumulated_value(self):
        """
        step:    5              10             15
               --|--+--+--+--+--|--+--+--+--+--|--
        value:         4                 3
        """
        persist = Persist()
        accumulate = Accumulator(persist, 5)

        # Persist data that would have been stored when
        # accumulate(7, 4, "key") was called.
        persist.set("key", (7, 8))
        step_data = accumulate(13, 3, "key")
        self.assertEqual(step_data, (10, float((2 * 4) + (3 * 3)) / 5))
        self.assertEqual(persist.get("key"), (13, 9))
Example #4
0
class PatchTest(LandscapeTest):
    def setUp(self):
        LandscapeTest.setUp(self)
        self.persist = Persist()
        self.manager = UpgradeManager()

    def test_wb_nopatches(self):
        """
        Applying no patches should make no change to the database,
        apart from maybe specifying a default version.
        """
        self.assertEqual(self.persist._hardmap, {})
        self.manager.apply(self.persist)
        self.assertEqual(self.persist._hardmap, {"system-version": 0})

    def test_one_patch(self):
        """Test that patches are called and passed a L{Persist} object."""
        calls = []
        self.manager.register_upgrader(1, calls.append)
        self.manager.apply(self.persist)
        self.assertEqual(calls, [self.persist])

    def test_two_patches(self):
        """Test that patches are run in order."""
        calls = []
        self.manager.register_upgrader(2, lambda x: calls.append(2))
        self.manager.register_upgrader(1, lambda x: calls.append(1))

        self.manager.apply(self.persist)
        self.assertEqual(calls, [1, 2])

    def test_record_version(self):
        """When a patch is run it should update the C{system-version}."""
        self.assertEqual(self.persist.get("system-version"), None)
        self.manager.register_upgrader(1, lambda x: None)
        self.manager.apply(self.persist)
        self.assertEqual(self.persist.get("system-version"), 1)

    def test_only_apply_unapplied_versions(self):
        """Upgraders should only be run if they haven't been run before."""
        calls = []
        self.manager.register_upgrader(1, lambda x: calls.append(1))
        self.manager.apply(self.persist)
        self.manager.apply(self.persist)
        self.assertEqual(calls, [1])

    def test_initialize(self):
        """Marking no upgraders as applied should leave the version at 0."""
        self.manager.initialize(self.persist)
        self.assertEqual(self.persist.get("system-version"), 0)

    def test_initialize_with_upgraders(self):
        """
        After registering some upgraders, initialize should set the
        version for the new persist to the highest version number
        available, without running any of the upgraders.
        """
        self.manager.register_upgrader(1, lambda x: 1 / 0)
        self.manager.register_upgrader(5, lambda x: 1 / 0)
        self.manager.register_upgrader(3, lambda x: 1 / 0)
        self.manager.initialize(self.persist)

        self.assertEqual(self.persist.get("system-version"), 5)

    def test_decorated_upgraders_run(self):
        """
        Upgraders that use the L{upgrader} decorator should
        automatically register themselves with a given
        L{UpgradeManager} and be run when the manager applies patches.
        """
        upgrade_manager = UpgradeManager()

        @upgrade_manager.upgrader(1)
        def upgrade(persist):
            self.persist.set("upgrade-called", True)

        upgrade_manager.apply(self.persist)
        self.assertTrue(self.persist.get("upgrade-called"))