Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dengliming committed Oct 18, 2022
1 parent 4e738dd commit 36c0433
Show file tree
Hide file tree
Showing 18 changed files with 283 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 dengliming.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.dengliming.redismodule.common;

import io.github.dengliming.redismodule.common.util.ArgsUtil;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class ArgsUtilTest {

@Test
public void testAppend() {
assertThat(ArgsUtil.append("key1", "a", "b", "c")).containsExactly("key1", "a", "b", "c");
Map<String, Object> params = new HashMap<>();
params.put("k1", "v1");
params.put("k2", "v2");
Object[] result = ArgsUtil.append("key2", params);
assertThat(result).hasSize(5);
assertThat(result).contains("key2");
assertThat(result).contains("k1", "v1");
assertThat(result).contains("k2", "v2");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2022 dengliming.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.dengliming.redismodule.common;

import io.github.dengliming.redismodule.common.util.RAssert;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class RAssertTest {

@Test
public void test() {
assertThrows(IllegalArgumentException.class, () -> RAssert.notNull(null, "test not null"),
"test not null");

assertThrows(IllegalArgumentException.class, () -> RAssert.notEmpty((CharSequence) null, "test CharSequence not empty"),
"test CharSequence not empty");

assertThrows(IllegalArgumentException.class, () -> RAssert.notEmpty(new Object[]{}, "test Object[] not empty"),
"test Object[] not empty");

assertThrows(IllegalArgumentException.class, () -> RAssert.notEmpty(new int[]{}, "test int[] not empty"),
"test int[] not empty");

assertThrows(IllegalArgumentException.class, () -> RAssert.notEmpty(new double[]{}, "test double[] not empty"),
"test double[] not empty");

assertThrows(IllegalArgumentException.class, () -> RAssert.isTrue(false, "test boolean is false"),
"test boolean is false");

assertThrows(IllegalArgumentException.class, () -> RAssert.notEmpty(new HashMap<>(), "test HashMap not empty"),
"test HashMap not empty");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2022 dengliming.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.dengliming.redismodule.common;

import io.github.dengliming.redismodule.common.util.TestSettings;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class TestSettingsTest {

@Test
public void test() {
assertThat(TestSettings.password()).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ public void build(List<Object> args) {
if (entryPoints != null && !entryPoints.isEmpty()) {
args.add(Keywords.ENTRY_POINTS);
args.add(entryPoints.size());
for (String entryPoint : entryPoints) {
args.add(entryPoint);
}
args.addAll(entryPoints);
}

if (script != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.github.dengliming.redismodule.redisai;

import io.github.dengliming.redismodule.redisai.args.SetModelArgs;
import io.github.dengliming.redismodule.redisai.args.StoreScriptArgs;
import io.github.dengliming.redismodule.redisai.model.Model;
import io.github.dengliming.redismodule.redisai.model.Script;
import io.github.dengliming.redismodule.redisai.model.Tensor;
Expand All @@ -27,12 +28,13 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;

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


/**
* @author dengliming
*/
Expand All @@ -56,9 +58,17 @@ public void testTensor() {
public void testModel() throws Exception {
RedisAI redisAI = getRedisAI();
// Set Model
byte[] blob = Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource("test_data/graph.pb").toURI()));
assertThat(redisAI.setModel("model1", new SetModelArgs().backEnd(Backend.TF).device(Device.CPU)
.inputs(Arrays.asList("a", "b")).outputs(Arrays.asList("mul")).blob(blob))).isTrue();
byte[] blob = Files.readAllBytes(Paths.get(Objects.requireNonNull(getClass().getClassLoader().getResource(
"test_data/graph.pb")).toURI()));
assertThat(redisAI.setModel("model1", new SetModelArgs()
.backEnd(Backend.TF)
.device(Device.CPU)
.inputs(Arrays.asList("a", "b"))
.tag("model1:1.0")
.batchSize(1024)
.minBatchSize(256)
.outputs(Collections.singletonList("mul"))
.blob(blob))).isTrue();

// Get Model
Model model = redisAI.getModel("model1");
Expand All @@ -67,6 +77,12 @@ public void testModel() throws Exception {
assertThat(model.getInputs()).containsExactly("a", "b");
assertThat(model.getOutputs()).containsExactly("mul");
assertThat(model.getBlob()).isEqualTo(blob);
assertThat(model.getBatchSize()).isEqualTo(1024);
assertThat(model.getMinBatchSize()).isEqualTo(256);
assertThat(model.getTag()).isEqualTo("model1:1.0");

// Delete Model
assertThat(redisAI.deleteModel("model1")).isTrue();
}

@Test
Expand All @@ -83,6 +99,8 @@ public void testScript() {
assertThat(scriptInfo).isNotNull();
assertThat(scriptInfo.getDevice()).isEqualTo(Device.CPU);
assertThat(scriptInfo.getSource()).isEqualTo(script);

assertThat(redisAI.deleteScript(key)).isTrue();
}

@Test
Expand Down Expand Up @@ -136,4 +154,22 @@ public void testInfo() {
redisAI.resetStat("not:exist");
});
}

@Test
public void testStoreScript() {
RedisAI redisAI = getRedisAI();

String key = "myscript";
String script = "def addtwo(tensors: List[Tensor], keys: List[str], args: List[str]):\n"
+ " a = tensors[0]\n"
+ " b = tensors[1]\n"
+ " return a + b\n";
assertThat(redisAI.storeScript(key, new StoreScriptArgs()
.script(script)
.device(Device.CPU)
.tag("myscript:v0.1")
.entryPoints(Collections.singletonList("addtwo")))).isTrue();

assertThat(redisAI.deleteScript(key)).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,20 @@ public RFuture<List<Integer>> queryAsync(String... items) {
/**
* Merges several sketches into one sketch.
*
* @param keyNum Number of sketches to be merged
* @param srcs Names of source sketches to be merged
* @param weights Multiple of each sketch. Default =1
* @return True if executed correctly, or False reply otherwise
*/
public boolean merge(int keyNum, String[] srcs, Integer[] weights) {
public boolean merge(String[] srcs, int[] weights) {
RAssert.notEmpty(srcs, "Srcs must not be empty");

return get(mergeAsync(keyNum, srcs, weights));
return get(mergeAsync(srcs, weights));
}

public RFuture<Boolean> mergeAsync(int keyNum, String[] srcs, Integer[] weights) {
public RFuture<Boolean> mergeAsync(String[] srcs, int[] weights) {
List<Object> params = new ArrayList<>();
params.add(getName());
params.add(keyNum);
params.add(srcs.length);
Collections.addAll(params, srcs);
if (weights.length > 0) {
params.add(Keywords.WEIGHTS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 dengliming.
* Copyright 2020-2022 dengliming.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,11 +20,11 @@
* @author dengliming
*/
public class BloomFilterInfo {
private Integer capacity;
private Integer size;
private Integer filterNum;
private Integer insertedNum;
private Integer expansionRate;
private final Integer capacity;
private final Integer size;
private final Integer filterNum;
private final Integer insertedNum;
private final Integer expansionRate;

public BloomFilterInfo(Integer capacity, Integer size, Integer filterNum, Integer insertedNum, Integer expansionRate) {
this.capacity = capacity;
Expand All @@ -38,39 +38,19 @@ public Integer getCapacity() {
return capacity;
}

public void setCapacity(Integer capacity) {
this.capacity = capacity;
}

public Integer getSize() {
return size;
}

public void setSize(Integer size) {
this.size = size;
}

public Integer getFilterNum() {
return filterNum;
}

public void setFilterNum(Integer filterNum) {
this.filterNum = filterNum;
}

public Integer getInsertedNum() {
return insertedNum;
}

public void setInsertedNum(Integer insertedNum) {
this.insertedNum = insertedNum;
}

public Integer getExpansionRate() {
return expansionRate;
}

public void setExpansionRate(Integer expansionRate) {
this.expansionRate = expansionRate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
* @author dengliming
*/
public class CountMinSketchInfo {
private Integer width;
private Integer depth;
private Integer count;
private final Integer width;
private final Integer depth;
private final Integer count;

public CountMinSketchInfo(Integer width, Integer depth, Integer count) {
this.width = width;
Expand All @@ -34,23 +34,11 @@ public Integer getWidth() {
return width;
}

public void setWidth(Integer width) {
this.width = width;
}

public Integer getDepth() {
return depth;
}

public void setDepth(Integer depth) {
this.depth = depth;
}

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}
}
Loading

0 comments on commit 36c0433

Please sign in to comment.