如何在python中模拟redis?

问题描述

我想在处理程序函数中模拟并修补这个 redis_client 以进行单元测试。

def handler(event,context):
    try:
        criteria = event["queryParams"]
        msg = "Search count initiated"
  **redis_client = redis_util.RedisUtil(os.environ.get('redis_host'),int(os.environ.get('redis_port')))**     
        name = criteria.get('name','')
        name = name.replace('"',"'")```

here redis_util is a local module that contain code mentioned below

RedisUtil 类: sqs = 无

def __init__(self,host,port):
    """
    The AWS SQS Util constructor
    :param host: Redis host
    :param poart: Redis port
    """
    self.redis_db = redis.Redis(host=host,port=port,db=0)

def get_redis(self):
    return self.redis_db
Thanks

解决方法

你可以试试这个

|    |   index |   case | action   |
|---:|--------:|-------:|:---------|
|  0 |       0 |      1 | A        |
|  1 |       1 |      1 | B        |
|  2 |       2 |      1 | C        |
|  3 |       3 |      1 | B        |
|  4 |       4 |      1 | D        |
|  5 |       6 |      2 | B        |
|  6 |       7 |      2 | B        |
|  7 |       8 |      2 | D        |
|  8 |      10 |      6 | B        |
|  9 |      11 |      6 | B        |
| 10 |      12 |      6 | D        |
| 11 |      13 |      6 | A        |
| 12 |      14 |      6 | F        |
| 13 |      15 |      3 | A        |
| 14 |      16 |      3 | B        |
| 15 |      17 |      3 | C        |
| 16 |      18 |      3 | B        |
| 17 |      19 |      3 | D        |