Add a repository for Patients.

main
KKlochko 2 years ago
parent f84b25b0f4
commit 519e48ba2c

@ -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.Patient
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Stepwise
import spock.lang.Subject
@Stepwise
class PatientRepositorySpec extends Specification {
@Shared
Sql sql
@Shared
DataBaseConfig db
@Shared
DataBaseConnection dbc
def setupSpec() {
db = (new LoadDataBaseConfigFromEnvFile()).load("db.testing")
dbc = new DataBaseConnection(db)
PatientRepository repo = new PatientRepository(dbc)
repo.createTable()
sql = Sql.newInstance(db.getUrl(), db.getProperties().get("user"), db.getProperties().get("password"))
sql.execute("insert into patients values " +
"('bcbbcdb4-702c-11ee-9113-c0e4349366bb', 'Oleh', 21, '380123451234')," +
"('becbafac-702c-11ee-9113-c0e4349366bb', 'Olga', 19, '380123451235')")
}
def cleanupSpec() {
sql.execute("delete from patients")
}
def "Read one indicator"() {
given: "I have an existing patient"
UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366bb")
and: "I have a repo"
@Subject
PatientRepository repo = new PatientRepository(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, age, phone FROM patients WHERE id = ?", id)
with(data) {
get("name") == 'Olga'
get("age") == 19
get("phone") == '380123451235'
}
}
def "Read all patients"() {
given: "I have a repo"
@Subject
PatientRepository repo = new PatientRepository(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, age, phone FROM patients")
data.collect{it.get("name")} == ['Oleh', 'Olga']
}
def "Add one patient"() {
given: "I create an patient"
Patient patient = new Patient("Kostia", 40, '380123451235')
and: "I have a repo"
@Subject
PatientRepository repo = new PatientRepository(dbc)
when: "inserting the entry"
def result = repo.create(patient)
then: "checking that the insert was successful"
result
patient.id
and: "checking that the entry is added"
def isExist = sql.firstRow("SELECT name, age, phone FROM patients WHERE id = ?", patient.id)
isExist
}
def "Update one patient"() {
given: "I have an existing patient"
UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366bb")
Patient patient = new Patient(id, "Oleh", 30, "380123451237")
and: "I have a repo"
@Subject
PatientRepository repo = new PatientRepository(dbc)
when: "updating the entry"
def result = repo.update(patient)
then: "checking that the update was successful"
result
and: "checking that the entry is updated"
def data = sql.firstRow("SELECT name, age, phone FROM patients WHERE id = ?", patient.id)
with(data) {
get("name") == patient.name
get("age") == patient.age
get("phone") == patient.phone
}
}
def "Remove one patient"() {
given: "I have the id of an existing patient"
UUID id = UUID.fromString("bcbbcdb4-702c-11ee-9113-c0e4349366bb")
and: "I have a repo"
@Subject
PatientRepository repo = new PatientRepository(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, age, phone FROM patients WHERE id = ?", id)
!isExist
}
}
Loading…
Cancel
Save