Пример #1
0
 def test_post(self):
     """
     Test to check if post request is completed succesfully
     """
     logging.info("Running Test Case #1: \nTest to check if "
                  "request is completed succesfully")
     post_request = requests.post('http://app:5000/hello',
                                  json={'value': 'world'})
     returned_value = post_request.json()
     expected_value = json.dumps({"hello": "success"})
     self.assertEqual(json.dumps(returned_value), expected_value)
     redis_db.flushall()
     post_request = requests.get('http://app:5000/clear/hello')
Пример #2
0
 def test_get_none_key(self):
     """
     Test to check if invalid key is given returns key not found
     """
     logging.info(
         "Running Test Case #2: \nTest to check if invalid key is given returns key not found"
     )
     get_request = requests.get('http://app:5000/invalid_key')
     returned_value = get_request.json()
     expected_value = json.dumps({'Error': 'Key not found'})
     self.assertEqual(json.dumps(returned_value), expected_value)
     redis_db.info()
     redis_db.flushall()
Пример #3
0
 def test_get_valid_key(self):
     """
     Test to check if valid key is given returns value
     """
     logging.info(
         "Running Test Case #3: \nTest to check if valid key is given returns value"
     )
     headers = {'Content-type': 'application/json'}
     post = requests.post('http://app:5000/hello',
                          data=json.dumps({"value": "world"}),
                          headers=headers)
     print(post.json())
     time.sleep(2)
     get_request = requests.get('http://app:5000/hello')
     print(get_request.json())
     returned_value = get_request.json()
     expected_value = json.dumps({"key": "hello", "value": "world"})
     self.assertEqual(json.dumps(returned_value), expected_value)
     redis_db.flushall()
Пример #4
0
    def test_get_valid_key_redis_cache_capacity_reached(self):
        """
        Test to check if redis cache LRU-STORE and LRU-KEYS is updated and
        LRU is used to evict a key with less zscore
        """
        logging.info(
            "Running Test Case #7: \nest to check if redis cache LRU-STORE and"
            " LRU-KEYS is updated and \n LRU is used to evict a key with less zscore"
        )
        headers = {'Content-type': 'application/json'}

        redis_db.flushall()
        clear_request = requests.get('http://app:5000/clear/hello')
        clear_request = requests.get('http://app:5000/clear/hello2')

        post_request = requests.post('http://app:5000/hello',
                                     data=json.dumps({"value": "world"}),
                                     headers=headers)
        time.sleep(2)
        post_request = requests.post('http://app:5000/hello2',
                                     data=json.dumps({"value": "world2"}),
                                     headers=headers)
        time.sleep(2)

        # Maximum Capacity has reached here
        get_request = requests.get(
            'http://app:5000/hello')  # Reads from Redis Instance
        key_rank = redis_db.zscore(CACHE_KEYS, "hello")  # 1.0
        key_rank2 = redis_db.zscore(CACHE_KEYS, "hello2")  # 0.0

        post_request = requests.post('http://app:5000/hello3',
                                     json={'value': 'world'})
        time.sleep(2)
        current_range = redis_db.zrange(CACHE_KEYS, 0, CACHE_SIZE)
        current_keys = redis_db.hkeys(CACHE_STORE)

        self.assertEqual([b'hello', b'hello3'], current_keys)
        self.assertEqual(int(CACHE_SIZE), redis_db.zcard(CACHE_KEYS))
        redis_db.flushall()
        clear_request = requests.get('http://app:5000/clear/hello')
        clear_request = requests.get('http://app:5000/clear/hello2')
Пример #5
0
 def test_get_valid_key_local_cache(self):
     """
     Test to check if get request gets value from local cache instead of REDIS
     Redis ZSCORE Should be same before request
     """
     logging.info(
         "Running Test Case #4 : \nTest to check if get request gets"
         " value from local cache instead of REDIS. \nRedis ZSCORE Should be same before request"
     )
     requests.get('http://app:5000/clear/hello5')
     post_request = requests.post('http://app:5000/hello5',
                                  json={'value': 'world'})
     time.sleep(2)
     get_request = requests.get(
         'http://app:5000/hello5')  # Reads from Redis Instance
     get_request = requests.get(
         'http://app:5000/hello5')  # Reads from from local cache
     key_rank = redis_db.zscore(CACHE_KEYS, "hello5")
     self.assertEqual(1.0, key_rank)  # Reads from
     requests.get('http://app:5000/clear/hello5')
     redis_db.flushall()