summaryrefslogtreecommitdiff
path: root/examples/redis-unstable/modules/vector-sets/tests/vrandmember.py
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:40:55 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:40:55 +0100
commit5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda (patch)
tree1acdfa5220cd13b7be43a2a01368e80d306473ca /examples/redis-unstable/modules/vector-sets/tests/vrandmember.py
parentc7ab12bba64d9c20ccd79b132dac475f7bc3923e (diff)
downloadcrep-5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda.tar.gz
Add Redis source code for testing
Diffstat (limited to 'examples/redis-unstable/modules/vector-sets/tests/vrandmember.py')
-rw-r--r--examples/redis-unstable/modules/vector-sets/tests/vrandmember.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/examples/redis-unstable/modules/vector-sets/tests/vrandmember.py b/examples/redis-unstable/modules/vector-sets/tests/vrandmember.py
new file mode 100644
index 0000000..ca9e006
--- /dev/null
+++ b/examples/redis-unstable/modules/vector-sets/tests/vrandmember.py
@@ -0,0 +1,55 @@
+from test import TestCase, generate_random_vector, fill_redis_with_vectors
+import struct
+
+class VRANDMEMBERTest(TestCase):
+ def getname(self):
+ return "VRANDMEMBER basic functionality"
+
+ def test(self):
+ # Test with empty key
+ result = self.redis.execute_command('VRANDMEMBER', self.test_key)
+ assert result is None, "VRANDMEMBER on non-existent key should return NULL"
+
+ result = self.redis.execute_command('VRANDMEMBER', self.test_key, 5)
+ assert isinstance(result, list) and len(result) == 0, "VRANDMEMBER with count on non-existent key should return empty array"
+
+ # Fill with vectors
+ dim = 4
+ count = 100
+ data = fill_redis_with_vectors(self.redis, self.test_key, count, dim)
+
+ # Test single random member
+ result = self.redis.execute_command('VRANDMEMBER', self.test_key)
+ assert result is not None, "VRANDMEMBER should return a random member"
+ assert result.decode() in data.names, "Random member should be in the set"
+
+ # Test multiple unique members (positive count)
+ positive_count = 10
+ result = self.redis.execute_command('VRANDMEMBER', self.test_key, positive_count)
+ assert isinstance(result, list), "VRANDMEMBER with positive count should return an array"
+ assert len(result) == positive_count, f"Should return {positive_count} members"
+
+ # Check for uniqueness
+ decoded_results = [r.decode() for r in result]
+ assert len(decoded_results) == len(set(decoded_results)), "Results should be unique with positive count"
+ for item in decoded_results:
+ assert item in data.names, "All returned items should be in the set"
+
+ # Test more members than in the set
+ result = self.redis.execute_command('VRANDMEMBER', self.test_key, count + 10)
+ assert len(result) == count, "Should return only the available members when asking for more than exist"
+
+ # Test with duplicates (negative count)
+ negative_count = -20
+ result = self.redis.execute_command('VRANDMEMBER', self.test_key, negative_count)
+ assert isinstance(result, list), "VRANDMEMBER with negative count should return an array"
+ assert len(result) == abs(negative_count), f"Should return {abs(negative_count)} members"
+
+ # Check that all returned elements are valid
+ decoded_results = [r.decode() for r in result]
+ for item in decoded_results:
+ assert item in data.names, "All returned items should be in the set"
+
+ # Test with count = 0 (edge case)
+ result = self.redis.execute_command('VRANDMEMBER', self.test_key, 0)
+ assert isinstance(result, list) and len(result) == 0, "VRANDMEMBER with count=0 should return empty array"