Skip to content

Commit 5ab846a

Browse files
Copilotaaronburtle
andcommitted
Remove null from level enum and add schema validation tests
Co-authored-by: aaronburtle <93220300+aaronburtle@users.noreply.github.com>
1 parent 6627bb0 commit 5ab846a

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

schemas/dab.draft.schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@
813813
"level": {
814814
"type": "string",
815815
"description": "Cache level (L1 or L1L2)",
816-
"enum": [ "L1", "L1L2", null ],
816+
"enum": [ "L1", "L1L2" ],
817817
"default": "L1L2"
818818
}
819819
}
@@ -1137,7 +1137,7 @@
11371137
"level": {
11381138
"type": "string",
11391139
"description": "Cache level (L1 or L1L2)",
1140-
"enum": [ "L1", "L1L2", null ],
1140+
"enum": [ "L1", "L1L2" ],
11411141
"default": "L1L2"
11421142
}
11431143
}

src/Service.Tests/Configuration/ConfigurationTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,63 @@ public void TestBasicConfigSchemaWithNoEntityFieldsIsInvalid()
19661966
Assert.IsTrue(result.ErrorMessage.Contains("Total schema validation errors: 1\n> Required properties are missing from object: entities."));
19671967
}
19681968

1969+
/// <summary>
1970+
/// Validates that the JSON schema correctly validates entity cache configuration properties.
1971+
/// Tests both valid configurations (proper level values, ttl >= 1) and invalid configurations
1972+
/// (invalid level values, ttl = 0).
1973+
/// </summary>
1974+
[DataTestMethod]
1975+
[DataRow("L1", 10, true, DisplayName = "Valid cache config with L1 and ttl=10")]
1976+
[DataRow("L1L2", 1, true, DisplayName = "Valid cache config with L1L2 and minimum ttl=1")]
1977+
[DataRow("L1L2", 3600, true, DisplayName = "Valid cache config with L1L2 and ttl=3600")]
1978+
[DataRow("L3", 10, false, DisplayName = "Invalid cache config with invalid level L3")]
1979+
[DataRow("L1", 0, false, DisplayName = "Invalid cache config with ttl=0 (below minimum)")]
1980+
[DataRow("L1L2", -1, false, DisplayName = "Invalid cache config with negative ttl")]
1981+
public void TestEntityCacheSchemaValidation(string level, int ttlSeconds, bool shouldBeValid)
1982+
{
1983+
string jsonData = $@"{{
1984+
""$schema"": ""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch/dab.draft.schema.json"",
1985+
""data-source"": {{
1986+
""database-type"": ""mssql"",
1987+
""connection-string"": ""Server=test;Database=test;""
1988+
}},
1989+
""entities"": {{
1990+
""Book"": {{
1991+
""source"": {{
1992+
""object"": ""books"",
1993+
""type"": ""table""
1994+
}},
1995+
""permissions"": [{{
1996+
""role"": ""anonymous"",
1997+
""actions"": [""read""]
1998+
}}],
1999+
""cache"": {{
2000+
""enabled"": true,
2001+
""ttl-seconds"": {ttlSeconds},
2002+
""level"": ""{level}""
2003+
}}
2004+
}}
2005+
}}
2006+
}}";
2007+
2008+
Mock<ILogger<JsonConfigSchemaValidator>> schemaValidatorLogger = new();
2009+
string jsonSchema = File.ReadAllText("dab.draft.schema.json");
2010+
JsonConfigSchemaValidator jsonSchemaValidator = new(schemaValidatorLogger.Object, new MockFileSystem());
2011+
2012+
JsonSchemaValidationResult result = jsonSchemaValidator.ValidateJsonConfigWithSchema(jsonSchema, jsonData);
2013+
2014+
if (shouldBeValid)
2015+
{
2016+
Assert.IsTrue(result.IsValid, $"Expected valid config but got errors: {result.ErrorMessage}");
2017+
Assert.IsTrue(EnumerableUtilities.IsNullOrEmpty(result.ValidationErrors));
2018+
}
2019+
else
2020+
{
2021+
Assert.IsFalse(result.IsValid, "Expected validation to fail but it passed");
2022+
Assert.IsFalse(EnumerableUtilities.IsNullOrEmpty(result.ValidationErrors));
2023+
}
2024+
}
2025+
19692026
/// <summary>
19702027
/// This test tries to validate a runtime config file that is not compliant with the runtime config JSON schema.
19712028
/// It validates no additional properties are defined in the config file.

0 commit comments

Comments
 (0)