Skip to content

Adds model unit tests and Update Policy schema validation #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

sdseaton
Copy link
Contributor

This pull request adds comprehensive unit tests to validate the behavior of various models and utilities in the KustoSchemaTools project. The changes include new test classes for models such as AADObject, Cluster, Database, ContinuousExport, and Function, as well as for utilities like KustoQuerySchemaExtractor. Additionally, the FluentAssertions library has been added as a dependency to improve test readability and assertion capabilities.

Dependency Addition:

  • Added FluentAssertions package to the test project for expressive assertion syntax (KustoSchemaTools.Tests.csproj).

Model-Specific Unit Tests:

AADObject Model:

  • Added tests to verify default initialization, property assignment, and equality comparison for AADObject.

Cluster Model:

  • Added tests to validate default initialization, property assignment, and handling of scripts for Cluster.

ContinuousExport Model:

  • Added tests to check default values, property assignment, and script generation for ContinuousExport.

Database Model:

  • Added tests to confirm default initialization and property assignment for Database, including handling of collections like Admins and Tables.

Function Model:

  • Added tests to verify default values, property assignment, and script generation for Function.

Query Schema Extraction and Validation:

  • Added comprehensive tests for KustoQuerySchemaExtractor to validate schema extraction, column references, and query validation, including handling of syntax errors, type conversions, and operations like summarize and join.

@Copilot Copilot AI review requested due to automatic review settings June 19, 2025 15:42
@sdseaton sdseaton changed the title Feature/up validation Adds model unit tests and Update Policy schema validation Jun 19, 2025
foreach (var targetColumn in targetTable.Columns!)
{
// If the query explicitly projects this column, validate its type compatibility
if (queryColumns.ContainsKey(targetColumn.Key))

Check notice

Code scanning / CodeQL

Inefficient use of ContainsKey Note

Inefficient use of 'ContainsKey' and
indexer
.

Copilot Autofix

AI about 22 hours ago

To fix the issue, replace the ContainsKey check followed by the indexer access with a single call to TryGetValue. This eliminates the redundant dictionary lookup and improves efficiency. Specifically, modify the code on line 269 to use TryGetValue to check for the existence of the key and retrieve its value in one operation. Ensure that the retrieved value is used in subsequent logic.

Suggested changeset 1
KustoSchemaTools/Model/UpdatePolicyValidator.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/KustoSchemaTools/Model/UpdatePolicyValidator.cs b/KustoSchemaTools/Model/UpdatePolicyValidator.cs
--- a/KustoSchemaTools/Model/UpdatePolicyValidator.cs
+++ b/KustoSchemaTools/Model/UpdatePolicyValidator.cs
@@ -268,5 +268,4 @@
                 // If the query explicitly projects this column, validate its type compatibility
-                if (queryColumns.ContainsKey(targetColumn.Key))
+                if (queryColumns.TryGetValue(targetColumn.Key, out var queryColumnType))
                 {
-                    var queryColumnType = queryColumns[targetColumn.Key];
                     if (!AreTypesCompatible(queryColumnType, targetColumn.Value, config))
@@ -281,3 +280,3 @@
             {
-                if (!targetTable.Columns.ContainsKey(queryColumn.Key))
+                if (!targetTable.Columns.TryGetValue(queryColumn.Key, out _))
                 {
EOF
@@ -268,5 +268,4 @@
// If the query explicitly projects this column, validate its type compatibility
if (queryColumns.ContainsKey(targetColumn.Key))
if (queryColumns.TryGetValue(targetColumn.Key, out var queryColumnType))
{
var queryColumnType = queryColumns[targetColumn.Key];
if (!AreTypesCompatible(queryColumnType, targetColumn.Value, config))
@@ -281,3 +280,3 @@
{
if (!targetTable.Columns.ContainsKey(queryColumn.Key))
if (!targetTable.Columns.TryGetValue(queryColumn.Key, out _))
{
Copilot is powered by AI and may make mistakes. Always verify output.
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the KustoSchemaTools project by introducing parser-based update policy validation, extending script generation methods, and adding comprehensive unit tests with FluentAssertions.

  • Added UpdatePolicyValidationConfig documentation and config options for strict vs. permissive validation.
  • Extended model APIs (Table, TablePolicy) to support optional update policy validation during script creation.
  • Introduced KustoQuerySchemaExtractor and EnhancedUpdatePolicyValidator for KQL parsing-based schema extraction and validation.
  • Added a wide suite of unit tests across models, utilities, and integration flows, and integrated FluentAssertions.

Reviewed Changes

Copilot reviewed 22 out of 25 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
docs/README_UpdatePolicyValidationConfig.md New guide for UpdatePolicyValidationConfig options and usage examples
KustoSchemaTools/Model/Table.cs Extended CreateScripts to accept database context and validate policies
KustoSchemaTools/Model/Policy.cs Added ValidateUpdatePolicies and script overloads with optional validation
KustoSchemaTools/Model/KustoQuerySchemaExtractor.cs New static class for KQL schema extraction and query validation
KustoSchemaTools/Model/EnhancedUpdatePolicyValidator.cs Parser-based update policy validator integrating new extractor
KustoSchemaTools/Examples/README_KustoParser.md Expanded examples for parser usage
KustoSchemaTools/Examples/KustoParserExamples.cs Added C# code samples demonstrating new parsing and validation features
KustoSchemaTools.Tests/KustoSchemaTools.Tests.csproj Added FluentAssertions dependency for expressive testing assertions
KustoSchemaTools.Tests/ (multiple files) Comprehensive unit tests for models, validators, and parser integration
Comments suppressed due to low confidence (1)

KustoSchemaTools/Examples/README_KustoParser.md:347

  • This example attempts to instantiate EnhancedUpdatePolicyValidator, but it is declared as a static class. Update the example to call the static method directly, e.g. var result = EnhancedUpdatePolicyValidator.ValidatePolicyWithKustoParser(...);.
var enhancedValidator = new EnhancedUpdatePolicyValidator();

/// <param name="database">The database context (optional, for update policy validation)</param>
/// <param name="validateUpdatePolicies">Whether to validate update policies before creating scripts</param>
/// <returns>List of database script containers</returns>
public List<DatabaseScriptContainer> CreateScripts(string name, bool isNew, Database? database = null, bool validateUpdatePolicies = false)
Copy link
Preview

Copilot AI Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isNew parameter is declared but never used in the method body. Consider removing it or implementing logic to handle new vs. existing tables to avoid confusion.

Suggested change
public List<DatabaseScriptContainer> CreateScripts(string name, bool isNew, Database? database = null, bool validateUpdatePolicies = false)
public List<DatabaseScriptContainer> CreateScripts(string name, Database? database = null, bool validateUpdatePolicies = false)

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant