-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finally support redis-py 3.* package or later
This commit adds compatibility layer, so old plugins do not need to adapt changes to support >3.* python3 package of redis. Some portions of that layer code is copy-pasted from shinqlx. Co-authored-By: Markus Gärtner <shino@shino.de>
- Loading branch information
Showing
4 changed files
with
230 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
env: | ||
PIP_BREAK_SYSTEM_PACKAGES: "1" | ||
|
||
jobs: | ||
test_redis: | ||
|
||
strategy: | ||
matrix: | ||
redis-package-version: ["2.*", "3.*", "4.*", "5.*"] | ||
os: ["ubuntu-24.04", "ubuntu-22.04", "ubuntu-20.04"] | ||
|
||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Start Redis | ||
uses: superchargejs/redis-github-action@1.1.0 | ||
- name: Install redis package | ||
run: python3 -m pip install redis==${{ matrix.redis-package-version }} | ||
- name: Run test | ||
run: python3 -m unittest discover python/tests/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../minqlx/database.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import random | ||
import string | ||
from time import sleep | ||
from unittest import TestCase | ||
|
||
import redis | ||
|
||
import database | ||
|
||
|
||
def random_string(): | ||
return "".join([random.choice(string.ascii_letters) for x in range(10)]) | ||
|
||
|
||
class RedisRegressionTestCase(TestCase): | ||
def setUp(self): | ||
self.c = database.Redis(None) | ||
self.c.connect("127.0.0.1:6379") | ||
|
||
def test_zadd_01(self): | ||
c = self.c | ||
key = "zadd_01_key_" + random_string() | ||
|
||
c.zadd(key, 5, "player1", 6, "player2") | ||
res = dict(c.zrange(key, 0, -1, withscores=True)) | ||
self.assertEqual(res["player1"], 5) | ||
self.assertEqual(res["player2"], 6) | ||
|
||
c.zadd(key, 2, "player1") | ||
res = dict(c.zrange(key, 0, -1, withscores=True)) | ||
self.assertEqual(res["player1"], 2) | ||
self.assertEqual(res["player2"], 6) | ||
|
||
c.zadd(key, 4, "player2") | ||
res = dict(c.zrange(key, 0, -1, withscores=True)) | ||
self.assertEqual(res["player1"], 2) | ||
self.assertEqual(res["player2"], 4) | ||
|
||
def test_zadd_02(self): | ||
c = self.c | ||
key = "zadd_02_key_" + random_string() | ||
|
||
# redis-py 3.* introduced xx, nx, ch and incr kwarg params | ||
# this codes deliberately does not work in redis-py 2.* | ||
if redis.VERSION < (3,): | ||
self.skipTest("Old version of redis package: %s" % (redis.VERSION,)) | ||
c.zadd(key, 5, "player1", 6, "player2") | ||
c.zadd(key, {"player1": 3}, xx=True) | ||
res = dict(c.zrange(key, 0, -1, withscores=True)) | ||
self.assertEqual(res["player1"], 3) | ||
|
||
c.zadd(key, 3, "player_does_not_exist", xx=True) | ||
res = dict(c.zrange(key, 0, -1, withscores=True)) | ||
self.assertNotIn("player_does_not_exist", res) | ||
|
||
def test_zincrby_01(self): | ||
key = "zincrby_01_key_" + random_string() | ||
c = self.c | ||
|
||
c.zincrby(key, "FIELD1") | ||
c.zincrby(key, "FIELD2", 2) | ||
res = dict(c.zrange(key, 0, -1, withscores=True)) | ||
self.assertEqual(res["FIELD1"], 1) | ||
self.assertEqual(res["FIELD2"], 2) | ||
|
||
c.zincrby(key, "FIELD1") | ||
c.zincrby(key, "FIELD2", 3) | ||
res = dict(c.zrange(key, 0, -1, withscores=True)) | ||
self.assertEqual(res["FIELD1"], 2) | ||
self.assertEqual(res["FIELD2"], 5) | ||
|
||
def test_lrem_01(self): | ||
key = "lrem_01_key_" + random_string() | ||
c = self.c | ||
|
||
c.rpush( | ||
key, | ||
"Cat", | ||
"Dog", | ||
"Horse", | ||
"Cat", | ||
"Dog", | ||
"Cat", | ||
"Monkey", | ||
"Dog", | ||
"Cat", | ||
"Dog", | ||
"Buffalo", | ||
) | ||
c.lrem(key, 2, "Cat") | ||
result = c.lrange(key, 0, -1) | ||
self.assertEqual( | ||
result, | ||
["Dog", "Horse", "Dog", "Cat", "Monkey", "Dog", "Cat", "Dog", "Buffalo"], | ||
) | ||
|
||
def test_setex_01(self): | ||
key = "setex_01_key_" + random_string() | ||
c = self.c | ||
test_value = "test value" | ||
timeout = 1 | ||
|
||
c.setex(key, timeout, test_value) | ||
self.assertEqual(c.get(key), test_value, "Too small timeout value?") | ||
sleep(timeout + 0.5) | ||
self.assertEqual(c.get(key), None) | ||
|
||
def test_mset_01(self): | ||
c = self.c | ||
c.mset(field1="example1", field2="example2") | ||
self.assertEqual(["example1", "example2"], c.mget("field1", "field2")) | ||
|
||
def test_mset_02(self): | ||
c = self.c | ||
c.mset({"field3": "example1", "field4": "example2"}) | ||
self.assertEqual(["example1", "example2"], c.mget("field3", "field4")) | ||
|
||
def test_msetnx_01_kwargs(self): | ||
c = self.c | ||
prefix = "msetnx_01_key_" + random_string() | ||
key1 = prefix + "_field1" | ||
key2 = prefix + "_field2" | ||
d = { | ||
key1: "example1", | ||
key2: "example2", | ||
} | ||
c.msetnx(**d) | ||
self.assertEqual(["example1", "example2"], c.mget(key1, key2)) | ||
|
||
d = { | ||
key1: "value should not be used", | ||
key2: "same here", | ||
} | ||
c.msetnx(**d) | ||
self.assertEqual(["example1", "example2"], c.mget(key1, key2)) | ||
|
||
def test_msetnx_02_dict(self): | ||
c = self.c | ||
prefix = "msetnx_02_key_" + random_string() | ||
key1 = prefix + "_field1" | ||
key2 = prefix + "_field2" | ||
d = { | ||
key1: "example1", | ||
key2: "example2", | ||
} | ||
c.msetnx(d) | ||
self.assertEqual(["example1", "example2"], c.mget(key1, key2)) | ||
|
||
d = { | ||
key1: "value should not be used", | ||
key2: "same here", | ||
} | ||
c.msetnx(d) | ||
self.assertEqual(["example1", "example2"], c.mget(key1, key2)) |