1
+ package com .redis .om .spring .ops .pds ;
2
+
3
+ import com .redis .om .spring .AbstractBaseDocumentTest ;
4
+ import com .redis .om .spring .ops .RedisModulesOperations ;
5
+ import org .junit .jupiter .api .BeforeEach ;
6
+ import org .junit .jupiter .api .Test ;
7
+ import org .springframework .beans .factory .annotation .Autowired ;
8
+ import redis .clients .jedis .exceptions .JedisDataException ;
9
+
10
+ import java .util .HashMap ;
11
+ import java .util .List ;
12
+ import java .util .Map ;
13
+
14
+ import static org .assertj .core .api .Assertions .assertThat ;
15
+ import static org .junit .jupiter .api .Assertions .*;
16
+
17
+ class OpsForTopKTest extends AbstractBaseDocumentTest {
18
+ @ Autowired
19
+ RedisModulesOperations <String > modulesOperations ;
20
+
21
+ TopKOperations <String > topK ;
22
+
23
+ @ BeforeEach
24
+ void beforeEach () {
25
+ topK = modulesOperations .opsForTopK ();
26
+ }
27
+
28
+ @ Test
29
+ void testBasicOperations () {
30
+ // Create a TopK filter with a capacity of 3
31
+ String result = topK .createFilter ("topkTest" , 3 );
32
+ assertEquals ("OK" , result );
33
+
34
+ // Add three items
35
+ List <String > dropped = topK .add ("topkTest" , "item1" , "item2" , "item3" );
36
+ assertEquals (3 , dropped .size (), "Should return a list with the same size of the added items" );
37
+ dropped .forEach (it -> assertNull (it , "No item should be dropped initially" ));
38
+
39
+ // Add one more item to exceed the capacity
40
+ dropped = topK .add ("topkTest" , "item4" );
41
+ assertEquals (1 , dropped .size (), "Should return a list with the same size of the added items" );
42
+ assertEquals ("item1" , dropped .get (0 ), "The first item should be dropped" );
43
+
44
+ // Query items
45
+ List <Boolean > exists = topK .query ("topkTest" , "item1" , "item2" , "item3" , "item4" );
46
+ // item1 should've been dropped, so it should not exist. All the others should exist.
47
+ assertFalse (exists .get (0 ), "item1 should not exist" );
48
+ exists .stream ().skip (1 ).forEach (it -> assertTrue (it , "All other items should exist" ));
49
+
50
+ // List items
51
+ List <String > topItems = topK .list ("topkTest" );
52
+ assertFalse (topItems .isEmpty (), "Should get the three items" );
53
+ assertThat (topItems .size ()).isEqualTo (3 );
54
+
55
+ // Get counts
56
+ Map <String , Long > counts = topK .listWithCount ("topkTest" );
57
+ assertFalse (counts .isEmpty (), "Should get counts for the three items" );
58
+ assertEquals (1L , counts .get ("item2" ), "item2 should have a count of 1" );
59
+ assertEquals (1L , counts .get ("item3" ), "item3 should have a count of 1" );
60
+ assertEquals (1L , counts .get ("item4" ), "item4 should have a count of 1" );
61
+
62
+ // Clean up after the test
63
+ template .delete ("topkTest" );
64
+ }
65
+
66
+ @ Test
67
+ void testIncrementBy () {
68
+ // Create a TopK filter
69
+ topK .createFilter ("topkIncrTest" , 3 );
70
+
71
+ // Increment a single item
72
+ String dropped = topK .incrementBy ("topkIncrTest" , "item1" , 5 );
73
+ assertNull (dropped , "No item should be dropped initially" );
74
+
75
+ // Increment multiple items
76
+ Map <String , Long > itemIncrMap = new HashMap <>();
77
+ itemIncrMap .put ("item2" , 3L );
78
+ itemIncrMap .put ("item3" , 7L );
79
+
80
+ List <String > droppedList = topK .incrementBy ("topkIncrTest" , itemIncrMap );
81
+ assertEquals (2 , droppedList .size (), "Should return a list with the same size of the added items" );
82
+ assertNull (droppedList .get (0 ), "No item should be dropped" );
83
+
84
+ // Check that the items exist with correct counts
85
+ Map <String , Long > counts = topK .listWithCount ("topkIncrTest" );
86
+ assertFalse (counts .isEmpty ());
87
+
88
+ // Verify counts are as expected (may vary due to probabilistic nature)
89
+ assertEquals (5L , counts .get ("item1" ), "item1 should have a count of 5" );
90
+ assertEquals (3L , counts .get ("item2" ), "item2 should have a count of 3" );
91
+ assertEquals (7L , counts .get ("item3" ), "item3 should have a count of 7" );
92
+
93
+ // Clean up after the test
94
+ template .delete ("topkIncrTest" );
95
+ }
96
+
97
+ @ Test
98
+ void testInfo () {
99
+ // Create a TopK filter with custom parameters
100
+ String status = topK .createFilter ("topkInfoTest" , 5 , 10 , 7 , 0.9 );
101
+ assertEquals ("OK" , status , "Filter should be created successfully" );
102
+
103
+ // Get filter info
104
+ Map <String , Object > info = topK .info ("topkInfoTest" );
105
+
106
+ // Verify expected keys in info map
107
+ assertNotNull (info );
108
+ assertFalse (info .isEmpty ());
109
+
110
+ // Verify specific filter parameters
111
+ assertEquals (5L , info .get ("k" ));
112
+ assertEquals (10L , info .get ("width" ));
113
+ assertEquals (7L , info .get ("depth" ));
114
+ assertEquals (0.9 , Double .parseDouble ((String ) info .get ("decay" )));
115
+
116
+ // Clean up after the test
117
+ template .delete ("topkInfoTest" );
118
+ }
119
+
120
+ @ Test
121
+ void testNonExistingKey () {
122
+ // Attempt to get info for a non-existing key
123
+ JedisDataException exception = assertThrows (JedisDataException .class ,
124
+ () -> topK .info ("nonExistingKey" ));
125
+
126
+ // Verify error message
127
+ assertEquals ("TopK: key does not exist" , exception .getMessage ());
128
+ }
129
+ }
0 commit comments