|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -package org.springframework.batch.extensions.bigquery.integration.reader.batch; |
18 |
| - |
19 |
| -import com.google.cloud.bigquery.QueryJobConfiguration; |
20 |
| -import com.google.cloud.bigquery.TableId; |
21 |
| -import org.junit.jupiter.api.Assertions; |
22 |
| -import org.junit.jupiter.api.Tag; |
23 |
| -import org.junit.jupiter.api.Test; |
24 |
| -import org.junit.jupiter.api.TestInfo; |
| 17 | +package org.springframework.batch.extensions.bigquery.gcloud.reader; |
| 18 | + |
| 19 | +import com.google.cloud.bigquery.*; |
| 20 | +import org.junit.jupiter.api.*; |
25 | 21 | import org.springframework.batch.extensions.bigquery.common.BigQueryDataLoader;
|
26 | 22 | import org.springframework.batch.extensions.bigquery.common.PersonDto;
|
27 | 23 | import org.springframework.batch.extensions.bigquery.common.TestConstants;
|
28 |
| -import org.springframework.batch.extensions.bigquery.integration.reader.base.BaseCsvJsonInteractiveQueryItemReaderTest; |
| 24 | +import org.springframework.batch.extensions.bigquery.gcloud.base.BaseBigQueryGcloudIntegrationTest; |
29 | 25 | import org.springframework.batch.extensions.bigquery.reader.BigQueryQueryItemReader;
|
30 | 26 | import org.springframework.batch.extensions.bigquery.reader.builder.BigQueryQueryItemReaderBuilder;
|
31 |
| -import org.springframework.batch.item.Chunk; |
32 | 27 |
|
33 |
| -@Tag("csv") |
34 |
| -class BigQueryBatchQueryCsvItemReaderTest extends BaseCsvJsonInteractiveQueryItemReaderTest { |
| 28 | +class BigQueryGcloudItemReaderTest extends BaseBigQueryGcloudIntegrationTest { |
35 | 29 |
|
36 |
| - @Test |
37 |
| - void batchQueryTest1(TestInfo testInfo) throws Exception { |
38 |
| - String tableName = getTableName(testInfo); |
39 |
| - new BigQueryDataLoader(bigQuery).loadCsvSample(tableName); |
40 |
| - Chunk<PersonDto> chunk = BigQueryDataLoader.CHUNK; |
| 30 | + private static final String TABLE = "csv"; |
| 31 | + |
| 32 | + @BeforeAll |
| 33 | + static void init() throws Exception { |
| 34 | + if (BIG_QUERY.getDataset(TestConstants.DATASET) == null) { |
| 35 | + BIG_QUERY.create(DatasetInfo.of(TestConstants.DATASET)); |
| 36 | + } |
| 37 | + |
| 38 | + if (BIG_QUERY.getTable(TestConstants.DATASET, TABLE) == null) { |
| 39 | + TableDefinition tableDefinition = StandardTableDefinition.of(PersonDto.getBigQuerySchema()); |
| 40 | + BIG_QUERY.create(TableInfo.of(TableId.of(TestConstants.DATASET, TABLE), tableDefinition)); |
| 41 | + } |
| 42 | + |
| 43 | + new BigQueryDataLoader(BIG_QUERY).loadCsvSample(TABLE); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterAll |
| 47 | + static void cleanupTest() { |
| 48 | + BIG_QUERY.delete(TableId.of(TestConstants.DATASET, TABLE)); |
| 49 | + } |
41 | 50 |
|
| 51 | + @Test |
| 52 | + void testBatchQuery() throws Exception { |
42 | 53 | QueryJobConfiguration jobConfiguration = QueryJobConfiguration
|
43 |
| - .newBuilder("SELECT p.name, p.age FROM spring_batch_extensions.%s p ORDER BY p.name LIMIT 2") |
44 |
| - .setDestinationTable(TableId.of(TestConstants.DATASET, tableName)) |
| 54 | + .newBuilder("SELECT p.name, p.age FROM spring_batch_extensions.%s p ORDER BY p.name LIMIT 2".formatted(TABLE)) |
| 55 | + .setDestinationTable(TableId.of(TestConstants.DATASET, TABLE)) |
45 | 56 | .setPriority(QueryJobConfiguration.Priority.BATCH)
|
46 | 57 | .build();
|
47 | 58 |
|
48 | 59 | BigQueryQueryItemReader<PersonDto> reader = new BigQueryQueryItemReaderBuilder<PersonDto>()
|
49 |
| - .bigQuery(bigQuery) |
| 60 | + .bigQuery(BIG_QUERY) |
50 | 61 | .rowMapper(TestConstants.PERSON_MAPPER)
|
51 | 62 | .jobConfiguration(jobConfiguration)
|
52 | 63 | .build();
|
53 | 64 |
|
54 | 65 | reader.afterPropertiesSet();
|
55 | 66 |
|
| 67 | + verifyResult(reader); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testInteractiveQuery() throws Exception { |
| 72 | + BigQueryQueryItemReader<PersonDto> reader = new BigQueryQueryItemReaderBuilder<PersonDto>() |
| 73 | + .bigQuery(BIG_QUERY) |
| 74 | + .query("SELECT p.name, p.age FROM spring_batch_extensions.%s p ORDER BY p.name LIMIT 2".formatted(TABLE)) |
| 75 | + .rowMapper(TestConstants.PERSON_MAPPER) |
| 76 | + .build(); |
| 77 | + |
| 78 | + reader.afterPropertiesSet(); |
| 79 | + |
| 80 | + verifyResult(reader); |
| 81 | + } |
| 82 | + |
| 83 | + private void verifyResult(BigQueryQueryItemReader<PersonDto> reader) throws Exception { |
56 | 84 | PersonDto actualFirstPerson = reader.read();
|
57 |
| - PersonDto expectedFirstPerson = chunk.getItems().get(0); |
| 85 | + PersonDto expectedFirstPerson = BigQueryDataLoader.CHUNK.getItems().get(0); |
58 | 86 |
|
59 | 87 | PersonDto actualSecondPerson = reader.read();
|
60 |
| - PersonDto expectedSecondPerson = chunk.getItems().get(1); |
| 88 | + PersonDto expectedSecondPerson = BigQueryDataLoader.CHUNK.getItems().get(1); |
61 | 89 |
|
62 | 90 | PersonDto actualThirdPerson = reader.read();
|
63 | 91 |
|
|
0 commit comments