Skip to content

Commit 36c0433

Browse files
committed
Improve test coverage
1 parent 4e738dd commit 36c0433

File tree

18 files changed

+283
-133
lines changed

18 files changed

+283
-133
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2022 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.common;
18+
19+
import io.github.dengliming.redismodule.common.util.ArgsUtil;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
public class ArgsUtilTest {
28+
29+
@Test
30+
public void testAppend() {
31+
assertThat(ArgsUtil.append("key1", "a", "b", "c")).containsExactly("key1", "a", "b", "c");
32+
Map<String, Object> params = new HashMap<>();
33+
params.put("k1", "v1");
34+
params.put("k2", "v2");
35+
Object[] result = ArgsUtil.append("key2", params);
36+
assertThat(result).hasSize(5);
37+
assertThat(result).contains("key2");
38+
assertThat(result).contains("k1", "v1");
39+
assertThat(result).contains("k2", "v2");
40+
}
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2022 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.common;
18+
19+
import io.github.dengliming.redismodule.common.util.RAssert;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.HashMap;
23+
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
26+
public class RAssertTest {
27+
28+
@Test
29+
public void test() {
30+
assertThrows(IllegalArgumentException.class, () -> RAssert.notNull(null, "test not null"),
31+
"test not null");
32+
33+
assertThrows(IllegalArgumentException.class, () -> RAssert.notEmpty((CharSequence) null, "test CharSequence not empty"),
34+
"test CharSequence not empty");
35+
36+
assertThrows(IllegalArgumentException.class, () -> RAssert.notEmpty(new Object[]{}, "test Object[] not empty"),
37+
"test Object[] not empty");
38+
39+
assertThrows(IllegalArgumentException.class, () -> RAssert.notEmpty(new int[]{}, "test int[] not empty"),
40+
"test int[] not empty");
41+
42+
assertThrows(IllegalArgumentException.class, () -> RAssert.notEmpty(new double[]{}, "test double[] not empty"),
43+
"test double[] not empty");
44+
45+
assertThrows(IllegalArgumentException.class, () -> RAssert.isTrue(false, "test boolean is false"),
46+
"test boolean is false");
47+
48+
assertThrows(IllegalArgumentException.class, () -> RAssert.notEmpty(new HashMap<>(), "test HashMap not empty"),
49+
"test HashMap not empty");
50+
}
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2022 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.common;
18+
19+
import io.github.dengliming.redismodule.common.util.TestSettings;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
public class TestSettingsTest {
25+
26+
@Test
27+
public void test() {
28+
assertThat(TestSettings.password()).isEmpty();
29+
}
30+
}

redisai/src/main/java/io/github/dengliming/redismodule/redisai/args/StoreScriptArgs.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ public void build(List<Object> args) {
7979
if (entryPoints != null && !entryPoints.isEmpty()) {
8080
args.add(Keywords.ENTRY_POINTS);
8181
args.add(entryPoints.size());
82-
for (String entryPoint : entryPoints) {
83-
args.add(entryPoint);
84-
}
82+
args.addAll(entryPoints);
8583
}
8684

8785
if (script != null) {

redisai/src/test/java/io/github/dengliming/redismodule/redisai/RedisAITest.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.github.dengliming.redismodule.redisai;
1818

1919
import io.github.dengliming.redismodule.redisai.args.SetModelArgs;
20+
import io.github.dengliming.redismodule.redisai.args.StoreScriptArgs;
2021
import io.github.dengliming.redismodule.redisai.model.Model;
2122
import io.github.dengliming.redismodule.redisai.model.Script;
2223
import io.github.dengliming.redismodule.redisai.model.Tensor;
@@ -27,12 +28,13 @@
2728
import java.nio.file.Files;
2829
import java.nio.file.Paths;
2930
import java.util.Arrays;
31+
import java.util.Collections;
3032
import java.util.Map;
33+
import java.util.Objects;
3134

3235
import static org.assertj.core.api.Assertions.assertThat;
3336
import static org.junit.jupiter.api.Assertions.assertThrows;
3437

35-
3638
/**
3739
* @author dengliming
3840
*/
@@ -56,9 +58,17 @@ public void testTensor() {
5658
public void testModel() throws Exception {
5759
RedisAI redisAI = getRedisAI();
5860
// Set Model
59-
byte[] blob = Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource("test_data/graph.pb").toURI()));
60-
assertThat(redisAI.setModel("model1", new SetModelArgs().backEnd(Backend.TF).device(Device.CPU)
61-
.inputs(Arrays.asList("a", "b")).outputs(Arrays.asList("mul")).blob(blob))).isTrue();
61+
byte[] blob = Files.readAllBytes(Paths.get(Objects.requireNonNull(getClass().getClassLoader().getResource(
62+
"test_data/graph.pb")).toURI()));
63+
assertThat(redisAI.setModel("model1", new SetModelArgs()
64+
.backEnd(Backend.TF)
65+
.device(Device.CPU)
66+
.inputs(Arrays.asList("a", "b"))
67+
.tag("model1:1.0")
68+
.batchSize(1024)
69+
.minBatchSize(256)
70+
.outputs(Collections.singletonList("mul"))
71+
.blob(blob))).isTrue();
6272

6373
// Get Model
6474
Model model = redisAI.getModel("model1");
@@ -67,6 +77,12 @@ public void testModel() throws Exception {
6777
assertThat(model.getInputs()).containsExactly("a", "b");
6878
assertThat(model.getOutputs()).containsExactly("mul");
6979
assertThat(model.getBlob()).isEqualTo(blob);
80+
assertThat(model.getBatchSize()).isEqualTo(1024);
81+
assertThat(model.getMinBatchSize()).isEqualTo(256);
82+
assertThat(model.getTag()).isEqualTo("model1:1.0");
83+
84+
// Delete Model
85+
assertThat(redisAI.deleteModel("model1")).isTrue();
7086
}
7187

7288
@Test
@@ -83,6 +99,8 @@ public void testScript() {
8399
assertThat(scriptInfo).isNotNull();
84100
assertThat(scriptInfo.getDevice()).isEqualTo(Device.CPU);
85101
assertThat(scriptInfo.getSource()).isEqualTo(script);
102+
103+
assertThat(redisAI.deleteScript(key)).isTrue();
86104
}
87105

88106
@Test
@@ -136,4 +154,22 @@ public void testInfo() {
136154
redisAI.resetStat("not:exist");
137155
});
138156
}
157+
158+
@Test
159+
public void testStoreScript() {
160+
RedisAI redisAI = getRedisAI();
161+
162+
String key = "myscript";
163+
String script = "def addtwo(tensors: List[Tensor], keys: List[str], args: List[str]):\n"
164+
+ " a = tensors[0]\n"
165+
+ " b = tensors[1]\n"
166+
+ " return a + b\n";
167+
assertThat(redisAI.storeScript(key, new StoreScriptArgs()
168+
.script(script)
169+
.device(Device.CPU)
170+
.tag("myscript:v0.1")
171+
.entryPoints(Collections.singletonList("addtwo")))).isTrue();
172+
173+
assertThat(redisAI.deleteScript(key)).isTrue();
174+
}
139175
}

redisbloom/src/main/java/io/github/dengliming/redismodule/redisbloom/CountMinSketch.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,20 @@ public RFuture<List<Integer>> queryAsync(String... items) {
123123
/**
124124
* Merges several sketches into one sketch.
125125
*
126-
* @param keyNum Number of sketches to be merged
127126
* @param srcs Names of source sketches to be merged
128127
* @param weights Multiple of each sketch. Default =1
129128
* @return True if executed correctly, or False reply otherwise
130129
*/
131-
public boolean merge(int keyNum, String[] srcs, Integer[] weights) {
130+
public boolean merge(String[] srcs, int[] weights) {
132131
RAssert.notEmpty(srcs, "Srcs must not be empty");
133132

134-
return get(mergeAsync(keyNum, srcs, weights));
133+
return get(mergeAsync(srcs, weights));
135134
}
136135

137-
public RFuture<Boolean> mergeAsync(int keyNum, String[] srcs, Integer[] weights) {
136+
public RFuture<Boolean> mergeAsync(String[] srcs, int[] weights) {
138137
List<Object> params = new ArrayList<>();
139138
params.add(getName());
140-
params.add(keyNum);
139+
params.add(srcs.length);
141140
Collections.addAll(params, srcs);
142141
if (weights.length > 0) {
143142
params.add(Keywords.WEIGHTS);
Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 dengliming.
2+
* Copyright 2020-2022 dengliming.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,11 +20,11 @@
2020
* @author dengliming
2121
*/
2222
public class BloomFilterInfo {
23-
private Integer capacity;
24-
private Integer size;
25-
private Integer filterNum;
26-
private Integer insertedNum;
27-
private Integer expansionRate;
23+
private final Integer capacity;
24+
private final Integer size;
25+
private final Integer filterNum;
26+
private final Integer insertedNum;
27+
private final Integer expansionRate;
2828

2929
public BloomFilterInfo(Integer capacity, Integer size, Integer filterNum, Integer insertedNum, Integer expansionRate) {
3030
this.capacity = capacity;
@@ -38,39 +38,19 @@ public Integer getCapacity() {
3838
return capacity;
3939
}
4040

41-
public void setCapacity(Integer capacity) {
42-
this.capacity = capacity;
43-
}
44-
4541
public Integer getSize() {
4642
return size;
4743
}
4844

49-
public void setSize(Integer size) {
50-
this.size = size;
51-
}
52-
5345
public Integer getFilterNum() {
5446
return filterNum;
5547
}
5648

57-
public void setFilterNum(Integer filterNum) {
58-
this.filterNum = filterNum;
59-
}
60-
6149
public Integer getInsertedNum() {
6250
return insertedNum;
6351
}
6452

65-
public void setInsertedNum(Integer insertedNum) {
66-
this.insertedNum = insertedNum;
67-
}
68-
6953
public Integer getExpansionRate() {
7054
return expansionRate;
7155
}
72-
73-
public void setExpansionRate(Integer expansionRate) {
74-
this.expansionRate = expansionRate;
75-
}
7656
}

redisbloom/src/main/java/io/github/dengliming/redismodule/redisbloom/model/CountMinSketchInfo.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
* @author dengliming
2121
*/
2222
public class CountMinSketchInfo {
23-
private Integer width;
24-
private Integer depth;
25-
private Integer count;
23+
private final Integer width;
24+
private final Integer depth;
25+
private final Integer count;
2626

2727
public CountMinSketchInfo(Integer width, Integer depth, Integer count) {
2828
this.width = width;
@@ -34,23 +34,11 @@ public Integer getWidth() {
3434
return width;
3535
}
3636

37-
public void setWidth(Integer width) {
38-
this.width = width;
39-
}
40-
4137
public Integer getDepth() {
4238
return depth;
4339
}
4440

45-
public void setDepth(Integer depth) {
46-
this.depth = depth;
47-
}
48-
4941
public Integer getCount() {
5042
return count;
5143
}
52-
53-
public void setCount(Integer count) {
54-
this.count = count;
55-
}
5644
}

0 commit comments

Comments
 (0)