File: vrandmember.py

package info (click to toggle)
redis 5%3A8.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,304 kB
  • sloc: ansic: 216,903; tcl: 51,562; sh: 4,625; perl: 4,214; cpp: 3,568; python: 2,954; makefile: 2,055; ruby: 639; javascript: 30; csh: 7
file content (55 lines) | stat: -rw-r--r-- 2,779 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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"