@@ -68,3 +68,155 @@ func snapshotDir(t *testing.T, dir string) map[string][]byte {
6868 require .NoError (t , err )
6969 return files
7070}
71+
72+ func TestIntScaleFactor (t * testing.T ) {
73+ tests := []struct {
74+ input string
75+ expected int
76+ }{
77+ {"1" , 1 },
78+ {"10" , 10 },
79+ {"1000" , 1000 },
80+ {"1k" , 1000 },
81+ {"10k" , 10000 },
82+ {"abc" , 0 },
83+ {"k" , 0 },
84+ }
85+ for _ , tc := range tests {
86+ s := & Schema {ScaleFactor : tc .input }
87+ assert .Equal (t , tc .expected , s .intScaleFactor (), "intScaleFactor(%q)" , tc .input )
88+ }
89+ }
90+
91+ func TestLoadSchemas (t * testing.T ) {
92+ data := []byte (`{"scale_factor":"10","file_format":"parquet","compression_method":"uncompressed"}` )
93+ schemas , err := loadSchemas (data )
94+ require .NoError (t , err )
95+ require .Len (t , schemas , 4 , "should produce 4 schema variants" )
96+
97+ // Verify the 4 combinations.
98+ assert .True (t , schemas [0 ].Iceberg && ! schemas [0 ].Partitioned )
99+ assert .True (t , schemas [1 ].Iceberg && schemas [1 ].Partitioned )
100+ assert .True (t , ! schemas [2 ].Iceberg && ! schemas [2 ].Partitioned )
101+ assert .True (t , ! schemas [3 ].Iceberg && schemas [3 ].Partitioned )
102+
103+ // Each schema should have independent maps.
104+ schemas [0 ].Tables ["foo" ] = & Table {Name : "foo" }
105+ assert .Empty (t , schemas [1 ].Tables , "schema variants should not share Tables map" )
106+ }
107+
108+ func TestLoadSchemasDefaults (t * testing.T ) {
109+ data := []byte (`{"scale_factor":"1","file_format":"orc","compression_method":"zstd"}` )
110+ schemas , err := loadSchemas (data )
111+ require .NoError (t , err )
112+
113+ // Verify defaults are applied.
114+ assert .Equal (t , "tpcds" , schemas [0 ].Workload )
115+ assert .Equal (t , "tpc-ds" , schemas [0 ].WorkloadDefinition )
116+
117+ // Verify zstd compression session vars.
118+ assert .Equal (t , "ZSTD" , schemas [0 ].SessionVariables ["iceberg.compression_codec" ])
119+ assert .Equal (t , "ZSTD" , schemas [3 ].SessionVariables ["hive.compression_codec" ])
120+ }
121+
122+ func TestGetNamedOutput (t * testing.T ) {
123+ tests := []struct {
124+ name string
125+ data string
126+ workload string
127+ expected string
128+ }{
129+ {
130+ "uncompressed" ,
131+ `{"scale_factor":"10","file_format":"parquet","compression_method":"uncompressed"}` ,
132+ "tpcds" ,
133+ "tpcds-sf10-parquet" ,
134+ },
135+ {
136+ "zstd" ,
137+ `{"scale_factor":"100","file_format":"orc","compression_method":"zstd"}` ,
138+ "tpch" ,
139+ "tpch-sf100-orc-zstd" ,
140+ },
141+ }
142+ for _ , tc := range tests {
143+ t .Run (tc .name , func (t * testing.T ) {
144+ result , err := getNamedOutput ([]byte (tc .data ), tc .workload )
145+ require .NoError (t , err )
146+ assert .Equal (t , tc .expected , result )
147+ })
148+ }
149+ }
150+
151+ func TestCleanOutputDir (t * testing.T ) {
152+ dir := t .TempDir ()
153+
154+ // Create some files.
155+ require .NoError (t , os .WriteFile (filepath .Join (dir , "a.sql" ), []byte ("test" ), 0644 ))
156+ require .NoError (t , os .WriteFile (filepath .Join (dir , "b.sh" ), []byte ("test" ), 0644 ))
157+
158+ // Create a subdirectory (should not be deleted).
159+ require .NoError (t , os .MkdirAll (filepath .Join (dir , "subdir" ), 0755 ))
160+
161+ err := cleanOutputDir (dir )
162+ require .NoError (t , err )
163+
164+ entries , err := os .ReadDir (dir )
165+ require .NoError (t , err )
166+ assert .Len (t , entries , 1 , "only subdirectory should remain" )
167+ assert .Equal (t , "subdir" , entries [0 ].Name ())
168+ }
169+
170+ func TestCleanOutputDirNonExistent (t * testing.T ) {
171+ err := cleanOutputDir ("/nonexistent/path" )
172+ assert .NoError (t , err , "should return nil for non-existent directory" )
173+ }
174+
175+ func TestIsPartitioned (t * testing.T ) {
176+ // Table with explicit PartitionedMinScale.
177+ tbl := & Table {PartitionedMinScale : 100 }
178+ assert .False (t , tbl .isPartitioned (10 ))
179+ assert .True (t , tbl .isPartitioned (100 ))
180+ assert .True (t , tbl .isPartitioned (1000 ))
181+
182+ // Table without PartitionedMinScale uses Partitioned field.
183+ tbl2 := & Table {Partitioned : true }
184+ assert .True (t , tbl2 .isPartitioned (1 ))
185+
186+ tbl3 := & Table {Partitioned : false }
187+ assert .False (t , tbl3 .isPartitioned (1 ))
188+ }
189+
190+ func TestInitIsVarchar (t * testing.T ) {
191+ varcharType := "VARCHAR(100)"
192+ intType := "INT"
193+
194+ tbl := & Table {
195+ Columns : []* Column {
196+ {Name : "a" , Type : & varcharType },
197+ {Name : "b" , Type : & intType },
198+ {Name : "c" , Type : nil },
199+ },
200+ }
201+ tbl .initIsVarchar ()
202+
203+ assert .True (t , tbl .Columns [0 ].IsVarchar )
204+ assert .False (t , tbl .Columns [1 ].IsVarchar )
205+ assert .False (t , tbl .Columns [2 ].IsVarchar )
206+ }
207+
208+ func TestSetNames (t * testing.T ) {
209+ s := & Schema {
210+ ScaleFactor : "100" ,
211+ FileFormat : "parquet" ,
212+ CompressionMethod : "zstd" ,
213+ Workload : "tpcds" ,
214+ Iceberg : true ,
215+ Partitioned : true ,
216+ }
217+ s .setNames ()
218+
219+ assert .Equal (t , "tpcds_sf100_parquet_partitioned_iceberg_zstd" , s .SchemaName )
220+ assert .Equal (t , "tpcds-sf100-parquet-partitioned-iceberg-zstd" , s .LocationName )
221+ assert .Equal (t , "tpcds_sf100_parquet_partitioned_iceberg" , s .UncompressedName )
222+ }
0 commit comments