├── tests
├── __init__.py
└── test_RedisLibrary.py
├── requirements.txt
├── setup.cfg
├── RedisLibrary
├── version.py
├── .gitignore
├── __init__.py
└── RedisLibraryKeywords.py
├── .gitignore
├── docs
└── index.html
├── generate.py
├── .travis.yml
├── CHANGE.md
├── setup.py
├── Makefile
└── README.md
/tests/__init__.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | robotframework>=3.0
2 | redis==4.1.0
--------------------------------------------------------------------------------
/setup.cfg:
--------------------------------------------------------------------------------
1 | [metadata]
2 | description-file = README.md
3 |
--------------------------------------------------------------------------------
/RedisLibrary/version.py:
--------------------------------------------------------------------------------
1 | # Update this before release
2 | VERSION = "1.2.7"
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 | dist
3 | .idea
4 | .cache
5 | .coverage
6 | *egg*
7 | build
8 | htmlcov
9 | result
10 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/.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'
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 | [](https://stackshare.io/nottyo/robotframework-redislibrary)
2 | [](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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 | ssl_cert_reqs="required"):
63 | """Connect to the Redis server.
64 |
65 | Arguments:
66 | - redis_host: hostname or IP address of the Redis server.
67 | - redis_port: Redis port number (default=6379)
68 | - db: Redis keyspace number (default=0)
69 | - redis_password: password for Redis authentication
70 | - ssl: Connect Redis with SSL or not (default is False)
71 | - ssl_ca_certs: CA Certification when connect Redis with SSL
72 | - ssl_cert_reqs: SSL certification requirements (default is required)
73 |
74 | Return redis connection object
75 |
76 | Examples:
77 | | ${redis_conn}= | Connect To Redis | redis-dev.com | 6379 | redis_password=password |
78 | | ${redis_conn}= | Connect To Redis | redis-dev.com | 6379 | redis_password=password | ssl=True | ssl_cert_reqs=None |
79 | """
80 | try:
81 | redis_conn = redis.StrictRedis(host=redis_host, port=redis_port, db=db,
82 | password=redis_password, ssl=ssl, ssl_ca_certs=ssl_ca_certs,
83 | ssl_cert_reqs=ssl_cert_reqs)
84 | except Exception as ex:
85 | logger.error(str(ex))
86 | raise Exception(str(ex))
87 | return redis_conn
88 |
89 | @keyword('Connect To Redis From URL')
90 | def connect_to_redis_from_url(self, redis_url, db=0):
91 | """Connect to the Redis server.
92 |
93 | Arguments:
94 | - redis_url: URL for connect to Redis. (redis://:@:)
95 | - db: Redis keyspace number (default=0)
96 |
97 | Return redis connection object
98 |
99 | Examples:
100 | | ${redis_conn}= | Connect To Redis From URL | redis://admin:adminpassword@redis-dev.com:6379 |
101 | """
102 | try:
103 | logger.info("Creating Redis Connection using : url=%s " % redis_url)
104 | redis_conn = redis.from_url(redis_url, db=db)
105 | except Exception as ex:
106 | logger.error(str(ex))
107 | raise Exception(str(ex))
108 | return redis_conn
109 |
110 | @keyword('Flush All')
111 | def flush_all(self, redis_conn):
112 | """ Delete all keys from Redis
113 |
114 | Arguments:
115 | - redis_conn: Redis connection object
116 |
117 | Examples:
118 | | Flush All | ${redis_conn} |
119 | """
120 | return redis_conn.flushall()
121 |
122 | @keyword('Delete From Redis')
123 | def delete_from_redis(self, redis_conn, key):
124 | """ Delete data from Redis
125 |
126 | Arguments:
127 | - redis_conn: Redis connection object
128 | - key: String keyword to find.
129 |
130 | Examples:
131 | | Delete From Redis | ${redis_conn} | BARCODE|1234567890 |
132 | """
133 | return redis_conn.delete(key)
134 |
135 | @keyword('Append To Redis')
136 | def append_to_redis(self, redis_conn, key, value):
137 | """ Append data to Redis. If key doesn't exist, create it with value.
138 | Return the new length of the value at key.
139 |
140 | Arguments:
141 | - redis_conn: Redis connection object.
142 | - key: String key.
143 | - value: String value.
144 |
145 | Examples:
146 | | Append To Redis | ${redis_conn} | BARCODE|1234567890 | ${data} |
147 |
148 | """
149 | return redis_conn.append(key, value)
150 |
151 | @keyword('Set To Redis')
152 | def set_to_redis(self, redis_conn, key, data, expire_time=3600):
153 | """ Set data to Redis
154 |
155 | Arguments:
156 | - redis_conn: Redis connection object
157 | - key: String keyword to find.
158 | - data: String data
159 | - expire_time: TTL default value is 3600s
160 |
161 | Examples:
162 | | Set To Redis | ${redis_conn} | BARCODE|0000000011 | ${data} |
163 | | Set To Redis | ${redis_conn} | BARCODE|1234567890 | ${data} | expire_time=600 |
164 | """
165 | return redis_conn.set(key, data, expire_time)
166 |
167 | @keyword('Get From Redis')
168 | def get_from_redis(self, redis_conn, key):
169 | """ Get cached data from Redis
170 |
171 | Arguments:
172 | - redis_conn: Redis connection object
173 | - key: String keyword to find.
174 |
175 | Examples:
176 | | ${data}= | Get From Redis | ${redis_conn} | BARCODE|1234567890 |
177 | """
178 | return redis_conn.get(key)
179 |
180 | @keyword('Expire Data From Redis')
181 | def expire_data_from_redis(self, redis_conn, key, expire_time=0):
182 | """ Expire items from Redis
183 |
184 | Arguments:
185 | - redis_conn: Redis connection object
186 | - key: String keyword to find.
187 | - expire_time: waiting time to expire data (Default = expire now)
188 |
189 | Examples:
190 | | Expire Data From Redis | ${redis_conn} | BARCODE|1234567890 |
191 | """
192 | redis_conn.expire(key, expire_time)
193 |
194 | @keyword('Get Time To Live In Redis')
195 | def get_time_to_live_in_redis(self, redis_conn, key):
196 | """ Return time to live in Redis (minutes)
197 |
198 | Arguments:
199 | - redis_conn: Redis connection object
200 | - key: String keyword to find.
201 |
202 | Examples:
203 | | Get Time To Live In Redis | ${redis_conn} | BARCODE|1234567890 |
204 | """
205 | ttl = redis_conn.ttl(key)
206 | if ttl > 0:
207 | return redis_conn.ttl(key) / 60
208 | else:
209 | return redis_conn.ttl(key)
210 |
211 | @keyword('Get Time To Live In Redis Second')
212 | def get_time_to_live_in_redis_second(self, redis_conn, key):
213 | """ Return time to live in Redis (seconds)
214 |
215 | Arguments:
216 | - redis_conn: Redis connection object
217 | - key: String keyword to find.
218 |
219 | Examples:
220 | | Get Time To Live In Redis Second | ${redis_conn} | BARCODE|1234567890 |
221 | """
222 | return redis_conn.ttl(key)
223 |
224 | @keyword('Redis Key Should Be Exist')
225 | def redis_key_should_be_exist(self, redis_conn, key):
226 | """ Keyword will fail if specify key doesn't exist in Redis
227 |
228 | Arguments:
229 | - redis_conn: Redis connection object
230 | - key: String keyword to find.
231 |
232 | Examples:
233 | | Redis Key Should Be Exist | ${redis_conn} | BARCODE|1234567890 |
234 | """
235 | if not redis_conn.exists(key):
236 | logger.error("Key: " + key + " doesn't exist in Redis.")
237 | raise AssertionError
238 |
239 | @keyword('Redis Key Should Not Be Exist')
240 | def redis_key_should_not_be_exist(self, redis_conn, key):
241 | """ Keyword will fail if specify key exist in Redis
242 |
243 | Arguments:
244 | - redis_conn: Redis connection object
245 | - key: String keyword to find.
246 |
247 | Examples:
248 | | Redis Key Should Not Be Exist | ${redis_conn} | BARCODE|1234567890 |
249 | """
250 | if redis_conn.exists(key):
251 | logger.error("Key: " + key + " exists in Redis.")
252 | raise AssertionError
253 |
254 | @keyword('Get Dictionary From Redis Hash')
255 | def get_dict_from_redis_hash(self, redis_conn, hash_name):
256 | """ Get cached data from Redis hashes
257 |
258 | Arguments:
259 | - redis_conn: Redis connection object
260 | - hash_name: Hash name.
261 |
262 | Examples:
263 | | ${data}= | Get Dictionary From Redis Hash | ${redis_conn} | HASHNAME |
264 | """
265 | return redis_conn.hgetall(hash_name)
266 |
267 | @keyword('Get From Redis Hash')
268 | def get_from_redis_hash(self, redis_conn, hash_name, key):
269 | """ Get cached data from Redis hashes by key
270 |
271 | Arguments:
272 | - redis_conn: Redis connection object
273 | - hash_name: Hash name.
274 | - key: String keyword to find.
275 |
276 | Examples:
277 | | ${data}= | Get From Redis Hash | ${redis_conn} | HASHNAME | BARCODE|1234567890 |
278 | """
279 | return redis_conn.hget(hash_name, key)
280 |
281 | @keyword('Set To Redis Hash')
282 | def set_to_redis_hash(self, redis_conn, hash_name, key, data):
283 | """ Set data to Redis within Hash
284 |
285 | Arguments:
286 | - redis_conn: Redis connection object
287 | - hash_name: String hash
288 | - key: String keyword to find.
289 | - data: String data
290 |
291 | Examples:
292 | | Set To Redis Hash | ${redis_conn} | HASHNAME | key | value |
293 | """
294 | return redis_conn.hset(hash_name, key, data)
295 |
296 | @keyword('Add Hash Map To Redis')
297 | def add_hash_map_to_redis(self, redis_conn, hash_name, dict_data):
298 | """ Set data to Redis within Hash
299 |
300 | Arguments:
301 | - redis_conn: Redis connection object
302 | - hash_name: String hash
303 | - dict_data: data as dict
304 |
305 | Examples:
306 | | Add Hash Map To Redis | ${redis_conn} | HASHNAME | {"name":"Fred","age":25} |
307 | """
308 | return redis_conn.hmset(hash_name, dict_data)
309 |
310 | @keyword('Delete From Redis Hash')
311 | def delete_from_redis_hash(self, redis_conn, hash_name, key):
312 | """Delete ``key`` from hash ``name``
313 |
314 | Arguments:
315 | - redis_conn: Redis connection object.
316 | - hash_name: Hash keyword to find.
317 | - key: String keyword to find.
318 |
319 | Examples:
320 | | Delete From Redis Hash | ${redis_conn} | HASHNAME | KEY |
321 | """
322 | return redis_conn.hdel(hash_name, key)
323 |
324 | @keyword('Redis Hash Key Should Be Exist')
325 | def redis_hash_key_should_be_exist(self, redis_conn, hash_name, key):
326 | """ Keyword will fail if specify hash key doesn't exist in Redis
327 |
328 | Arguments:
329 | - redis_conn: Redis connection object
330 | - hash_name: Hash name.
331 | - key: String keyword to find.
332 |
333 | Examples:
334 | | Redis Hash Key Should Be Exist | ${redis_conn} | BARCODE|1234567890 |
335 | """
336 | if not redis_conn.hexists(hash_name, key):
337 | logger.error("Hash: " + hash_name + " and Key: " +
338 | key + " doesn't exist in Redis.")
339 | raise AssertionError
340 |
341 | @keyword('Redis Hash Key Should Not Be Exist')
342 | def redis_hash_key_should_not_be_exist(self, redis_conn, hash_name, key):
343 | """ Keyword will fail if specify hash key exist in Redis
344 |
345 | Arguments:
346 | - redis_conn: Redis connection object
347 | - hash_name: Hash name.
348 | - key: String keyword to find.
349 | Examples:
350 | | Redis Hash Key Should Not Be Exist | ${redis_conn} | BARCODE|1234567890 |
351 | """
352 | if redis_conn.hexists(hash_name, key):
353 | logger.error("Hash: " + hash_name + " and Key: " +
354 | key + " exists in Redis.")
355 | raise AssertionError
356 |
357 | @keyword('Get Set From Redis Set')
358 | def get_set_from_redis_set(self, redis_conn, set_name):
359 | """ Get cached members from Redis sets.
360 |
361 | Arguments:
362 | - redis_conn: Redis connection object
363 | - set_name: Set name to find.
364 |
365 | Examples:
366 | | ${data}= | Get Set From Redis Set | ${redis_conn} | Fruit |
367 | """
368 | return redis_conn.smembers(set_name)
369 |
370 | @keyword('Add Set Data To Redis Set')
371 | def add_set_data_to_redis_set(self, redis_conn, set_name, *args):
372 | """ Add set data into Redis.
373 |
374 | Arguments:
375 | - redis_conn: Redis connection object
376 | - set_name: Set name as key in redis
377 | - *args: Item that you need to put in set
378 |
379 | Examples:
380 | | Add Set Data To Redis Set | ${redis_conn} | Fruit | Banana | Apple | Orage |
381 | """
382 | return redis_conn.sadd(set_name, *args)
383 |
384 | @keyword('Item Should Exist In Redis Set')
385 | def item_should_exist_in_redis_set(self, redis_conn, set_name, item):
386 | """ Check item should exist in set.
387 |
388 | Arguments:
389 | - redis_conn: Redis connection object
390 | - set_name: Set name as key in redis
391 | - Item: Item that you need check
392 |
393 | Examples:
394 | | Item Should Exist In Redis Set | ${redis_conn} | Fruit | Apple |
395 | """
396 | if not redis_conn.sismember(set_name, item):
397 | logger.error("Item: " + item + " doesn't exist in Redis.")
398 | raise AssertionError
399 |
400 | @keyword('Item Should Not Exist In Redis Set')
401 | def item_should_not_exist_in_redis_set(self, redis_conn, set_name, item):
402 | """ Check item should not exist in set.
403 |
404 | Arguments:
405 | - redis_conn: Redis connection object
406 | - set_name: Set name as key in redis
407 | - Item: Item that you need check
408 |
409 | Examples:
410 | | Item Should Not Exist In Redis Set | ${redis_conn} | Fruit | Mongo |
411 | """
412 | if redis_conn.sismember(set_name, item):
413 | logger.error("Item: " + item + " exists in Redis.")
414 | raise AssertionError
415 |
416 | @keyword('Get Length of Redis Set')
417 | def get_length_of_redis_set(self, redis_conn, set_name):
418 | """ Get length of set.
419 |
420 | Arguments:
421 | - redis_conn: Redis connection object
422 | - set_name: Set name as key in redis
423 |
424 | Examples:
425 | | ${set_length} | Get Length of Redis Set | ${redis_conn} | Fruit |
426 | """
427 | return redis_conn.scard(set_name)
428 |
429 | @keyword('Delete Set Data In Redis Set')
430 | def delete_set_data_in_redis_set(self, redis_conn, set_name, *args):
431 | """ Delete set of item from set.
432 |
433 | If you need to delete set from redis use 'Delete From Redis' keyword.
434 |
435 | Arguments:
436 | - redis_conn: Redis connection object
437 | - set_name: Set name as key in redis
438 | - *args: Item that you need to delete from set
439 |
440 | Examples:
441 | | Delete Set Data In Redis Set | ${redis_conn} | Fruit | Banana | Orage |
442 | """
443 | return redis_conn.srem(set_name, *args)
444 |
445 | @keyword('Push Item To First Index In List Redis')
446 | def push_item_to_first_index_in_list_redis(self, redis_conn, list_name, *args):
447 | """ Push item to first index in list. If you many arguments, last arguments will be the first item.
448 |
449 | Arguments:
450 | - redis_conn: Redis connection object
451 | - list_name: List name as key in redis
452 | - *args: Item that you need to put in set
453 |
454 | Examples:
455 | | Push Item To First Index In List Redis | ${redis_conn} | Country | Germany | Italy | France | Spain |
456 | | ${list_items} | Get All Item From List Redis | ${redis_conn} | Country |
457 |
458 | Result from ``Get All Item From List Redis``: [b'Spain', b'France', b'Italy', b'Germany']
459 | """
460 | return redis_conn.lpush(list_name, *args)
461 |
462 | @keyword('Push Item To Last Index In List Redis')
463 | def push_item_to_last_index_in_list_redis(self, redis_conn, list_name, *args):
464 | """ Push item to last index in list. If you many arguments, last arguments will be the last item.
465 |
466 | Arguments:
467 | - redis_conn: Redis connection object
468 | - list_name: List name as key in redis
469 | - *args: Item that you need to put in set
470 |
471 | Examples:
472 | | Push Item To Last Index In List Redis | ${redis_conn} | Country | Germany | Italy | France | Spain |
473 | | ${list_items} | Get All Item From List Redis | ${redis_conn} | Country |
474 |
475 | Result from ``Get All Item From List Redis``: [b'Germany', b'Italy', b'France', b'Spain']
476 | """
477 | return redis_conn.rpush(list_name, *args)
478 |
479 | @keyword('Update Item In List Redis')
480 | def update_item_in_list_redis(self, redis_conn, list_name, index, item):
481 | """Update item in list by specific index.
482 |
483 | Arguments:
484 | - redis_conn: Redis connection object
485 | - list_name: List name as key in redis
486 | - index: Index in list that you need to update
487 | - item: New item
488 |
489 | Examples:
490 | | Update Item In List Redis | ${redis_conn} | Country | 1 | England |
491 | """
492 | return redis_conn.lset(list_name, index, item)
493 |
494 | @keyword('Get Item From List Redis')
495 | def get_item_from_list_redis(self, redis_conn, list_name, index):
496 | """Get item in list by specific index.
497 |
498 | Arguments:
499 | - redis_conn: Redis connection object
500 | - list_name: List name as key in redis
501 | - index: Index in list that you need to update
502 |
503 | Examples:
504 | | ${item_data} | Get Item From List Redis | ${redis_conn} | Country | 1 |
505 | """
506 | return redis_conn.lindex(list_name, index)
507 |
508 | @keyword('Get All Item From List Redis')
509 | def get_all_item_from_list_redis(self, redis_conn, list_name):
510 | """Get all items in list.
511 |
512 | Arguments:
513 | - redis_conn: Redis connection object
514 | - list_name: List name as key in redis
515 |
516 | Examples:
517 | | ${list_items} | Get All Item From List Redis | ${redis_conn} | Country |
518 | """
519 | return redis_conn.lrange(list_name, 0, -1)
520 |
521 | @keyword('Get Length From List Redis')
522 | def get_length_from_list_redis(self, redis_conn, list_name):
523 | """Get length of list.
524 |
525 | Arguments:
526 | - redis_conn: Redis connection object
527 | - list_name: List name as key in redis
528 |
529 | Examples:
530 | | ${list_length} | Get Length From List Redis | ${redis_conn} | Country |
531 | """
532 | return redis_conn.llen(list_name)
533 |
534 | @keyword('Get Index of Item From List Redis')
535 | def get_index_of_item_from_list_redis(self, redis_conn, list_name, item):
536 | """Get indexs of item that metched in list.
537 |
538 | Arguments:
539 | - redis_conn: Redis connection object
540 | - list_name: List name as key in redis
541 | - item: Search item
542 |
543 | Examples:
544 | | ${list_index} | Get Index of Item From List Redis | ${redis_conn} | Country | Germany |
545 |
546 | Keyword will return result as list of index: [0, 1, 5]
547 | """
548 | return [i for i, j in enumerate(redis_conn.lrange(list_name, 0, -1)) if j == str.encode(item)]
549 |
550 | @keyword('Get All Match Keys')
551 | def get_all_match_keys(self, redis_conn, key, count=100):
552 | """ Get all key that matches with specific keyword
553 |
554 | Arguments:
555 | - redis_conn: Redis connection object
556 | - key: String keyword to find may contain wildcard.
557 | - count: Element number of returns
558 |
559 | Examples:
560 | | @{key_list}= | Get All Match Keys | ${redis_conn} | BARCODE* | 1000 |
561 | """
562 | return redis_conn.scan(0, key, count)[1]
563 |
564 | @keyword('Delete Item From List Redis')
565 | def delete_item_from_list_redis(self, redis_conn, list_name, index, item=None):
566 | """Delete data from list by specific index.
567 |
568 | Arguments:
569 | - redis_conn: Redis connection object
570 | - list_name: List name as key in redis
571 | - index: Index in list that you need to delete
572 | - item: Compare item. If it is None, keyword will not compare with item in index.
573 | But if is not None, keyword will compare it with item in index before delete.
574 | If not matched keyword will failed.
575 |
576 | Examples 1:
577 | | Delete Item From List Redis | ${redis_conn} | Country | 2 |
578 |
579 | Examples 2: keyword will compare it with item in index before delete.
580 | If not matched keyword will failed
581 | | Delete Item From List Redis | ${redis_conn} | Country | 2 | Spain |
582 | """
583 | if item is not None:
584 | if not redis_conn.lindex(list_name, index) == str.encode(item):
585 | logger.error("Item: " + item + " not matched with index item in list.")
586 | raise AssertionError
587 | redis_conn.lset(list_name, index, 'DELETE_ITEM')
588 | redis_conn.lrem(list_name, 1, 'DELETE_ITEM')
589 |
--------------------------------------------------------------------------------