21
21
import static org .junit .Assert .assertNotNull ;
22
22
import static org .junit .Assert .assertNull ;
23
23
import static org .junit .Assert .assertTrue ;
24
- import static org .junit .Assume .assumeFalse ;
25
24
26
25
import com .google .cloud .spanner .Database ;
27
26
import com .google .cloud .spanner .DatabaseAdminClient ;
@@ -62,10 +61,6 @@ public class ITJdbcDatabaseMetaDataTest extends ITAbstractJdbcTest {
62
61
63
62
@ BeforeClass
64
63
public static void setup () throws Exception {
65
- assumeFalse (
66
- "Named schemas are not yet supported on the emulator" ,
67
- EmulatorSpannerHelper .isUsingEmulator ());
68
-
69
64
database =
70
65
env .getOrCreateDatabase (
71
66
Dialect .GOOGLE_STANDARD_SQL , getMusicTablesDdl (Dialect .GOOGLE_STANDARD_SQL ));
@@ -89,6 +84,18 @@ public static void setup() throws Exception {
89
84
.map (statement -> statement .replace (" ON " , " ON test." ))
90
85
.map (statement -> statement .replace (" ON test.DELETE" , " ON DELETE" ))
91
86
.map (statement -> statement .replace (" REFERENCES " , " REFERENCES test." ))
87
+ .map (
88
+ statement ->
89
+ EmulatorSpannerHelper .isUsingEmulator ()
90
+ ? statement .replace ("Fk_Concerts_Singer" , "test_Fk_Concerts_Singer" )
91
+ : statement )
92
+ .map (
93
+ statement ->
94
+ EmulatorSpannerHelper .isUsingEmulator ()
95
+ ? statement .replace (
96
+ "Fk_TableWithRef_TableWithAllColumnTypes" ,
97
+ "test_Fk_TableWithRef_TableWithAllColumnTypes" )
98
+ : statement )
92
99
.collect (Collectors .toList ());
93
100
tables .add (0 , "create schema test" );
94
101
client
@@ -110,6 +117,7 @@ private static final class Column {
110
117
private final boolean nullable ;
111
118
private final Integer charOctetLength ;
112
119
private final boolean computed ;
120
+ private final String defaultValue ;
113
121
114
122
private Column (
115
123
String name ,
@@ -120,7 +128,17 @@ private Column(
120
128
Integer radix ,
121
129
boolean nullable ,
122
130
Integer charOctetLength ) {
123
- this (name , type , typeName , colSize , decimalDigits , radix , nullable , charOctetLength , false );
131
+ this (
132
+ name ,
133
+ type ,
134
+ typeName ,
135
+ colSize ,
136
+ decimalDigits ,
137
+ radix ,
138
+ nullable ,
139
+ charOctetLength ,
140
+ false ,
141
+ null );
124
142
}
125
143
126
144
private Column (
@@ -132,7 +150,8 @@ private Column(
132
150
Integer radix ,
133
151
boolean nullable ,
134
152
Integer charOctetLength ,
135
- boolean computed ) {
153
+ boolean computed ,
154
+ String defaultValue ) {
136
155
this .name = name ;
137
156
this .type = type ;
138
157
this .typeName = typeName ;
@@ -142,21 +161,42 @@ private Column(
142
161
this .nullable = nullable ;
143
162
this .charOctetLength = charOctetLength ;
144
163
this .computed = computed ;
164
+ this .defaultValue = defaultValue ;
145
165
}
146
166
}
147
167
148
168
private static final List <Column > EXPECTED_COLUMNS =
149
169
Arrays .asList (
150
170
new Column ("ColInt64" , Types .BIGINT , "INT64" , 19 , null , 10 , false , null ),
151
- new Column ("ColFloat64" , Types .DOUBLE , "FLOAT64" , 15 , 16 , 2 , false , null ),
152
- new Column ("ColFloat32" , Types .REAL , "FLOAT32" , 15 , 16 , 2 , false , null ),
171
+ new Column ("ColFloat64" , Types .DOUBLE , "FLOAT64" , 15 , 16 , 2 , false , null , false , "0.0" ),
172
+ new Column ("ColFloat32" , Types .REAL , "FLOAT32" , 15 , 16 , 2 , false , null , false , "0.0" ),
153
173
new Column ("ColBool" , Types .BOOLEAN , "BOOL" , null , null , null , false , null ),
154
- new Column ("ColString" , Types .NVARCHAR , "STRING(100)" , 100 , null , null , false , 100 ),
174
+ new Column (
175
+ "ColString" ,
176
+ Types .NVARCHAR ,
177
+ "STRING(100)" ,
178
+ 100 ,
179
+ null ,
180
+ null ,
181
+ false ,
182
+ 100 ,
183
+ false ,
184
+ "'Hello World!'" ),
155
185
new Column (
156
186
"ColStringMax" , Types .NVARCHAR , "STRING(MAX)" , 2621440 , null , null , false , 2621440 ),
157
187
new Column ("ColBytes" , Types .BINARY , "BYTES(100)" , 100 , null , null , false , null ),
158
188
new Column ("ColBytesMax" , Types .BINARY , "BYTES(MAX)" , 10485760 , null , null , false , null ),
159
- new Column ("ColDate" , Types .DATE , "DATE" , 10 , null , null , false , null ),
189
+ new Column (
190
+ "ColDate" ,
191
+ Types .DATE ,
192
+ "DATE" ,
193
+ 10 ,
194
+ null ,
195
+ null ,
196
+ false ,
197
+ null ,
198
+ false ,
199
+ "DATE '2000-01-01'" ),
160
200
new Column ("ColTimestamp" , Types .TIMESTAMP , "TIMESTAMP" , 35 , null , null , false , null ),
161
201
new Column ("ColCommitTS" , Types .TIMESTAMP , "TIMESTAMP" , 35 , null , null , false , null ),
162
202
new Column ("ColNumeric" , Types .NUMERIC , "NUMERIC" , 15 , null , 10 , false , null ),
@@ -202,7 +242,8 @@ private Column(
202
242
null ,
203
243
true ,
204
244
2621440 ,
205
- true ));
245
+ true ,
246
+ null ));
206
247
207
248
@ Test
208
249
public void testGetColumns () throws SQLException {
@@ -244,7 +285,7 @@ public void testGetColumns() throws SQLException {
244
285
col .nullable ? DatabaseMetaData .columnNullable : DatabaseMetaData .columnNoNulls ,
245
286
rs .getInt ("NULLABLE" ));
246
287
assertNull (rs .getString ("REMARKS" ));
247
- assertNull ( rs .getString ("COLUMN_DEF" ));
288
+ assertEquals ( col . defaultValue , rs .getString ("COLUMN_DEF" ));
248
289
assertEquals (0 , rs .getInt ("SQL_DATA_TYPE" ));
249
290
assertEquals (0 , rs .getInt ("SQL_DATETIME_SUB" ));
250
291
if (col .charOctetLength == null ) {
@@ -360,7 +401,11 @@ public void testGetCrossReferences() throws SQLException {
360
401
assertEquals (1 , rs .getShort ("KEY_SEQ" ));
361
402
assertEquals (DatabaseMetaData .importedKeyNoAction , rs .getShort ("UPDATE_RULE" ));
362
403
assertEquals (DatabaseMetaData .importedKeyNoAction , rs .getShort ("DELETE_RULE" ));
363
- assertEquals ("Fk_Concerts_Singer" , rs .getString ("FK_NAME" ));
404
+ if (EmulatorSpannerHelper .isUsingEmulator () && "test" .equals (schema )) {
405
+ assertEquals ("test_Fk_Concerts_Singer" , rs .getString ("FK_NAME" ));
406
+ } else {
407
+ assertEquals ("Fk_Concerts_Singer" , rs .getString ("FK_NAME" ));
408
+ }
364
409
assertEquals ("PK_Singers" , rs .getString ("PK_NAME" ));
365
410
assertEquals (DatabaseMetaData .importedKeyNotDeferrable , rs .getShort ("DEFERRABILITY" ));
366
411
assertFalse (rs .next ());
@@ -389,7 +434,11 @@ public void testGetCrossReferences() throws SQLException {
389
434
assertEquals (1 , rs .getShort ("KEY_SEQ" ));
390
435
assertEquals (DatabaseMetaData .importedKeyNoAction , rs .getShort ("UPDATE_RULE" ));
391
436
assertEquals (DatabaseMetaData .importedKeyNoAction , rs .getShort ("DELETE_RULE" ));
392
- assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
437
+ if (EmulatorSpannerHelper .isUsingEmulator () && "test" .equals (schema )) {
438
+ assertEquals ("test_Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
439
+ } else {
440
+ assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
441
+ }
393
442
assertEquals (DatabaseMetaData .importedKeyNotDeferrable , rs .getShort ("DEFERRABILITY" ));
394
443
395
444
assertTrue (rs .next ());
@@ -404,7 +453,11 @@ public void testGetCrossReferences() throws SQLException {
404
453
assertEquals (2 , rs .getShort ("KEY_SEQ" ));
405
454
assertEquals (DatabaseMetaData .importedKeyNoAction , rs .getShort ("UPDATE_RULE" ));
406
455
assertEquals (DatabaseMetaData .importedKeyNoAction , rs .getShort ("DELETE_RULE" ));
407
- assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
456
+ if (EmulatorSpannerHelper .isUsingEmulator () && "test" .equals (schema )) {
457
+ assertEquals ("test_Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
458
+ } else {
459
+ assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
460
+ }
408
461
assertEquals (DatabaseMetaData .importedKeyNotDeferrable , rs .getShort ("DEFERRABILITY" ));
409
462
410
463
assertTrue (rs .next ());
@@ -419,7 +472,11 @@ public void testGetCrossReferences() throws SQLException {
419
472
assertEquals (3 , rs .getShort ("KEY_SEQ" ));
420
473
assertEquals (DatabaseMetaData .importedKeyNoAction , rs .getShort ("UPDATE_RULE" ));
421
474
assertEquals (DatabaseMetaData .importedKeyNoAction , rs .getShort ("DELETE_RULE" ));
422
- assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
475
+ if (EmulatorSpannerHelper .isUsingEmulator () && "test" .equals (schema )) {
476
+ assertEquals ("test_Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
477
+ } else {
478
+ assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
479
+ }
423
480
assertEquals (DatabaseMetaData .importedKeyNotDeferrable , rs .getShort ("DEFERRABILITY" ));
424
481
425
482
assertFalse (rs .next ());
@@ -519,6 +576,12 @@ public void testGetIndexInfo() throws SQLException {
519
576
connection .getMetaData ().getIndexInfo (DEFAULT_CATALOG , schema , null , false , false )) {
520
577
521
578
for (IndexInfo index : EXPECTED_INDICES ) {
579
+ // The emulator does not generate indexes for foreign keys in a non-default schema.
580
+ if (EmulatorSpannerHelper .isUsingEmulator ()
581
+ && "test" .equals (schema )
582
+ && ("FOREIGN_KEY" .equals (index .indexName ) || "GENERATED" .equals (index .indexName ))) {
583
+ continue ;
584
+ }
522
585
assertTrue (rs .next ());
523
586
assertEquals (DEFAULT_CATALOG , rs .getString ("TABLE_CAT" ));
524
587
assertEquals (schema , rs .getString ("TABLE_SCHEM" ));
@@ -612,7 +675,11 @@ private void assertImportedKeysTableWithRef(String schema, ResultSet rs) throws
612
675
assertEquals (1 , rs .getShort ("KEY_SEQ" ));
613
676
assertEquals (DatabaseMetaData .importedKeyRestrict , rs .getInt ("UPDATE_RULE" ));
614
677
assertEquals (DatabaseMetaData .importedKeyRestrict , rs .getInt ("DELETE_RULE" ));
615
- assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
678
+ if (EmulatorSpannerHelper .isUsingEmulator () && "test" .equals (schema )) {
679
+ assertEquals ("test_Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
680
+ } else {
681
+ assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
682
+ }
616
683
assertNotNull (rs .getString ("PK_NAME" )); // Index name is generated.
617
684
assertEquals (DatabaseMetaData .importedKeyNotDeferrable , rs .getInt ("DEFERRABILITY" ));
618
685
@@ -628,7 +695,11 @@ private void assertImportedKeysTableWithRef(String schema, ResultSet rs) throws
628
695
assertEquals (2 , rs .getShort ("KEY_SEQ" ));
629
696
assertEquals (DatabaseMetaData .importedKeyRestrict , rs .getInt ("UPDATE_RULE" ));
630
697
assertEquals (DatabaseMetaData .importedKeyRestrict , rs .getInt ("DELETE_RULE" ));
631
- assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
698
+ if (EmulatorSpannerHelper .isUsingEmulator () && "test" .equals (schema )) {
699
+ assertEquals ("test_Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
700
+ } else {
701
+ assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
702
+ }
632
703
assertNotNull (rs .getString ("PK_NAME" )); // Index name is generated.
633
704
assertEquals (DatabaseMetaData .importedKeyNotDeferrable , rs .getInt ("DEFERRABILITY" ));
634
705
@@ -644,7 +715,11 @@ private void assertImportedKeysTableWithRef(String schema, ResultSet rs) throws
644
715
assertEquals (3 , rs .getShort ("KEY_SEQ" ));
645
716
assertEquals (DatabaseMetaData .importedKeyRestrict , rs .getInt ("UPDATE_RULE" ));
646
717
assertEquals (DatabaseMetaData .importedKeyRestrict , rs .getInt ("DELETE_RULE" ));
647
- assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
718
+ if (EmulatorSpannerHelper .isUsingEmulator () && "test" .equals (schema )) {
719
+ assertEquals ("test_Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
720
+ } else {
721
+ assertEquals ("Fk_TableWithRef_TableWithAllColumnTypes" , rs .getString ("FK_NAME" ));
722
+ }
648
723
assertNotNull (rs .getString ("PK_NAME" )); // Index name is generated.
649
724
assertEquals (DatabaseMetaData .importedKeyNotDeferrable , rs .getInt ("DEFERRABILITY" ));
650
725
@@ -684,7 +759,11 @@ private void assertImportedKeysConcerts(String schema, ResultSet rs) throws SQLE
684
759
assertEquals (1 , rs .getShort ("KEY_SEQ" ));
685
760
assertEquals (DatabaseMetaData .importedKeyRestrict , rs .getInt ("UPDATE_RULE" ));
686
761
assertEquals (DatabaseMetaData .importedKeyRestrict , rs .getInt ("DELETE_RULE" ));
687
- assertEquals ("Fk_Concerts_Singer" , rs .getString ("FK_NAME" ));
762
+ if (EmulatorSpannerHelper .isUsingEmulator () && "test" .equals (schema )) {
763
+ assertEquals ("test_Fk_Concerts_Singer" , rs .getString ("FK_NAME" ));
764
+ } else {
765
+ assertEquals ("Fk_Concerts_Singer" , rs .getString ("FK_NAME" ));
766
+ }
688
767
assertEquals ("PK_Singers" , rs .getString ("PK_NAME" ));
689
768
assertEquals (DatabaseMetaData .importedKeyNotDeferrable , rs .getInt ("DEFERRABILITY" ));
690
769
@@ -720,7 +799,11 @@ private void assertExportedKeysSingers(String schema, ResultSet rs) throws SQLEx
720
799
assertEquals (1 , rs .getShort ("KEY_SEQ" ));
721
800
assertEquals (DatabaseMetaData .importedKeyRestrict , rs .getInt ("UPDATE_RULE" ));
722
801
assertEquals (DatabaseMetaData .importedKeyRestrict , rs .getInt ("DELETE_RULE" ));
723
- assertEquals ("Fk_Concerts_Singer" , rs .getString ("FK_NAME" ));
802
+ if (EmulatorSpannerHelper .isUsingEmulator () && "test" .equals (schema )) {
803
+ assertEquals ("test_Fk_Concerts_Singer" , rs .getString ("FK_NAME" ));
804
+ } else {
805
+ assertEquals ("Fk_Concerts_Singer" , rs .getString ("FK_NAME" ));
806
+ }
724
807
assertEquals ("PK_Singers" , rs .getString ("PK_NAME" ));
725
808
assertEquals (DatabaseMetaData .importedKeyNotDeferrable , rs .getInt ("DEFERRABILITY" ));
726
809
@@ -888,9 +971,6 @@ public void testGetTables() throws SQLException {
888
971
try (ResultSet rs =
889
972
connection .getMetaData ().getTables (DEFAULT_CATALOG , schema , null , null )) {
890
973
for (Table table : EXPECTED_TABLES ) {
891
- if (EmulatorSpannerHelper .isUsingEmulator () && table .name .equals ("SingersView" )) {
892
- continue ;
893
- }
894
974
assertTrue (rs .next ());
895
975
assertEquals (DEFAULT_CATALOG , rs .getString ("TABLE_CAT" ));
896
976
assertEquals (schema , rs .getString ("TABLE_SCHEM" ));
0 commit comments