use of com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode in project pgadapter by GoogleCloudPlatform.
the class RowDescriptionTest method SendPayloadStatementTest.
@Test
public void SendPayloadStatementTest() throws Exception {
int COLUMN_COUNT = 2;
String COLUMN_NAME = "default-column-name";
Type rowType = Type.struct(StructField.of(COLUMN_NAME, Type.string()), StructField.of(COLUMN_NAME, Type.string()));
when(metadata.getColumnCount()).thenReturn(COLUMN_COUNT);
when(metadata.getType()).thenReturn(rowType);
when(metadata.getColumnType(Mockito.anyInt())).thenReturn(Type.int64());
when(statement.getResultFormatCode(Mockito.anyInt())).thenReturn((short) 0).thenReturn((short) 0).thenReturn((short) 1);
JSONParser parser = new JSONParser();
JSONObject commandMetadata = (JSONObject) parser.parse(EMPTY_COMMAND_JSON);
OptionsMetadata options = new OptionsMetadata("jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database", 8888, TextFormat.POSTGRESQL, false, false, false, false, commandMetadata);
QueryMode mode = QueryMode.EXTENDED;
RowDescriptionResponse response = new RowDescriptionResponse(output, statement, metadata, options, mode);
response.sendPayload();
DataInputStream outputReader = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
// column count
assertEquals(COLUMN_COUNT, outputReader.readShort());
for (int i = 0; i < COLUMN_COUNT; i++) {
// column name
int numOfBytes = COLUMN_NAME.getBytes(UTF8).length;
byte[] bytes = new byte[numOfBytes];
assertEquals(numOfBytes, outputReader.read(bytes, 0, numOfBytes));
assertEquals(new String(bytes, UTF8), COLUMN_NAME);
// null terminator
assertEquals(DEFAULT_FLAG, outputReader.readByte());
// table oid
assertEquals(DEFAULT_FLAG, outputReader.readInt());
// column index
assertEquals(DEFAULT_FLAG, outputReader.readShort());
// type oid
assertEquals(Oid.INT8, outputReader.readInt());
// type size
assertEquals(8, outputReader.readShort());
// type modifier
assertEquals(DEFAULT_FLAG, outputReader.readInt());
// format code
assertEquals(i, outputReader.readShort());
}
}
use of com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode in project pgadapter by GoogleCloudPlatform.
the class RowDescriptionTest method SendPayloadStatementWithBinaryOptionTest.
@Test
public void SendPayloadStatementWithBinaryOptionTest() throws Exception {
int COLUMN_COUNT = 1;
String COLUMN_NAME = "default-column-name";
Type rowType = Type.struct(StructField.of(COLUMN_NAME, Type.string()));
when(metadata.getColumnCount()).thenReturn(COLUMN_COUNT);
when(metadata.getType()).thenReturn(rowType);
when(metadata.getColumnType(Mockito.anyInt())).thenReturn(Type.int64());
when(statement.getResultFormatCode(Mockito.anyInt())).thenReturn((short) 0);
JSONParser parser = new JSONParser();
JSONObject commandMetadata = (JSONObject) parser.parse(EMPTY_COMMAND_JSON);
OptionsMetadata options = new OptionsMetadata("jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database", 8888, TextFormat.POSTGRESQL, true, false, false, false, commandMetadata);
QueryMode mode = QueryMode.EXTENDED;
RowDescriptionResponse response = new RowDescriptionResponse(output, statement, metadata, options, mode);
response.sendPayload();
DataInputStream outputReader = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
// column count
assertEquals(COLUMN_COUNT, outputReader.readShort());
// column name
int numOfBytes = COLUMN_NAME.getBytes(UTF8).length;
byte[] bytes = new byte[numOfBytes];
assertEquals(numOfBytes, outputReader.read(bytes, 0, numOfBytes));
assertEquals(COLUMN_NAME, new String(bytes, UTF8));
// null terminator
assertEquals(DEFAULT_FLAG, outputReader.readByte());
// table oid
assertEquals(DEFAULT_FLAG, outputReader.readInt());
// column index
assertEquals(DEFAULT_FLAG, outputReader.readShort());
// type oid
assertEquals(Oid.INT8, outputReader.readInt());
// type size
assertEquals(8, outputReader.readShort());
// type modifier
assertEquals(DEFAULT_FLAG, outputReader.readInt());
// format code
assertEquals(1, outputReader.readShort());
}
use of com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode in project pgadapter by GoogleCloudPlatform.
the class RowDescriptionTest method SendPayloadNullStatementTest.
@Test
public void SendPayloadNullStatementTest() throws Exception {
int COLUMN_COUNT = 1;
String COLUMN_NAME = "default-column-name";
Type rowType = Type.struct(StructField.of(COLUMN_NAME, Type.string()));
when(metadata.getColumnCount()).thenReturn(COLUMN_COUNT);
when(metadata.getType()).thenReturn(rowType);
when(metadata.getColumnType(Mockito.anyInt())).thenReturn(Type.int64());
JSONParser parser = new JSONParser();
JSONObject commandMetadata = (JSONObject) parser.parse(EMPTY_COMMAND_JSON);
OptionsMetadata options = new OptionsMetadata("jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database", 8888, TextFormat.POSTGRESQL, false, false, false, false, commandMetadata);
QueryMode mode = QueryMode.EXTENDED;
RowDescriptionResponse response = new RowDescriptionResponse(output, null, metadata, options, mode);
response.sendPayload();
DataInputStream outputReader = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
// column count
assertEquals(COLUMN_COUNT, outputReader.readShort());
// column name
int numOfBytes = COLUMN_NAME.getBytes(UTF8).length;
byte[] bytes = new byte[numOfBytes];
assertEquals(numOfBytes, outputReader.read(bytes, 0, numOfBytes));
assertEquals(new String(bytes, UTF8), COLUMN_NAME);
// null terminator
assertEquals(DEFAULT_FLAG, outputReader.readByte());
// table oid
assertEquals(DEFAULT_FLAG, outputReader.readInt());
// column index
assertEquals(DEFAULT_FLAG, outputReader.readShort());
// type oid
assertEquals(Oid.INT8, outputReader.readInt());
// type size
assertEquals(8, outputReader.readShort());
// type modifier
assertEquals(DEFAULT_FLAG, outputReader.readInt());
// format code
assertEquals(0, outputReader.readShort());
}
use of com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode in project pgadapter by GoogleCloudPlatform.
the class RowDescriptionTest method OidTest.
@Test
public void OidTest() throws Exception {
when(metadata.getColumnCount()).thenReturn(0);
when(metadata.getColumnType(0)).thenReturn(Type.int64());
when(metadata.getColumnType(1)).thenReturn(Type.pgNumeric());
when(metadata.getColumnType(2)).thenReturn(Type.float64());
when(metadata.getColumnType(3)).thenReturn(Type.string());
when(metadata.getColumnType(4)).thenReturn(Type.bytes());
when(metadata.getColumnType(5)).thenReturn(Type.bool());
when(metadata.getColumnType(6)).thenReturn(Type.date());
when(metadata.getColumnType(7)).thenReturn(Type.timestamp());
JSONParser parser = new JSONParser();
JSONObject commandMetadata = (JSONObject) parser.parse(EMPTY_COMMAND_JSON);
OptionsMetadata options = new OptionsMetadata("jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database", 8888, TextFormat.POSTGRESQL, false, false, false, false, commandMetadata);
QueryMode mode = QueryMode.SIMPLE;
RowDescriptionResponse response = new RowDescriptionResponse(output, statement, metadata, options, mode);
// Types.BIGINT
assertEquals(Oid.INT8, response.getOidType(0));
assertEquals(8, response.getOidTypeSize(Oid.INT8));
// Types.NUMERIC
assertEquals(Oid.NUMERIC, response.getOidType(1));
assertEquals(-1, response.getOidTypeSize(Oid.NUMERIC));
// Types.DOUBLE
assertEquals(Oid.FLOAT8, response.getOidType(2));
assertEquals(8, response.getOidTypeSize(Oid.FLOAT8));
// Types.VARCHAR
assertEquals(Oid.VARCHAR, response.getOidType(3));
assertEquals(-1, response.getOidTypeSize(Oid.VARCHAR));
// Types.BINARY
assertEquals(Oid.BYTEA, response.getOidType(4));
assertEquals(-1, response.getOidTypeSize(Oid.BYTEA));
// Types.BIT
assertEquals(Oid.BOOL, response.getOidType(5));
assertEquals(1, response.getOidTypeSize(Oid.BOOL));
// Types.DATE
assertEquals(Oid.DATE, response.getOidType(6));
assertEquals(8, response.getOidTypeSize(Oid.DATE));
// Types.TIMESTAMP
assertEquals(Oid.TIMESTAMPTZ, response.getOidType(7));
assertEquals(12, response.getOidTypeSize(Oid.TIMESTAMPTZ));
}
Aggregations