├── .gitignore ├── .travis.yml ├── CHANGE.md ├── Makefile ├── README.md ├── RedisLibrary ├── .gitignore ├── RedisLibraryKeywords.py ├── __init__.py └── version.py ├── docs ├── RedisLibrary.html └── index.html ├── generate.py ├── requirements.txt ├── setup.cfg ├── setup.py └── tests ├── __init__.py └── test_RedisLibrary.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | dist 3 | .idea 4 | .cache 5 | .coverage 6 | *egg* 7 | build 8 | htmlcov 9 | result 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "3.9" 4 | install: 5 | - pip install --upgrade pip 6 | - pip install -r requirements.txt 7 | - pip install urllib3==1.26.15 setuptools==70.0.0 twine==5.1.1 #install package for deploy 8 | script: make clean test coverage docs dist 9 | deploy: 10 | provider: pypi 11 | edge: 12 | branch: v1.8.45 13 | user: nottyo 14 | skip_cleanup: true 15 | password: 16 | secure: J3bcyW+HyB+ycBn71elapBrD+UDhlHIdsVs1YT4WB34bfqNIzVMwvBN6utr4ZcYD7k/t4fZoKbP4D7wo3WE2FgWkLQzNyZcj0OUDmKR7ZJKUmH+xQYPPw0zUzxuFg1ceK5d3fG/DhKR/YNQyb+fvVazSg+YK1/GuOOeExWjmAdM6YB15pG0vW21ktZP7645kldv/TQFrR8YORO0VL2VFBNvgzO1P+eLNTjNaEzLO0nWRKDoPkNEI9r5S2R+lodsZI/fcMWD4w8CRSt8UyVe1nj714xB/YVspYgXYNNMJLXfx4WNWx2vQghmHuf9qN8oxGkYGKJ0tt+gGcDt3MebIdAkvtY4kxoUUNHE09zoKfWutB6J7i4lk5R/6/Sg5DUk5BEIJ/UmuEJuFEYzUzLqylV49nr0buT/jaZKddiJWSrNmXfK2ObhSxkaI6xq6SZRIXory+vBiPMJKuuZs48Ks5PdV4n3EQx46cotwUCyRXJMLpkhsisdvBBuMfvP2oGdCLCJnyC88ImQGRbf+GgIEBzptgxo0mOgE+36Bg8rlt9aczUmDYqntqxob78ISBFoVYiqMVVkeYUWelqfVAkIJIpykhSg/A3e3ZF74jpk67wvD+c5vkAAYyHas21oq6GKFJARXrA74HC3C1gKtd3ov6scVG02czKYBjrHffvCusGo= 17 | on: 18 | tags: true 19 | branch: master 20 | -------------------------------------------------------------------------------- /CHANGE.md: -------------------------------------------------------------------------------- 1 | Change Log: `robotframework-redislibrary` 2 | ================================ 3 | 4 | ## Version 1.2.7 5 | **Date:** 01-Oct-2024 6 | - Update travis python 3.7 to 3.9 7 | 8 | ## Version 1.2.6 9 | **Date:** 01-Oct-2024 10 | - Update connect_to_redis_from_url https://github.com/robotframework-thailand/robotframework-redislibrary/pull/38 11 | 12 | ## Version 1.2.4 13 | **Date:** 04-Mar-2022 14 | - add keywords Get Redis Master (Redis Cluster) 15 | 16 | ## Version 1.2.3 17 | **Date:** 18-Aug-2021 18 | - add keywords Get Redis Master (Redis sentinel) 19 | 20 | ## Version 1.0.0 21 | **Date:** 27-Dec-2019 22 | - Fixed import of redis in setup.py 23 | - Fixed keyword 'Get Time To Live In Redis' to handle when get TTL as minus value. 24 | - Remove keyword 'Check If Key Not Exists', please use 'Redis Key Should Not Be Exist' 25 | - Change default expire_time of 'Set To Redis' keyword to 3600 seconds 26 | - Keyword 'Connect To Redis' will support password and SSL 27 | - Add keywords for operate with set in redis 28 | - Add keywords for operate with set in redis 29 | - Add more unit test and now it cover all keywords. 30 | - Add keywords 'Connect To Redis From URL' 31 | 32 | ## Version 0.3 33 | 34 | **Date:** 31-July-2018 35 | 36 | 1. Add new keyword 37 | - get_time_to_live_in_redis_second 38 | - set_to_redis_hash 39 | - delete_from_redis_hash 40 | - redis_key_should_not_be_exist 41 | - redis_hash_key_should_be_exist 42 | - redis_hash_key_should_not_be_exist 43 | 2. Change function name of 'check_if_key_exits' to 'redis_key_should_be_exist' -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean clean-test clean-pyc clean-build docs help 2 | .DEFAULT_GOAL := help 3 | define BROWSER_PYSCRIPT 4 | import os, webbrowser, sys 5 | try: 6 | from urllib import pathname2url 7 | except: 8 | from urllib.request import pathname2url 9 | 10 | webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1]))) 11 | endef 12 | export BROWSER_PYSCRIPT 13 | 14 | define PRINT_HELP_PYSCRIPT 15 | import re, sys 16 | 17 | for line in sys.stdin: 18 | match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) 19 | if match: 20 | target, help = match.groups() 21 | print("%-20s %s" % (target, help)) 22 | endef 23 | export PRINT_HELP_PYSCRIPT 24 | BROWSER := python -c "$$BROWSER_PYSCRIPT" 25 | 26 | help: 27 | @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) 28 | 29 | clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts 30 | 31 | 32 | clean-build: ## remove build artifacts 33 | rm -fr build/ 34 | rm -fr dist/ 35 | rm -fr .eggs/ 36 | find . -name '*.egg-info' -exec rm -fr {} + 37 | find . -name '*.egg' -exec rm -f {} + 38 | 39 | clean-pyc: ## remove Python file artifacts 40 | find . -name '*.pyc' -exec rm -f {} + 41 | find . -name '*.pyo' -exec rm -f {} + 42 | find . -name '*~' -exec rm -f {} + 43 | find . -name '__pycache__' -exec rm -fr {} + 44 | 45 | clean-test: ## remove test and coverage artifacts 46 | rm -fr .tox/ 47 | rm -f .coverage 48 | rm -fr htmlcov/ 49 | 50 | test: ## run tests quickly with the default Python 51 | python setup.py test 52 | 53 | coverage: ## check code coverage quickly with the default Python 54 | pip install coverage 55 | coverage run --source RedisLibrary setup.py test 56 | coverage report -m 57 | coverage html 58 | 59 | docs: ## generate Sphinx HTML documentation, including API docs 60 | rm -f docs/robotframework-redislibrary.rst 61 | rm -f docs/modules.rst 62 | python generate.py 63 | 64 | release: clean ## package and upload a release 65 | python setup.py sdist upload 66 | python setup.py bdist_wheel upload 67 | 68 | dist: clean ## builds source and wheel package 69 | python setup.py sdist 70 | python setup.py bdist_wheel 71 | ls -l dist 72 | 73 | install: clean test coverage docs ## install the package to the active Python's site-packages 74 | python setup.py install 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![StackShare](https://img.shields.io/badge/tech-stack-0690fa.svg?style=flat)](https://stackshare.io/nottyo/robotframework-redislibrary) 2 | [![Build Status](https://travis-ci.org/robotframework-thailand/robotframework-redislibrary.svg?branch=master)](https://travis-ci.org/robotframework-thailand/robotframework-redislibrary) 3 | # RedisLibrary 4 | 5 | `RedisLibrary` is a [Robot Framework](http://www.robotframework.org) test library which provides keywords for manipulating in-memory data stores in [Redis](https://redis.io/) 6 | 7 | [Redis](https://redis.io/) is an open-source software project that implements data structure servers. It is networked, in-memory, and stores keys with optional durability. 8 | 9 | You can add, get, update and delete your data from Redis. The keywords are implemented using [redis-py](https://github.com/andymccurdy/redis-py) 10 | 11 | # Usage 12 | 13 | Install `robotframework-redislibrary` via `pip` command 14 | 15 | ```bash 16 | pip install -U robotframework-redislibrary 17 | ``` 18 | 19 | # Example Test Case 20 | | *** Settings *** | | | | | 21 | | ------------------ | ------------------- | ----------------- | --------------- | --------------- | 22 | | Library | RedisLibrary | | | | 23 | | *** Test Cases *** | | | | | 24 | | TestRedisSample | | | | | 25 | | ${redis_conn}= | Connect To Redis | myredis-dev.com | port=6379 | | 26 | | ${data}= | Get From Redis | ${redis_conn} | BARCODE\|1234567| | 27 | | Should Be Equal As Strings | ${data} | TestExpectedData | | | 28 | | ${obj_to_add}= | Create Dictionary | name=testFullName | | | 29 | | Append To Redis | ${redis_conn} | BARCOE\|1234567 | ${object_to_add}| | 30 | | @{key_list}= | Get All Match Keys | ${redis_conn} | BARCODE* | 1000 | 31 | 32 | # Documentation 33 | For the detail keyword documentation. Go to this following link: 34 | 35 | https://robotframework-thailand.github.io/robotframework-redislibrary/RedisLibrary.html 36 | 37 | # Help & Issues 38 | Mention me on Twitter [@nottyo](https://twitter.com/nottyo) 39 | -------------------------------------------------------------------------------- /RedisLibrary/.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | -------------------------------------------------------------------------------- /RedisLibrary/RedisLibraryKeywords.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from robot.api import logger 3 | from robot.api.deco import keyword 4 | import redis 5 | from redis.sentinel import Sentinel 6 | from redis.cluster import RedisCluster as RedisCluster 7 | from redis.cluster import ClusterNode 8 | __author__ = 'Traitanit Huangsri' 9 | __email__ = 'traitanit.hua@gmail.com' 10 | 11 | 12 | class RedisLibraryKeywords(object): 13 | 14 | @keyword('Get Redis Cluster') 15 | def get_redis_cluster(self, redis_host, redis_port=6379): 16 | """Get from the Redis master's address corresponding. 17 | 18 | Arguments: 19 | - redis_host: hostname or IP address of the Redis server. 20 | - redis_port: Redis port number (default=6379) 21 | 22 | Return cluster detail 23 | 24 | Examples: 25 | | @{cluster_detail}= | Get Redis Cluster | 'redis-dev.com' | 6379 | 26 | """ 27 | try: 28 | 29 | nodes = [ClusterNode(redis_host, redis_port)] 30 | redis_cluster = RedisCluster(startup_nodes=nodes) 31 | 32 | except Exception as ex: 33 | logger.error(str(ex)) 34 | raise Exception(str(ex)) 35 | return redis_cluster 36 | 37 | @keyword('Get Redis Master') 38 | def get_redis_master(self, redis_host, redis_port=26379, service_name=None): 39 | """Get from the Redis master's address corresponding. 40 | 41 | Arguments: 42 | - redis_host: hostname or IP address of the Redis server. 43 | - redis_port: Redis port number (default=6379) 44 | - service_name: Redis master's address corresponding 45 | 46 | Return sentinel detail lists 47 | 48 | Examples: 49 | | @{sentinel_detail}= | Get Redis Master | 'redis-dev.com' | 6379 | 'service-name' | 50 | """ 51 | try: 52 | sentinel = Sentinel([(redis_host, redis_port)], socket_timeout=0.1) 53 | sentinel_detail = sentinel.discover_master(service_name) 54 | 55 | except Exception as ex: 56 | logger.error(str(ex)) 57 | raise Exception(str(ex)) 58 | return sentinel_detail 59 | 60 | @keyword('Connect To Redis') 61 | def connect_to_redis(self, redis_host, redis_port=6379, db=0, redis_password=None, ssl=False, ssl_ca_certs=None): 62 | """Connect to the Redis server. 63 | 64 | Arguments: 65 | - redis_host: hostname or IP address of the Redis server. 66 | - redis_port: Redis port number (default=6379) 67 | - db: Redis keyspace number (default=0) 68 | - redis_password: password for Redis authentication 69 | - ssl: Connect Redis with SSL or not (default is False) 70 | - ssl_ca_certs: CA Certification when connect Redis with SSL 71 | 72 | Return redis connection object 73 | 74 | Examples: 75 | | ${redis_conn}= | Connect To Redis | redis-dev.com | 6379 | redis_password=password | 76 | """ 77 | try: 78 | redis_conn = redis.StrictRedis(host=redis_host, port=redis_port, db=db, 79 | password=redis_password, ssl=ssl, ssl_ca_certs=ssl_ca_certs) 80 | except Exception as ex: 81 | logger.error(str(ex)) 82 | raise Exception(str(ex)) 83 | return redis_conn 84 | 85 | @keyword('Connect To Redis From URL') 86 | def connect_to_redis_from_url(self, redis_url, db=0): 87 | """Connect to the Redis server. 88 | 89 | Arguments: 90 | - redis_url: URL for connect to Redis. (redis://:@:) 91 | - db: Redis keyspace number (default=0) 92 | 93 | Return redis connection object 94 | 95 | Examples: 96 | | ${redis_conn}= | Connect To Redis From URL | redis://admin:adminpassword@redis-dev.com:6379 | 97 | """ 98 | try: 99 | logger.info("Creating Redis Connection using : url=%s " % redis_url) 100 | redis_conn = redis.from_url(redis_url, db=db) 101 | except Exception as ex: 102 | logger.error(str(ex)) 103 | raise Exception(str(ex)) 104 | return redis_conn 105 | 106 | @keyword('Flush All') 107 | def flush_all(self, redis_conn): 108 | """ Delete all keys from Redis 109 | 110 | Arguments: 111 | - redis_conn: Redis connection object 112 | 113 | Examples: 114 | | Flush All | ${redis_conn} | 115 | """ 116 | return redis_conn.flushall() 117 | 118 | @keyword('Delete From Redis') 119 | def delete_from_redis(self, redis_conn, key): 120 | """ Delete data from Redis 121 | 122 | Arguments: 123 | - redis_conn: Redis connection object 124 | - key: String keyword to find. 125 | 126 | Examples: 127 | | Delete From Redis | ${redis_conn} | BARCODE|1234567890 | 128 | """ 129 | return redis_conn.delete(key) 130 | 131 | @keyword('Append To Redis') 132 | def append_to_redis(self, redis_conn, key, value): 133 | """ Append data to Redis. If key doesn't exist, create it with value. 134 | Return the new length of the value at key. 135 | 136 | Arguments: 137 | - redis_conn: Redis connection object. 138 | - key: String key. 139 | - value: String value. 140 | 141 | Examples: 142 | | Append To Redis | ${redis_conn} | BARCODE|1234567890 | ${data} | 143 | 144 | """ 145 | return redis_conn.append(key, value) 146 | 147 | @keyword('Set To Redis') 148 | def set_to_redis(self, redis_conn, key, data, expire_time=3600): 149 | """ Set data to Redis 150 | 151 | Arguments: 152 | - redis_conn: Redis connection object 153 | - key: String keyword to find. 154 | - data: String data 155 | - expire_time: TTL default value is 3600s 156 | 157 | Examples: 158 | | Set To Redis | ${redis_conn} | BARCODE|0000000011 | ${data} | 159 | | Set To Redis | ${redis_conn} | BARCODE|1234567890 | ${data} | expire_time=600 | 160 | """ 161 | return redis_conn.set(key, data, expire_time) 162 | 163 | @keyword('Get From Redis') 164 | def get_from_redis(self, redis_conn, key): 165 | """ Get cached data from Redis 166 | 167 | Arguments: 168 | - redis_conn: Redis connection object 169 | - key: String keyword to find. 170 | 171 | Examples: 172 | | ${data}= | Get From Redis | ${redis_conn} | BARCODE|1234567890 | 173 | """ 174 | return redis_conn.get(key) 175 | 176 | @keyword('Expire Data From Redis') 177 | def expire_data_from_redis(self, redis_conn, key, expire_time=0): 178 | """ Expire items from Redis 179 | 180 | Arguments: 181 | - redis_conn: Redis connection object 182 | - key: String keyword to find. 183 | - expire_time: waiting time to expire data (Default = expire now) 184 | 185 | Examples: 186 | | Expire Data From Redis | ${redis_conn} | BARCODE|1234567890 | 187 | """ 188 | redis_conn.expire(key, expire_time) 189 | 190 | @keyword('Get Time To Live In Redis') 191 | def get_time_to_live_in_redis(self, redis_conn, key): 192 | """ Return time to live in Redis (minutes) 193 | 194 | Arguments: 195 | - redis_conn: Redis connection object 196 | - key: String keyword to find. 197 | 198 | Examples: 199 | | Get Time To Live In Redis | ${redis_conn} | BARCODE|1234567890 | 200 | """ 201 | ttl = redis_conn.ttl(key) 202 | if ttl > 0: 203 | return redis_conn.ttl(key) / 60 204 | else: 205 | return redis_conn.ttl(key) 206 | 207 | @keyword('Get Time To Live In Redis Second') 208 | def get_time_to_live_in_redis_second(self, redis_conn, key): 209 | """ Return time to live in Redis (seconds) 210 | 211 | Arguments: 212 | - redis_conn: Redis connection object 213 | - key: String keyword to find. 214 | 215 | Examples: 216 | | Get Time To Live In Redis Second | ${redis_conn} | BARCODE|1234567890 | 217 | """ 218 | return redis_conn.ttl(key) 219 | 220 | @keyword('Redis Key Should Be Exist') 221 | def redis_key_should_be_exist(self, redis_conn, key): 222 | """ Keyword will fail if specify key doesn't exist in Redis 223 | 224 | Arguments: 225 | - redis_conn: Redis connection object 226 | - key: String keyword to find. 227 | 228 | Examples: 229 | | Redis Key Should Be Exist | ${redis_conn} | BARCODE|1234567890 | 230 | """ 231 | if not redis_conn.exists(key): 232 | logger.error("Key: " + key + " doesn't exist in Redis.") 233 | raise AssertionError 234 | 235 | @keyword('Redis Key Should Not Be Exist') 236 | def redis_key_should_not_be_exist(self, redis_conn, key): 237 | """ Keyword will fail if specify key exist in Redis 238 | 239 | Arguments: 240 | - redis_conn: Redis connection object 241 | - key: String keyword to find. 242 | 243 | Examples: 244 | | Redis Key Should Not Be Exist | ${redis_conn} | BARCODE|1234567890 | 245 | """ 246 | if redis_conn.exists(key): 247 | logger.error("Key: " + key + " exists in Redis.") 248 | raise AssertionError 249 | 250 | @keyword('Get Dictionary From Redis Hash') 251 | def get_dict_from_redis_hash(self, redis_conn, hash_name): 252 | """ Get cached data from Redis hashes 253 | 254 | Arguments: 255 | - redis_conn: Redis connection object 256 | - hash_name: Hash name. 257 | 258 | Examples: 259 | | ${data}= | Get Dictionary From Redis Hash | ${redis_conn} | HASHNAME | 260 | """ 261 | return redis_conn.hgetall(hash_name) 262 | 263 | @keyword('Get From Redis Hash') 264 | def get_from_redis_hash(self, redis_conn, hash_name, key): 265 | """ Get cached data from Redis hashes by key 266 | 267 | Arguments: 268 | - redis_conn: Redis connection object 269 | - hash_name: Hash name. 270 | - key: String keyword to find. 271 | 272 | Examples: 273 | | ${data}= | Get From Redis Hash | ${redis_conn} | HASHNAME | BARCODE|1234567890 | 274 | """ 275 | return redis_conn.hget(hash_name, key) 276 | 277 | @keyword('Set To Redis Hash') 278 | def set_to_redis_hash(self, redis_conn, hash_name, key, data): 279 | """ Set data to Redis within Hash 280 | 281 | Arguments: 282 | - redis_conn: Redis connection object 283 | - hash_name: String hash 284 | - key: String keyword to find. 285 | - data: String data 286 | 287 | Examples: 288 | | Set To Redis Hash | ${redis_conn} | HASHNAME | key | value | 289 | """ 290 | return redis_conn.hset(hash_name, key, data) 291 | 292 | @keyword('Add Hash Map To Redis') 293 | def add_hash_map_to_redis(self, redis_conn, hash_name, dict_data): 294 | """ Set data to Redis within Hash 295 | 296 | Arguments: 297 | - redis_conn: Redis connection object 298 | - hash_name: String hash 299 | - dict_data: data as dict 300 | 301 | Examples: 302 | | Add Hash Map To Redis | ${redis_conn} | HASHNAME | {"name":"Fred","age":25} | 303 | """ 304 | return redis_conn.hmset(hash_name, dict_data) 305 | 306 | @keyword('Delete From Redis Hash') 307 | def delete_from_redis_hash(self, redis_conn, hash_name, key): 308 | """Delete ``key`` from hash ``name`` 309 | 310 | Arguments: 311 | - redis_conn: Redis connection object. 312 | - hash_name: Hash keyword to find. 313 | - key: String keyword to find. 314 | 315 | Examples: 316 | | Delete From Redis Hash | ${redis_conn} | HASHNAME | KEY | 317 | """ 318 | return redis_conn.hdel(hash_name, key) 319 | 320 | @keyword('Redis Hash Key Should Be Exist') 321 | def redis_hash_key_should_be_exist(self, redis_conn, hash_name, key): 322 | """ Keyword will fail if specify hash key doesn't exist in Redis 323 | 324 | Arguments: 325 | - redis_conn: Redis connection object 326 | - hash_name: Hash name. 327 | - key: String keyword to find. 328 | 329 | Examples: 330 | | Redis Hash Key Should Be Exist | ${redis_conn} | BARCODE|1234567890 | 331 | """ 332 | if not redis_conn.hexists(hash_name, key): 333 | logger.error("Hash: " + hash_name + " and Key: " + 334 | key + " doesn't exist in Redis.") 335 | raise AssertionError 336 | 337 | @keyword('Redis Hash Key Should Not Be Exist') 338 | def redis_hash_key_should_not_be_exist(self, redis_conn, hash_name, key): 339 | """ Keyword will fail if specify hash key exist in Redis 340 | 341 | Arguments: 342 | - redis_conn: Redis connection object 343 | - hash_name: Hash name. 344 | - key: String keyword to find. 345 | Examples: 346 | | Redis Hash Key Should Not Be Exist | ${redis_conn} | BARCODE|1234567890 | 347 | """ 348 | if redis_conn.hexists(hash_name, key): 349 | logger.error("Hash: " + hash_name + " and Key: " + 350 | key + " exists in Redis.") 351 | raise AssertionError 352 | 353 | @keyword('Get Set From Redis Set') 354 | def get_set_from_redis_set(self, redis_conn, set_name): 355 | """ Get cached members from Redis sets. 356 | 357 | Arguments: 358 | - redis_conn: Redis connection object 359 | - set_name: Set name to find. 360 | 361 | Examples: 362 | | ${data}= | Get Set From Redis Set | ${redis_conn} | Fruit | 363 | """ 364 | return redis_conn.smembers(set_name) 365 | 366 | @keyword('Add Set Data To Redis Set') 367 | def add_set_data_to_redis_set(self, redis_conn, set_name, *args): 368 | """ Add set data into Redis. 369 | 370 | Arguments: 371 | - redis_conn: Redis connection object 372 | - set_name: Set name as key in redis 373 | - *args: Item that you need to put in set 374 | 375 | Examples: 376 | | Add Set Data To Redis Set | ${redis_conn} | Fruit | Banana | Apple | Orage | 377 | """ 378 | return redis_conn.sadd(set_name, *args) 379 | 380 | @keyword('Item Should Exist In Redis Set') 381 | def item_should_exist_in_redis_set(self, redis_conn, set_name, item): 382 | """ Check item should exist in set. 383 | 384 | Arguments: 385 | - redis_conn: Redis connection object 386 | - set_name: Set name as key in redis 387 | - Item: Item that you need check 388 | 389 | Examples: 390 | | Item Should Exist In Redis Set | ${redis_conn} | Fruit | Apple | 391 | """ 392 | if not redis_conn.sismember(set_name, item): 393 | logger.error("Item: " + item + " doesn't exist in Redis.") 394 | raise AssertionError 395 | 396 | @keyword('Item Should Not Exist In Redis Set') 397 | def item_should_not_exist_in_redis_set(self, redis_conn, set_name, item): 398 | """ Check item should not exist in set. 399 | 400 | Arguments: 401 | - redis_conn: Redis connection object 402 | - set_name: Set name as key in redis 403 | - Item: Item that you need check 404 | 405 | Examples: 406 | | Item Should Not Exist In Redis Set | ${redis_conn} | Fruit | Mongo | 407 | """ 408 | if redis_conn.sismember(set_name, item): 409 | logger.error("Item: " + item + " exists in Redis.") 410 | raise AssertionError 411 | 412 | @keyword('Get Length of Redis Set') 413 | def get_length_of_redis_set(self, redis_conn, set_name): 414 | """ Get length of set. 415 | 416 | Arguments: 417 | - redis_conn: Redis connection object 418 | - set_name: Set name as key in redis 419 | 420 | Examples: 421 | | ${set_length} | Get Length of Redis Set | ${redis_conn} | Fruit | 422 | """ 423 | return redis_conn.scard(set_name) 424 | 425 | @keyword('Delete Set Data In Redis Set') 426 | def delete_set_data_in_redis_set(self, redis_conn, set_name, *args): 427 | """ Delete set of item from set. 428 | 429 | If you need to delete set from redis use 'Delete From Redis' keyword. 430 | 431 | Arguments: 432 | - redis_conn: Redis connection object 433 | - set_name: Set name as key in redis 434 | - *args: Item that you need to delete from set 435 | 436 | Examples: 437 | | Delete Set Data In Redis Set | ${redis_conn} | Fruit | Banana | Orage | 438 | """ 439 | return redis_conn.srem(set_name, *args) 440 | 441 | @keyword('Push Item To First Index In List Redis') 442 | def push_item_to_first_index_in_list_redis(self, redis_conn, list_name, *args): 443 | """ Push item to first index in list. If you many arguments, last arguments will be the first item. 444 | 445 | Arguments: 446 | - redis_conn: Redis connection object 447 | - list_name: List name as key in redis 448 | - *args: Item that you need to put in set 449 | 450 | Examples: 451 | | Push Item To First Index In List Redis | ${redis_conn} | Country | Germany | Italy | France | Spain | 452 | | ${list_items} | Get All Item From List Redis | ${redis_conn} | Country | 453 | 454 | Result from ``Get All Item From List Redis``: [b'Spain', b'France', b'Italy', b'Germany'] 455 | """ 456 | return redis_conn.lpush(list_name, *args) 457 | 458 | @keyword('Push Item To Last Index In List Redis') 459 | def push_item_to_last_index_in_list_redis(self, redis_conn, list_name, *args): 460 | """ Push item to last index in list. If you many arguments, last arguments will be the last item. 461 | 462 | Arguments: 463 | - redis_conn: Redis connection object 464 | - list_name: List name as key in redis 465 | - *args: Item that you need to put in set 466 | 467 | Examples: 468 | | Push Item To Last Index In List Redis | ${redis_conn} | Country | Germany | Italy | France | Spain | 469 | | ${list_items} | Get All Item From List Redis | ${redis_conn} | Country | 470 | 471 | Result from ``Get All Item From List Redis``: [b'Germany', b'Italy', b'France', b'Spain'] 472 | """ 473 | return redis_conn.rpush(list_name, *args) 474 | 475 | @keyword('Update Item In List Redis') 476 | def update_item_in_list_redis(self, redis_conn, list_name, index, item): 477 | """Update item in list by specific index. 478 | 479 | Arguments: 480 | - redis_conn: Redis connection object 481 | - list_name: List name as key in redis 482 | - index: Index in list that you need to update 483 | - item: New item 484 | 485 | Examples: 486 | | Update Item In List Redis | ${redis_conn} | Country | 1 | England | 487 | """ 488 | return redis_conn.lset(list_name, index, item) 489 | 490 | @keyword('Get Item From List Redis') 491 | def get_item_from_list_redis(self, redis_conn, list_name, index): 492 | """Get item in list by specific index. 493 | 494 | Arguments: 495 | - redis_conn: Redis connection object 496 | - list_name: List name as key in redis 497 | - index: Index in list that you need to update 498 | 499 | Examples: 500 | | ${item_data} | Get Item From List Redis | ${redis_conn} | Country | 1 | 501 | """ 502 | return redis_conn.lindex(list_name, index) 503 | 504 | @keyword('Get All Item From List Redis') 505 | def get_all_item_from_list_redis(self, redis_conn, list_name): 506 | """Get all items in list. 507 | 508 | Arguments: 509 | - redis_conn: Redis connection object 510 | - list_name: List name as key in redis 511 | 512 | Examples: 513 | | ${list_items} | Get All Item From List Redis | ${redis_conn} | Country | 514 | """ 515 | return redis_conn.lrange(list_name, 0, -1) 516 | 517 | @keyword('Get Length From List Redis') 518 | def get_length_from_list_redis(self, redis_conn, list_name): 519 | """Get length of list. 520 | 521 | Arguments: 522 | - redis_conn: Redis connection object 523 | - list_name: List name as key in redis 524 | 525 | Examples: 526 | | ${list_length} | Get Length From List Redis | ${redis_conn} | Country | 527 | """ 528 | return redis_conn.llen(list_name) 529 | 530 | @keyword('Get Index of Item From List Redis') 531 | def get_index_of_item_from_list_redis(self, redis_conn, list_name, item): 532 | """Get indexs of item that metched in list. 533 | 534 | Arguments: 535 | - redis_conn: Redis connection object 536 | - list_name: List name as key in redis 537 | - item: Search item 538 | 539 | Examples: 540 | | ${list_index} | Get Index of Item From List Redis | ${redis_conn} | Country | Germany | 541 | 542 | Keyword will return result as list of index: [0, 1, 5] 543 | """ 544 | return [i for i, j in enumerate(redis_conn.lrange(list_name, 0, -1)) if j == str.encode(item)] 545 | 546 | @keyword('Get All Match Keys') 547 | def get_all_match_keys(self, redis_conn, key, count=100): 548 | """ Get all key that matches with specific keyword 549 | 550 | Arguments: 551 | - redis_conn: Redis connection object 552 | - key: String keyword to find may contain wildcard. 553 | - count: Element number of returns 554 | 555 | Examples: 556 | | @{key_list}= | Get All Match Keys | ${redis_conn} | BARCODE* | 1000 | 557 | """ 558 | return redis_conn.scan(0, key, count)[1] 559 | 560 | @keyword('Delete Item From List Redis') 561 | def delete_item_from_list_redis(self, redis_conn, list_name, index, item=None): 562 | """Delete data from list by specific index. 563 | 564 | Arguments: 565 | - redis_conn: Redis connection object 566 | - list_name: List name as key in redis 567 | - index: Index in list that you need to delete 568 | - item: Compare item. If it is None, keyword will not compare with item in index. 569 | But if is not None, keyword will compare it with item in index before delete. 570 | If not matched keyword will failed. 571 | 572 | Examples 1: 573 | | Delete Item From List Redis | ${redis_conn} | Country | 2 | 574 | 575 | Examples 2: keyword will compare it with item in index before delete. 576 | If not matched keyword will failed 577 | | Delete Item From List Redis | ${redis_conn} | Country | 2 | Spain | 578 | """ 579 | if item is not None: 580 | if not redis_conn.lindex(list_name, index) == str.encode(item): 581 | logger.error("Item: " + item + " not matched with index item in list.") 582 | raise AssertionError 583 | redis_conn.lset(list_name, index, 'DELETE_ITEM') 584 | redis_conn.lrem(list_name, 1, 'DELETE_ITEM') 585 | -------------------------------------------------------------------------------- /RedisLibrary/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from .RedisLibraryKeywords import RedisLibraryKeywords 3 | from .version import VERSION 4 | 5 | __author__ = 'Traitanit Huangsri' 6 | __email__ = 'traitanit.hua@gmail.com' 7 | 8 | 9 | class RedisLibrary(RedisLibraryKeywords): 10 | """ 11 | `RedisLibrary` is a [http://www.robotframework.org|Robot Framework] test library which provides keywords for manipulating in-memory data stores in [https://redis.io/|Redis] 12 | 13 | Redis is an open-source software project that implements data structure servers. It is networked, in-memory, and stores keys with optional durability. 14 | 15 | You can add, get, update and delete your data from Redis. The keywords are implemented using [https://github.com/andymccurdy/redis-py|redis-py] 16 | 17 | *Usage* 18 | 19 | Install `robotframework-redislibrary` via `pip` command 20 | 21 | ``pip install -U robotframework-redislibrary`` 22 | 23 | Here is a sample 24 | 25 | | ***** Settings ***** | 26 | | Library | RedisLibrary | 27 | | ***** Test Cases ***** | 28 | | Get Requests | 29 | | | ${redis_conn} | Connect To Redis | ${redis_host} | ${redis_port} | db=${0} | 30 | | | Set Test Variable | ${redis_key} | 1234567890 | 31 | | | Set Test Variable | ${redis_data} | {"data":{"message":"Hello Worlds!!!"}} | 32 | | | Append To Redis | ${redis_conn} | ${redis_key} | ${redis_data} | 33 | | | ${response_data} | Get From Redis | ${redis_conn} | ${redis_key} | 34 | | | Should Be Equal as Strings | ${response_data} | ${redis_data} | 35 | | | Redis Key Should Be Exist | ${redis_conn} | ${redis_key} | 36 | | | Delete From Redis | ${redis_conn} | ${redis_key} | 37 | | | Redis Key Should Not Be Exist | ${redis_conn} | ${redis_key} | 38 | | | @{key_list}= | Get All Match Keys | ${redis_conn} | BARCODE* | 1000 | 39 | 40 | References: 41 | 42 | + Redis-Py Documentation - https://redis-py.readthedocs.io/en/latest/ 43 | """ 44 | ROBOT_LIBRARY_SCOPE = "GLOBAL" 45 | ROBOT_LIBRARY_DOC_FORMAT = "ROBOT" 46 | ROBOT_LIBRARY_VERSION = VERSION 47 | -------------------------------------------------------------------------------- /RedisLibrary/version.py: -------------------------------------------------------------------------------- 1 | # Update this before release 2 | VERSION = "1.2.7" 3 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Redirect Page... 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /generate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from os.path import join, dirname 3 | try: 4 | from robot.libdoc import libdoc 5 | except: 6 | def main(): 7 | print("""Robot Framework 2.7 or later required for generating documentation""") 8 | else: 9 | def main(): 10 | libdoc(join(dirname(__file__),'RedisLibrary'), 11 | join(dirname(__file__),'docs','RedisLibrary.html')) 12 | 13 | if __name__ == '__main__': 14 | main() 15 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | robotframework>=3.0 2 | redis==4.1.0 -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from setuptools import setup, find_packages 5 | from os.path import abspath, dirname, join 6 | 7 | # Get version 8 | version_file = join(dirname(abspath(__file__)), 'RedisLibrary', 'version.py') 9 | with open(version_file) as file: 10 | code = compile(file.read(), version_file, 'exec') 11 | exec(code) 12 | 13 | requirements = [ 14 | 'robotframework>=3.0', 15 | 'redis>=2.10.5' 16 | ] 17 | 18 | test_requirements = [ 19 | 'tox', 20 | 'coverage', 21 | 'fakeredis==0.8.2' 22 | ] 23 | 24 | CLASSIFIERS = """ 25 | Development Status :: 5 - Production/Stable 26 | License :: Public Domain 27 | Operating System :: OS Independent 28 | Programming Language :: Python 29 | Topic :: Software Development :: Testing 30 | """[1:-1] 31 | 32 | with open("README.md", "r") as fh: 33 | long_description = fh.read() 34 | 35 | setup( 36 | name='robotframework-redislibrary', 37 | version=VERSION, 38 | description="robotframework-redislibrary is a Robot Framework test library for manipulating in-memory data which store in Redis", 39 | long_description=long_description, 40 | long_description_content_type="text/markdown", 41 | author="Traitanit Huangsri", 42 | author_email='traitanit.hua@gmail.com', 43 | url='https://github.com/robotframework-thailand/robotframework-redislibrary.git', 44 | packages=[ 45 | 'RedisLibrary' 46 | ], 47 | package_dir={'robotframework-redislibrary': 48 | 'RedisLibrary'}, 49 | include_package_data=True, 50 | install_requires=requirements, 51 | zip_safe=False, 52 | keywords='robotframework redislibrary redis', 53 | classifiers=CLASSIFIERS.splitlines(), 54 | test_suite='tests', 55 | tests_require=test_requirements 56 | ) 57 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | -------------------------------------------------------------------------------- /tests/test_RedisLibrary.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = 'Traitanit Huangsri' 4 | __email__ = 'traitanit.hua@gmail.com' 5 | 6 | from RedisLibrary import RedisLibrary 7 | import unittest, fakeredis, ast 8 | 9 | 10 | class RedisLibraryTest(unittest.TestCase): 11 | redis = None 12 | fake_redis = None 13 | 14 | def setUp(self): 15 | self.redis = RedisLibrary() 16 | self.fake_redis = fakeredis.FakeStrictRedis() 17 | self.fake_redis.set('name', 'nottyo', px=300000) 18 | self.fake_redis.set('home_address', '1111', ex=600000) 19 | 20 | def test_flush_all(self): 21 | self.redis.flush_all(self.fake_redis) 22 | home_address = self.redis.get_from_redis(self.fake_redis, 'home_address') 23 | name = self.redis.get_from_redis(self.fake_redis, 'name') 24 | self.assertIsNone(home_address) 25 | self.assertIsNone(name) 26 | 27 | def test_delete_from_redis(self): 28 | self.redis.delete_from_redis(self.fake_redis, 'home_address') 29 | home_address = self.redis.get_from_redis(self.fake_redis, 'home_address') 30 | self.assertIsNone(home_address) 31 | 32 | def test_delete_from_redis_no_key(self): 33 | self.redis.delete_from_redis(self.fake_redis, 'no_key') 34 | 35 | def test_append_to_redis(self): 36 | detail_address = {'city': 'Bangkok', 'country': 'Thailand'} 37 | self.redis.append_to_redis(self.fake_redis, 'detail_address', str(detail_address)) 38 | data = self.redis.get_from_redis(self.fake_redis, 'detail_address').decode('UTF-8') 39 | self.assertDictEqual(ast.literal_eval(data), detail_address) 40 | 41 | def test_set_to_redis(self): 42 | self.redis.set_to_redis(self.fake_redis, 'home_address', '2222') 43 | data = self.redis.get_from_redis(self.fake_redis, 'home_address').decode('UTF-8') 44 | self.assertEqual(data, '2222') 45 | 46 | def test_get_from_redis(self): 47 | self.assertEqual(self.redis.get_from_redis(self.fake_redis, 'home_address'), b'1111') 48 | 49 | def test_get_from_redis_key_not_exists(self): 50 | self.assertEqual(self.redis.get_from_redis(self.fake_redis, 'key_not_exists'), None) 51 | 52 | def test_expire_data_from_redis(self): 53 | self.redis.expire_data_from_redis(self.fake_redis, 'home_address', expire_time=0) 54 | home_address = self.redis.get_from_redis(self.fake_redis, 'home_address') 55 | self.assertIsNone(home_address) 56 | 57 | def test_get_time_to_live_in_redis(self): 58 | ttl = self.redis.get_time_to_live_in_redis(self.fake_redis, 'name') 59 | self.assertEqual(ttl, 5) 60 | 61 | def test_get_time_to_live_in_redis_key_not_exists(self): 62 | self.assertEqual(self.redis.get_time_to_live_in_redis(self.fake_redis, 'no_key'), -2) 63 | 64 | def test_get_time_to_live_in_redis_second(self): 65 | self.fake_redis.set('year', '2020', 3600) 66 | ttl = self.redis.get_time_to_live_in_redis_second(self.fake_redis, 'name') 67 | self.assertEqual(ttl, 300) 68 | 69 | def test_get_time_to_live_in_redis_second_key_not_exists(self): 70 | self.assertEqual(self.redis.get_time_to_live_in_redis_second(self.fake_redis, 'no_key'), -2) 71 | 72 | def test_redis_key_should_be_exist_success(self): 73 | self.redis.redis_key_should_be_exist(self.fake_redis, 'name') 74 | 75 | def test_redis_key_should_be_exist_failed(self): 76 | with self.assertRaises(AssertionError): 77 | self.redis.redis_key_should_be_exist(self.fake_redis, 'non_existing_key') 78 | 79 | def test_redis_key_should_not_be_exist_success(self): 80 | self.redis.redis_key_should_not_be_exist(self.fake_redis, 'non_existing_key') 81 | 82 | def test_redis_key_should_not_be_exist_failed(self): 83 | with self.assertRaises(AssertionError): 84 | self.redis.redis_key_should_not_be_exist(self.fake_redis, 'name') 85 | 86 | def test_get_dict_from_redis_hash(self): 87 | self.redis.set_to_redis_hash(self.fake_redis, '7498d0b2', 'star_01', 'Pluto') 88 | self.redis.set_to_redis_hash(self.fake_redis, '7498d0b2', 'star_02', 'Mars') 89 | self.assertEqual(self.redis.get_dict_from_redis_hash(self.fake_redis, '7498d0b2'), {b'star_01': b'Pluto', b'star_02': b'Mars'}) 90 | 91 | def test_get_dict_from_redis_hash_key_not_exists(self): 92 | self.assertEqual(self.redis.get_dict_from_redis_hash(self.fake_redis, '7498d0b2'), {}) 93 | 94 | def test_get_from_redis_hash(self): 95 | self.redis.add_hash_map_to_redis(self.fake_redis, '7498d0b2', {'star_01':'Pluto', 'star_02':'Mars'}) 96 | self.assertEqual(self.redis.get_from_redis_hash(self.fake_redis, '7498d0b2', 'star_01'), b'Pluto') 97 | self.assertEqual(self.redis.get_from_redis_hash(self.fake_redis, '7498d0b2', 'star_02'), b'Mars') 98 | 99 | def test_get_from_redis_hash_no_key(self): 100 | self.redis.add_hash_map_to_redis(self.fake_redis, '7498d0b2', {'star_01':'Pluto', 'star_02':'Mars'}) 101 | self.assertEqual(self.redis.get_from_redis_hash(self.fake_redis, '7498d0b2', 'star_03'), None) 102 | 103 | def test_get_from_redis_hash_no_hash(self): 104 | self.assertEqual(self.redis.get_from_redis_hash(self.fake_redis, '7498d0b2', 'star_03'), None) 105 | 106 | def test_set_to_redis_hash(self): 107 | self.redis.set_to_redis_hash(self.fake_redis, '7498d0b2', 'star_01', 'Pluto') 108 | self.assertEqual(self.redis.get_from_redis_hash(self.fake_redis, '7498d0b2', 'star_01'), b'Pluto') 109 | 110 | def test_add_hash_map_to_redis(self): 111 | self.redis.add_hash_map_to_redis(self.fake_redis, '7498d0b2', {'star_01':'Saturn','star_02':'Jupiter'}) 112 | self.assertEqual(self.redis.get_dict_from_redis_hash(self.fake_redis, '7498d0b2'), {b'star_01': b'Saturn', b'star_02': b'Jupiter'}) 113 | 114 | def test_delete_from_redis_hash(self): 115 | self.redis.add_hash_map_to_redis(self.fake_redis, '7498d0b2', {'star_01':'Saturn','star_02':'Jupiter'}) 116 | self.redis.delete_from_redis_hash(self.fake_redis, '7498d0b2', 'star_01') 117 | self.assertEqual(self.redis.get_dict_from_redis_hash(self.fake_redis, '7498d0b2'), {b'star_02': b'Jupiter'}) 118 | 119 | def test_delete_from_redis_hash_no_key(self): 120 | self.redis.add_hash_map_to_redis(self.fake_redis, '7498d0b2', {'star_01':'Saturn','star_02':'Jupiter'}) 121 | self.redis.delete_from_redis_hash(self.fake_redis, '7498d0b2', 'star_03') 122 | self.assertEqual(self.redis.get_dict_from_redis_hash(self.fake_redis, '7498d0b2'), {b'star_01': b'Saturn', b'star_02': b'Jupiter'}) 123 | 124 | def test_delete_from_redis_hash_no_hash(self): 125 | self.redis.delete_from_redis_hash(self.fake_redis, 'no_hash', 'star_01') 126 | 127 | def test_redis_hash_key_should_be_exist_success(self): 128 | self.redis.set_to_redis_hash(self.fake_redis, '7498d0b2', 'star_01', 'Pluto') 129 | self.redis.redis_hash_key_should_be_exist(self.fake_redis, '7498d0b2', 'star_01') 130 | 131 | def test_redis_hash_key_should_be_exist_failed_no_key(self): 132 | with self.assertRaises(AssertionError): 133 | self.redis.redis_hash_key_should_be_exist(self.fake_redis, '7498d0b2', 'non_existing_key') 134 | 135 | def test_redis_hash_key_should_be_exist_failed_no_hash(self): 136 | with self.assertRaises(AssertionError): 137 | self.redis.redis_hash_key_should_be_exist(self.fake_redis, 'non_hash', 'star_01') 138 | 139 | def test_redis_hash_key_should_not_be_exist_success_no_key(self): 140 | self.redis.redis_hash_key_should_not_be_exist(self.fake_redis, '7498d0b2', 'non_existing_key') 141 | 142 | def test_redis_hash_key_should_not_be_exist_success_no_hash(self): 143 | self.redis.redis_hash_key_should_not_be_exist(self.fake_redis, 'non_hash', 'star_01') 144 | 145 | def test_redis_hash_key_should_not_be_exist_failed(self): 146 | self.redis.set_to_redis_hash(self.fake_redis, '7498d0b2', 'star_01', 'Pluto') 147 | with self.assertRaises(AssertionError): 148 | self.redis.redis_hash_key_should_not_be_exist(self.fake_redis, '7498d0b2', 'star_01') 149 | 150 | def test_get_set_from_redis_set(self): 151 | self.redis.add_set_data_to_redis_set(self.fake_redis, "fruit", "banana", "apple", "orage") 152 | self.assertEqual(self.redis.get_set_from_redis_set(self.fake_redis, 'fruit'), {b'banana', b'apple', b"orage"}) 153 | 154 | def test_get_set_from_redis_set_key_not_exists(self): 155 | self.assertEqual(self.redis.get_set_from_redis_set(self.fake_redis, 'fruit'), set()) 156 | 157 | def test_item_should_exist_in_redis_set_success(self): 158 | self.redis.add_set_data_to_redis_set(self.fake_redis, "fruit", "banana", "apple", "orage") 159 | self.redis.item_should_exist_in_redis_set(self.fake_redis, "fruit", "apple") 160 | 161 | def test_item_should_exist_in_redis_set_failed(self): 162 | self.redis.add_set_data_to_redis_set(self.fake_redis, "fruit", "banana", "apple", "orage") 163 | with self.assertRaises(AssertionError): 164 | self.redis.item_should_exist_in_redis_set(self.fake_redis, "fruit", "mongo") 165 | 166 | def test_item_should_not_exist_in_redis_set_success(self): 167 | self.redis.add_set_data_to_redis_set(self.fake_redis, "fruit", "banana", "apple", "orage") 168 | with self.assertRaises(AssertionError): 169 | self.redis.item_should_not_exist_in_redis_set(self.fake_redis, "fruit", "apple") 170 | 171 | def test_item_should_not_exist_in_redis_set_failed(self): 172 | self.redis.add_set_data_to_redis_set(self.fake_redis, "fruit", "banana", "apple", "orage") 173 | self.redis.item_should_not_exist_in_redis_set(self.fake_redis, "fruit", "mongo") 174 | 175 | def test_get_length_of_redis_set(self): 176 | self.redis.add_set_data_to_redis_set(self.fake_redis, "fruit", "banana", "apple", "orage") 177 | self.assertEqual(self.redis.get_length_of_redis_set(self.fake_redis, 'fruit'), 3) 178 | 179 | def test_get_length_of_redis_set_key_not_exists(self): 180 | self.assertEqual(self.redis.get_length_of_redis_set(self.fake_redis, 'fruit'), 0) 181 | 182 | def test_delete_set_data_in_redis_set(self): 183 | self.redis.add_set_data_to_redis_set(self.fake_redis, "fruit", "banana", "apple", "orage", "mongo") 184 | self.redis.delete_set_data_in_redis_set(self.fake_redis, "fruit", "banana", "orage") 185 | self.assertEqual(self.redis.get_set_from_redis_set(self.fake_redis, 'fruit'), {b'apple', b'mongo'}) 186 | 187 | def test_delete_set_data_in_redis_set_key_not_exists(self): 188 | self.redis.delete_set_data_in_redis_set(self.fake_redis, "fruit", "banana", "orage") 189 | 190 | def test_push_item_to_first_index_in_list_redis(self): 191 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 'Germany') 192 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 'Italy', 'France', 'Spain') 193 | self.assertEqual(self.redis.get_all_item_from_list_redis(self.fake_redis, 'Country'), 194 | [b'Spain', b'France', b'Italy', b'Germany']) 195 | 196 | def test_push_item_to_last_index_in_list_redis(self): 197 | self.redis.push_item_to_last_index_in_list_redis(self.fake_redis, 'Country', 'Germany') 198 | self.redis.push_item_to_last_index_in_list_redis(self.fake_redis, 'Country', 'Italy', 'France', 'Spain') 199 | self.assertEqual(self.redis.get_all_item_from_list_redis(self.fake_redis, 'Country'), 200 | [b'Germany', b'Italy', b'France', b'Spain']) 201 | 202 | def test_update_item_in_list_redis(self): 203 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 'Germany', 'Italy', 'France', 'Spain') 204 | self.redis.update_item_in_list_redis(self.fake_redis, 'Country', 0, 'France') 205 | self.redis.update_item_in_list_redis(self.fake_redis, 'Country', 2, 'France') 206 | self.assertEqual(self.redis.get_all_item_from_list_redis(self.fake_redis, 'Country'), 207 | [b'France', b'France', b'France', b'Germany']) 208 | 209 | def test_get_item_from_list_redis(self): 210 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 'Germany', 'Italy', 'France', 'Spain') 211 | self.assertEqual(self.redis.get_item_from_list_redis(self.fake_redis, 'Country', 0), 212 | b'Spain') 213 | self.assertEqual(self.redis.get_item_from_list_redis(self.fake_redis, 'Country', 2), 214 | b'Italy') 215 | 216 | def test_get_item_from_list_redis_no_index(self): 217 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 'Germany', 'Italy', 'France', 'Spain') 218 | self.assertEqual(self.redis.get_item_from_list_redis(self.fake_redis, 'Country', 5), None) 219 | 220 | def test_get_item_from_list_redis_no_list(self): 221 | self.assertEqual(self.redis.get_item_from_list_redis(self.fake_redis, 'Country', 5), None) 222 | 223 | def test_get_all_item_from_list_redis(self): 224 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 'Germany', 'Italy', 'France', 'Spain') 225 | self.assertEqual(self.redis.get_all_item_from_list_redis(self.fake_redis, 'Country'), 226 | [b'Spain', b'France', b'Italy', b'Germany']) 227 | 228 | def test_get_all_item_from_list_redis_empty_list(self): 229 | self.redis.push_item_to_last_index_in_list_redis(self.fake_redis, 'Country', 'Germany') 230 | self.fake_redis.lrem('Country', 1, 'Germany') 231 | self.assertEqual(self.redis.get_all_item_from_list_redis(self.fake_redis, 'Country'), []) 232 | 233 | def test_get_all_item_from_list_redis_no_list(self): 234 | self.assertEqual(self.redis.get_all_item_from_list_redis(self.fake_redis, 'Country'), []) 235 | 236 | def test_get_length_from_list_redis(self): 237 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 'Germany', 'Italy', 'France', 'Spain') 238 | self.assertEqual(self.redis.get_length_from_list_redis(self.fake_redis, 'Country'), 4) 239 | 240 | def test_get_length_from_list_redis_empty_list(self): 241 | self.redis.push_item_to_last_index_in_list_redis(self.fake_redis, 'Country', 'Germany') 242 | self.fake_redis.lrem('Country', 1, 'Germany') 243 | self.assertEqual(self.redis.get_length_from_list_redis(self.fake_redis, 'Country'), 0) 244 | 245 | def test_get_length_from_list_redis_no_list(self): 246 | self.assertEqual(self.redis.get_length_from_list_redis(self.fake_redis, 'Country'), 0) 247 | 248 | def test_get_index_of_item_from_list_redis(self): 249 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 250 | 'Germany', 'Italy', 'France', 'Spain', 'Germany', 'Germany') 251 | self.assertEqual(self.redis.get_index_of_item_from_list_redis(self.fake_redis, 'Country', 'Germany'), 252 | [0, 1, 5]) 253 | 254 | def test_get_index_of_item_from_list_redis_no_item(self): 255 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 256 | 'Germany', 'Italy', 'France', 'Spain', 'Germany', 'Germany') 257 | self.assertEqual(self.redis.get_index_of_item_from_list_redis(self.fake_redis, 'Country', 'England'), []) 258 | 259 | def test_get_index_of_item_from_list_redis_empty_list(self): 260 | self.redis.push_item_to_last_index_in_list_redis(self.fake_redis, 'Country', 'Germany') 261 | self.fake_redis.lrem('Country', 1, 'Germany') 262 | self.assertEqual(self.redis.get_index_of_item_from_list_redis(self.fake_redis, 'Country', 'Germany'), []) 263 | 264 | def test_get_index_of_item_from_list_redis_no_list(self): 265 | self.assertEqual(self.redis.get_index_of_item_from_list_redis(self.fake_redis, 'Country', 'Germany'), []) 266 | 267 | def test_get_all_match_keys(self): 268 | self.fake_redis.set('CountryAsia', 'Thailand', px=300000) 269 | self.fake_redis.set('CountryEurope', 'Germany', px=300000) 270 | self.assertEqual(self.redis.get_all_match_keys(self.fake_redis, '*'), sorted([b'name', b'home_address', b'CountryAsia', b'CountryEurope'])) 271 | 272 | def test_get_all_match_keys_with_count_1(self): 273 | self.fake_redis.set('CountryAsia', 'Thailand', px=300000) 274 | self.fake_redis.set('CountryEurope', 'Germany', px=300000) 275 | self.assertEqual(self.redis.get_all_match_keys(self.fake_redis, '*', 1), sorted([b'name', b'home_address', b'CountryAsia', b'CountryEurope'])[:1]) 276 | 277 | def test_get_all_match_keys_with_empty_key(self): 278 | self.fake_redis.set('CountryAsia', 'Thailand', px=300000) 279 | self.fake_redis.set('CountryEurope', 'Germany', px=300000) 280 | self.assertEqual(self.redis.get_all_match_keys(self.fake_redis, ''), sorted([b'name', b'home_address', b'CountryAsia', b'CountryEurope'])) 281 | 282 | def test_get_all_match_keys_with_wildcard(self): 283 | self.fake_redis.set('CountryAsia', 'Thailand', px=300000) 284 | self.fake_redis.set('CountryEurope', 'Germany', px=300000) 285 | self.assertEqual(self.redis.get_all_match_keys(self.fake_redis, 'Country*'), sorted([b'CountryAsia', b'CountryEurope'])) 286 | 287 | def test_get_all_match_keys_with_wildcard_and_count_0(self): 288 | self.fake_redis.set('CountryAsia', 'Thailand', px=300000) 289 | self.fake_redis.set('CountryEurope', 'Germany', px=300000) 290 | self.assertEqual(self.redis.get_all_match_keys(self.fake_redis, 'Country*', 0), sorted([b'CountryAsia', b'CountryEurope'])) 291 | 292 | def test_get_all_match_keys_without_wildcard(self): 293 | self.fake_redis.set('CountryAsia', 'Thailand', px=300000) 294 | self.fake_redis.set('CountryEurope', 'Germany', px=300000) 295 | self.fake_redis.set('CountryAsiaAndEurope', 'Germany', px=300000) 296 | self.assertEqual(self.redis.get_all_match_keys(self.fake_redis, 'CountryAsia'), [b'CountryAsia']) 297 | 298 | def test_get_all_match_keys_empty_list(self): 299 | self.assertEqual(self.redis.get_all_match_keys(self.fake_redis, 'Country*'), []) 300 | 301 | def test_delete_item_from_list_redis(self): 302 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 'Germany', 'Italy', 'France', 'Spain') 303 | self.redis.delete_item_from_list_redis(self.fake_redis, 'Country', 2, 'Italy') 304 | self.assertEqual(self.redis.get_all_item_from_list_redis(self.fake_redis, 'Country'), 305 | [b'Spain', b'France', b'Germany']) 306 | 307 | def test_delete_item_from_list_redis_item_is_null(self): 308 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 'Germany', 'Italy', 'France', 'Spain') 309 | self.redis.delete_item_from_list_redis(self.fake_redis, 'Country', 2) 310 | self.assertEqual(self.redis.get_all_item_from_list_redis(self.fake_redis, 'Country'), 311 | [b'Spain', b'France', b'Germany']) 312 | 313 | def test_delete_item_from_list_redis_item_not_matched(self): 314 | self.redis.push_item_to_first_index_in_list_redis(self.fake_redis, 'Country', 'Germany', 'Italy', 'France', 'Spain') 315 | with self.assertRaises(AssertionError): 316 | self.redis.delete_item_from_list_redis(self.fake_redis, 'Country', 2, 'Spain') 317 | 318 | def tearDown(self): 319 | self.fake_redis.flushall() 320 | --------------------------------------------------------------------------------