def test_persists(self):
        """Test if memoization persists
        """
        called = [0]

        def environment(expected_calls=1):
            """Creates target function in scope, check number of calls
            """
            @persistent_memoize
            def target(arg):
                """Test function..."""
                called[0] += 1
                return 2 * arg

            result1 = target(1)
            self.assertEqual(result1, 2)
            self.assertEqual(called[0], expected_calls)

            result2 = target(1)
            self.assertEqual(result2, 2)
            self.assertEqual(called[0], expected_calls)

        environment(expected_calls=1)
        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            state.reset_instance()
            environment(expected_calls=2)
            environment(expected_calls=2)
            state.reset_instance()
            state.load(temp.name, lazy=True)
            environment(expected_calls=2)
    def test_get_section_lazy(self):
        """Test getting a section of the state lazily
        """
        state['a.a'] = 12
        state['a.b'] = 13
        state['c'] = 24

        self.assertIn('a', state)
        section_a = state['a']
        self.assertIn('a', section_a)
        self.assertIn('b', section_a)
        self.assertNotIn('c', section_a)
        self.assertEqual(section_a['a'], 12)
        self.assertEqual(section_a['b'], 13)

        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            state.reset_instance()
            self.assertNotIn('a', state)

            state.load(temp.name)

            self.assertIn('a', state)

            section_a = state['a']

            self.assertIn('a', section_a)
            self.assertIn('b', section_a)
            self.assertNotIn('c', section_a)
            self.assertEqual(section_a['a'], 12)
            self.assertEqual(section_a['b'], 13)
Beispiel #3
0
    def test_get_section_lazy(self):
        """Test getting a section of the state lazily
        """
        state['a.a'] = 12
        state['a.b'] = 13
        state['c'] = 24

        self.assertIn('a', state)
        section_a = state['a']
        self.assertIn('a', section_a)
        self.assertIn('b', section_a)
        self.assertNotIn('c', section_a)
        self.assertEqual(section_a['a'], 12)
        self.assertEqual(section_a['b'], 13)

        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            state.reset_instance()
            self.assertNotIn('a', state)

            state.load(temp.name)

            self.assertIn('a', state)

            section_a = state['a']

            self.assertIn('a', section_a)
            self.assertIn('b', section_a)
            self.assertNotIn('c', section_a)
            self.assertEqual(section_a['a'], 12)
            self.assertEqual(section_a['b'], 13)
Beispiel #4
0
 def test_with_block_load_fail(self):
     """Test with-block of the StateHandler does not destroy when failing
     """
     state['a'] = 123
     with tempfile.NamedTemporaryFile() as temp:
         state.reset_instance()
         state['a'] = 12
         with StateHandler(temp.name, load=True):
             self.assertEqual(len(state), 1)
             self.assertEqual(state['a'], 12)
Beispiel #5
0
 def test_in_lazy(self):
     """Test checking for an attribute in a lazily loaded state
     """
     self._setup_basic_state()
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('list', state)
         state.load(temp.name, lazy=True)
         self.assertIn('list', state)
Beispiel #6
0
 def test_with_block_does_load(self):
     """Test the with-block of the StateHandler loads if required
     """
     state['a'] = 123
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         with StateHandler(temp.name, load=True):
             self.assertEqual(len(state), 1)
             self.assertEqual(state['a'], 123)
 def test_with_block_does_not_load(self):
     """Test the basic with-block of the StateHandler does not load anything
     """
     state['a'] = 1
     self.assertEqual(len(state), 1)
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         with StateHandler(temp.name):
             self.assertEqual(len(state), 0)
Beispiel #8
0
 def test_with_block_does_not_load(self):
     """Test the basic with-block of the StateHandler does not load anything
     """
     state['a'] = 1
     self.assertEqual(len(state), 1)
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         with StateHandler(temp.name):
             self.assertEqual(len(state), 0)
 def test_in_lazy(self):
     """Test checking for an attribute in a lazily loaded state
     """
     self._setup_basic_state()
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('list', state)
         state.load(temp.name, lazy=True)
         self.assertIn('list', state)
Beispiel #10
0
 def test_with_block_load_fail(self):
     """Test with-block of the StateHandler does not destroy when failing
     """
     state['a'] = 123
     with tempfile.NamedTemporaryFile() as temp:
         state.reset_instance()
         state['a'] = 12
         with StateHandler(temp.name, load=True):
             self.assertEqual(len(state), 1)
             self.assertEqual(state['a'], 12)
Beispiel #11
0
 def test_with_block_does_load(self):
     """Test the with-block of the StateHandler loads if required
     """
     state['a'] = 123
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         with StateHandler(temp.name, load=True):
             self.assertEqual(len(state), 1)
             self.assertEqual(state['a'], 123)
 def test_triggers_state_changed(self):
     """Test if memoization triggers state change
     """
     with tempfile.NamedTemporaryFile() as temp:
         cache = PersistentCache('bla')
         state.save(temp.name)
         state.reset_instance()
         state.load(temp.name, lazy=True)
         self.assertFalse(state.changed)
         cache[1] = 42
         self.assertTrue(state.changed)
Beispiel #13
0
 def test_saving_loading_lazy_array(self):
     """Test saving and loading a numpy array
     """
     random = np.random.rand(100)
     state['random'] = random
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('random', state)
         state.load(temp.name)
         self.assertTrue((random == state['random']).all())
Beispiel #14
0
 def test_saving_loading_lazy_array(self):
     """Test saving and loading a numpy array
     """
     random = np.random.rand(100)
     state['random'] = random
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('random', state)
         state.load(temp.name)
         self.assertTrue((random == state['random']).all())
Beispiel #15
0
 def test_with_block_does_save(self):
     """Test the with-block of the StateHandler saves if required
     """
     state['a'] = 1
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         with StateHandler(temp.name, save=True):
             state['a'] = 42
         state.reset_instance()
         state.load(temp.name)
         self.assertEqual(state['a'], 42)
Beispiel #16
0
 def test_with_block_does_save(self):
     """Test the with-block of the StateHandler saves if required
     """
     state['a'] = 1
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         with StateHandler(temp.name, save=True):
             state['a'] = 42
         state.reset_instance()
         state.load(temp.name)
         self.assertEqual(state['a'], 42)
Beispiel #17
0
 def test_loading_list_performance(self):
     """Test loading a list and make sure it's reasonably fast
     """
     random = np.random.randint(0, 255, 1024*1024).tolist()
     state['random'] = random
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('random', state)
         tic = datetime.now()
         state.load(temp.name)
         toc = datetime.now()
         self.assertTrue((toc - tic).total_seconds() < 0.5)
 def test_cache_persists_reset(self):
     """Test if the cache persists after resetting and reloading state
     """
     cache = PersistentCache('bla')
     cache[1] = 42
     self.assertIn(1, cache)
     self.assertEqual(cache[1], 42)
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         state.load(temp.name)
         self.assertIn(1, cache)
         self.assertEqual(cache[1], 42)
Beispiel #19
0
 def test_loading_numpy_performance(self):
     """Test loading a numpy array and make sure it's reasonably fast
     """
     random = np.array(np.random.randint(0, 255, 1024 * 1024))
     state['random'] = random
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('random', state)
         tic = datetime.now()
         state.load(temp.name)
         toc = datetime.now()
         self.assertTrue((toc - tic).total_seconds() < 0.5)
Beispiel #20
0
    def test_with_block_show(self):
        """Test with-block of the StateHandler does not cause show to fail
        """
        with tempfile.NamedTemporaryFile() as temp:
            state.reset_instance()
            state['a'] = 12
            with StateHandler(temp.name, load=True):
                self.assertEqual(len(state), 1)
                self.assertEqual(state['a'], 12)

                buf = io.StringIO()
                with stdout_redirector(buf):
                    state.show()
                self.assertNotEqual(len(buf.getvalue()), 0)
                self.assertRegexpMatches(buf.getvalue(), r"a:12")
Beispiel #21
0
    def test_with_block_show(self):
        """Test with-block of the StateHandler does not cause show to fail
        """
        with tempfile.NamedTemporaryFile() as temp:
            state.reset_instance()
            state['a'] = 12
            with StateHandler(temp.name, load=True):
                self.assertEqual(len(state), 1)
                self.assertEqual(state['a'], 12)

                buf = io.StringIO()
                with stdout_redirector(buf):
                    state.show()
                self.assertNotEqual(len(buf.getvalue()), 0)
                self.assertRegexpMatches(buf.getvalue(), r"a:12")
Beispiel #22
0
    def test_get_section_lazy2(self):
        """Test getting directly a section of the state lazily
        """
        state['a.b'] = 12

        self.assertIn('a.b', state)
        self.assertEqual(state['a.b'], 12)

        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            state.reset_instance()

            state.load(temp.name)

            self.assertIn('a.b', state)
            self.assertEqual(state['a.b'], 12)
Beispiel #23
0
    def test_save_load_file_keys(self):
        """Test saving file and reloading yields identical keys
        """
        state['normal_key'] = 1
        state['dashed-key'] = 3
        state['dotted.key'] = 4

        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            state.reset_instance()

            state.load(temp.name, lazy=False)

            # Make sure the keys are there
            for key in ['normal_key', 'dashed-key', 'dotted.key']:
                self.assertIn(key, state)
Beispiel #24
0
    def test_no_unnecessary_save_lazy(self):
        """Test saving the state does not save just after loading lazily
        """
        self.assertFalse(state.need_saving())
        state['bla'] = 'bla'
        self.assertTrue(state.need_saving())

        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            self.assertFalse(state.need_saving())
            state.reset_instance()
            self.assertFalse(state.need_saving())
            state.load(temp.name, lazy=True)
            self.assertFalse(state.need_saving())
            self.assertEqual(state['bla'], 'bla')
            self.assertFalse(state.need_saving())
Beispiel #25
0
    def test_get_section_lazy2(self):
        """Test getting directly a section of the state lazily
        """
        state['a.b'] = 12

        self.assertIn('a.b', state)
        self.assertEqual(state['a.b'], 12)

        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            state.reset_instance()

            state.load(temp.name)

            self.assertIn('a.b', state)
            self.assertEqual(state['a.b'], 12)
Beispiel #26
0
    def test_no_unnecessary_save_lazy(self):
        """Test saving the state does not save just after loading lazily
        """
        self.assertFalse(state.need_saving())
        state['bla'] = 'bla'
        self.assertTrue(state.need_saving())

        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            self.assertFalse(state.need_saving())
            state.reset_instance()
            self.assertFalse(state.need_saving())
            state.load(temp.name, lazy=True)
            self.assertFalse(state.need_saving())
            self.assertEqual(state['bla'], 'bla')
            self.assertFalse(state.need_saving())
Beispiel #27
0
 def test_saving_unloaded_value(self):
     """Test saving does not delete unloaded values
     """
     state['a'] = 12
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('a', state)
         state.load(temp.name)
         state['b'] = 12
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('a', state)
         self.assertNotIn('b', state)
         state.load(temp.name)
         self.assertIn('a', state)
         self.assertIn('b', state)
Beispiel #28
0
 def test_saving_unloaded_value(self):
     """Test saving does not delete unloaded values
     """
     state['a'] = 12
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('a', state)
         state.load(temp.name)
         state['b'] = 12
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('a', state)
         self.assertNotIn('b', state)
         state.load(temp.name)
         self.assertIn('a', state)
         self.assertIn('b', state)
Beispiel #29
0
    def test_save_load_file_keys(self):
        """Test saving file and reloading yields identical keys
        """
        state['normal_key'] = 1
        state['dashed-key'] = 3
        state['dotted.key'] = 4

        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            state.reset_instance()

            state.load(temp.name, lazy=False)

            # Make sure the keys are there
            for key in ['normal_key',
                        'dashed-key',
                        'dotted.key']:
                self.assertIn(key, state)
Beispiel #30
0
    def test_saving_deleted_value(self):
        """Test saving really deletes entry
        """
        state['a'] = 12
        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            state.reset_instance()
            self.assertNotIn('a', state)
            state.load(temp.name)
            self.assertIn('a', state)
            del state['a']

            self.assertNotIn('a', state)
            state.save(temp.name)
            state.reset_instance()
            self.assertNotIn('a', state)
            state.load(temp.name)
            self.assertNotIn('a', state)
Beispiel #31
0
    def test_saving_deleted_value(self):
        """Test saving really deletes entry
        """
        state['a'] = 12
        with tempfile.NamedTemporaryFile() as temp:
            state.save(temp.name)
            state.reset_instance()
            self.assertNotIn('a', state)
            state.load(temp.name)
            self.assertIn('a', state)
            del state['a']

            self.assertNotIn('a', state)
            state.save(temp.name)
            state.reset_instance()
            self.assertNotIn('a', state)
            state.load(temp.name)
            self.assertNotIn('a', state)
Beispiel #32
0
 def test_saving_loading_np_array2(self):
     """Test saving and loading numpy arrays of higher dimension
     """
     random1 = np.random.rand(321, 123)
     random2 = np.random.randint(0, 100, (123, 345))
     state['random1'] = random1
     state['random2'] = random2
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('random1', state)
         self.assertNotIn('random2', state)
         state.load(temp.name, lazy=False)
         self.assertTrue((random1 == state['random1']).all())
         self.assertTrue((random2 == state['random2']).all())
         self.assertEqual(state['random1'].shape, random1.shape)
         self.assertEqual(state['random2'].shape, random2.shape)
         self.assertEqual(state['random1'].dtype, random1.dtype)
         self.assertEqual(state['random2'].dtype, random2.dtype)
Beispiel #33
0
    def test_with_block_locks(self):
        """Test the with-block of the StateHandler locks the state
        """
        state['a'] = 123
        with tempfile.NamedTemporaryFile() as temp:

            def other_op():
                """Function to run in another process"""
                StateHandler.STATE_LOCK_TIMEOUT = 0.001
                other_handler = StateHandler(temp.name, load=True)

                self.assertRaises(RuntimeError, other_handler.__enter__)

            state.save(temp.name)
            state.reset_instance()
            with StateHandler(temp.name, load=True):
                process = multiprocessing.Process(target=other_op)
                process.start()
                process.join()
Beispiel #34
0
    def test_with_block_locks(self):
        """Test the with-block of the StateHandler locks the state
        """
        state['a'] = 123
        with tempfile.NamedTemporaryFile() as temp:
            def other_op():
                """Function to run in another process"""
                StateHandler.STATE_LOCK_TIMEOUT = 0.001
                other_handler = StateHandler(temp.name, load=True)

                self.assertRaises(RuntimeError,
                                  other_handler.__enter__)

            state.save(temp.name)
            state.reset_instance()
            with StateHandler(temp.name, load=True):
                process = multiprocessing.Process(target=other_op)
                process.start()
                process.join()
Beispiel #35
0
 def test_saving_loading_np_array2(self):
     """Test saving and loading numpy arrays of higher dimension
     """
     random1 = np.random.rand(321, 123)
     random2 = np.random.randint(0, 100, (123, 345))
     state['random1'] = random1
     state['random2'] = random2
     with tempfile.NamedTemporaryFile() as temp:
         state.save(temp.name)
         state.reset_instance()
         self.assertNotIn('random1', state)
         self.assertNotIn('random2', state)
         state.load(temp.name, lazy=False)
         self.assertTrue((random1 == state['random1']).all())
         self.assertTrue((random2 == state['random2']).all())
         self.assertEqual(state['random1'].shape, random1.shape)
         self.assertEqual(state['random2'].shape, random2.shape)
         self.assertEqual(state['random1'].dtype, random1.dtype)
         self.assertEqual(state['random2'].dtype, random2.dtype)
 def tearDown(self):
     """Teardown test fixture
     """
     state.reset_instance()
 def tearDown(self):
     """Teardown test fixture
     """
     state.reset_instance()
Beispiel #38
0
 def tearDown(self):
     """Clean up after the test
     """
     state.reset_instance()
Beispiel #39
0
 def tearDown(self):
     """Clean up after the test
     """
     state.reset_instance()