|
| 1 | +/* |
| 2 | + * Copyright 2023 Qameta Software OÜ |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.qameta.allure.jooq; |
| 17 | + |
| 18 | +import io.qameta.allure.Allure; |
| 19 | +import io.qameta.allure.model.Attachment; |
| 20 | +import io.qameta.allure.model.Status; |
| 21 | +import io.qameta.allure.model.StepResult; |
| 22 | +import io.qameta.allure.model.TestResult; |
| 23 | +import io.qameta.allure.test.AllureResults; |
| 24 | +import io.zonky.test.db.postgres.embedded.EmbeddedPostgres; |
| 25 | +import org.jooq.DSLContext; |
| 26 | +import org.jooq.Field; |
| 27 | +import org.jooq.Name; |
| 28 | +import org.jooq.Record; |
| 29 | +import org.jooq.SQLDialect; |
| 30 | +import org.jooq.Table; |
| 31 | +import org.jooq.impl.DSL; |
| 32 | +import org.jooq.impl.DataSourceConnectionProvider; |
| 33 | +import org.jooq.impl.DefaultConfiguration; |
| 34 | +import org.jooq.impl.DefaultDSLContext; |
| 35 | +import org.jooq.impl.SQLDataType; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | + |
| 38 | +import javax.sql.DataSource; |
| 39 | +import java.io.IOException; |
| 40 | +import java.io.UncheckedIOException; |
| 41 | +import java.nio.charset.StandardCharsets; |
| 42 | +import java.util.function.Consumer; |
| 43 | + |
| 44 | +import static io.qameta.allure.test.RunUtils.runWithinTestContext; |
| 45 | +import static org.assertj.core.api.Assertions.assertThat; |
| 46 | +import static org.assertj.core.api.Assertions.tuple; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author charlie (Dmitry Baev). |
| 50 | + */ |
| 51 | +class AllureJooqTest { |
| 52 | + |
| 53 | + @Test |
| 54 | + void shouldSupportFetchSqlStatements() { |
| 55 | + final AllureResults results = execute(dsl -> dsl.fetchSingle("select 1")); |
| 56 | + |
| 57 | + final TestResult result = results.getTestResults().get(0); |
| 58 | + assertThat(result.getSteps()) |
| 59 | + .extracting(StepResult::getName, StepResult::getStatus) |
| 60 | + .containsExactly( |
| 61 | + tuple("select 1", Status.PASSED) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void shouldAddResultSetsAsAttachments() { |
| 67 | + final AllureResults results = execute(dsl -> dsl.fetchSingle("select 1 as one, 2 as two")); |
| 68 | + final TestResult result = results.getTestResults().get(0); |
| 69 | + final StepResult step = result.getSteps().get(0); |
| 70 | + assertThat(step.getAttachments()) |
| 71 | + .extracting(Attachment::getName, Attachment::getType) |
| 72 | + .containsExactly( |
| 73 | + tuple("ResultSet", "text/csv") |
| 74 | + ); |
| 75 | + |
| 76 | + final Attachment attachment = step.getAttachments().get(0); |
| 77 | + |
| 78 | + final byte[] content = results.getAttachments().get(attachment.getSource()); |
| 79 | + |
| 80 | + assertThat(new String(content, StandardCharsets.UTF_8)) |
| 81 | + .contains("one,two\n1,2\n"); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void shouldSupportCreateTableStatements() { |
| 87 | + final AllureResults results = execute(dsl -> { |
| 88 | + final Name tableName = DSL.name("first_table"); |
| 89 | + final Field<Long> id = DSL.field("id", SQLDataType.BIGINT); |
| 90 | + final Field<String> name = DSL.field("name", SQLDataType.VARCHAR); |
| 91 | + dsl.createTable(tableName) |
| 92 | + .column(id) |
| 93 | + .column(name) |
| 94 | + .primaryKey(id) |
| 95 | + .execute(); |
| 96 | + |
| 97 | + final Table<Record> table = DSL.table(tableName); |
| 98 | + |
| 99 | + dsl.insertInto(table, id, name) |
| 100 | + .values(1L, "first") |
| 101 | + .values(2L, "second") |
| 102 | + .execute(); |
| 103 | + }); |
| 104 | + |
| 105 | + final TestResult result = results.getTestResults().get(0); |
| 106 | + assertThat(result.getSteps()) |
| 107 | + .extracting(StepResult::getName, StepResult::getStatus) |
| 108 | + .containsExactly( |
| 109 | + tuple("create table \"first_table\" (id bigint, name varchar, primary key (id))", Status.PASSED), |
| 110 | + tuple("insert into \"first_table\" (id, name) values (1, 'first'), (2, 'second')", Status.PASSED) |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + private static AllureResults execute(final Consumer<DSLContext> dslContextConsumer) { |
| 115 | + final EmbeddedPostgres.Builder builder = EmbeddedPostgres.builder(); |
| 116 | + try (EmbeddedPostgres postgres = builder.start()) { |
| 117 | + final DataSource dataSource = postgres.getPostgresDatabase(); |
| 118 | + |
| 119 | + final DataSourceConnectionProvider connectionProvider = new DataSourceConnectionProvider(dataSource); |
| 120 | + final DefaultConfiguration configuration = new DefaultConfiguration(); |
| 121 | + configuration.set(SQLDialect.POSTGRES); |
| 122 | + configuration.set(connectionProvider); |
| 123 | + |
| 124 | + return runWithinTestContext( |
| 125 | + () -> { |
| 126 | + final DefaultDSLContext dsl = new DefaultDSLContext(configuration); |
| 127 | + dslContextConsumer.accept(dsl); |
| 128 | + }, |
| 129 | + Allure::setLifecycle, |
| 130 | + allureLifecycle -> configuration.setExecuteListener(new AllureJooq(allureLifecycle)) |
| 131 | + ); |
| 132 | + } catch (IOException e) { |
| 133 | + throw new UncheckedIOException(e); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments