|
| 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