-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeaviateContextDITests.cs
More file actions
321 lines (268 loc) · 10.2 KB
/
WeaviateContextDITests.cs
File metadata and controls
321 lines (268 loc) · 10.2 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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Weaviate.Client.DependencyInjection;
using Xunit;
namespace Weaviate.Client.Managed.Tests.DependencyInjection;
public class WeaviateContextDITests
{
[Fact]
public void AddWeaviateContext_RegistersContextType()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>();
// Act
var provider = services.BuildServiceProvider();
var context = provider.GetService<TestStoreContext>();
// Assert
Assert.NotNull(context);
Assert.NotNull(context.Products);
}
[Fact]
public void AddWeaviateContext_PropagatesOptions()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>(options =>
{
options.AutoCreateCollections = true;
options.AutoMigrate = true;
options.AllowBreakingMigrations = true;
});
// Act
var provider = services.BuildServiceProvider();
var context = provider.GetRequiredService<TestStoreContext>();
// Assert
Assert.True(context.Options.AutoCreateCollections);
Assert.True(context.Options.AutoMigrate);
Assert.True(context.Options.AllowBreakingMigrations);
}
[Fact]
public void AddWeaviateContext_OnConfiguringCanOverrideOptions()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<OverridingContext>(options =>
{
options.AutoMigrate = false;
});
// Act
var provider = services.BuildServiceProvider();
var context = provider.GetRequiredService<OverridingContext>();
// Assert - OnConfiguring enables AutoMigrate regardless of DI config
Assert.True(context.Options.AutoMigrate);
}
[Fact]
public void AddWeaviateContext_DefaultLifetimeIsSingleton()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>();
var provider = services.BuildServiceProvider();
// Act
var context1 = provider.GetRequiredService<TestStoreContext>();
var context2 = provider.GetRequiredService<TestStoreContext>();
// Assert - same instance
Assert.Same(context1, context2);
}
[Fact]
public async Task AddWeaviateContext_ScopedLifetimeProducesDifferentInstances()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>(lifetime: ServiceLifetime.Scoped);
var provider = services.BuildServiceProvider();
// Act
TestStoreContext context1;
TestStoreContext context2;
await using (var scope1 = provider.CreateAsyncScope())
{
context1 = scope1.ServiceProvider.GetRequiredService<TestStoreContext>();
}
await using (var scope2 = provider.CreateAsyncScope())
{
context2 = scope2.ServiceProvider.GetRequiredService<TestStoreContext>();
}
// Assert - different instances per scope
Assert.NotSame(context1, context2);
}
[Fact]
public async Task AddWeaviateContext_ScopedReturnsSameWithinScope()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>(lifetime: ServiceLifetime.Scoped);
var provider = services.BuildServiceProvider();
// Act
await using var scope = provider.CreateAsyncScope();
var context1 = scope.ServiceProvider.GetRequiredService<TestStoreContext>();
var context2 = scope.ServiceProvider.GetRequiredService<TestStoreContext>();
// Assert - same within a scope
Assert.Same(context1, context2);
}
[Fact]
public void AddWeaviateContext_MultipleContextTypes_RegisteredIndependently()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>(options =>
{
options.AutoMigrate = true;
});
services.AddWeaviateContext<SecondContext>(options =>
{
options.AutoMigrate = false;
});
// Act
var provider = services.BuildServiceProvider();
var storeContext = provider.GetRequiredService<TestStoreContext>();
var secondContext = provider.GetRequiredService<SecondContext>();
// Assert - different types with independent options
Assert.True(storeContext.Options.AutoMigrate);
Assert.False(secondContext.Options.AutoMigrate);
}
[Fact]
public void AddWeaviateContext_WithoutWeaviateClient_ThrowsMeaningfulError()
{
// Arrange - no AddWeaviate* call
var services = new ServiceCollection();
services.AddWeaviateContext<TestStoreContext>();
// Act & Assert
var provider = services.BuildServiceProvider();
var ex = Assert.Throws<InvalidOperationException>(() =>
provider.GetRequiredService<TestStoreContext>()
);
Assert.Contains(nameof(WeaviateClient), ex.Message);
}
[Fact]
public void AddWeaviateContext_WithoutOptionsConstructor_StillResolves()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<SingleCtorContext>();
// Act
var provider = services.BuildServiceProvider();
var context = provider.GetService<SingleCtorContext>();
// Assert
Assert.NotNull(context);
}
[Fact]
public void AddWeaviateContext_WithoutOptions_UsesDefaults()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>();
// Act
var provider = services.BuildServiceProvider();
var context = provider.GetRequiredService<TestStoreContext>();
// Assert - all defaults
Assert.False(context.Options.AutoCreateCollections);
Assert.False(context.Options.AutoMigrate);
Assert.False(context.Options.AllowBreakingMigrations);
}
[Fact]
public void AddWeaviateContext_Idempotent_DoesNotDoubleRegister()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>();
services.AddWeaviateContext<TestStoreContext>(); // second call
// Act
var provider = services.BuildServiceProvider();
var contexts = provider.GetServices<TestStoreContext>().ToList();
// Assert - only one registration
Assert.Single(contexts);
}
[Fact]
public void AddWeaviateContext_ExposesUnderlyingClient()
{
// Arrange
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>();
// Act
var provider = services.BuildServiceProvider();
var context = provider.GetRequiredService<TestStoreContext>();
var client = provider.GetRequiredService<WeaviateClient>();
// Assert
Assert.Same(client, context.Client);
}
[Fact]
public void AddWeaviateContext_SetsIntegrationHeader()
{
var services = new ServiceCollection();
services.AddWeaviateLocal(eagerInitialization: false);
services.AddWeaviateContext<TestStoreContext>();
var provider = services.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<WeaviateOptions>>().Value;
Assert.NotNull(options.Headers);
Assert.True(options.Headers.ContainsKey("X-Weaviate-Client-Integration"));
Assert.Matches(
@"^weaviate-client-csharp-managed/\d+",
options.Headers["X-Weaviate-Client-Integration"]
);
}
#region Test Types
[WeaviateCollection("Products")]
public class Product
{
public string Name { get; set; } = string.Empty;
}
[WeaviateCollection("Categories")]
public class Category
{
public string Name { get; set; } = string.Empty;
}
public class TestStoreContext : WeaviateContext
{
public TestStoreContext(WeaviateClient client)
: base(client) { }
public TestStoreContext(
WeaviateClient client,
WeaviateContextOptions<TestStoreContext> options
)
: base(client, options) { }
public CollectionSet<Product> Products { get; set; } = null!;
}
public class SecondContext : WeaviateContext
{
public SecondContext(WeaviateClient client)
: base(client) { }
public SecondContext(WeaviateClient client, WeaviateContextOptions<SecondContext> options)
: base(client, options) { }
public CollectionSet<Category> Categories { get; set; } = null!;
}
public class OverridingContext : WeaviateContext
{
public OverridingContext(WeaviateClient client)
: base(client) { }
public OverridingContext(
WeaviateClient client,
WeaviateContextOptions<OverridingContext> options
)
: base(client, options) { }
protected override void OnConfiguring(WeaviateContextOptionsBuilder options)
{
options.UseAutoMigrate(true);
}
}
/// <summary>
/// Context with only the single-parameter constructor (backward compat).
/// </summary>
public class SingleCtorContext : WeaviateContext
{
public SingleCtorContext(WeaviateClient client)
: base(client) { }
}
#endregion
}