19
19
import static com .google .cloud .spanner .SpannerMatchers .isSpannerException ;
20
20
import static com .google .cloud .spanner .Type .array ;
21
21
import static com .google .cloud .spanner .Type .json ;
22
+ import static com .google .cloud .spanner .Type .pgJsonb ;
22
23
import static com .google .cloud .spanner .testing .EmulatorSpannerHelper .isUsingEmulator ;
23
24
import static com .google .common .truth .Truth .assertThat ;
24
25
import static org .junit .Assert .assertArrayEquals ;
54
55
import com .google .cloud .spanner .Type ;
55
56
import com .google .cloud .spanner .Value ;
56
57
import com .google .cloud .spanner .connection .ConnectionOptions ;
57
- import com .google .cloud .spanner .testing .EmulatorSpannerHelper ;
58
58
import com .google .common .collect .ImmutableList ;
59
59
import com .google .protobuf .NullValue ;
60
60
import com .google .rpc .Code ;
@@ -98,9 +98,7 @@ public class ITWriteTest {
98
98
public static List <DialectTestParameter > data () {
99
99
List <DialectTestParameter > params = new ArrayList <>();
100
100
params .add (new DialectTestParameter (Dialect .GOOGLE_STANDARD_SQL ));
101
- if (!EmulatorSpannerHelper .isUsingEmulator ()) {
102
- params .add (new DialectTestParameter (Dialect .POSTGRESQL ));
103
- }
101
+ params .add (new DialectTestParameter (Dialect .POSTGRESQL ));
104
102
return params ;
105
103
}
106
104
@@ -149,7 +147,7 @@ public static List<DialectTestParameter> data() {
149
147
+ " StringValue VARCHAR,"
150
148
+ " JsonValue JSONB,"
151
149
+ " BytesValue BYTEA,"
152
- + " TimestampValue TIMESTAMPTZ ,"
150
+ + " TimestampValue SPANNER.COMMIT_TIMESTAMP ,"
153
151
+ " DateValue DATE,"
154
152
+ " NumericValue NUMERIC,"
155
153
+ " BoolArrayValue BOOL[],"
@@ -181,12 +179,10 @@ public static void setUpDatabase()
181
179
env .getTestHelper ().createTestDatabase (GOOGLE_STANDARD_SQL_SCHEMA );
182
180
183
181
googleStandardSQLClient = env .getTestHelper ().getDatabaseClient (googleStandardSQLDatabase );
184
- if (!EmulatorSpannerHelper .isUsingEmulator ()) {
185
- Database postgreSQLDatabase =
186
- env .getTestHelper ()
187
- .createTestDatabase (Dialect .POSTGRESQL , Arrays .asList (POSTGRESQL_SCHEMA ));
188
- postgreSQLClient = env .getTestHelper ().getDatabaseClient (postgreSQLDatabase );
189
- }
182
+ Database postgreSQLDatabase =
183
+ env .getTestHelper ()
184
+ .createTestDatabase (Dialect .POSTGRESQL , Arrays .asList (POSTGRESQL_SCHEMA ));
185
+ postgreSQLClient = env .getTestHelper ().getDatabaseClient (postgreSQLDatabase );
190
186
}
191
187
192
188
@ Before
@@ -481,31 +477,42 @@ public void writeStringNull() {
481
477
482
478
@ Test
483
479
public void writeJson () {
484
- assumeFalse ("PostgreSQL does not yet support JSON" , dialect .dialect == Dialect .POSTGRESQL );
485
480
write (baseInsert ().set ("JsonValue" ).to (Value .json ("{\" rating\" :9,\" open\" :true}" )).build ());
486
481
Struct row = readLastRow ("JsonValue" );
487
482
assertThat (row .isNull (0 )).isFalse ();
488
- assertThat (row .getColumnType ("JsonValue" )).isEqualTo (json ());
489
- assertThat (row .getJson (0 )).isEqualTo ("{\" open\" :true,\" rating\" :9}" );
483
+ if (dialect .dialect == Dialect .POSTGRESQL ) {
484
+ assertThat (row .getColumnType ("jsonvalue" )).isEqualTo (pgJsonb ());
485
+ assertThat (row .getPgJsonb (0 )).isEqualTo ("{\" open\" : true, \" rating\" : 9}" );
486
+ } else {
487
+ assertThat (row .getColumnType ("JsonValue" )).isEqualTo (json ());
488
+ assertThat (row .getJson (0 )).isEqualTo ("{\" open\" :true,\" rating\" :9}" );
489
+ }
490
490
}
491
491
492
492
@ Test
493
493
public void writeJsonEmpty () {
494
- assumeFalse ("PostgreSQL does not yet support JSON" , dialect .dialect == Dialect .POSTGRESQL );
495
494
write (baseInsert ().set ("JsonValue" ).to (Value .json ("{}" )).build ());
496
495
Struct row = readLastRow ("JsonValue" );
497
496
assertThat (row .isNull (0 )).isFalse ();
498
- assertThat (row .getColumnType ("JsonValue" )).isEqualTo (json ());
499
- assertThat (row .getJson (0 )).isEqualTo ("{}" );
497
+ if (dialect .dialect == Dialect .POSTGRESQL ) {
498
+ assertThat (row .getColumnType ("jsonvalue" )).isEqualTo (pgJsonb ());
499
+ assertThat (row .getPgJsonb (0 )).isEqualTo ("{}" );
500
+ } else {
501
+ assertThat (row .getColumnType ("JsonValue" )).isEqualTo (json ());
502
+ assertThat (row .getJson (0 )).isEqualTo ("{}" );
503
+ }
500
504
}
501
505
502
506
@ Test
503
507
public void writeJsonNull () {
504
- assumeFalse ("PostgreSQL does not yet support JSON" , dialect .dialect == Dialect .POSTGRESQL );
505
508
write (baseInsert ().set ("JsonValue" ).to (Value .json (null )).build ());
506
509
Struct row = readLastRow ("JsonValue" );
507
510
assertThat (row .isNull (0 )).isTrue ();
508
- assertThat (row .getColumnType ("JsonValue" )).isEqualTo (json ());
511
+ if (dialect .dialect == Dialect .POSTGRESQL ) {
512
+ assertThat (row .getColumnType ("jsonvalue" )).isEqualTo (pgJsonb ());
513
+ } else {
514
+ assertThat (row .getColumnType ("JsonValue" )).isEqualTo (json ());
515
+ }
509
516
}
510
517
511
518
@ Test
@@ -626,8 +633,6 @@ public void writeBytesNull() {
626
633
627
634
@ Test
628
635
public void writeTimestamp () {
629
- assumeFalse (
630
- "PostgresSQL does not yet support Timestamp" , dialect .dialect == Dialect .POSTGRESQL );
631
636
Timestamp timestamp = Timestamp .parseTimestamp ("2016-09-15T00:00:00.111111Z" );
632
637
write (baseInsert ().set ("TimestampValue" ).to (timestamp ).build ());
633
638
Struct row = readLastRow ("TimestampValue" );
@@ -644,8 +649,6 @@ public void writeTimestampNull() {
644
649
645
650
@ Test
646
651
public void writeCommitTimestamp () {
647
- assumeFalse (
648
- "PostgreSQL does not yet support Commit Timestamp" , dialect .dialect == Dialect .POSTGRESQL );
649
652
Timestamp commitTimestamp =
650
653
write (baseInsert ().set ("TimestampValue" ).to (Value .COMMIT_TIMESTAMP ).build ());
651
654
Struct row = readLastRow ("TimestampValue" );
@@ -830,47 +833,64 @@ public void writeStringArray() {
830
833
831
834
@ Test
832
835
public void writeJsonArrayNull () {
833
- assumeFalse ("PostgreSQL does not yet support Array" , dialect .dialect == Dialect .POSTGRESQL );
834
836
write (baseInsert ().set ("JsonArrayValue" ).toJsonArray (null ).build ());
835
837
Struct row = readLastRow ("JsonArrayValue" );
836
838
assertThat (row .isNull (0 )).isTrue ();
837
- assertThat (row .getColumnType ("JsonArrayValue" )).isEqualTo (array (json ()));
839
+ if (dialect .dialect == Dialect .POSTGRESQL ) {
840
+ assertThat (row .getColumnType ("jsonarrayvalue" )).isEqualTo (array (pgJsonb ()));
841
+ } else {
842
+ assertThat (row .getColumnType ("JsonArrayValue" )).isEqualTo (array (json ()));
843
+ }
838
844
}
839
845
840
846
@ Test
841
847
public void writeJsonArrayEmpty () {
842
- assumeFalse ("PostgreSQL does not yet support Array" , dialect .dialect == Dialect .POSTGRESQL );
843
848
write (baseInsert ().set ("JsonArrayValue" ).toJsonArray (Collections .emptyList ()).build ());
844
849
Struct row = readLastRow ("JsonArrayValue" );
845
850
assertThat (row .isNull (0 )).isFalse ();
846
- assertThat (row .getColumnType ("JsonArrayValue" )).isEqualTo (array (json ()));
847
- assertThat (row .getJsonList (0 )).containsExactly ();
851
+ if (dialect .dialect == Dialect .POSTGRESQL ) {
852
+ assertThat (row .getColumnType ("jsonarrayvalue" )).isEqualTo (array (pgJsonb ()));
853
+ assertThat (row .getPgJsonbList (0 )).containsExactly ();
854
+ } else {
855
+ assertThat (row .getColumnType ("JsonArrayValue" )).isEqualTo (array (json ()));
856
+ assertThat (row .getJsonList (0 )).containsExactly ();
857
+ }
848
858
}
849
859
850
860
@ Test
851
861
public void writeJsonArray () {
852
- assumeFalse ("PostgreSQL does not yet support Array" , dialect .dialect == Dialect .POSTGRESQL );
853
862
write (baseInsert ().set ("JsonArrayValue" ).toJsonArray (Arrays .asList ("[]" , null , "{}" )).build ());
854
863
Struct row = readLastRow ("JsonArrayValue" );
855
864
assertThat (row .isNull (0 )).isFalse ();
856
- assertThat (row .getColumnType ("JsonArrayValue" )).isEqualTo (array (json ()));
857
- assertThat (row .getJsonList (0 )).containsExactly ("[]" , null , "{}" ).inOrder ();
865
+ if (dialect .dialect == Dialect .POSTGRESQL ) {
866
+ assertThat (row .getColumnType ("jsonarrayvalue" )).isEqualTo (array (pgJsonb ()));
867
+ assertThat (row .getPgJsonbList (0 )).containsExactly ("[]" , null , "{}" ).inOrder ();
868
+ } else {
869
+ assertThat (row .getColumnType ("JsonArrayValue" )).isEqualTo (array (json ()));
870
+ assertThat (row .getJsonList (0 )).containsExactly ("[]" , null , "{}" ).inOrder ();
871
+ }
858
872
}
859
873
860
874
@ Test
861
875
public void writeJsonArrayNoNulls () {
862
- assumeFalse ("PostgreSQL does not yet support Array" , dialect .dialect == Dialect .POSTGRESQL );
863
876
write (
864
877
baseInsert ()
865
878
.set ("JsonArrayValue" )
866
879
.toJsonArray (Arrays .asList ("[]" , "{\" color\" :\" red\" ,\" value\" :\" #f00\" }" , "{}" ))
867
880
.build ());
868
881
Struct row = readLastRow ("JsonArrayValue" );
869
882
assertThat (row .isNull (0 )).isFalse ();
870
- assertThat (row .getColumnType ("JsonArrayValue" )).isEqualTo (array (json ()));
871
- assertThat (row .getJsonList (0 ))
872
- .containsExactly ("[]" , "{\" color\" :\" red\" ,\" value\" :\" #f00\" }" , "{}" )
873
- .inOrder ();
883
+ if (dialect .dialect == Dialect .POSTGRESQL ) {
884
+ assertThat (row .getColumnType ("jsonarrayvalue" )).isEqualTo (array (pgJsonb ()));
885
+ assertThat (row .getPgJsonbList (0 ))
886
+ .containsExactly ("[]" , "{\" color\" : \" red\" , \" value\" : \" #f00\" }" , "{}" )
887
+ .inOrder ();
888
+ } else {
889
+ assertThat (row .getColumnType ("JsonArrayValue" )).isEqualTo (array (json ()));
890
+ assertThat (row .getJsonList (0 ))
891
+ .containsExactly ("[]" , "{\" color\" :\" red\" ,\" value\" :\" #f00\" }" , "{}" )
892
+ .inOrder ();
893
+ }
874
894
}
875
895
876
896
@ Test
@@ -1430,9 +1450,7 @@ public void testTypeNamesPostgreSQL() {
1430
1450
1431
1451
assertTrue (resultSet .next ());
1432
1452
assertEquals ("timestampvalue" , resultSet .getString ("column_name" ));
1433
- assertEquals (
1434
- Type .timestamp ().getSpannerTypeName (dialect .dialect ),
1435
- resultSet .getString ("spanner_type" ));
1453
+ assertEquals ("spanner.commit_timestamp" , resultSet .getString ("spanner_type" ));
1436
1454
1437
1455
assertTrue (resultSet .next ());
1438
1456
assertEquals ("datevalue" , resultSet .getString ("column_name" ));
0 commit comments