Skip to content

Commit 03cf63d

Browse files
authored
Merge pull request #384 from weaviate/test/multi-vector-adapter
Add tests for WeaviateObject.Adapter
2 parents 6fe6bdd + c24a65b commit 03cf63d

3 files changed

Lines changed: 83 additions & 2 deletions

File tree

src/main/java/io/weaviate/client/v1/batch/model/ObjectGetResponse.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class ObjectGetResponse {
2626
Map<String, Object> properties;
2727
Map<String, Object> additional;
2828
Float[] vector;
29-
Float[][] multiVector;
3029
Map<String, Float[]> vectors;
3130
Map<String, Float[][]> multiVectors;
3231
Object vectorWeights;

src/main/java/io/weaviate/client/v1/data/model/WeaviateObject.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ public static class Adapter implements JsonSerializer<WeaviateObject>, JsonDeser
8080
public JsonElement serialize(WeaviateObject src, Type typeOfSrc, JsonSerializationContext ctx) {
8181
JsonObject result = gson.toJsonTree(src).getAsJsonObject();
8282

83+
if (result.has("vectors") && result.getAsJsonObject("vectors").isEmpty()) {
84+
result.remove("vectors");
85+
}
86+
8387
// Add multi-vectors to the named vectors map.
84-
if (src.multiVectors != null) {
88+
if (src.multiVectors != null && !src.multiVectors.isEmpty()) {
8589
if (!result.has("vectors")) {
8690
result.add("vectors", new JsonObject());
8791
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.weaviate.client.v1.data.model;
2+
3+
import java.util.Collections;
4+
5+
import org.assertj.core.api.Assertions;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
9+
import com.google.gson.Gson;
10+
import com.google.gson.GsonBuilder;
11+
import com.google.gson.JsonElement;
12+
import com.google.gson.JsonParser;
13+
import com.jparams.junit4.JParamsTestRunner;
14+
import com.jparams.junit4.data.DataMethod;
15+
16+
@RunWith(JParamsTestRunner.class)
17+
/**
18+
* Test that WeaviateObject vectors are de-/serialized correctly. Specifically,
19+
* single- and multi-vectors should be correctly combined under the "vectors"
20+
* key in case any named vectors are present.
21+
*/
22+
public class WeaviateObjectAdapterTest {
23+
private static final Gson gson = new GsonBuilder()
24+
.registerTypeAdapter(WeaviateObject.class, WeaviateObject.Adapter.INSTANCE)
25+
.create();
26+
27+
public static Object[][] testCasesJson() {
28+
return new Object[][] {
29+
{
30+
WeaviateObject.builder().vector(new Float[] { 1f, 2f, 3f }).build(),
31+
"{\"vector\":[1.0,2.0,3.0]}"
32+
},
33+
{
34+
WeaviateObject.builder().vectors(Collections.singletonMap("single", new Float[] { 1f, 2f, 3f })).build(),
35+
"{\"vectors\":{\"single\":[1.0,2.0,3.0]}}"
36+
},
37+
{
38+
WeaviateObject.builder()
39+
.multiVectors(Collections.singletonMap("multi", new Float[][] {
40+
{ 1f, 2f, 3f },
41+
{ 4f, 5f, 6f },
42+
}))
43+
.build(),
44+
"{\"vectors\":{\"multi\":[[1.0,2.0,3.0],[4.0, 5.0, 6.0]]}}"
45+
},
46+
{
47+
WeaviateObject.builder()
48+
.vectors(Collections.singletonMap("single", new Float[] { 1f, 2f, 3f }))
49+
.multiVectors(Collections.singletonMap("multi", new Float[][] {
50+
{ 1f, 2f, 3f },
51+
{ 4f, 5f, 6f },
52+
}))
53+
.build(),
54+
"{\"vectors\":{\"single\":[1.0,2.0,3.0],\"multi\":[[1.0,2.0,3.0],[4.0, 5.0, 6.0]]}}"
55+
},
56+
};
57+
}
58+
59+
@Test
60+
@DataMethod(source = WeaviateObjectAdapterTest.class, method = "testCasesJson")
61+
public void test_toJson(WeaviateObject in, String want) {
62+
String got = gson.toJson(in);
63+
assertSameJson(got, want);
64+
}
65+
66+
@Test
67+
@DataMethod(source = WeaviateObjectAdapterTest.class, method = "testCasesJson")
68+
public void test_fromJson(WeaviateObject want, String in) {
69+
WeaviateObject got = gson.fromJson(in, WeaviateObject.class);
70+
Assertions.assertThat(got).usingRecursiveComparison().isEqualTo(want);
71+
}
72+
73+
private void assertSameJson(String got, String want) {
74+
JsonElement gotEl = JsonParser.parseString(got);
75+
JsonElement wantEl = JsonParser.parseString(want);
76+
Assertions.assertThat(gotEl).isEqualTo(wantEl);
77+
}
78+
}

0 commit comments

Comments
 (0)