33
33
import org .apache .gravitino .Schema ;
34
34
import org .apache .gravitino .cli .CommandContext ;
35
35
import org .apache .gravitino .cli .outputs .PlainFormat ;
36
+ import org .apache .gravitino .rel .Table ;
37
+ import org .apache .gravitino .rel .types .Type ;
38
+ import org .apache .gravitino .rel .types .Types ;
36
39
import org .junit .jupiter .api .AfterEach ;
37
40
import org .junit .jupiter .api .Assertions ;
38
41
import org .junit .jupiter .api .BeforeEach ;
@@ -64,7 +67,7 @@ void testMetalakeDetailsWithPlainFormat() {
64
67
65
68
PlainFormat .output (mockMetalake , mockContext );
66
69
String output = new String (outContent .toByteArray (), StandardCharsets .UTF_8 ).trim ();
67
- Assertions .assertEquals ("demo_metalake, This is a demo metalake" , output );
70
+ Assertions .assertEquals ("demo_metalake,This is a demo metalake" , output );
68
71
}
69
72
70
73
@ Test
@@ -85,8 +88,7 @@ void testCatalogDetailsWithPlainFormat() {
85
88
86
89
PlainFormat .output (mockCatalog , mockContext );
87
90
String output = new String (outContent .toByteArray (), StandardCharsets .UTF_8 ).trim ();
88
- Assertions .assertEquals (
89
- "demo_catalog, RELATIONAL, demo_provider, This is a demo catalog" , output );
91
+ Assertions .assertEquals ("demo_catalog,RELATIONAL,demo_provider,This is a demo catalog" , output );
90
92
}
91
93
92
94
@ Test
@@ -102,6 +104,75 @@ void testListCatalogWithPlainFormat() {
102
104
Assertions .assertEquals ("catalog1\n " + "catalog2" , output );
103
105
}
104
106
107
+ @ Test
108
+ void testSchemaDetailsWithPlainFormat () {
109
+ CommandContext mockContext = getMockContext ();
110
+ Schema mockSchema = getMockSchema ();
111
+ PlainFormat .output (mockSchema , mockContext );
112
+ String output = new String (outContent .toByteArray (), StandardCharsets .UTF_8 ).trim ();
113
+ Assertions .assertEquals ("demo_schema,This is a demo schema" , output );
114
+ }
115
+
116
+ @ Test
117
+ void testListSchemaWithPlainFormat () {
118
+ CommandContext mockContext = getMockContext ();
119
+ Schema mockSchema1 = getMockSchema ("schema1" , "This is a schema" );
120
+ Schema mockSchema2 = getMockSchema ("schema2" , "This is another schema" );
121
+
122
+ PlainFormat .output (new Schema [] {mockSchema1 , mockSchema2 }, mockContext );
123
+ String output = new String (outContent .toByteArray (), StandardCharsets .UTF_8 ).trim ();
124
+ Assertions .assertEquals ("schema1\n " + "schema2" , output );
125
+ }
126
+
127
+ @ Test
128
+ void testTableDetailsWithPlainFormat () {
129
+ CommandContext mockContext = getMockContext ();
130
+ Table mockTable = getMockTable ();
131
+ PlainFormat .output (mockTable , mockContext );
132
+ String output = new String (outContent .toByteArray (), StandardCharsets .UTF_8 ).trim ();
133
+ Assertions .assertEquals ("demo_table,This is a demo table" , output );
134
+ }
135
+
136
+ @ Test
137
+ void testAuditWithTableFormat () {
138
+ CommandContext mockContext = getMockContext ();
139
+ Audit mockAudit = mock (Audit .class );
140
+ when (mockAudit .creator ()).thenReturn ("demo_user" );
141
+ when (mockAudit .createTime ()).thenReturn (Instant .ofEpochMilli (1611111111111L ));
142
+ when (mockAudit .lastModifier ()).thenReturn ("demo_user" );
143
+ when (mockAudit .lastModifiedTime ()).thenReturn (Instant .ofEpochMilli (1611111111111L ));
144
+
145
+ PlainFormat .output (mockAudit , mockContext );
146
+ String output = new String (outContent .toByteArray (), StandardCharsets .UTF_8 ).trim ();
147
+ Assertions .assertEquals (
148
+ "demo_user,2021-01-20T02:51:51.111Z,demo_user,2021-01-20T02:51:51.111Z" , output );
149
+ }
150
+
151
+ @ Test
152
+ void testAuditWithTableFormatWithNullValues () {
153
+ CommandContext mockContext = getMockContext ();
154
+ Audit mockAudit = mock (Audit .class );
155
+ when (mockAudit .creator ()).thenReturn ("demo_user" );
156
+ when (mockAudit .createTime ()).thenReturn (null );
157
+ when (mockAudit .lastModifier ()).thenReturn (null );
158
+ when (mockAudit .lastModifiedTime ()).thenReturn (null );
159
+
160
+ PlainFormat .output (mockAudit , mockContext );
161
+ String output = new String (outContent .toByteArray (), StandardCharsets .UTF_8 ).trim ();
162
+ Assertions .assertEquals ("demo_user,N/A,N/A,N/A" , output );
163
+ }
164
+
165
+ @ Test
166
+ void testListTableWithPlainFormat () {
167
+ CommandContext mockContext = getMockContext ();
168
+ Table mockTable1 = getMockTable ("table1" , "This is a table" );
169
+ Table mockTable2 = getMockTable ("table2" , "This is another table" );
170
+
171
+ PlainFormat .output (new Table [] {mockTable1 , mockTable2 }, mockContext );
172
+ String output = new String (outContent .toByteArray (), StandardCharsets .UTF_8 ).trim ();
173
+ Assertions .assertEquals ("table1\n " + "table2" , output );
174
+ }
175
+
105
176
@ Test
106
177
void testOutputWithUnsupportType () {
107
178
CommandContext mockContext = getMockContext ();
@@ -159,13 +230,35 @@ private Schema getMockSchema(String name, String comment) {
159
230
return mockSchema ;
160
231
}
161
232
162
- private Audit getMockAudit () {
163
- Audit mockAudit = mock (Audit .class );
164
- when (mockAudit .creator ()).thenReturn ("demo_user" );
165
- when (mockAudit .createTime ()).thenReturn (Instant .ofEpochMilli (1611111111111L ));
166
- when (mockAudit .lastModifier ()).thenReturn ("demo_user" );
167
- when (mockAudit .lastModifiedTime ()).thenReturn (Instant .ofEpochMilli (1611111111111L ));
233
+ private Table getMockTable () {
234
+ return getMockTable ("demo_table" , "This is a demo table" );
235
+ }
236
+
237
+ private Table getMockTable (String name , String comment ) {
238
+ Table mockTable = mock (Table .class );
239
+ org .apache .gravitino .rel .Column mockColumnInt =
240
+ getMockColumn ("id" , Types .IntegerType .get (), "This is a int column" , false , true );
241
+ org .apache .gravitino .rel .Column mockColumnString =
242
+ getMockColumn ("name" , Types .StringType .get (), "This is a string column" , true , false );
243
+
244
+ when (mockTable .name ()).thenReturn (name );
245
+ when (mockTable .comment ()).thenReturn (comment );
246
+ when (mockTable .columns ())
247
+ .thenReturn (new org .apache .gravitino .rel .Column [] {mockColumnInt , mockColumnString });
248
+
249
+ return mockTable ;
250
+ }
251
+
252
+ private org .apache .gravitino .rel .Column getMockColumn (
253
+ String name , Type dataType , String comment , boolean nullable , boolean autoIncrement ) {
254
+
255
+ org .apache .gravitino .rel .Column mockColumn = mock (org .apache .gravitino .rel .Column .class );
256
+ when (mockColumn .name ()).thenReturn (name );
257
+ when (mockColumn .dataType ()).thenReturn (dataType );
258
+ when (mockColumn .comment ()).thenReturn (comment );
259
+ when (mockColumn .nullable ()).thenReturn (nullable );
260
+ when (mockColumn .autoIncrement ()).thenReturn (autoIncrement );
168
261
169
- return mockAudit ;
262
+ return mockColumn ;
170
263
}
171
264
}
0 commit comments