Rename the project and the package name in tests.
This commit is contained in:
+149
@@ -0,0 +1,149 @@
|
||||
package space.kklochko.spring_rest_example.db.repositories
|
||||
|
||||
import groovy.sql.Sql
|
||||
import jakarta.persistence.EntityManager
|
||||
import org.junit.Ignore
|
||||
import space.kklochko.spring_rest_example.config.factories.LoadDataBaseConfigFromEnvFile
|
||||
import space.kklochko.spring_rest_example.config.models.DataBaseConfig
|
||||
import space.kklochko.spring_rest_example.db.factories.DataBaseConnection
|
||||
import space.kklochko.spring_rest_example.db.factories.EntityManagerConnection
|
||||
import space.kklochko.spring_rest_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
|
||||
EntityManager manager
|
||||
|
||||
@Shared
|
||||
DataBaseConfig db
|
||||
|
||||
@Shared
|
||||
DataBaseConnection dbc
|
||||
|
||||
def setupSpec() {
|
||||
db = (new LoadDataBaseConfigFromEnvFile()).load("db.testing")
|
||||
dbc = new DataBaseConnection(db)
|
||||
|
||||
manager = (new EntityManagerConnection("testing")).connect()
|
||||
|
||||
sql = Sql.newInstance(db.getUrl(), db.getProperties().get("user"), db.getProperties().get("password"))
|
||||
|
||||
sql.execute("insert into departments (id, name, location, phone) 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(manager)
|
||||
|
||||
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(manager)
|
||||
|
||||
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(manager)
|
||||
|
||||
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(manager)
|
||||
|
||||
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(manager)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
package space.kklochko.spring_rest_example.db.repositories
|
||||
|
||||
import groovy.sql.Sql
|
||||
import jakarta.persistence.EntityManager
|
||||
import space.kklochko.spring_rest_example.config.factories.LoadDataBaseConfigFromEnvFile
|
||||
import space.kklochko.spring_rest_example.config.models.DataBaseConfig
|
||||
import space.kklochko.spring_rest_example.db.factories.DataBaseConnection
|
||||
import space.kklochko.spring_rest_example.db.factories.EntityManagerConnection
|
||||
import space.kklochko.spring_rest_example.models.Indicator
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
import spock.lang.Stepwise
|
||||
import spock.lang.Subject
|
||||
|
||||
import java.sql.Timestamp
|
||||
|
||||
@Stepwise
|
||||
class IndicatorRepositorySpec extends Specification {
|
||||
@Shared
|
||||
Sql sql
|
||||
|
||||
@Shared
|
||||
EntityManager manager
|
||||
|
||||
@Shared
|
||||
DataBaseConfig db
|
||||
|
||||
@Shared
|
||||
DataBaseConnection dbc
|
||||
|
||||
def setupSpec() {
|
||||
db = (new LoadDataBaseConfigFromEnvFile()).load("db.testing")
|
||||
dbc = new DataBaseConnection(db)
|
||||
|
||||
manager = (new EntityManagerConnection("testing")).connect()
|
||||
|
||||
sql = Sql.newInstance(db.getUrl(), db.getProperties().get("user"), db.getProperties().get("password"))
|
||||
|
||||
sql.execute("insert into indicators (id, type, values, timestamp) 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(manager)
|
||||
|
||||
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(manager)
|
||||
|
||||
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)
|
||||
indicator.setTimestamp(timestamp)
|
||||
|
||||
and: "I have a repo"
|
||||
@Subject
|
||||
IndicatorRepository repo = new IndicatorRepository(manager)
|
||||
|
||||
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 id, type, values, timestamp FROM indicators WHERE id = ?", indicator.id)
|
||||
isExist
|
||||
isExist.get("id") == indicator.id
|
||||
|
||||
where: "I have indicators"
|
||||
indicator << //[new Indicator("Blood", "Fine", null),
|
||||
[new Indicator(UUID.fromString("3b3f62f4-75b4-11ee-99cb-c0e4349366af"), "Blood", "Fine", null)]
|
||||
}
|
||||
|
||||
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(manager)
|
||||
|
||||
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(manager)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
package space.kklochko.spring_rest_example.db.repositories
|
||||
|
||||
import groovy.sql.Sql
|
||||
import jakarta.persistence.EntityManager
|
||||
import org.junit.Ignore
|
||||
import space.kklochko.spring_rest_example.config.factories.LoadDataBaseConfigFromEnvFile
|
||||
import space.kklochko.spring_rest_example.config.models.DataBaseConfig
|
||||
import space.kklochko.spring_rest_example.db.factories.DataBaseConnection
|
||||
import space.kklochko.spring_rest_example.db.factories.EntityManagerConnection
|
||||
import space.kklochko.spring_rest_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
|
||||
EntityManager manager
|
||||
|
||||
@Shared
|
||||
DataBaseConfig db
|
||||
|
||||
@Shared
|
||||
DataBaseConnection dbc
|
||||
|
||||
def setupSpec() {
|
||||
db = (new LoadDataBaseConfigFromEnvFile()).load("db.testing")
|
||||
dbc = new DataBaseConnection(db)
|
||||
|
||||
manager = (new EntityManagerConnection("testing")).connect()
|
||||
|
||||
sql = Sql.newInstance(db.getUrl(), db.getProperties().get("user"), db.getProperties().get("password"))
|
||||
|
||||
sql.execute("insert into patients (id, name, age, phone) 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 patients"() {
|
||||
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(manager)
|
||||
|
||||
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(manager)
|
||||
|
||||
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(manager)
|
||||
|
||||
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(manager)
|
||||
|
||||
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(manager)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user