22using KustoSchemaTools . Parser ;
33using KustoSchemaTools . Plugins ;
44using KustoSchemaTools . Model ;
5+ using KustoSchemaTools . Changes ;
6+ using Kusto . Data ;
7+ using System . IO ;
58
69namespace KustoSchemaTools . Tests . Parser
710{
@@ -17,27 +20,158 @@ public async Task GetDatabase()
1720 var factory = new YamlDatabaseHandlerFactory < Model . Database > ( )
1821 . WithPlugin ( new TablePlugin ( ) )
1922 . WithPlugin ( new FunctionPlugin ( ) )
20- . WithPlugin ( new MaterializedViewsPlugin ( ) )
2123 . WithPlugin ( new DatabaseCleanup ( ) ) ;
2224 var loader = factory . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
2325
2426 var db = await loader . LoadAsync ( ) ;
2527
2628 Assert . NotNull ( db ) ;
2729 Assert . Equal ( 2 , db . Tables . Count ) ;
28- Assert . Single ( db . Functions ) ;
2930 Assert . Equal ( 6 , db . Functions [ "UP" ] . Body . RowLength ( ) ) ;
3031 Assert . Equal ( "DemoDatabase" , db . Name ) ;
31- var policies = db . Tables [ "sourceTable" ] . Policies ;
32- Assert . NotNull ( policies ) ;
33- Assert . Equal ( "120d" , policies . Retention ) ;
34- Assert . Equal ( "120d" , policies . HotCache ) ;
35- Assert . Equal ( "Test team" , db . Team ) ;
36- Assert . True ( db . Tables [ "sourceTable" ] . RestrictedViewAccess ) ;
37-
38- // these tests do not compile! to be removed in a future PR.
39- // Assert.Equal("120d", db.Tables["tableWithUp"].RetentionAndCachePolicy.Retention);
40- // Assert.Equal("120d", db.Tables["sourceTable"].RetentionAndCachePolicy.HotCache);
32+
33+ var st = db . Tables [ "sourceTable" ] ;
34+ Assert . NotNull ( st ) ;
35+ Assert . NotNull ( st . Policies ) ;
36+ Assert . True ( st . Policies ! . RestrictedViewAccess ) ;
37+ Assert . Equal ( "120d" , st . Policies ? . HotCache ) ;
38+
39+ var tt = db . Tables [ "tableWithUp" ] ;
40+ Assert . NotNull ( tt ) ;
41+ Assert . NotNull ( tt . Policies ) ;
42+ Assert . False ( tt . Policies ! . RestrictedViewAccess ) ;
43+ Assert . Equal ( "120d" , tt . Policies ? . Retention ) ;
44+ }
45+
46+ [ Fact ]
47+ public async Task VerifyFunctionPreformatted ( )
48+ {
49+ // WITHOUT the DatabaseCleanup plugin
50+ var factoryWithoutCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
51+ . WithPlugin ( new TablePlugin ( ) )
52+ . WithPlugin ( new FunctionPlugin ( ) ) ;
53+ // DatabaseCleanup intentionally omitted
54+ var loaderWithoutCleanup = factoryWithoutCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
55+ var dbWithoutCleanup = await loaderWithoutCleanup . LoadAsync ( ) ;
56+
57+ // with the DatabaseCleanup plugin
58+ var factoryWithCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
59+ . WithPlugin ( new TablePlugin ( ) )
60+ . WithPlugin ( new FunctionPlugin ( ) )
61+ . WithPlugin ( new MaterializedViewsPlugin ( ) )
62+ . WithPlugin ( new DatabaseCleanup ( ) ) ;
63+ var loaderWithCleanup = factoryWithCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
64+ var dbWithCleanup = await loaderWithCleanup . LoadAsync ( ) ;
65+
66+ // Assert
67+ Assert . NotNull ( dbWithCleanup ) ;
68+ Assert . NotNull ( dbWithoutCleanup ) ;
69+ Assert . Equal ( dbWithCleanup . Functions . Count , dbWithoutCleanup . Functions . Count ) ;
70+
71+ // Verify the UP function has preformatted set to false (default)
72+ var up_withCleanup = dbWithCleanup . Functions [ "UP" ] ;
73+ var up_withoutCleanup = dbWithoutCleanup . Functions [ "UP" ] ;
74+ Assert . NotNull ( up_withCleanup ) ;
75+ Assert . NotNull ( up_withoutCleanup ) ;
76+ Assert . False ( up_withCleanup . Preformatted ) ;
77+ Assert . False ( up_withoutCleanup . Preformatted ) ;
78+
79+ // this case is simple and formatting has no impact.
80+ Assert . Equal ( up_withoutCleanup . Body . RowLength ( ) , up_withCleanup . Body . RowLength ( ) ) ;
81+
82+ // Verify the needs_formatting query changed when formatting.
83+ var f_withCleanup = dbWithCleanup . Functions [ "needs_formatting" ] ;
84+ var f_withoutCleanup = dbWithoutCleanup . Functions [ "needs_formatting" ] ;
85+ Assert . NotNull ( f_withCleanup ) ;
86+ Assert . NotNull ( f_withoutCleanup ) ;
87+ Assert . False ( f_withCleanup . Preformatted ) ;
88+ Assert . False ( f_withoutCleanup . Preformatted ) ;
89+
90+ // preformatted function should have been formatted by DatabaseCleanup
91+ Assert . NotEqual ( f_withCleanup . Body , f_withoutCleanup . Body ) ;
92+
93+ // much more complicated function where formatting breaks the query
94+ var complicated_with_cleanup = dbWithCleanup . Functions [ "complicated" ] . Body ;
95+ var complicated_without_cleanup = dbWithoutCleanup . Functions [ "complicated" ] . Body ;
96+ Assert . NotEqual ( complicated_with_cleanup , complicated_without_cleanup ) ;
97+
98+ var complicated_pf_with_cleanup = dbWithCleanup . Functions [ "complicated_preformatted" ] . Body ;
99+ var complicated_pf_without_cleanup = dbWithoutCleanup . Functions [ "complicated_preformatted" ] . Body ;
100+
101+ // preformatted option makes query match non-formatted version
102+ Assert . Equal ( complicated_pf_without_cleanup , complicated_pf_with_cleanup ) ;
103+
104+ // preformatted option makes query match non-formatted version
105+ Assert . Equal ( complicated_without_cleanup , complicated_pf_with_cleanup ) ;
106+ }
107+
108+ [ Fact ]
109+ public async Task VerifyMaterializedView ( )
110+ {
111+ // WITHOUT the DatabaseCleanup plugin
112+ var factoryWithoutCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
113+ . WithPlugin ( new TablePlugin ( ) )
114+ . WithPlugin ( new MaterializedViewsPlugin ( ) ) ;
115+ // DatabaseCleanup intentionally omitted
116+ var loaderWithoutCleanup = factoryWithoutCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
117+ var dbWithoutCleanup = await loaderWithoutCleanup . LoadAsync ( ) ;
118+
119+ // with the DatabaseCleanup plugin
120+ var factoryWithCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
121+ . WithPlugin ( new TablePlugin ( ) )
122+ . WithPlugin ( new MaterializedViewsPlugin ( ) )
123+ . WithPlugin ( new DatabaseCleanup ( ) ) ;
124+ var loaderWithCleanup = factoryWithCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
125+ var dbWithCleanup = await loaderWithCleanup . LoadAsync ( ) ;
126+
127+ // Assert
128+ Assert . NotNull ( dbWithCleanup ) ;
129+ Assert . NotNull ( dbWithoutCleanup ) ;
130+ Assert . Equal ( dbWithCleanup . MaterializedViews . Count , dbWithoutCleanup . MaterializedViews . Count ) ;
131+
132+ // basic materialized view tests
133+ void AssertMaterializedView (
134+ string file_name ,
135+ bool should_match )
136+ {
137+ var mv_with_cleanup = dbWithCleanup . MaterializedViews [ file_name ] ;
138+ var mv_without_cleanup = dbWithoutCleanup . MaterializedViews [ file_name ] ;
139+ Assert . NotNull ( mv_with_cleanup ) ;
140+ Assert . NotNull ( mv_without_cleanup ) ;
141+ Assert . Equal ( should_match , mv_without_cleanup . Query == mv_with_cleanup . Query ) ;
142+
143+ Assert . DoesNotContain ( "Preformatted" , mv_with_cleanup . Query ) ;
144+ Assert . DoesNotContain ( "Preformatted" , mv_without_cleanup . Query ) ;
145+ }
146+ AssertMaterializedView ( "mv" , false ) ;
147+ AssertMaterializedView ( "mv_preformatted" , true ) ;
148+ }
149+
150+ [ Fact ]
151+ public async Task VerifyFunctionWithCommentAtEnd ( )
152+ {
153+ // This test verifies that functions with comments at the end without a newline
154+ // are handled correctly when scripts are generated
155+
156+ // Arrange - First load the database
157+ var factory = new YamlDatabaseHandlerFactory < Model . Database > ( )
158+ . WithPlugin ( new TablePlugin ( ) )
159+ . WithPlugin ( new FunctionPlugin ( ) )
160+ . WithPlugin ( new DatabaseCleanup ( ) ) ;
161+ var loader = factory . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
162+
163+ // Act - Load the database
164+ var db = await loader . LoadAsync ( ) ;
165+ var commentEndFunction = db . Functions [ "COMMENT_END" ] ;
166+ Assert . NotNull ( commentEndFunction ) ;
167+
168+ // Generate the script container for the function
169+ var scriptContainers = commentEndFunction . CreateScripts ( "COMMENT_END" , false ) ;
170+ Assert . Single ( scriptContainers ) ;
171+
172+ var script = scriptContainers [ 0 ] . Script . Text ;
173+ var expected = ".create-or-alter function with(SkipValidation=```False```, View=```False```, Folder=```test```, DocString=```Function with comment at end```) COMMENT_END () { sourceTable\n | limit 100\n | where IsNotEmpty(EventId) // this is a comment at the end\n }" ;
174+ Assert . Equal ( expected , script ) ;
41175 }
42176 }
43- }
177+ }
0 commit comments