Best Python code snippet using tempest_python
test_store.py
Source:test_store.py
1# -*- coding: utf-8 -*-2from __future__ import absolute_import3from uuid import uuid14import pytest5from exam import before, fixture6from mock import patch7from sentry.cache.redis import RedisCache8from sentry.models import Option9from sentry.options.store import OptionsStore10from sentry.testutils import TestCase11class OptionsStoreTest(TestCase):12 @fixture13 def store(self):14 return OptionsStore(15 cache=RedisCache()16 )17 @fixture18 def key(self):19 return self.make_key()20 @before21 def flush_local_cache(self):22 self.store.flush_local_cache()23 def make_key(self, ttl=10, grace=10):24 return self.store.make_key(uuid1().hex, '', object, 0, ttl, grace)25 def test_simple(self):26 store, key = self.store, self.key27 assert store.get(key) is None28 assert store.set(key, 'bar')29 assert store.get(key) == 'bar'30 assert store.delete(key)31 def test_simple_without_cache(self):32 store = OptionsStore(cache=None)33 key = self.key34 assert store.get(key) is None35 with pytest.raises(AssertionError):36 store.set(key, 'bar')37 with pytest.raises(AssertionError):38 store.delete(key)39 def test_db_and_cache_unavailable(self):40 store, key = self.store, self.key41 with patch.object(Option.objects, 'get_queryset', side_effect=Exception()):42 # we can't update options if the db is unavailable43 with self.assertRaises(Exception):44 store.set(key, 'bar')45 # Assert nothing was written to the local_cache46 assert not store._local_cache47 store.set(key, 'bar')48 with patch.object(Option.objects, 'get_queryset', side_effect=Exception()):49 assert store.get(key) == 'bar'50 with patch.object(store.cache, 'get', side_effect=Exception()):51 assert store.get(key) == 'bar'52 store.flush_local_cache()53 assert store.get(key) is None54 @patch('sentry.options.store.time')55 def test_key_with_grace(self, mocked_time):56 store, key = self.store, self.make_key(10, 10)57 mocked_time.return_value = 058 store.set(key, 'bar')59 with patch.object(Option.objects, 'get_queryset', side_effect=Exception()):60 with patch.object(store.cache, 'get', side_effect=Exception()):61 # Serves the value beyond TTL62 mocked_time.return_value = 1563 assert store.get(key) == 'bar'64 mocked_time.return_value = 2165 assert store.get(key) is None66 # It should have also been evicted67 assert not store._local_cache68 @patch('sentry.options.store.time')69 def test_key_ttl(self, mocked_time):70 store, key = self.store, self.make_key(10, 0)71 mocked_time.return_value = 072 store.set(key, 'bar')73 with patch.object(Option.objects, 'get_queryset', side_effect=Exception()):74 with patch.object(store.cache, 'get', side_effect=Exception()):75 assert store.get(key) == 'bar'76 Option.objects.filter(key=key.name).update(value='lol')77 store.cache.delete(key.cache_key)78 # Still within TTL, so don't check database79 assert store.get(key) == 'bar'80 mocked_time.return_value = 1581 with patch.object(Option.objects, 'get_queryset', side_effect=Exception()):82 with patch.object(store.cache, 'get', side_effect=Exception()):83 assert store.get(key) is None84 assert store.get(key) == 'lol'85 @patch('sentry.options.store.time')86 def test_clean_local_cache(self, mocked_time):87 store = self.store88 mocked_time.return_value = 089 key1 = self.make_key(10, 0) # should expire after 1090 key2 = self.make_key(10, 5) # should expire after 1591 key3 = self.make_key(10, 10) # should expire after 2092 key4 = self.make_key(10, 15) # should expire after 2593 store.set(key1, 'x')94 store.set(key2, 'x')95 store.set(key3, 'x')96 store.set(key4, 'x')97 assert len(store._local_cache) == 498 mocked_time.return_value = 099 store.clean_local_cache()100 assert len(store._local_cache) == 4101 mocked_time.return_value = 11102 store.clean_local_cache()103 assert len(store._local_cache) == 3104 assert key1.cache_key not in store._local_cache105 mocked_time.return_value = 21106 store.clean_local_cache()107 assert len(store._local_cache) == 1108 assert key1.cache_key not in store._local_cache109 assert key2.cache_key not in store._local_cache110 assert key3.cache_key not in store._local_cache111 mocked_time.return_value = 26112 store.clean_local_cache()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!