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,129 @@ 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 ) ;
30+ Assert . Equal ( 4 , db . Functions . Count ) ;
2931 Assert . Equal ( 6 , db . Functions [ "UP" ] . Body . RowLength ( ) ) ;
3032 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);
33+
34+ var st = db . Tables [ "sourceTable" ] ;
35+ Assert . NotNull ( st ) ;
36+ Assert . NotNull ( st . Policies ) ;
37+ Assert . True ( st . Policies ! . RestrictedViewAccess ) ;
38+ Assert . Equal ( "120d" , st . Policies ? . HotCache ) ;
39+
40+ var tt = db . Tables [ "tableWithUp" ] ;
41+ Assert . NotNull ( tt ) ;
42+ Assert . NotNull ( tt . Policies ) ;
43+ Assert . False ( tt . Policies ! . RestrictedViewAccess ) ;
44+ Assert . Equal ( "120d" , tt . Policies ? . Retention ) ;
45+ }
46+
47+ [ Fact ]
48+ public async Task VerifyFunctionPreformatted ( )
49+ {
50+ // WITHOUT the DatabaseCleanup plugin
51+ var factoryWithoutCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
52+ . WithPlugin ( new TablePlugin ( ) )
53+ . WithPlugin ( new FunctionPlugin ( ) ) ;
54+ // DatabaseCleanup intentionally omitted
55+ var loaderWithoutCleanup = factoryWithoutCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
56+ var dbWithoutCleanup = await loaderWithoutCleanup . LoadAsync ( ) ;
57+
58+ // with the DatabaseCleanup plugin
59+ var factoryWithCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
60+ . WithPlugin ( new TablePlugin ( ) )
61+ . WithPlugin ( new FunctionPlugin ( ) )
62+ . WithPlugin ( new MaterializedViewsPlugin ( ) )
63+ . WithPlugin ( new DatabaseCleanup ( ) ) ;
64+ var loaderWithCleanup = factoryWithCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
65+ var dbWithCleanup = await loaderWithCleanup . LoadAsync ( ) ;
66+
67+ // Assert
68+ Assert . NotNull ( dbWithCleanup ) ;
69+ Assert . NotNull ( dbWithoutCleanup ) ;
70+ Assert . Equal ( dbWithCleanup . Functions . Count , dbWithoutCleanup . Functions . Count ) ;
71+
72+ // Verify the UP function has preformatted set to false (default)
73+ var up_withCleanup = dbWithCleanup . Functions [ "UP" ] ;
74+ var up_withoutCleanup = dbWithoutCleanup . Functions [ "UP" ] ;
75+ Assert . NotNull ( up_withCleanup ) ;
76+ Assert . NotNull ( up_withoutCleanup ) ;
77+ Assert . False ( up_withCleanup . Preformatted ) ;
78+ Assert . False ( up_withoutCleanup . Preformatted ) ;
79+
80+ // this case is simple and formatting has no impact.
81+ Assert . Equal ( up_withoutCleanup . Body . RowLength ( ) , up_withCleanup . Body . RowLength ( ) ) ;
82+
83+ // Verify the needs_formatting query changed when formatting.
84+ var f_withCleanup = dbWithCleanup . Functions [ "needs_formatting" ] ;
85+ var f_withoutCleanup = dbWithoutCleanup . Functions [ "needs_formatting" ] ;
86+ Assert . NotNull ( f_withCleanup ) ;
87+ Assert . NotNull ( f_withoutCleanup ) ;
88+ Assert . False ( f_withCleanup . Preformatted ) ;
89+ Assert . False ( f_withoutCleanup . Preformatted ) ;
90+
91+ // preformatted function should have been formatted by DatabaseCleanup
92+ Assert . NotEqual ( f_withCleanup . Body , f_withoutCleanup . Body ) ;
93+
94+ // much more complicated function where formatting breaks the query
95+ var complicated_with_cleanup = dbWithCleanup . Functions [ "complicated" ] . Body ;
96+ var complicated_without_cleanup = dbWithoutCleanup . Functions [ "complicated" ] . Body ;
97+ Assert . NotEqual ( complicated_with_cleanup , complicated_without_cleanup ) ;
98+
99+ var complicated_pf_with_cleanup = dbWithCleanup . Functions [ "complicated_preformatted" ] . Body ;
100+ var complicated_pf_without_cleanup = dbWithoutCleanup . Functions [ "complicated_preformatted" ] . Body ;
101+
102+ // preformatted option makes query match non-formatted version
103+ Assert . Equal ( complicated_pf_without_cleanup , complicated_pf_with_cleanup ) ;
104+
105+ // preformatted option makes query match non-formatted version
106+ Assert . Equal ( complicated_without_cleanup , complicated_pf_with_cleanup ) ;
107+ }
108+
109+ [ Fact ]
110+ public async Task VerifyMaterializedView ( )
111+ {
112+ // WITHOUT the DatabaseCleanup plugin
113+ var factoryWithoutCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
114+ . WithPlugin ( new TablePlugin ( ) )
115+ . WithPlugin ( new MaterializedViewsPlugin ( ) ) ;
116+ // DatabaseCleanup intentionally omitted
117+ var loaderWithoutCleanup = factoryWithoutCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
118+ var dbWithoutCleanup = await loaderWithoutCleanup . LoadAsync ( ) ;
119+
120+ // with the DatabaseCleanup plugin
121+ var factoryWithCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
122+ . WithPlugin ( new TablePlugin ( ) )
123+ . WithPlugin ( new MaterializedViewsPlugin ( ) )
124+ . WithPlugin ( new DatabaseCleanup ( ) ) ;
125+ var loaderWithCleanup = factoryWithCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
126+ var dbWithCleanup = await loaderWithCleanup . LoadAsync ( ) ;
127+
128+ // Assert
129+ Assert . NotNull ( dbWithCleanup ) ;
130+ Assert . NotNull ( dbWithoutCleanup ) ;
131+ Assert . Equal ( dbWithCleanup . MaterializedViews . Count , dbWithoutCleanup . MaterializedViews . Count ) ;
132+
133+ // basic materialized view tests
134+ void AssertMaterializedView (
135+ string file_name ,
136+ bool should_match )
137+ {
138+ var mv_with_cleanup = dbWithCleanup . MaterializedViews [ file_name ] ;
139+ var mv_without_cleanup = dbWithoutCleanup . MaterializedViews [ file_name ] ;
140+ Assert . NotNull ( mv_with_cleanup ) ;
141+ Assert . NotNull ( mv_without_cleanup ) ;
142+ Assert . Equal ( should_match , mv_without_cleanup . Query == mv_with_cleanup . Query ) ;
143+ }
144+ AssertMaterializedView ( "mv" , false ) ;
145+ AssertMaterializedView ( "mv_preformatted" , true ) ;
41146 }
42147 }
43148}
0 commit comments