Skip to content

Commit 561d3ce

Browse files
authored
test: add comprehensive edge case tests for QdrantVectorStore.Builder (#3986)
Signed-off-by: Alex Klimenko <[email protected]>
1 parent c7f2856 commit 561d3ce

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

vector-stores/spring-ai-qdrant-store/src/test/java/org/springframework/ai/vectorstore/qdrant/QdrantVectorStoreBuilderTests.java

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,143 @@ void nullBatchingStrategyShouldThrowException() {
9696
.hasMessage("BatchingStrategy must not be null");
9797
}
9898

99+
@Test
100+
void nullCollectionNameShouldThrowException() {
101+
assertThatThrownBy(
102+
() -> QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel).collectionName(null).build())
103+
.isInstanceOf(IllegalArgumentException.class)
104+
.hasMessage("collectionName must not be empty");
105+
}
106+
107+
@Test
108+
void whitespaceOnlyCollectionNameShouldThrowException() {
109+
assertThatThrownBy(
110+
() -> QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel).collectionName(" ").build())
111+
.isInstanceOf(IllegalArgumentException.class)
112+
.hasMessage("collectionName must not be empty");
113+
}
114+
115+
@Test
116+
void builderShouldReturnNewInstanceOnEachBuild() {
117+
QdrantVectorStore.Builder builder = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel);
118+
119+
QdrantVectorStore vectorStore1 = builder.build();
120+
QdrantVectorStore vectorStore2 = builder.build();
121+
122+
assertThat(vectorStore1).isNotSameAs(vectorStore2);
123+
}
124+
125+
@Test
126+
void builderShouldAllowMethodChaining() {
127+
QdrantVectorStore vectorStore = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel)
128+
.collectionName("test_collection")
129+
.initializeSchema(true)
130+
.batchingStrategy(new TokenCountBatchingStrategy())
131+
.build();
132+
133+
assertThat(vectorStore).isNotNull();
134+
assertThat(vectorStore).hasFieldOrPropertyWithValue("collectionName", "test_collection");
135+
assertThat(vectorStore).hasFieldOrPropertyWithValue("initializeSchema", true);
136+
}
137+
138+
@Test
139+
void builderShouldMaintainStateAcrossMultipleCalls() {
140+
QdrantVectorStore.Builder builder = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel)
141+
.collectionName("persistent_collection");
142+
143+
QdrantVectorStore vectorStore1 = builder.build();
144+
QdrantVectorStore vectorStore2 = builder.initializeSchema(true).build();
145+
146+
// Both should have the same collection name
147+
assertThat(vectorStore1).hasFieldOrPropertyWithValue("collectionName", "persistent_collection");
148+
assertThat(vectorStore2).hasFieldOrPropertyWithValue("collectionName", "persistent_collection");
149+
150+
// But different initializeSchema values
151+
assertThat(vectorStore1).hasFieldOrPropertyWithValue("initializeSchema", false);
152+
assertThat(vectorStore2).hasFieldOrPropertyWithValue("initializeSchema", true);
153+
}
154+
155+
@Test
156+
void builderShouldOverridePreviousValues() {
157+
QdrantVectorStore vectorStore = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel)
158+
.collectionName("first_collection")
159+
.collectionName("second_collection")
160+
.initializeSchema(true)
161+
.initializeSchema(false)
162+
.build();
163+
164+
assertThat(vectorStore).hasFieldOrPropertyWithValue("collectionName", "second_collection");
165+
assertThat(vectorStore).hasFieldOrPropertyWithValue("initializeSchema", false);
166+
}
167+
168+
@Test
169+
void builderWithMinimalConfiguration() {
170+
QdrantVectorStore vectorStore = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel).build();
171+
172+
assertThat(vectorStore).isNotNull();
173+
// Should use default values
174+
assertThat(vectorStore).hasFieldOrPropertyWithValue("collectionName", "vector_store");
175+
assertThat(vectorStore).hasFieldOrPropertyWithValue("initializeSchema", false);
176+
}
177+
178+
@Test
179+
void builderWithDifferentBatchingStrategies() {
180+
TokenCountBatchingStrategy strategy1 = new TokenCountBatchingStrategy();
181+
TokenCountBatchingStrategy strategy2 = new TokenCountBatchingStrategy();
182+
183+
QdrantVectorStore vectorStore1 = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel)
184+
.batchingStrategy(strategy1)
185+
.build();
186+
187+
QdrantVectorStore vectorStore2 = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel)
188+
.batchingStrategy(strategy2)
189+
.build();
190+
191+
assertThat(vectorStore1).hasFieldOrPropertyWithValue("batchingStrategy", strategy1);
192+
assertThat(vectorStore2).hasFieldOrPropertyWithValue("batchingStrategy", strategy2);
193+
}
194+
195+
@Test
196+
void builderShouldAcceptValidCollectionNames() {
197+
String[] validNames = { "collection_with_underscores", "collection-with-dashes", "collection123", "Collection",
198+
"c", "very_long_collection_name_that_should_still_be_valid_according_to_most_naming_conventions" };
199+
200+
for (String name : validNames) {
201+
QdrantVectorStore vectorStore = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel)
202+
.collectionName(name)
203+
.build();
204+
205+
assertThat(vectorStore).hasFieldOrPropertyWithValue("collectionName", name);
206+
}
207+
}
208+
209+
@Test
210+
void builderStateShouldBeIndependentBetweenInstances() {
211+
QdrantVectorStore.Builder builder1 = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel)
212+
.collectionName("collection1");
213+
214+
QdrantVectorStore.Builder builder2 = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel)
215+
.collectionName("collection2");
216+
217+
QdrantVectorStore vectorStore1 = builder1.build();
218+
QdrantVectorStore vectorStore2 = builder2.build();
219+
220+
assertThat(vectorStore1).hasFieldOrPropertyWithValue("collectionName", "collection1");
221+
assertThat(vectorStore2).hasFieldOrPropertyWithValue("collectionName", "collection2");
222+
}
223+
224+
@Test
225+
void builderShouldHandleBooleanToggling() {
226+
QdrantVectorStore.Builder builder = QdrantVectorStore.builder(this.qdrantClient, this.embeddingModel);
227+
228+
// Test toggling initializeSchema
229+
QdrantVectorStore vectorStore1 = builder.initializeSchema(true).build();
230+
QdrantVectorStore vectorStore2 = builder.initializeSchema(false).build();
231+
QdrantVectorStore vectorStore3 = builder.initializeSchema(true).build();
232+
233+
assertThat(vectorStore1).hasFieldOrPropertyWithValue("initializeSchema", true);
234+
assertThat(vectorStore2).hasFieldOrPropertyWithValue("initializeSchema", false);
235+
assertThat(vectorStore3).hasFieldOrPropertyWithValue("initializeSchema", true);
236+
}
237+
99238
}

0 commit comments

Comments
 (0)