def test_get_all_cached_users_from_db(self): """Method to test get_all_cached_users when data is not present in the cache""" get_user_model().objects.create_user(username='******', password='******') cache.clear() self.assertEqual(cache.get(CACHE_KEYS['all_users']), None) test_cache = TheCacher.get_all_cached_users() self.assertEqual(test_cache.__class__.__name__, 'QuerySet')
def test_get_latest_news(self): """ Test getting the latest news when cache is empty """ cache.set(CACHE_KEYS['latest_news'], None) news = TheCacher.get_latest_news() self.assertTrue(news) self.assertTrue(cache.get(CACHE_KEYS['latest_news']))
def view_users(request): """ View User """ users = TheCacher.get_all_cached_users() if users is not None: serializer = UserSerializer(users, many=True) response = Response({'users': serializer.data}, status=200) else: response = Response({'error': 'Unknown error'}, status=500) return response
def list(self, request): """ List bikes API data """ models = TheCacher.get_cached_bus_data() if not models: api_translator = APITranslator("bus", 1) response = api_translator.build_api_request() models = api_translator.response_to_model(response) json_content = self.serializer_class(models, many=True) return Response(json_content.data, status=200)
def list(self, request): """ Method to provide API details """ models = TheCacher.get_cached_pollution_data() if not models: api_translator = APITranslator("pollution", 1) response = api_translator.build_api_request() models = api_translator.response_to_model(response) json_content = self.serializer_class(models, many=True) return Response(json_content.data, status=200)
def create_user_profile(sender, instance, created, **kwargs): """ Method to create user profile """ if created: TheCacher().reset_all_cached_users() Profile.objects.create(user=instance)
def fetch_news_data(): """ Cache news """ TheCacher.get_latest_news()
def test_get_all_cached_users_when_cache_is_present(self): """Method to test get_all_cached_users when data is present in the cache""" cache.set(CACHE_KEYS['all_users'], True) test_cache = TheCacher.get_all_cached_users() self.assertEqual(test_cache, True) cache.clear()
def test_reset_all_cached_users(self): """Method to test reset_all_cached_users""" cache.set(CACHE_KEYS['all_users'], True) self.assertEqual(cache.get(CACHE_KEYS['all_users']), True) TheCacher.reset_all_cached_users() self.assertEqual(cache.get(CACHE_KEYS['all_users']), None)