|
| 1 | +package io.weaviate.client.v1.batch.api; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.List; |
| 5 | +import java.util.stream.Collectors; |
| 6 | + |
| 7 | +import org.assertj.core.api.Assertions; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpStatus; |
| 11 | + |
| 12 | +import io.weaviate.client.base.Result; |
| 13 | +import io.weaviate.client.base.WeaviateError; |
| 14 | +import io.weaviate.client.base.WeaviateErrorMessage; |
| 15 | +import io.weaviate.client.grpc.protocol.v1.WeaviateProtoBatch; |
| 16 | +import io.weaviate.client.v1.batch.model.ObjectGetResponse; |
| 17 | +import io.weaviate.client.v1.data.model.WeaviateObject; |
| 18 | + |
| 19 | +public class ObjectsBatcherTest { |
| 20 | + @Test |
| 21 | + public void test_resultFromBatchObjectsReply() { |
| 22 | + // Arrange |
| 23 | + List<WeaviateObject> batch = Arrays.asList( |
| 24 | + WeaviateObject.builder().id("id-0").build(), |
| 25 | + WeaviateObject.builder().id("id-1").build(), |
| 26 | + WeaviateObject.builder().id("id-2").build()); |
| 27 | + WeaviateProtoBatch.BatchObjectsReply reply = WeaviateProtoBatch.BatchObjectsReply.newBuilder() |
| 28 | + .addAllErrors(Arrays.asList( |
| 29 | + WeaviateProtoBatch.BatchObjectsReply.BatchError.newBuilder() |
| 30 | + .setIndex(0).setError("error-0") |
| 31 | + .build(), |
| 32 | + WeaviateProtoBatch.BatchObjectsReply.BatchError.newBuilder() |
| 33 | + .setIndex(1).setError("error-1") |
| 34 | + .build())) |
| 35 | + .build(); |
| 36 | + |
| 37 | + // Act |
| 38 | + Result<ObjectGetResponse[]> got = ObjectsBatcher.resultFromBatchObjectsReply(reply, batch); |
| 39 | + |
| 40 | + // Assert |
| 41 | + List<ObjectGetResponse> succeeded = Arrays.stream(got.getResult()) |
| 42 | + .filter(result -> result.getResult().getErrors() == null) |
| 43 | + .collect(Collectors.toList()); |
| 44 | + List<ObjectGetResponse> failed = Arrays.stream(got.getResult()) |
| 45 | + .filter(result -> result.getResult().getErrors() != null) |
| 46 | + .collect(Collectors.toList()); |
| 47 | + Assertions.assertThat(got.getResult()).hasSize(3); |
| 48 | + Assertions.assertThat(succeeded).hasSize(1); |
| 49 | + Assertions.assertThat(failed).hasSize(2); |
| 50 | + |
| 51 | + Assertions.assertThat(got.getError()).returns(HttpStatus.SC_UNPROCESSABLE_ENTITY, WeaviateError::getStatusCode); |
| 52 | + Assertions.assertThat(got.getError().getMessages()) |
| 53 | + .hasSize(2) |
| 54 | + .extracting(WeaviateErrorMessage::getMessage) |
| 55 | + .contains("error-0", "error-1"); |
| 56 | + |
| 57 | + } |
| 58 | +} |
0 commit comments