def get_all() -> list: """ Gets list of all users in the DB. If any error occurs when pulling data from the DB, we use cached data in the Redis. If even that fails, exception will be raised. :return: list """ try: db = components.get_psql() redis = components.get_redis() result = _get_all_from_db(db) _save_all_to_redis(redis, result) return result except psycopg2.DatabaseError: return _get_all_from_redis(components.get_redis())
def test_get_all(self): with closing(TestDatabase()) as test_db: redis = components.get_redis() test_db.use_table('users') expected_users = ["user_A", "user_B", "user_C"] with unittest.mock.patch('microservice.components.get_psql') as db_mock: db_mock.return_value = test_db.connection self.assertListEqual(expected_users, users.get_all()) self.assertEqual(json.dumps(expected_users), redis.get(users.REDIS_KEY))
def test_get_all(self): with closing(TestDatabase()) as test_db: redis = components.get_redis() test_db.use_table('users') expected_users = ["user_A", "user_B", "user_C"] with unittest.mock.patch( 'microservice.components.get_psql') as db_mock: db_mock.return_value = test_db.connection self.assertListEqual(expected_users, users.get_all()) self.assertEqual(json.dumps(expected_users), redis.get(users.REDIS_KEY))