11using KustoSchemaTools . Helpers ;
22using KustoSchemaTools . Parser ;
33using KustoSchemaTools . Plugins ;
4+ using KustoSchemaTools . Model ;
5+ using KustoSchemaTools . Changes ;
6+ using Kusto . Data ;
7+ using System . IO ;
48
59namespace KustoSchemaTools . Tests . Parser
610{
@@ -14,24 +18,132 @@ public class YamlDatabaseParserTests
1418 [ Fact ]
1519 public async Task GetDatabase ( )
1620 {
17- var factory = new YamlDatabaseHandlerFactory ( )
21+ var factory = new YamlDatabaseHandlerFactory < Model . Database > ( )
1822 . WithPlugin ( new TablePlugin ( ) )
1923 . WithPlugin ( new FunctionPlugin ( ) )
20- . WithPlugin ( new MaterializedViewsPlugin ( ) )
2124 . WithPlugin ( new DatabaseCleanup ( ) ) ;
2225 var loader = factory . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
2326
2427 var db = await loader . LoadAsync ( ) ;
2528
2629 Assert . NotNull ( db ) ;
2730 Assert . Equal ( 2 , db . Tables . Count ) ;
28- Assert . Equal ( 1 , db . Functions . Count ) ;
31+ Assert . Equal ( 4 , db . Functions . Count ) ;
2932 Assert . Equal ( 6 , db . Functions [ "UP" ] . Body . RowLength ( ) ) ;
3033 Assert . Equal ( "DemoDatabase" , db . Name ) ;
31- Assert . True ( db . Tables [ "sourceTable" ] . RestrictedViewAccess ) ;
32- Assert . Equal ( "120d" , db . Tables [ "tableWithUp" ] . RetentionAndCachePolicy . Retention ) ;
33- Assert . Equal ( "120d" , db . Tables [ "sourceTable" ] . RetentionAndCachePolicy . HotCache ) ;
34+
35+ var st = db . Tables [ "sourceTable" ] ;
36+ Assert . NotNull ( st ) ;
37+ Assert . NotNull ( st . Policies ) ;
38+ Assert . True ( st . Policies ! . RestrictedViewAccess ) ;
39+ Assert . Equal ( "120d" , st . Policies ? . HotCache ) ;
40+
41+ var tt = db . Tables [ "tableWithUp" ] ;
42+ Assert . NotNull ( tt ) ;
43+ Assert . NotNull ( tt . Policies ) ;
44+ Assert . False ( tt . Policies ! . RestrictedViewAccess ) ;
45+ Assert . Equal ( "120d" , tt . Policies ? . Retention ) ;
46+ }
47+
48+ [ Fact ]
49+ public async Task VerifyFunctionPreformatted ( )
50+ {
51+ // WITHOUT the DatabaseCleanup plugin
52+ var factoryWithoutCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
53+ . WithPlugin ( new TablePlugin ( ) )
54+ . WithPlugin ( new FunctionPlugin ( ) ) ;
55+ // DatabaseCleanup intentionally omitted
56+ var loaderWithoutCleanup = factoryWithoutCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
57+ var dbWithoutCleanup = await loaderWithoutCleanup . LoadAsync ( ) ;
58+
59+ // with the DatabaseCleanup plugin
60+ var factoryWithCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
61+ . WithPlugin ( new TablePlugin ( ) )
62+ . WithPlugin ( new FunctionPlugin ( ) )
63+ . WithPlugin ( new MaterializedViewsPlugin ( ) )
64+ . WithPlugin ( new DatabaseCleanup ( ) ) ;
65+ var loaderWithCleanup = factoryWithCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
66+ var dbWithCleanup = await loaderWithCleanup . LoadAsync ( ) ;
67+
68+ // Assert
69+ Assert . NotNull ( dbWithCleanup ) ;
70+ Assert . NotNull ( dbWithoutCleanup ) ;
71+ Assert . Equal ( dbWithCleanup . Functions . Count , dbWithoutCleanup . Functions . Count ) ;
72+
73+ // Verify the UP function has preformatted set to false (default)
74+ var up_withCleanup = dbWithCleanup . Functions [ "UP" ] ;
75+ var up_withoutCleanup = dbWithoutCleanup . Functions [ "UP" ] ;
76+ Assert . NotNull ( up_withCleanup ) ;
77+ Assert . NotNull ( up_withoutCleanup ) ;
78+ Assert . False ( up_withCleanup . Preformatted ) ;
79+ Assert . False ( up_withoutCleanup . Preformatted ) ;
80+
81+ // this case is simple and formatting has no impact.
82+ Assert . Equal ( up_withoutCleanup . Body . RowLength ( ) , up_withCleanup . Body . RowLength ( ) ) ;
83+
84+ // Verify the needs_formatting query changed when formatting.
85+ var f_withCleanup = dbWithCleanup . Functions [ "needs_formatting" ] ;
86+ var f_withoutCleanup = dbWithoutCleanup . Functions [ "needs_formatting" ] ;
87+ Assert . NotNull ( f_withCleanup ) ;
88+ Assert . NotNull ( f_withoutCleanup ) ;
89+ Assert . False ( f_withCleanup . Preformatted ) ;
90+ Assert . False ( f_withoutCleanup . Preformatted ) ;
91+
92+ // preformatted function should have been formatted by DatabaseCleanup
93+ Assert . NotEqual ( f_withCleanup . Body , f_withoutCleanup . Body ) ;
94+
95+ // much more complicated function where formatting breaks the query
96+ var complicated_with_cleanup = dbWithCleanup . Functions [ "complicated" ] . Body ;
97+ var complicated_without_cleanup = dbWithoutCleanup . Functions [ "complicated" ] . Body ;
98+ Assert . NotEqual ( complicated_with_cleanup , complicated_without_cleanup ) ;
99+
100+ var complicated_pf_with_cleanup = dbWithCleanup . Functions [ "complicated_preformatted" ] . Body ;
101+ var complicated_pf_without_cleanup = dbWithoutCleanup . Functions [ "complicated_preformatted" ] . Body ;
102+
103+ // preformatted option makes query match non-formatted version
104+ Assert . Equal ( complicated_pf_without_cleanup , complicated_pf_with_cleanup ) ;
105+
106+ // preformatted option makes query match non-formatted version
107+ Assert . Equal ( complicated_without_cleanup , complicated_pf_with_cleanup ) ;
34108 }
35109
110+ [ Fact ]
111+ public async Task VerifyMaterializedView ( )
112+ {
113+ // WITHOUT the DatabaseCleanup plugin
114+ var factoryWithoutCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
115+ . WithPlugin ( new TablePlugin ( ) )
116+ . WithPlugin ( new MaterializedViewsPlugin ( ) ) ;
117+ // DatabaseCleanup intentionally omitted
118+ var loaderWithoutCleanup = factoryWithoutCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
119+ var dbWithoutCleanup = await loaderWithoutCleanup . LoadAsync ( ) ;
120+
121+ // with the DatabaseCleanup plugin
122+ var factoryWithCleanup = new YamlDatabaseHandlerFactory < Model . Database > ( )
123+ . WithPlugin ( new TablePlugin ( ) )
124+ . WithPlugin ( new MaterializedViewsPlugin ( ) )
125+ . WithPlugin ( new DatabaseCleanup ( ) ) ;
126+ var loaderWithCleanup = factoryWithCleanup . Create ( Path . Combine ( BasePath , Deployment ) , Database ) ;
127+ var dbWithCleanup = await loaderWithCleanup . LoadAsync ( ) ;
128+
129+ // Assert
130+ Assert . NotNull ( dbWithCleanup ) ;
131+ Assert . NotNull ( dbWithoutCleanup ) ;
132+ Assert . Equal ( dbWithCleanup . MaterializedViews . Count , dbWithoutCleanup . MaterializedViews . Count ) ;
133+
134+ // basic materialized view tests
135+ void AssertMaterializedView (
136+ string file_name ,
137+ bool should_match )
138+ {
139+ var mv_with_cleanup = dbWithCleanup . MaterializedViews [ file_name ] ;
140+ var mv_without_cleanup = dbWithoutCleanup . MaterializedViews [ file_name ] ;
141+ Assert . NotNull ( mv_with_cleanup ) ;
142+ Assert . NotNull ( mv_without_cleanup ) ;
143+ Assert . Equal ( should_match , mv_without_cleanup . Query == mv_with_cleanup . Query ) ;
144+ }
145+ AssertMaterializedView ( "mv" , false ) ;
146+ AssertMaterializedView ( "mv_preformatted" , true ) ;
147+ }
36148 }
37149}
0 commit comments