diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 01bc600..e26047d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest defaults: run: - working-directory: ./examples + working-directory: ./tests steps: - uses: actions/checkout@v4 with: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 00a48ff..ecb6c20 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,7 +1,7 @@ # Release Checklist - Run all tests to make sure there are no regressions - Update the `version` in gradle.properties to match this release's tag -- Update all unpinned dependencies in both the library and examples project's gradle.properties +- Update all unpinned dependencies in both the library and tests project's gradle.properties - Make sure the versions in the README's Requirements section match the used dependency versions - Add a new section to the CHANGELOG describing what has changed - Update the versions in the README's Getting Started section diff --git a/README.md b/README.md index 7d0eafc..e99d78d 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,9 @@ A dynamic, type-safe way to write your queries [Changelog](CHANGELOG.md) | [Contributing](CONTRIBUTING.md) ## Getting Started -Examples given use Gradle's Kotlin DSL +
+Gradle Kotlin DSL + Add the KSP Gradle plugin to your build file ```kotlin plugins { @@ -60,6 +62,7 @@ project.afterEvaluate { } } ``` +
## Requirements - Quarkus version `3.9.2` or newer diff --git a/examples/src/main/kotlin/ch/icken/Examples.kt b/examples/src/main/kotlin/ch/icken/Examples.kt deleted file mode 100644 index abac153..0000000 --- a/examples/src/main/kotlin/ch/icken/Examples.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2023-2024 Thijs Koppen - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.icken - -import ch.icken.model.Employee -import ch.icken.model.generated.* -import ch.icken.query.* -import java.time.LocalDate - -//WHERE FIRST_NAME = 'John' -//Using type-safe sealed result wrapper -fun findJohn() = Employee.where { firstName eq "John" }.singleSafe() - -//SELECT COUNT(*) FROM EMPLOYEE WHERE GENDER != 'M' -fun countNotMen() = Employee.count { gender neq Employee.Gender.M } - -//WHERE BIRTH_DATE < 1970-01-01 -fun bornBefore(date: LocalDate) = Employee.multiple { birthDate lt date } - -//WHERE LAST_NAME LIKE '%son' -//Using find, which allows Panache pagination etc. to be used still -fun findAllSons() = Employee.find { lastName like "%son" }.list() - -//WHERE (SALARY > 50000.0 AND SALARY <= 60000.0) -// OR SALARY BETWEEN 75000.0 AND 85000.0 -//Then we take the average using Java 8 streams -fun averageOfVerySpecificSalaryRanges() = - Employee.where { - salary.gt(50_000.0) - .and { salary lte 60_000.0 } - } - .or { salary.between(75_000.0, 85_000.0) } - .stream() - .mapToDouble { it.salary } - .average() - .orElse(0.0) diff --git a/examples/src/test/kotlin/ch/icken/model/ExamplesTest.kt b/examples/src/test/kotlin/ch/icken/model/ExamplesTest.kt deleted file mode 100644 index 35230d3..0000000 --- a/examples/src/test/kotlin/ch/icken/model/ExamplesTest.kt +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2023-2024 Thijs Koppen - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.icken.model - -import ch.icken.* -import ch.icken.query.PanacheSingleResult -import io.quarkus.test.junit.QuarkusTest -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertInstanceOf -import org.junit.jupiter.api.Test -import java.time.LocalDate - -@QuarkusTest -class ExamplesTest { - - @Test - fun runAllExamples() { - - // Given - - // When - val john = findJohn() - val numberOfNotMen = countNotMen() - val bornBeforeEpoch = bornBefore(LocalDate.EPOCH) - val sons = findAllSons() - val averageSalary = averageOfVerySpecificSalaryRanges() - - // Then - assertInstanceOf(PanacheSingleResult.Result::class.java, john) - assertEquals(4, numberOfNotMen) - assertEquals(2, bornBeforeEpoch.size) - assertEquals(2, sons.size) - assertEquals(67_500.0, averageSalary) - } -} diff --git a/examples/.gitignore b/tests/.gitignore similarity index 100% rename from examples/.gitignore rename to tests/.gitignore diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..5a6c4ed --- /dev/null +++ b/tests/README.md @@ -0,0 +1,6 @@ +# panache-kotlin-dsl Tests +This projects contains integration tests for panache-kotlin-dsl. +These have been pulled into a separate project so KSP can be run like it would be by an implementing project, +and thus produces representative test results. +This also helps in making sure the API provided by panache-kotlin-dsl does not regress, +and that the main project is not cluttered up with code pertaining to code quality scans etc. diff --git a/examples/build.gradle.kts b/tests/build.gradle.kts similarity index 95% rename from examples/build.gradle.kts rename to tests/build.gradle.kts index d7ed1b3..4f3c87f 100644 --- a/examples/build.gradle.kts +++ b/tests/build.gradle.kts @@ -68,7 +68,6 @@ kover { filters { //Exclude non-library classes/packages excludes { - classes("ch.icken.ExamplesKt") packages("ch.icken.model") } } @@ -80,8 +79,8 @@ sonar { property("sonar.projectName", "panache-kotlin-dsl") property("sonar.coverage.jacoco.xmlReportPaths", "**/build/reports/kover/report.xml") //Exclude non-library classes/packages - property("sonar.coverage.exclusions", "**/ch/icken/model/*,**/ch/icken/Examples.kt") - property("sonar.exclusions", "**/ch/icken/model/*,**/ch/icken/Examples.kt") + property("sonar.coverage.exclusions", "**/ch/icken/model/*") + property("sonar.exclusions", "**/ch/icken/model/*") } } diff --git a/examples/gradle.properties b/tests/gradle.properties similarity index 100% rename from examples/gradle.properties rename to tests/gradle.properties diff --git a/examples/gradle/wrapper/gradle-wrapper.jar b/tests/gradle/wrapper/gradle-wrapper.jar similarity index 100% rename from examples/gradle/wrapper/gradle-wrapper.jar rename to tests/gradle/wrapper/gradle-wrapper.jar diff --git a/examples/gradle/wrapper/gradle-wrapper.properties b/tests/gradle/wrapper/gradle-wrapper.properties similarity index 100% rename from examples/gradle/wrapper/gradle-wrapper.properties rename to tests/gradle/wrapper/gradle-wrapper.properties diff --git a/examples/gradlew b/tests/gradlew similarity index 100% rename from examples/gradlew rename to tests/gradlew diff --git a/examples/gradlew.bat b/tests/gradlew.bat similarity index 100% rename from examples/gradlew.bat rename to tests/gradlew.bat diff --git a/examples/settings.gradle.kts b/tests/settings.gradle.kts similarity index 98% rename from examples/settings.gradle.kts rename to tests/settings.gradle.kts index 79460ff..110d7b6 100644 --- a/examples/settings.gradle.kts +++ b/tests/settings.gradle.kts @@ -44,4 +44,4 @@ pluginManagement { project(":panache-kotlin-dsl").projectDir = file("..") } -rootProject.name = "examples" +rootProject.name = "tests" diff --git a/examples/src/main/kotlin/ch/icken/model/Assignment.kt b/tests/src/main/kotlin/ch/icken/model/Assignment.kt similarity index 100% rename from examples/src/main/kotlin/ch/icken/model/Assignment.kt rename to tests/src/main/kotlin/ch/icken/model/Assignment.kt diff --git a/examples/src/main/kotlin/ch/icken/model/Client.kt b/tests/src/main/kotlin/ch/icken/model/Client.kt similarity index 100% rename from examples/src/main/kotlin/ch/icken/model/Client.kt rename to tests/src/main/kotlin/ch/icken/model/Client.kt diff --git a/examples/src/main/kotlin/ch/icken/model/Department.kt b/tests/src/main/kotlin/ch/icken/model/Department.kt similarity index 100% rename from examples/src/main/kotlin/ch/icken/model/Department.kt rename to tests/src/main/kotlin/ch/icken/model/Department.kt diff --git a/examples/src/main/kotlin/ch/icken/model/Employee.kt b/tests/src/main/kotlin/ch/icken/model/Employee.kt similarity index 100% rename from examples/src/main/kotlin/ch/icken/model/Employee.kt rename to tests/src/main/kotlin/ch/icken/model/Employee.kt diff --git a/examples/src/main/resources/application.properties b/tests/src/main/resources/application.properties similarity index 100% rename from examples/src/main/resources/application.properties rename to tests/src/main/resources/application.properties diff --git a/examples/src/main/resources/import.sql b/tests/src/main/resources/import.sql similarity index 100% rename from examples/src/main/resources/import.sql rename to tests/src/main/resources/import.sql diff --git a/tests/src/test/kotlin/ch/icken/model/QueryTests.kt b/tests/src/test/kotlin/ch/icken/model/QueryTests.kt new file mode 100644 index 0000000..24e055b --- /dev/null +++ b/tests/src/test/kotlin/ch/icken/model/QueryTests.kt @@ -0,0 +1,87 @@ +/* + * Copyright 2023-2024 Thijs Koppen + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.icken.model + +import ch.icken.model.generated.* +import ch.icken.query.* +import io.quarkus.test.junit.QuarkusTest +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertInstanceOf +import org.junit.jupiter.api.Test +import java.time.LocalDate + +@QuarkusTest +class QueryTests { + + @Test + fun findJohn() { + + //WHERE FIRST_NAME = 'John' + //Using type-safe sealed result wrapper + val john = Employee.where { firstName eq "John" }.singleSafe() + + assertInstanceOf(PanacheSingleResult.Result::class.java, john) + } + + @Test + fun countNotMen() { + + //SELECT COUNT(*) FROM EMPLOYEE WHERE GENDER != 'M' + val numberOfNotMen = Employee.count { gender neq Employee.Gender.M } + + assertEquals(4, numberOfNotMen) + } + + @Test + fun bornBeforeEpoch() { + + //WHERE BIRTH_DATE < 1970-01-01 + val bornBeforeEpoch = Employee.multiple { birthDate lt LocalDate.EPOCH } + + assertEquals(2, bornBeforeEpoch.size) + } + + @Test + fun findAllSons() { + + //WHERE LAST_NAME LIKE '%son' + //Using find, which allows Panache pagination etc. to be used still + val sons = Employee.find { lastName like "%son" }.list() + + assertEquals(2, sons.size) + } + + @Test + fun averageSalary() { + + //WHERE (SALARY > 50000.0 AND SALARY <= 60000.0) + // OR SALARY BETWEEN 75000.0 AND 85000.0 + //Then we take the average using Java 8 streams + val averageSalary = Employee + .where { + salary.gt(50_000.0) + .and { salary lte 60_000.0 } + } + .or { salary.between(75_000.0, 85_000.0) } + .stream() + .mapToDouble { it.salary } + .average() + .orElse(0.0) + + assertEquals(67_500.0, averageSalary) + } +}