-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathAutoConfigTests.cs
More file actions
291 lines (249 loc) · 11.5 KB
/
AutoConfigTests.cs
File metadata and controls
291 lines (249 loc) · 11.5 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Cli.Tests;
/// <summary>
/// Tests for the auto-config CLI command.
/// </summary>
[TestClass]
public class AutoConfigTests
{
private IFileSystem? _fileSystem;
private FileSystemRuntimeConfigLoader? _runtimeConfigLoader;
[TestInitialize]
public void TestInitialize()
{
_fileSystem = FileSystemUtils.ProvisionMockFileSystem();
_runtimeConfigLoader = new FileSystemRuntimeConfigLoader(_fileSystem);
ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory();
ConfigGenerator.SetLoggerForCliConfigGenerator(loggerFactory.CreateLogger<ConfigGenerator>());
SetCliUtilsLogger(loggerFactory.CreateLogger<Utils>());
}
[TestCleanup]
public void TestCleanup()
{
_fileSystem = null;
_runtimeConfigLoader = null;
}
/// <summary>
/// Tests that a new autoentities definition is successfully created with patterns.
/// </summary>
[TestMethod]
public void TestCreateAutoentitiesDefinition_WithPatterns()
{
// Arrange
InitOptions initOptions = CreateBasicInitOptionsForMsSqlWithConfig(config: TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(ConfigGenerator.TryGenerateConfig(initOptions, _runtimeConfigLoader!, _fileSystem!));
AutoConfigOptions options = new(
definitionName: "test-def",
patternsInclude: new[] { "dbo.%", "sys.%" },
patternsExclude: new[] { "dbo.internal%" },
patternsName: "{schema}_{table}",
config: TEST_RUNTIME_CONFIG_FILE
);
// Act
bool success = ConfigGenerator.TryConfigureAutoentities(options, _runtimeConfigLoader!, _fileSystem!);
// Assert
Assert.IsTrue(success);
Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? config));
Assert.IsNotNull(config.Autoentities);
Assert.IsTrue(config.Autoentities.Autoentities.ContainsKey("test-def"));
Autoentity autoentity = config.Autoentities.Autoentities["test-def"];
Assert.AreEqual(2, autoentity.Patterns.Include.Length);
Assert.AreEqual("dbo.%", autoentity.Patterns.Include[0]);
Assert.AreEqual("sys.%", autoentity.Patterns.Include[1]);
Assert.AreEqual(1, autoentity.Patterns.Exclude.Length);
Assert.AreEqual("dbo.internal%", autoentity.Patterns.Exclude[0]);
Assert.AreEqual("{schema}_{table}", autoentity.Patterns.Name);
}
/// <summary>
/// Tests that template options are correctly configured for an autoentities definition.
/// </summary>
[TestMethod]
public void TestConfigureAutoentitiesDefinition_WithTemplateOptions()
{
// Arrange
InitOptions initOptions = CreateBasicInitOptionsForMsSqlWithConfig(config: TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(ConfigGenerator.TryGenerateConfig(initOptions, _runtimeConfigLoader!, _fileSystem!));
AutoConfigOptions options = new(
definitionName: "test-def",
templateRestEnabled: true,
templateGraphqlEnabled: false,
templateMcpDmlTools: "true",
templateCacheEnabled: true,
templateCacheTtlSeconds: 30,
templateCacheLevel: "L1",
templateHealthEnabled: true,
config: TEST_RUNTIME_CONFIG_FILE
);
// Act
bool success = ConfigGenerator.TryConfigureAutoentities(options, _runtimeConfigLoader!, _fileSystem!);
// Assert
Assert.IsTrue(success);
Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? config));
Autoentity autoentity = config.Autoentities!.Autoentities["test-def"];
Assert.IsTrue(autoentity.Template.Rest.Enabled);
Assert.IsFalse(autoentity.Template.GraphQL.Enabled);
Assert.IsTrue(autoentity.Template.Mcp!.DmlToolEnabled);
Assert.AreEqual(true, autoentity.Template.Cache.Enabled);
Assert.AreEqual(30, autoentity.Template.Cache.TtlSeconds);
Assert.AreEqual(EntityCacheLevel.L1, autoentity.Template.Cache.Level);
Assert.IsTrue(autoentity.Template.Health.Enabled);
}
/// <summary>
/// Tests that an existing autoentities definition is successfully updated.
/// </summary>
[TestMethod]
public void TestUpdateExistingAutoentitiesDefinition()
{
// Arrange
InitOptions initOptions = CreateBasicInitOptionsForMsSqlWithConfig(config: TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(ConfigGenerator.TryGenerateConfig(initOptions, _runtimeConfigLoader!, _fileSystem!));
// Create initial definition
AutoConfigOptions initialOptions = new(
definitionName: "test-def",
patternsInclude: new[] { "dbo.%" },
templateCacheTtlSeconds: 10,
permissions: new[] { "anonymous", "read" },
config: TEST_RUNTIME_CONFIG_FILE
);
Assert.IsTrue(ConfigGenerator.TryConfigureAutoentities(initialOptions, _runtimeConfigLoader!, _fileSystem!));
// Update definition
AutoConfigOptions updateOptions = new(
definitionName: "test-def",
patternsExclude: new[] { "dbo.internal%" },
templateCacheTtlSeconds: 60,
permissions: new[] { "authenticated", "create,read,update,delete" },
config: TEST_RUNTIME_CONFIG_FILE
);
// Act
bool success = ConfigGenerator.TryConfigureAutoentities(updateOptions, _runtimeConfigLoader!, _fileSystem!);
// Assert
Assert.IsTrue(success);
Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? config));
Autoentity autoentity = config.Autoentities!.Autoentities["test-def"];
// Include should remain from initial setup
Assert.AreEqual(1, autoentity.Patterns.Include.Length);
Assert.AreEqual("dbo.%", autoentity.Patterns.Include[0]);
// Exclude should be added
Assert.AreEqual(1, autoentity.Patterns.Exclude.Length);
Assert.AreEqual("dbo.internal%", autoentity.Patterns.Exclude[0]);
// Cache TTL should be updated
Assert.AreEqual(60, autoentity.Template.Cache.TtlSeconds);
// Permissions should be replaced
Assert.AreEqual(1, autoentity.Permissions.Length);
Assert.AreEqual("authenticated", autoentity.Permissions[0].Role);
}
/// <summary>
/// Tests that permissions are correctly parsed and applied.
/// </summary>
[TestMethod]
public void TestConfigureAutoentitiesDefinition_WithMultipleActions()
{
// Arrange
InitOptions initOptions = CreateBasicInitOptionsForMsSqlWithConfig(config: TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(ConfigGenerator.TryGenerateConfig(initOptions, _runtimeConfigLoader!, _fileSystem!));
AutoConfigOptions options = new(
definitionName: "test-def",
permissions: new[] { "authenticated", "create,read,update,delete" },
config: TEST_RUNTIME_CONFIG_FILE
);
// Act
bool success = ConfigGenerator.TryConfigureAutoentities(options, _runtimeConfigLoader!, _fileSystem!);
// Assert
Assert.IsTrue(success);
Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? config));
Autoentity autoentity = config.Autoentities!.Autoentities["test-def"];
Assert.AreEqual(1, autoentity.Permissions.Length);
Assert.AreEqual("authenticated", autoentity.Permissions[0].Role);
Assert.AreEqual(4, autoentity.Permissions[0].Actions.Length);
}
/// <summary>
/// Tests that invalid MCP dml-tool value is handled correctly.
/// </summary>
[TestMethod]
public void TestConfigureAutoentitiesDefinition_InvalidMcpDmlTool()
{
// Arrange
InitOptions initOptions = CreateBasicInitOptionsForMsSqlWithConfig(config: TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(ConfigGenerator.TryGenerateConfig(initOptions, _runtimeConfigLoader!, _fileSystem!));
AutoConfigOptions options = new(
definitionName: "test-def",
templateMcpDmlTools: "invalid-value",
permissions: new[] { "anonymous", "read" },
config: TEST_RUNTIME_CONFIG_FILE
);
// Act
bool success = ConfigGenerator.TryConfigureAutoentities(options, _runtimeConfigLoader!, _fileSystem!);
// Assert - Should fail due to invalid MCP value
Assert.IsFalse(success);
}
/// <summary>
/// Tests that invalid cache level value is handled correctly.
/// </summary>
[TestMethod]
public void TestConfigureAutoentitiesDefinition_InvalidCacheLevel()
{
// Arrange
InitOptions initOptions = CreateBasicInitOptionsForMsSqlWithConfig(config: TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(ConfigGenerator.TryGenerateConfig(initOptions, _runtimeConfigLoader!, _fileSystem!));
AutoConfigOptions options = new(
definitionName: "test-def",
templateCacheLevel: "InvalidLevel",
permissions: new[] { "anonymous", "read" },
config: TEST_RUNTIME_CONFIG_FILE
);
// Act
bool success = ConfigGenerator.TryConfigureAutoentities(options, _runtimeConfigLoader!, _fileSystem!);
// Assert - Should fail due to invalid cache level
Assert.IsFalse(success);
}
/// <summary>
/// Tests that multiple autoentities definitions can coexist.
/// </summary>
[TestMethod]
public void TestMultipleAutoentitiesDefinitions()
{
// Arrange
InitOptions initOptions = CreateBasicInitOptionsForMsSqlWithConfig(config: TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(ConfigGenerator.TryGenerateConfig(initOptions, _runtimeConfigLoader!, _fileSystem!));
// Create first definition
AutoConfigOptions options1 = new(
definitionName: "def-1",
patternsInclude: new[] { "dbo.%" },
permissions: new[] { "anonymous", "read" },
config: TEST_RUNTIME_CONFIG_FILE
);
Assert.IsTrue(ConfigGenerator.TryConfigureAutoentities(options1, _runtimeConfigLoader!, _fileSystem!));
// Create second definition
AutoConfigOptions options2 = new(
definitionName: "def-2",
patternsInclude: new[] { "sys.%" },
permissions: new[] { "authenticated", "*" },
config: TEST_RUNTIME_CONFIG_FILE
);
// Act
bool success = ConfigGenerator.TryConfigureAutoentities(options2, _runtimeConfigLoader!, _fileSystem!);
// Assert
Assert.IsTrue(success);
Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? config));
Assert.AreEqual(2, config.Autoentities!.Autoentities.Count);
Assert.IsTrue(config.Autoentities.Autoentities.ContainsKey("def-1"));
Assert.IsTrue(config.Autoentities.Autoentities.ContainsKey("def-2"));
}
/// <summary>
/// Tests that attempting to configure autoentities without a config file fails.
/// </summary>
[TestMethod]
public void TestConfigureAutoentitiesDefinition_NoConfigFile()
{
// Arrange
AutoConfigOptions options = new(
definitionName: "test-def",
permissions: new[] { "anonymous", "read" }
);
// Act
bool success = ConfigGenerator.TryConfigureAutoentities(options, _runtimeConfigLoader!, _fileSystem!);
// Assert
Assert.IsFalse(success);
}
}