Add the repository for Deparment.

This commit is contained in:
2023-10-22 20:42:42 +03:00
parent e528454576
commit fead10291c
3 changed files with 279 additions and 1 deletions
@@ -0,0 +1,144 @@
package space.kklochko.jdbc_hospital_example.db.repositories
import groovy.sql.Sql
import space.kklochko.jdbc_hospital_example.config.factories.LoadDataBaseConfigFromEnvFile
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.Department
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Stepwise
import spock.lang.Subject
@Stepwise
class DepartmentRepositorySpec extends Specification {
@Shared
Sql sql
@Shared
DataBaseConfig db
@Shared
DataBaseConnection dbc
def setupSpec() {
db = (new LoadDataBaseConfigFromEnvFile()).load("db.testing")
dbc = new DataBaseConnection(db)
DepartmentRepository repo = new DepartmentRepository(dbc)
repo.createTable()
sql = Sql.newInstance(db.getUrl(), db.getProperties().get("user"), db.getProperties().get("password"))
sql.execute("insert into departments values " +
"('bcbbcdb4-702c-11ee-9113-c0e4349366cb', 'First department', 'Stepan Bandera Street', '380123451244')," +
"('becbafac-702c-11ee-9113-c0e4349366cb', 'Second department', 'Taras Shevchenko street', '380123451245')")
}
def cleanupSpec() {
sql.execute("delete from departments")
}
def "Read one department"() {
given: "I have an existing department"
UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366cb")
and: "I have a repo"
@Subject
DepartmentRepository repo = new DepartmentRepository(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 name, location, phone FROM departments WHERE id = ?", id)
with(data) {
get("name") == 'Second department'
get("location") == 'Taras Shevchenko street'
get("phone") == '380123451245'
}
}
def "Read all indicators"() {
given: "I have a repo"
@Subject
DepartmentRepository repo = new DepartmentRepository(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 name, location, phone FROM departments")
data.collect{it.get("location")} == ['Stepan Bandera Street', 'Taras Shevchenko street']
}
def "Add one department"() {
given: "I create an department"
Department department = new Department("Department", "Lesya Ukrainka Street", "380123451235")
and: "I have a repo"
@Subject
DepartmentRepository repo = new DepartmentRepository(dbc)
when: "inserting the entry"
def result = repo.create(department)
then: "checking that the insert was successful"
result
department.id
and: "checking that the entry is added"
def isExist = sql.firstRow("SELECT name, location, phone FROM departments WHERE id = ?", department.id)
isExist
}
def "Update one department"() {
given: "I have an existing department"
UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366cb")
Department department = new Department(id, "Department", "Lesya Ukrainka Street", "380123451235")
and: "I have a repo"
@Subject
DepartmentRepository repo = new DepartmentRepository(dbc)
when: "updating the entry"
def result = repo.update(department)
then: "checking that the update was successful"
result
and: "checking that the entry is updated"
def data = sql.firstRow("SELECT name, location, phone FROM departments WHERE id = ?", department.id)
with(data) {
get("name") == department.name
get("location") == department.location
get("phone") == department.phone
}
}
def "Remove one department"() {
given: "I have the id of an existing department"
UUID id = UUID.fromString("bcbbcdb4-702c-11ee-9113-c0e4349366cb")
and: "I have a repo"
@Subject
DepartmentRepository repo = new DepartmentRepository(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 name, location, phone FROM departments WHERE id = ?", id)
!isExist
}
}
@@ -39,7 +39,7 @@ class PatientRepositorySpec extends Specification {
sql.execute("delete from patients")
}
def "Read one indicator"() {
def "Read one patients"() {
given: "I have an existing patient"
UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366bb")