-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Minor refactoring and refactored some unit test #2167
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -32,8 +32,11 @@ | |||||
import java.util.ArrayList; | ||||||
import java.util.Arrays; | ||||||
import java.util.Collection; | ||||||
import java.util.Collections; | ||||||
import java.util.List; | ||||||
import java.util.Map; | ||||||
import java.util.function.Predicate; | ||||||
import java.util.stream.Collectors; | ||||||
import java.util.stream.IntStream; | ||||||
|
||||||
import static com.carrotsearch.randomizedtesting.RandomizedTest.$; | ||||||
|
@@ -44,6 +47,7 @@ | |||||
import static org.mockito.Mockito.mock; | ||||||
import static org.mockito.Mockito.mockConstruction; | ||||||
import static org.mockito.Mockito.mockStatic; | ||||||
import static org.mockito.Mockito.never; | ||||||
import static org.mockito.Mockito.times; | ||||||
import static org.mockito.Mockito.verify; | ||||||
import static org.mockito.Mockito.when; | ||||||
|
@@ -86,6 +90,7 @@ public static Collection<Object[]> data() { | |||||
"Multi Field", | ||||||
List.of( | ||||||
Map.of(0, new float[] { 1, 2, 3 }, 1, new float[] { 2, 3, 4 }, 2, new float[] { 3, 4, 5 }), | ||||||
Collections.emptyMap(), | ||||||
Map.of( | ||||||
0, | ||||||
new float[] { 1, 2, 3, 4 }, | ||||||
|
@@ -105,18 +110,16 @@ public static Collection<Object[]> data() { | |||||
@SneakyThrows | ||||||
public void testFlush() { | ||||||
// Given | ||||||
List<KNNVectorValues<float[]>> expectedVectorValues = new ArrayList<>(); | ||||||
IntStream.range(0, vectorsPerField.size()).forEach(i -> { | ||||||
final List<KNNVectorValues<float[]>> expectedVectorValues = vectorsPerField.stream().map(vectors -> { | ||||||
final TestVectorValues.PreDefinedFloatVectorValues randomVectorValues = new TestVectorValues.PreDefinedFloatVectorValues( | ||||||
new ArrayList<>(vectorsPerField.get(i).values()) | ||||||
new ArrayList<>(vectors.values()) | ||||||
); | ||||||
final KNNVectorValues<float[]> knnVectorValues = KNNVectorValuesFactory.getVectorValues( | ||||||
VectorDataType.FLOAT, | ||||||
randomVectorValues | ||||||
); | ||||||
expectedVectorValues.add(knnVectorValues); | ||||||
|
||||||
}); | ||||||
return knnVectorValues; | ||||||
}).collect(Collectors.toList()); | ||||||
|
||||||
try ( | ||||||
MockedStatic<NativeEngineFieldVectorsWriter> fieldWriterMockedStatic = mockStatic(NativeEngineFieldVectorsWriter.class); | ||||||
|
@@ -172,15 +175,19 @@ public void testFlush() { | |||||
|
||||||
IntStream.range(0, vectorsPerField.size()).forEach(i -> { | ||||||
try { | ||||||
verify(nativeIndexWriter).flushIndex(expectedVectorValues.get(i), vectorsPerField.get(i).size()); | ||||||
if (vectorsPerField.get(i).isEmpty()) { | ||||||
verify(nativeIndexWriter, never()).flushIndex(expectedVectorValues.get(i), vectorsPerField.get(i).size()); | ||||||
} else { | ||||||
verify(nativeIndexWriter).flushIndex(expectedVectorValues.get(i), vectorsPerField.get(i).size()); | ||||||
} | ||||||
} catch (Exception e) { | ||||||
throw new RuntimeException(e); | ||||||
} | ||||||
}); | ||||||
|
||||||
final Long expectedTimesGetVectorValuesIsCalled = vectorsPerField.stream().filter(Predicate.not(Map::isEmpty)).count(); | ||||||
knnVectorValuesFactoryMockedStatic.verify( | ||||||
() -> KNNVectorValuesFactory.getVectorValues(any(VectorDataType.class), any(DocsWithFieldSet.class), any()), | ||||||
times(expectedVectorValues.size()) | ||||||
times(Math.toIntExact(expectedTimesGetVectorValuesIsCalled)) | ||||||
); | ||||||
} | ||||||
} | ||||||
|
@@ -264,16 +271,21 @@ public void testFlush_WithQuantization() { | |||||
|
||||||
IntStream.range(0, vectorsPerField.size()).forEach(i -> { | ||||||
try { | ||||||
verify(knn990QuantWriterMockedConstruction.constructed().get(0)).writeState(i, quantizationState); | ||||||
verify(nativeIndexWriter).flushIndex(expectedVectorValues.get(i), vectorsPerField.get(i).size()); | ||||||
if (vectorsPerField.get(i).isEmpty()) { | ||||||
verify(knn990QuantWriterMockedConstruction.constructed().get(0), never()).writeState(i, quantizationState); | ||||||
verify(nativeIndexWriter, never()).flushIndex(expectedVectorValues.get(i), vectorsPerField.get(i).size()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, verifying no interactions is a much stronger check
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure will that work. We have interactions from other fields. I only want to verify that it doesn't interact when there are empty fields. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, I missed that it has other values apart from empty vector |
||||||
} else { | ||||||
verify(knn990QuantWriterMockedConstruction.constructed().get(0)).writeState(i, quantizationState); | ||||||
verify(nativeIndexWriter).flushIndex(expectedVectorValues.get(i), vectorsPerField.get(i).size()); | ||||||
} | ||||||
} catch (Exception e) { | ||||||
throw new RuntimeException(e); | ||||||
} | ||||||
}); | ||||||
|
||||||
final Long expectedTimesGetVectorValuesIsCalled = vectorsPerField.stream().filter(Predicate.not(Map::isEmpty)).count(); | ||||||
knnVectorValuesFactoryMockedStatic.verify( | ||||||
() -> KNNVectorValuesFactory.getVectorValues(any(VectorDataType.class), any(DocsWithFieldSet.class), any()), | ||||||
times(expectedVectorValues.size() * 2) | ||||||
times(Math.toIntExact(expectedTimesGetVectorValuesIsCalled) * 2) | ||||||
); | ||||||
} | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want any interactions with quantwriter in this case right?