Exemplo n.º 1
0
 def test_get_creates_new_object_if_original_object_is_expired(self):
     self.cache = ObjectCache(ttl_seconds=-10)
     cached_object = self.cache.get(TestObject)
     assert_that(self.cache.get(TestObject)).is_not_same_as(cached_object)
Exemplo n.º 2
0
 def setUp(self):
     self.cache = ObjectCache()
Exemplo n.º 3
0
 def test_get_time_to_live(self):
     self.cache = ObjectCache(ttl_seconds=60)
     assert_that(self.cache.get_time_to_live()).is_equal_to(60)
Exemplo n.º 4
0
class TestObjectCache(TestCase):
    def setUp(self):
        self.cache = ObjectCache()

    def test_get_time_to_live_default_value(self):
        assert_that(self.cache.get_time_to_live()).is_equal_to(30)

    def test_get_time_to_live(self):
        self.cache = ObjectCache(ttl_seconds=60)
        assert_that(self.cache.get_time_to_live()).is_equal_to(60)

    def test_get(self):
        cached_object = self.cache.get(TestObject)
        assert_that(cached_object).is_instance_of(TestObject)

    def test_get_caches_objects(self):
        cached_object = self.cache.get(TestObject)
        assert_that(self.cache.get(TestObject)).is_same_as(cached_object)

    def test_clear_clears_cache(self):
        cached_object = self.cache.get(TestObject)

        self.cache.clear()
        assert_that(self.cache.get(TestObject)).is_not_same_as(cached_object)

    def test_clear_resets_counters(self):
        self.cache.get(TestObject)
        self.cache.get(TestObject)

        self.cache.clear()

        assert_that(self.cache.get_ratio()).is_equal_to(0.0)

    def test_get_creates_new_object_if_original_object_is_expired(self):
        self.cache = ObjectCache(ttl_seconds=-10)
        cached_object = self.cache.get(TestObject)
        assert_that(self.cache.get(TestObject)).is_not_same_as(cached_object)

    def test_get_different_objects_for_different_create_objects(self):
        class OtherTestObject(TestObject):
            pass

        cached_object = self.cache.get(TestObject)
        other_cached_object = self.cache.get(OtherTestObject)

        assert_that(cached_object).is_not_equal_to(other_cached_object)

    def test_get_with_arg_is_called_with_same_arg(self):
        argument1 = object()
        argument2 = object()

        cached_object = self.cache.get(TestObject, argument1, argument2)

        assert_that(cached_object.get_args()).contains(argument1, argument2)
        assert_that(not cached_object.get_kwargs())

    def test_get_with_arg_is_cached(self):
        argument = object()

        cached_object = self.cache.get(TestObject, argument)
        assert_that(self.cache.get(TestObject,
                                   argument)).is_same_as(cached_object)

    def test_get_with_kwargs_is_called_with_same_kwargs(self):
        argument1 = object()
        argument2 = object()

        cached_object = self.cache.get(TestObject,
                                       foo=argument1,
                                       bar=argument2)

        assert_that(not cached_object.get_args())
        assert_that(cached_object.get_kwargs()).is_equal_to({
            'foo': argument1,
            'bar': argument2
        })

    def test_get_with_kwargs_is_cached(self):
        argument1 = object()
        argument2 = object()

        cached_object = self.cache.get(TestObject,
                                       foo=argument1,
                                       bar=argument2)
        assert_that(self.cache.get(TestObject, bar=argument2,
                                   foo=argument1)).is_same_as(cached_object)

    def test_get_ratio(self):
        assert_that(self.cache.get_ratio()).is_equal_to(0.0)

        self.cache.get(TestObject)
        assert_that(self.cache.get_ratio()).is_equal_to(0.0)

        self.cache.get(TestObject)
        assert_that(self.cache.get_ratio()).is_equal_to(0.5)
Exemplo n.º 5
0
 def test_get_creates_new_object_if_original_object_is_expired(self):
     self.cache = ObjectCache(ttl_seconds=-10)
     cached_object = self.cache.get(TestObject)
     assert_that(self.cache.get(TestObject), is_not(same_instance(cached_object)))
Exemplo n.º 6
0
 def test_get_time_to_live(self):
     self.cache = ObjectCache(ttl_seconds=60)
     assert_that(self.cache.get_time_to_live(), is_(60))
Exemplo n.º 7
0
 def setUp(self):
     self.cache = ObjectCache()
Exemplo n.º 8
0
class TestObjectCache(TestCase):
    def setUp(self):
        self.cache = ObjectCache()

    def test_get_time_to_live_default_value(self):
        assert_that(self.cache.get_time_to_live(), is_(30))

    def test_get_time_to_live(self):
        self.cache = ObjectCache(ttl_seconds=60)
        assert_that(self.cache.get_time_to_live(), is_(60))

    def test_get(self):
        cached_object = self.cache.get(TestObject)
        assert_that(cached_object, instance_of(TestObject))

    def test_get_caches_objects(self):
        cached_object = self.cache.get(TestObject)
        assert_that(self.cache.get(TestObject), is_(same_instance(cached_object)))

    def test_clear_clears_cache(self):
        cached_object = self.cache.get(TestObject)

        self.cache.clear()
        assert_that(self.cache.get(TestObject), is_not(same_instance(cached_object)))

    def test_clear_resets_counters(self):
        self.cache.get(TestObject)
        self.cache.get(TestObject)

        self.cache.clear()

        assert_that(self.cache.get_ratio(), is_(0.0))

    def test_get_creates_new_object_if_original_object_is_expired(self):
        self.cache = ObjectCache(ttl_seconds=-10)
        cached_object = self.cache.get(TestObject)
        assert_that(self.cache.get(TestObject), is_not(same_instance(cached_object)))

    def test_get_different_objects_for_different_create_objects(self):
        class OtherTestObject(TestObject):
            pass

        cached_object = self.cache.get(TestObject)
        other_cached_object = self.cache.get(OtherTestObject)

        assert_that(cached_object, is_not(other_cached_object))

    def test_get_with_arg_is_called_with_same_arg(self):
        argument1 = object()
        argument2 = object()

        cached_object = self.cache.get(TestObject, argument1, argument2)

        assert_that(cached_object.get_args(), contains(argument1, argument2))
        assert_that(not cached_object.get_kwargs())

    def test_get_with_arg_is_cached(self):
        argument = object()

        cached_object = self.cache.get(TestObject, argument)
        assert_that(self.cache.get(TestObject, argument), is_(same_instance(cached_object)))

    def test_get_with_kwargs_is_called_with_same_kwargs(self):
        argument1 = object()
        argument2 = object()

        cached_object = self.cache.get(TestObject, foo=argument1, bar=argument2)

        assert_that(not cached_object.get_args())
        assert_that(cached_object.get_kwargs(), is_({'foo': argument1, 'bar': argument2}))

    def test_get_with_kwargs_is_cached(self):
        argument1 = object()
        argument2 = object()

        cached_object = self.cache.get(TestObject, foo=argument1, bar=argument2)
        assert_that(self.cache.get(TestObject, bar=argument2, foo=argument1), is_(same_instance(cached_object)))

    def test_get_ratio(self):
        assert_that(self.cache.get_ratio(), is_(0.0))

        self.cache.get(TestObject)
        assert_that(self.cache.get_ratio(), is_(0.0))

        self.cache.get(TestObject)
        assert_that(self.cache.get_ratio(), is_(0.5))