From 5170f528cdf285ff82eb967de556b3ccfd724f8c Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 21 Oct 2023 21:40:38 +0300 Subject: [PATCH] Add tests for the indicator repository. --- src/test/groovy/DatabaseDrivenSpec.groovy | 37 ----- .../IndicatorRepositorySpec.groovy | 148 ++++++++++++++++++ 2 files changed, 148 insertions(+), 37 deletions(-) delete mode 100644 src/test/groovy/DatabaseDrivenSpec.groovy create mode 100644 src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/IndicatorRepositorySpec.groovy diff --git a/src/test/groovy/DatabaseDrivenSpec.groovy b/src/test/groovy/DatabaseDrivenSpec.groovy deleted file mode 100644 index 51f69f8..0000000 --- a/src/test/groovy/DatabaseDrivenSpec.groovy +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2009 the original author or authors. - * - * 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. - */ - -import groovy.sql.Sql -import spock.lang.Shared -import spock.lang.Specification - -class DatabaseDrivenSpec extends Specification { - @Shared sql = Sql.newInstance("jdbc:h2:mem:", "org.h2.Driver") - - // insert data (usually the database would already contain the data) - def setupSpec() { - sql.execute("create table maxdata (id int primary key, a int, b int, c int)") - sql.execute("insert into maxdata values (1, 3, 7, 7), (2, 5, 4, 5), (3, 9, 9, 9), (4, 2, -3, 2)") - } - - def "Maximum of #a and #b is #c"() { - expect: - Math.max(a, b) == c - - where: - [a, b, c] << sql.rows("select a, b, c from maxdata") - } -} diff --git a/src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/IndicatorRepositorySpec.groovy b/src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/IndicatorRepositorySpec.groovy new file mode 100644 index 0000000..388900a --- /dev/null +++ b/src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/IndicatorRepositorySpec.groovy @@ -0,0 +1,148 @@ +package space.kklochko.jdbc_hospital_example.db.repositories + +import groovy.sql.Sql +import space.kklochko.jdbc_hospital_example.config.models.DataBaseConfig +import space.kklochko.jdbc_hospital_example.db.factories.DataBaseConnection +import space.kklochko.jdbc_hospital_example.models.Indicator +import spock.lang.Shared +import spock.lang.Specification +import spock.lang.Stepwise +import spock.lang.Subject + +import java.sql.Timestamp +import java.util.UUID + +@Stepwise +class IndicatorRepositorySpec extends Specification { + @Shared + Sql sql + + @Shared + DataBaseConfig db + + @Shared + DataBaseConnection dbc + + def setupSpec() { + db = new DataBaseConfig() + dbc = new DataBaseConnection(db) + + IndicatorRepository repo = new IndicatorRepository(dbc) + repo.createTable() + + sql = Sql.newInstance(db.getUrl(), db.getProperties().get("user"), db.getProperties().get("password")) + + sql.execute("insert into indicators values " + + "('bcbbcdb4-702c-11ee-9113-c0e4349366ab', 'Samples', 'fine', '2023-10-17 16:30:00')," + + "('becbafac-702c-11ee-9113-c0e4349366ab', 'Some samples', 'well', '2023-10-17 11:30:00')") + + } + + def cleanupSpec() { + sql.execute("delete from indicators") + } + + def "Read one indicator"() { + given: "I have an existing indicator" + UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366ab") + + and: "I have a repo" + @Subject + IndicatorRepository repo = new IndicatorRepository(dbc) + + when: "reading the entry" + def result = repo.read(id) + + then: "checking that the reading was successful" + result + + and: "checking the entry with the expected data" + def data = sql.firstRow("SELECT type, values, timestamp FROM indicators WHERE id = ?", id) + with(data) { + get("type") == 'Some samples' + get("values") == 'well' + get("timestamp") == Timestamp.valueOf('2023-10-17 11:30:00') + } + } + + def "Read all indicators"() { + given: "I have a repo" + @Subject + IndicatorRepository repo = new IndicatorRepository(dbc) + + when: "reading the entries" + def result = repo.readAll() + + then: "checking that the reading was successful" + result + + and: "checking the entries with the expected data" + def data = sql.rows("SELECT type, values, timestamp FROM indicators") + data.collect{it.get("type")} == ['Samples', 'Some samples'] + } + + def "Add one indicator"() { + given: "I create an indicator" + Timestamp timestamp = Timestamp.valueOf("2023-10-17 14:30:00") + Indicator indicator = new Indicator("Blood", "Fine", timestamp) + + and: "I have a repo" + @Subject + IndicatorRepository repo = new IndicatorRepository(dbc) + + when: "inserting the entry" + def result = repo.create(indicator) + + then: "checking that the insert was successful" + result + indicator.id + + and: "checking that the entry is added" + def isExist = sql.firstRow("SELECT type, values, timestamp FROM indicators WHERE id = ?", indicator.id) + isExist + } + + def "Update one indicator"() { + given: "I have an existing indicator" + UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366ab") + Timestamp timestamp = Timestamp.valueOf("2023-10-18 14:30:00") + Indicator indicator = new Indicator(id, "Blood2", "Fine2", timestamp) + + and: "I have a repo" + @Subject + IndicatorRepository repo = new IndicatorRepository(dbc) + + when: "updating the entry" + def result = repo.update(indicator) + + then: "checking that the update was successful" + result + + and: "checking that the entry is updated" + def data = sql.firstRow("SELECT type, values, timestamp FROM indicators WHERE id = ?", indicator.id) + with(data) { + get("type") == indicator.type + get("values") == indicator.values + get("timestamp") == indicator.timestamp + } + } + + def "Remove one indicator"() { + given: "I have the id of an existing indicator" + UUID id = UUID.fromString("bcbbcdb4-702c-11ee-9113-c0e4349366ab") + + and: "I have a repo" + @Subject + IndicatorRepository repo = new IndicatorRepository(dbc) + + when: "deleting the entry" + def result = repo.delete(id) + + then: "checking that the delete was successful" + result + + and: "checking that the entry was deleted" + def isExist = sql.firstRow("SELECT type, values, timestamp FROM indicators WHERE id = ?", id) + !isExist + } +}