Skip to content

Commit

Permalink
TCEs for select command pages (#3361)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com>
  • Loading branch information
dwdougherty and vladvildanov authored Sep 6, 2024
1 parent e00f3d2 commit 2fb40c1
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 0 deletions.
200 changes: 200 additions & 0 deletions doctests/cmds_generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
# EXAMPLE: cmds_generic
# HIDE_START
import redis

r = redis.Redis(decode_responses=True)
# HIDE_END

# STEP_START del
res = r.set("key1", "Hello")
print(res)
# >>> True

res = r.set("key2", "World")
print(res)
# >>> True

res = r.delete("key1", "key2", "key3")
print(res)
# >>> 2
# REMOVE_START
assert res == 2
# REMOVE_END
# STEP_END

# STEP_START expire
res = r.set("mykey", "Hello")
print(res)
# >>> True

res = r.expire("mykey", 10)
print(res)
# >>> True

res = r.ttl("mykey")
print(res)
# >>> 10
# REMOVE_START
assert res == 10
# REMOVE_END

res = r.set("mykey", "Hello World")
print(res)
# >>> True

res = r.ttl("mykey")
print(res)
# >>> -1
# REMOVE_START
assert res == -1
# REMOVE_END

res = r.expire("mykey", 10, xx=True)
print(res)
# >>> False
# REMOVE_START
assert res == False
# REMOVE_END

res = r.ttl("mykey")
print(res)
# >>> -1
# REMOVE_START
assert res == -1
# REMOVE_END

res = r.expire("mykey", 10, nx=True)
print(res)
# >>> True
# REMOVE_START
assert res == True
# REMOVE_END

res = r.ttl("mykey")
print(res)
# >>> 10
# REMOVE_START
assert res == 10
r.delete("mykey")
# REMOVE_END
# STEP_END

# STEP_START ttl
res = r.set("mykey", "Hello")
print(res)
# >>> True

res = r.expire("mykey", 10)
print(res)
# >>> True

res = r.ttl("mykey")
print(res)
# >>> 10
# REMOVE_START
assert res == 10
r.delete("mykey")
# REMOVE_END
# STEP_END

# STEP_START scan1
res = r.sadd("myset", *set([1, 2, 3, "foo", "foobar", "feelsgood"]))
print(res)
# >>> 6

res = list(r.sscan_iter("myset", match="f*"))
print(res)
# >>> ['foobar', 'foo', 'feelsgood']
# REMOVE_START
assert sorted(res) == sorted(["foo", "foobar", "feelsgood"])
r.delete("myset")
# REMOVE_END
# STEP_END

# STEP_START scan2
# REMOVE_START
for i in range(1, 1001):
r.set(f"key:{i}", i)
# REMOVE_END

cursor, key = r.scan(cursor=0, match='*11*')
print(cursor, key)

cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)

cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)

cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)

cursor, keys = r.scan(cursor, match='*11*', count=1000)
print(cursor, keys)

# REMOVE_START
assert len(keys) == 18
cursor = '0'
prefix = "key:*"
while cursor != 0:
cursor, keys = r.scan(cursor=cursor, match=prefix, count=1000)
if keys:
r.delete(*keys)
# REMOVE_END
# STEP_END

# STEP_START scan3
res = r.geoadd("geokey", (0, 0, "value"))
print(res)
# >>> 1

res = r.zadd("zkey", {"value": 1000})
print(res)
# >>> 1

res = r.type("geokey")
print(res)
# >>> zset
# REMOVE_START
assert res == "zset"
# REMOVE_END

res = r.type("zkey")
print(res)
# >>> zset
# REMOVE_START
assert res == "zset"
# REMOVE_END

cursor, keys = r.scan(cursor=0, _type="zset")
print(keys)
# >>> ['zkey', 'geokey']
# REMOVE_START
assert sorted(keys) == sorted(["zkey", "geokey"])
r.delete("geokey", "zkey")
# REMOVE_END
# STEP_END

# STEP_START scan4
res = r.hset("myhash", mapping={"a": 1, "b": 2})
print(res)
# >>> 2

cursor, keys = r.hscan("myhash", 0)
print(keys)
# >>> {'a': '1', 'b': '2'}
# REMOVE_START
assert keys == {'a': '1', 'b': '2'}
# REMOVE_END

cursor, keys = r.hscan("myhash", 0, no_values=True)
print(keys)
# >>> ['a', 'b']
# REMOVE_START
assert keys == ['a', 'b']
# REMOVE_END

# REMOVE_START
r.delete("myhash")
# REMOVE_END
# STEP_END
63 changes: 63 additions & 0 deletions doctests/cmds_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# EXAMPLE: cmds_hash
# HIDE_START
import redis

r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
# HIDE_END

# STEP_START hset
res1 = r.hset("myhash", "field1", "Hello")
print(res1)
# >>> 1

res2 = r.hget("myhash", "field1")
print(res2)
# >>> Hello

res3 = r.hset("myhash", mapping={"field2": "Hi", "field3": "World"})
print(res3)
# >>> 2

res4 = r.hget("myhash", "field2")
print(res4)
# >>> Hi

res5 = r.hget("myhash", "field3")
print(res5)
# >>> World

res6 = r.hgetall("myhash")
print(res6)
# >>> { "field1": "Hello", "field2": "Hi", "field3": "World" }

# REMOVE_START
assert res1 == 1
assert res2 == "Hello"
assert res3 == 2
assert res4 == "Hi"
assert res5 == "World"
assert res6 == { "field1": "Hello", "field2": "Hi", "field3": "World" }
r.delete("myhash")
# REMOVE_END
# STEP_END

# STEP_START hget
res7 = r.hset("myhash", "field1", "foo")
print(res7)
# >>> 1

res8 = r.hget("myhash", "field1")
print(res8)
# >>> foo

res9 = r.hget("myhash", "field2")
print(res9)
# >>> None

# REMOVE_START
assert res7 == 1
assert res8 == "foo"
assert res9 == None
r.delete("myhash")
# REMOVE_END
# STEP_END
89 changes: 89 additions & 0 deletions doctests/cmds_sorted_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# EXAMPLE: cmds_sorted_set
# HIDE_START
import redis

r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
# HIDE_END

# STEP_START zadd
res = r.zadd("myzset", {"one": 1})
print(res)
# >>> 1
# REMOVE_START
assert res == 1
# REMOVE_END

res = r.zadd("myzset", {"uno": 1})
print(res)
# >>> 1
# REMOVE_START
assert res == 1
# REMOVE_END

res = r.zadd("myzset", {"two": 2, "three": 3})
print(res)
# >>> 2
# REMOVE_START
assert res == 2
# REMOVE_END

res = r.zrange("myzset", 0, -1, withscores=True)
# >>> [('one', 1.0), ('uno', 1.0), ('two', 2.0), ('three', 3.0)]
# REMOVE_START
assert res == [('one', 1.0), ('uno', 1.0), ('two', 2.0), ('three', 3.0)]
# REMOVE_END

# REMOVE_START
r.delete("myzset")
# REMOVE_END
# STEP_END

# STEP_START zrange1
res = r.zadd("myzset", {"one": 1, "two":2, "three":3})
print(res)
# >>> 3

res = r.zrange("myzset", 0, -1)
print(res)
# >>> ['one', 'two', 'three']
# REMOVE_START
assert res == ['one', 'two', 'three']
# REMOVE_END

res = r.zrange("myzset", 2, 3)
print(res)
# >>> ['three']
# REMOVE_START
assert res == ['three']
# REMOVE_END

res = r.zrange("myzset", -2, -1)
print(res)
# >>> ['two', 'three']
# REMOVE_START
assert res == ['two', 'three']
r.delete("myzset")
# REMOVE_END
# STEP_END

# STEP_START zrange2
res = r.zadd("myzset", {"one": 1, "two":2, "three":3})
res = r.zrange("myzset", 0, 1, withscores=True)
print(res)
# >>> [('one', 1.0), ('two', 2.0)]
# REMOVE_START
assert res == [('one', 1.0), ('two', 2.0)]
r.delete("myzset")
# REMOVE_END
# STEP_END

# STEP_START zrange3
res = r.zadd("myzset", {"one": 1, "two":2, "three":3})
res = r.zrange("myzset", 2, 3, byscore=True, offset=1, num=1)
print(res)
# >>> ['three']
# REMOVE_START
assert res == ['three']
r.delete("myzset")
# REMOVE_END
# STEP_END
19 changes: 19 additions & 0 deletions doctests/cmds_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# EXAMPLE: cmds_string
# HIDE_START
import redis

r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
# HIDE_END

# STEP_START incr
res = r.set("mykey", "10")
print(res)
# >>> True
res = r.incr("mykey")
print(res)
# >>> 11
# REMOVE_START
assert res == 11
r.delete("mykey")
# REMOVE_END
# STEP_END

0 comments on commit 2fb40c1

Please sign in to comment.