1919import static com .google .cloud .spanner .SpannerMatchers .isSpannerException ;
2020import static com .google .cloud .spanner .Type .array ;
2121import static com .google .cloud .spanner .Type .json ;
22+ import static com .google .cloud .spanner .Type .pgJsonb ;
2223import static com .google .cloud .spanner .testing .EmulatorSpannerHelper .isUsingEmulator ;
2324import static com .google .common .truth .Truth .assertThat ;
2425import static org .junit .Assert .assertArrayEquals ;
5455import com .google .cloud .spanner .Type ;
5556import com .google .cloud .spanner .Value ;
5657import com .google .cloud .spanner .connection .ConnectionOptions ;
57- import com .google .cloud .spanner .testing .EmulatorSpannerHelper ;
5858import com .google .common .collect .ImmutableList ;
5959import com .google .protobuf .NullValue ;
6060import com .google .rpc .Code ;
@@ -98,9 +98,7 @@ public class ITWriteTest {
9898 public static List <DialectTestParameter > data () {
9999 List <DialectTestParameter > params = new ArrayList <>();
100100 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 ));
104102 return params ;
105103 }
106104
@@ -149,7 +147,7 @@ public static List<DialectTestParameter> data() {
149147 + " StringValue VARCHAR,"
150148 + " JsonValue JSONB,"
151149 + " BytesValue BYTEA,"
152- + " TimestampValue TIMESTAMPTZ ,"
150+ + " TimestampValue SPANNER.COMMIT_TIMESTAMP ,"
153151 + " DateValue DATE,"
154152 + " NumericValue NUMERIC,"
155153 + " BoolArrayValue BOOL[],"
@@ -181,12 +179,10 @@ public static void setUpDatabase()
181179 env .getTestHelper ().createTestDatabase (GOOGLE_STANDARD_SQL_SCHEMA );
182180
183181 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 );
190186 }
191187
192188 @ Before
@@ -481,31 +477,42 @@ public void writeStringNull() {
481477
482478 @ Test
483479 public void writeJson () {
484- assumeFalse ("PostgreSQL does not yet support JSON" , dialect .dialect == Dialect .POSTGRESQL );
485480 write (baseInsert ().set ("JsonValue" ).to (Value .json ("{\" rating\" :9,\" open\" :true}" )).build ());
486481 Struct row = readLastRow ("JsonValue" );
487482 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+ }
490490 }
491491
492492 @ Test
493493 public void writeJsonEmpty () {
494- assumeFalse ("PostgreSQL does not yet support JSON" , dialect .dialect == Dialect .POSTGRESQL );
495494 write (baseInsert ().set ("JsonValue" ).to (Value .json ("{}" )).build ());
496495 Struct row = readLastRow ("JsonValue" );
497496 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+ }
500504 }
501505
502506 @ Test
503507 public void writeJsonNull () {
504- assumeFalse ("PostgreSQL does not yet support JSON" , dialect .dialect == Dialect .POSTGRESQL );
505508 write (baseInsert ().set ("JsonValue" ).to (Value .json (null )).build ());
506509 Struct row = readLastRow ("JsonValue" );
507510 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+ }
509516 }
510517
511518 @ Test
@@ -626,8 +633,6 @@ public void writeBytesNull() {
626633
627634 @ Test
628635 public void writeTimestamp () {
629- assumeFalse (
630- "PostgresSQL does not yet support Timestamp" , dialect .dialect == Dialect .POSTGRESQL );
631636 Timestamp timestamp = Timestamp .parseTimestamp ("2016-09-15T00:00:00.111111Z" );
632637 write (baseInsert ().set ("TimestampValue" ).to (timestamp ).build ());
633638 Struct row = readLastRow ("TimestampValue" );
@@ -644,8 +649,6 @@ public void writeTimestampNull() {
644649
645650 @ Test
646651 public void writeCommitTimestamp () {
647- assumeFalse (
648- "PostgreSQL does not yet support Commit Timestamp" , dialect .dialect == Dialect .POSTGRESQL );
649652 Timestamp commitTimestamp =
650653 write (baseInsert ().set ("TimestampValue" ).to (Value .COMMIT_TIMESTAMP ).build ());
651654 Struct row = readLastRow ("TimestampValue" );
@@ -830,47 +833,64 @@ public void writeStringArray() {
830833
831834 @ Test
832835 public void writeJsonArrayNull () {
833- assumeFalse ("PostgreSQL does not yet support Array" , dialect .dialect == Dialect .POSTGRESQL );
834836 write (baseInsert ().set ("JsonArrayValue" ).toJsonArray (null ).build ());
835837 Struct row = readLastRow ("JsonArrayValue" );
836838 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+ }
838844 }
839845
840846 @ Test
841847 public void writeJsonArrayEmpty () {
842- assumeFalse ("PostgreSQL does not yet support Array" , dialect .dialect == Dialect .POSTGRESQL );
843848 write (baseInsert ().set ("JsonArrayValue" ).toJsonArray (Collections .emptyList ()).build ());
844849 Struct row = readLastRow ("JsonArrayValue" );
845850 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+ }
848858 }
849859
850860 @ Test
851861 public void writeJsonArray () {
852- assumeFalse ("PostgreSQL does not yet support Array" , dialect .dialect == Dialect .POSTGRESQL );
853862 write (baseInsert ().set ("JsonArrayValue" ).toJsonArray (Arrays .asList ("[]" , null , "{}" )).build ());
854863 Struct row = readLastRow ("JsonArrayValue" );
855864 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+ }
858872 }
859873
860874 @ Test
861875 public void writeJsonArrayNoNulls () {
862- assumeFalse ("PostgreSQL does not yet support Array" , dialect .dialect == Dialect .POSTGRESQL );
863876 write (
864877 baseInsert ()
865878 .set ("JsonArrayValue" )
866879 .toJsonArray (Arrays .asList ("[]" , "{\" color\" :\" red\" ,\" value\" :\" #f00\" }" , "{}" ))
867880 .build ());
868881 Struct row = readLastRow ("JsonArrayValue" );
869882 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+ }
874894 }
875895
876896 @ Test
@@ -1430,9 +1450,7 @@ public void testTypeNamesPostgreSQL() {
14301450
14311451 assertTrue (resultSet .next ());
14321452 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" ));
14361454
14371455 assertTrue (resultSet .next ());
14381456 assertEquals ("datevalue" , resultSet .getString ("column_name" ));
0 commit comments