Exemple #1
0
 def test_touch(self):
     """
     Ensure that the touch method updates the timestamp for a referenced
     item.
     """
     self.assertTrue(hasattr(DataStore, 'touch'))
     ds = DataStore()
     timestamp = time.time()
     ds._get_item = MagicMock(return_value=('bar', timestamp, timestamp))
     ds._set_item = MagicMock()
     ds.touch('foo')
     self.assertEqual(1, ds._set_item.call_count)
     args = ds._set_item.call_args_list[0][0]
     self.assertEqual('foo', args[0])
     self.assertEqual('bar', args[1][0])
     self.assertEqual(args[1][1], timestamp)
     self.assertTrue(args[1][2] > timestamp)
Exemple #2
0
 def test__setitem__existing_value(self):
     """
     Check the DataStore base class has a __setitem__ method.
     """
     self.assertTrue(hasattr(DataStore, '__setitem__'))
     ds = DataStore()
     val = 'some arbitrary yet meaningless value'
     timestamp = time.time()
     ds._get_item = MagicMock(return_value=(val, timestamp, 0.0))
     ds._set_item = MagicMock()
     ds['foo'] = val
     self.assertEqual(1, ds._set_item.call_count)
     call_args = ds._set_item.call_args[0]
     self.assertEqual('foo', call_args[0])
     self.assertIsInstance(call_args[1], tuple)
     self.assertEqual(val, call_args[1][0])
     self.assertTrue(timestamp < call_args[1][1])
     self.assertEqual(0.0, call_args[1][2])
Exemple #3
0
    def test__setitem__new_value(self):
        """
        Check the DataStore base class has a __setitem__ method.
        """
        self.assertTrue(hasattr(DataStore, '__setitem__'))
        ds = DataStore()
        val = 'some arbitrary yet meaningless value'
        ds._set_item = MagicMock()

        def side_effect(*args, **kwargs):
            raise KeyError()

        ds._get_item = MagicMock(side_effect=side_effect)
        ds['foo'] = val
        self.assertEqual(1, ds._set_item.call_count)
        call_args = ds._set_item.call_args[0]
        self.assertEqual('foo', call_args[0])
        self.assertIsInstance(call_args[1], tuple)
        self.assertEqual(val, call_args[1][0])
        self.assertIsInstance(call_args[1][1], float)
        self.assertEqual(call_args[1][2], 0.0)