|
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 | + @BeforeAll |
| 31 | + static void init() throws Exception { |
| 32 | + if (BIG_QUERY.getDataset(TestConstants.DATASET) == null) { |
| 33 | + BIG_QUERY.create(DatasetInfo.of(TestConstants.DATASET)); |
| 34 | + } |
| 35 | + |
| 36 | + if (BIG_QUERY.getTable(TestConstants.DATASET, TestConstants.CSV) == null) { |
| 37 | + TableDefinition tableDefinition = StandardTableDefinition.of(PersonDto.getBigQuerySchema()); |
| 38 | + BIG_QUERY.create(TableInfo.of(TableId.of(TestConstants.DATASET, TestConstants.CSV), tableDefinition)); |
| 39 | + } |
| 40 | + |
| 41 | + new BigQueryDataLoader(BIG_QUERY).loadCsvSample(TestConstants.CSV); |
| 42 | + } |
41 | 43 |
|
| 44 | + @AfterAll |
| 45 | + static void cleanupTest() { |
| 46 | + BIG_QUERY.delete(TableId.of(TestConstants.DATASET, TestConstants.CSV)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testBatchQuery() throws Exception { |
42 | 51 | 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)) |
| 52 | + .newBuilder("SELECT p.name, p.age FROM spring_batch_extensions.%s p ORDER BY p.name LIMIT 2".formatted(TestConstants.CSV)) |
| 53 | + .setDestinationTable(TableId.of(TestConstants.DATASET, TestConstants.CSV)) |
45 | 54 | .setPriority(QueryJobConfiguration.Priority.BATCH)
|
46 | 55 | .build();
|
47 | 56 |
|
48 | 57 | BigQueryQueryItemReader<PersonDto> reader = new BigQueryQueryItemReaderBuilder<PersonDto>()
|
49 |
| - .bigQuery(bigQuery) |
| 58 | + .bigQuery(BIG_QUERY) |
50 | 59 | .rowMapper(TestConstants.PERSON_MAPPER)
|
51 | 60 | .jobConfiguration(jobConfiguration)
|
52 | 61 | .build();
|
53 | 62 |
|
54 | 63 | reader.afterPropertiesSet();
|
55 | 64 |
|
| 65 | + verifyResult(reader); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testInteractiveQuery() throws Exception { |
| 70 | + BigQueryQueryItemReader<PersonDto> reader = new BigQueryQueryItemReaderBuilder<PersonDto>() |
| 71 | + .bigQuery(BIG_QUERY) |
| 72 | + .query("SELECT p.name, p.age FROM spring_batch_extensions.%s p ORDER BY p.name LIMIT 2".formatted(TestConstants.CSV)) |
| 73 | + .rowMapper(TestConstants.PERSON_MAPPER) |
| 74 | + .build(); |
| 75 | + |
| 76 | + reader.afterPropertiesSet(); |
| 77 | + |
| 78 | + verifyResult(reader); |
| 79 | + } |
| 80 | + |
| 81 | + private void verifyResult(BigQueryQueryItemReader<PersonDto> reader) throws Exception { |
56 | 82 | PersonDto actualFirstPerson = reader.read();
|
57 |
| - PersonDto expectedFirstPerson = chunk.getItems().get(0); |
| 83 | + PersonDto expectedFirstPerson = BigQueryDataLoader.CHUNK.getItems().get(0); |
58 | 84 |
|
59 | 85 | PersonDto actualSecondPerson = reader.read();
|
60 |
| - PersonDto expectedSecondPerson = chunk.getItems().get(1); |
| 86 | + PersonDto expectedSecondPerson = BigQueryDataLoader.CHUNK.getItems().get(1); |
61 | 87 |
|
62 | 88 | PersonDto actualThirdPerson = reader.read();
|
63 | 89 |
|
|
0 commit comments