-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCollectionsITest.java
More file actions
331 lines (271 loc) · 12.4 KB
/
Copy pathCollectionsITest.java
File metadata and controls
331 lines (271 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package io.weaviate.integration;
import java.io.IOException;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.Test;
import io.weaviate.ConcurrentTest;
import io.weaviate.client6.v1.api.WeaviateApiException;
import io.weaviate.client6.v1.api.WeaviateClient;
import io.weaviate.client6.v1.api.collections.CollectionConfig;
import io.weaviate.client6.v1.api.collections.DataType;
import io.weaviate.client6.v1.api.collections.Generative;
import io.weaviate.client6.v1.api.collections.InvertedIndex;
import io.weaviate.client6.v1.api.collections.ObjectTtl;
import io.weaviate.client6.v1.api.collections.Property;
import io.weaviate.client6.v1.api.collections.Quantization;
import io.weaviate.client6.v1.api.collections.ReferenceProperty;
import io.weaviate.client6.v1.api.collections.Replication;
import io.weaviate.client6.v1.api.collections.VectorConfig;
import io.weaviate.client6.v1.api.collections.config.Shard;
import io.weaviate.client6.v1.api.collections.config.ShardStatus;
import io.weaviate.client6.v1.api.collections.generative.DummyGenerative;
import io.weaviate.client6.v1.api.collections.query.BaseQueryOptions;
import io.weaviate.client6.v1.api.collections.vectorindex.Hnsw;
import io.weaviate.client6.v1.api.collections.vectorizers.SelfProvidedVectorizer;
import io.weaviate.containers.Container;
import io.weaviate.containers.Weaviate;
public class CollectionsITest extends ConcurrentTest {
private static WeaviateClient client = Container.WEAVIATE.getClient();
@Test
public void testCreateGetDelete() throws IOException {
var collectionName = ns("Things");
client.collections.create(collectionName,
col -> col
.properties(Property.text("username"), Property.integer("age"))
.vectorConfig(VectorConfig.selfProvided()));
var thingsCollection = client.collections.getConfig(collectionName);
Assertions.assertThat(thingsCollection).get()
.hasFieldOrPropertyWithValue("collectionName", collectionName)
.extracting(CollectionConfig::vectors, InstanceOfAssertFactories.map(String.class, VectorConfig.class))
.as("default vector").extractingByKey("default")
.satisfies(defaultVector -> {
Assertions.assertThat(defaultVector)
.as("has none vectorizer").isInstanceOf(SelfProvidedVectorizer.class);
Assertions.assertThat(defaultVector).extracting(VectorConfig::vectorIndex)
.isInstanceOf(Hnsw.class);
});
client.collections.delete(collectionName);
var noCollection = client.collections.getConfig(collectionName);
Assertions.assertThat(noCollection).as("after delete").isEmpty();
}
@Test
public void testCrossReferences() throws IOException {
// Arrange: Create Owners collection
var nsOwners = ns("Owners");
client.collections.create(nsOwners);
// Act: Create Things collection with owner -> owners
var nsThings = ns("Things");
client.collections.create(nsThings,
col -> col.references(ReferenceProperty.to("ownedBy", nsOwners)));
var things = client.collections.use(nsThings);
// Assert: Things --ownedBy-> Owners
Assertions.assertThat(things.config.get())
.as("after create Things").get()
.satisfies(c -> {
Assertions.assertThat(c.references())
.as("ownedBy").filteredOn(p -> p.propertyName().equals("ownedBy")).first()
.extracting(p -> p.dataTypes(), InstanceOfAssertFactories.LIST)
.containsOnly(nsOwners);
});
// Arrange: Create OnlineStores and Markets collections
var nsOnlineStores = ns("OnlineStores");
client.collections.create(nsOnlineStores);
var nsMarkets = ns("Markets");
client.collections.create(nsMarkets);
// Act: Update Things collections to add polymorphic reference
things.config.addReference("soldIn", nsOnlineStores, nsMarkets);
// Assert: Things --soldIn-> [OnlineStores, Markets]
Assertions.assertThat(things.config.get())
.as("after add property").get()
.satisfies(c -> {
Assertions.assertThat(c.references())
.as("soldIn").filteredOn(p -> p.propertyName().equals("soldIn")).first()
.extracting(p -> p.dataTypes(), InstanceOfAssertFactories.LIST)
.containsOnly(nsOnlineStores, nsMarkets);
});
}
@Test
public void testListDeleteAll() throws Exception {
// Use a separate container for this test so as not to interfere
// with other tests.
try (final var _client = Weaviate.createDefault().getBareClient()) {
var nsA = ns("A");
var nsB = ns("B");
var nsC = ns("C");
_client.collections.create(nsA);
_client.collections.create(nsB);
_client.collections.create(nsC);
Assertions.assertThat(_client.collections.exists(nsA)).isTrue();
Assertions.assertThat(_client.collections.exists(nsB)).isTrue();
Assertions.assertThat(_client.collections.exists(nsC)).isTrue();
Assertions.assertThat(_client.collections.exists(ns("X"))).isFalse();
var all = _client.collections.list();
Assertions.assertThat(all)
.hasSizeGreaterThanOrEqualTo(3)
.extracting(CollectionConfig::collectionName)
.contains(nsA, nsB, nsC);
_client.collections.deleteAll();
all = _client.collections.list();
Assertions.assertThat(all.isEmpty());
}
}
@Test
public void testUpdateCollection() throws IOException {
var nsBoxes = ns("Boxes");
var nsThings = ns("Things");
client.collections.create(nsBoxes);
client.collections.create(nsThings,
collection -> collection
.description("Things stored in boxes")
.properties(
Property.text("name"),
Property.integer("width",
w -> w.description("how wide this thing is")))
.invertedIndex(idx -> idx.cleanupIntervalSeconds(10))
.replication(repl -> repl.asyncEnabled(true)));
var things = client.collections.use(nsThings);
// Act
things.config.update(c -> c
.description("Things stored on shelves")
.propertyDescription("width", "not height")
.invertedIndex(idx -> idx.cleanupIntervalSeconds(30))
.replication(repl -> repl.asyncEnabled(false)));
// Assert
var updated = things.config.get();
Assertions.assertThat(updated).get()
.returns("Things stored on shelves", CollectionConfig::description)
.satisfies(collection -> {
Assertions.assertThat(collection)
.extracting(CollectionConfig::properties, InstanceOfAssertFactories.list(Property.class))
.extracting(Property::description).contains("not height");
Assertions.assertThat(collection)
.extracting(CollectionConfig::invertedIndex).returns(30, InvertedIndex::cleanupIntervalSeconds);
Assertions.assertThat(collection)
.extracting(CollectionConfig::replication).returns(false, Replication::asyncEnabled);
});
}
@Test
public void testShards() throws IOException {
var nsShatteredCups = ns("ShatteredCups");
client.collections.create(nsShatteredCups);
var cups = client.collections.use(nsShatteredCups);
// Act: get initial shard state
var shards = cups.config.getShards();
Assertions.assertThat(shards).as("single-tenant collections has 1 shard").hasSize(1);
var singleShard = shards.get(0);
// Act: flip the status
var wantStatus = singleShard.status().equals("READY") ? ShardStatus.READONLY : ShardStatus.READY;
var updated = cups.config.updateShards(wantStatus, singleShard.name());
Assertions.assertThat(updated)
.as("shard status changed")
.hasSize(1)
.extracting(Shard::status)
.containsOnly(wantStatus.name());
}
@Test(expected = WeaviateApiException.class)
public void testInvalidCollectionName() throws IOException {
client.collections.create("^collection@weaviate.io$");
}
@Test
public void testNestedProperties() throws IOException {
var nsBuildings = ns("Buildings");
client.collections.create(
nsBuildings, c -> c.properties(
Property.object("address", p -> p.nestedProperties(
Property.text("street"),
Property.integer("building_nr"),
Property.bool("isOneWay"))),
Property.objectArray("apartments", p -> p.nestedProperties(
Property.integer("door_nr"),
Property.number("area")))));
var config = client.collections.getConfig(nsBuildings);
var properties = Assertions.assertThat(config).get()
.extracting(CollectionConfig::properties, InstanceOfAssertFactories.list(Property.class))
.hasSize(2).actual();
Assertions.assertThat(properties.get(0))
.returns("address", Property::propertyName)
.returns(DataType.OBJECT, p -> p.dataTypes().get(0))
.extracting(Property::nestedProperties, InstanceOfAssertFactories.list(Property.class))
.extracting(Property::dataTypes).extracting(types -> types.get(0))
.containsExactly(DataType.TEXT, DataType.INT, DataType.BOOL);
Assertions.assertThat(properties.get(1))
.returns("apartments", Property::propertyName)
.returns(DataType.OBJECT_ARRAY, p -> p.dataTypes().get(0))
.extracting(Property::nestedProperties, InstanceOfAssertFactories.list(Property.class))
.extracting(Property::dataTypes).extracting(types -> types.get(0))
.containsExactly(DataType.INT, DataType.NUMBER);
}
@Test
public void test_updateQuantization_uncompressed() throws IOException {
// Arrange
var nsThings = ns("Things");
var things = client.collections.create(nsThings,
c -> c.vectorConfig(VectorConfig.selfProvided(
self -> self.quantization(Quantization.uncompressed()))));
// Act
things.config.update(
c -> c.vectorConfig(VectorConfig.selfProvided(
self -> self.quantization(Quantization.bq()))));
// Assert
var config = things.config.get();
Assertions.assertThat(config).get()
.extracting(CollectionConfig::vectors)
.extracting("default", InstanceOfAssertFactories.type(VectorConfig.class))
.extracting(VectorConfig::quantization)
.returns(Quantization.Kind.BQ, Quantization::_kind);
}
@Test
public void test_updateGenerative() throws IOException {
// Arrange
var nsThings = ns("Things");
var things = client.collections.create(nsThings,
c -> c.vectorConfig(VectorConfig.selfProvided()));
// Act
things.config.update(c -> c.generativeModule(new DummyGenerative()));
// Assert
var config = things.config.get();
Assertions.assertThat(config).get()
.extracting(CollectionConfig::generativeModule).isNotNull()
.returns(Generative.Kind.DUMMY, Generative::_kind);
}
@Test
public void test_objectTtl() throws IOException {
Weaviate.Version.V135.orSkip();
// Arrange
var nsThings = ns("Things");
// Act: create collection
var things = client.collections.create(nsThings,
c -> c.objectTtl(ttl -> ttl
.deleteByCreationTime()
.defaultTtlSeconds(120)));
// Assert: correct Object TTL config
var created = things.config.get();
Assertions.assertThat(created).get()
.as("created collection")
.extracting(CollectionConfig::objectTtl).isNotNull()
.returns(true, ObjectTtl::enabled)
.returns(BaseQueryOptions.CREATION_TIME_PROPERTY, ObjectTtl::deleteOn)
.returns(120, ObjectTtl::defaultTtlSeconds);
// Act: update TTL config
things.config.update(
c -> c.objectTtl(ttl -> ttl
.deleteByUpdateTime()
.defaultTtlSeconds(400)));
// Assert: correct Object TTL config
var updated = things.config.get();
Assertions.assertThat(updated).get()
.as("updated collection")
.extracting(CollectionConfig::objectTtl).isNotNull()
.returns(true, ObjectTtl::enabled)
.returns(BaseQueryOptions.LAST_UPDATE_TIME_PROPERTY, ObjectTtl::deleteOn)
.returns(400, ObjectTtl::defaultTtlSeconds);
// Act: disable TTL config
things.config.update(c -> c.objectTtl(ttl -> ttl.enabled(false)));
// Assert: correct Object TTL config
var disabled = things.config.get();
Assertions.assertThat(disabled).get()
.as("disabled object TTL")
.extracting(CollectionConfig::objectTtl).isNotNull()
.returns(false, ObjectTtl::enabled);
}
}