From 7f9af282a3261283f58be61a558650e8bc9c9586 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 29 Oct 2023 12:39:50 +0200 Subject: [PATCH] Add tests for CommandEntity. --- .../cli/commands/CommandEntitySpec.groovy | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 src/test/groovy/space/kklochko/jdbc_hospital_example/cli/commands/CommandEntitySpec.groovy diff --git a/src/test/groovy/space/kklochko/jdbc_hospital_example/cli/commands/CommandEntitySpec.groovy b/src/test/groovy/space/kklochko/jdbc_hospital_example/cli/commands/CommandEntitySpec.groovy new file mode 100644 index 0000000..1d5de23 --- /dev/null +++ b/src/test/groovy/space/kklochko/jdbc_hospital_example/cli/commands/CommandEntitySpec.groovy @@ -0,0 +1,162 @@ +package space.kklochko.jdbc_hospital_example.cli.commands + +import groovy.sql.Sql +import space.kklochko.jdbc_hospital_example.cli.commands.factories.CommandFactory +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.db.repositories.DepartmentRepository +import space.kklochko.jdbc_hospital_example.db.repositories.IndicatorRepository +import space.kklochko.jdbc_hospital_example.db.repositories.PatientRepository +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 + +@Stepwise +class CommandEntitySpec extends Specification { + @Shared + Sql sql + + @Shared + DataBaseConfig db + + @Shared + DataBaseConnection dbc + + def setupSpec() { + db = (new LoadDataBaseConfigFromEnvFile()).load("db.testing") + dbc = new DataBaseConnection(db) + + IndicatorRepository repo = new IndicatorRepository(dbc) + repo.createTable() + + sql = Sql.newInstance(db.getUrl(), db.getProperties().get("user"), db.getProperties().get("password")) + } + + def "Test insert commands"() { + given: "I have a command data" + CommandData data = new CommandData("insert", datatype, values) + UUID id = UUID.fromString(data.arguments.get("id")) + + and: "I have factory for the command" + CommandFactory factory = new CommandFactory() + @Subject + CommandEntity command = factory.create(data, repository) + + when: "The command is executed" + def result = command.run() + + then: "checking that the insert was successful" + result + id + + and: "checking that the entry is added" + def isExist = sql.firstRow(verifyStatement, id) + isExist + + cleanup: "remove the data" + sql.execute("delete from " + datatype + "s") + + where: + datatype | values | repository || verifyStatement + "indicator" | ["id": "3b3f62f4-75b4-11ee-99cb-c0e4349366ab", + "type": "blood, blood", + "values": "Good samples.", + "timestamp": "2023-10-17 14:30:00"] | new IndicatorRepository(dbc) || "SELECT type FROM indicators WHERE id = ?" + "patient" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366bb", + "name": "Oleh", + "age": "21", + "phone": "380123451234"] | new PatientRepository(dbc) || "SELECT name FROM patients WHERE id = ?" + "department" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366cb", + "name": "First department", + "location": "Stepan Bandera Street", + "phone": "380123451244"] | new DepartmentRepository(dbc) || "SELECT name FROM departments WHERE id = ?" + } + + def "Test update commands"() { + given: "I have a command data" + CommandData data = new CommandData("update", datatype, values) + UUID id = UUID.fromString(data.arguments.get("id")) + + and: "I have a fixture" + sql.execute(fixture) + + and: "I have factory for the command" + CommandFactory factory = new CommandFactory() + @Subject + CommandEntity command = factory.create(data, repository) + + when: "The command is executed" + def result = command.run() + + then: "checking that the insert was successful" + result + id + + and: "checking that the entry is added" + def isExist = sql.firstRow(verifyStatement, id) + isExist + + cleanup: "remove the data" + sql.execute("delete from " + datatype + "s") + + where: + datatype | values | fixture | repository || verifyStatement + "indicator" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366ab", + "type": "blood, blood", + "values": "Good samples.", + "timestamp": "2023-10-17 14:30:00"] | "insert into indicators values ('bcbbcdb4-702c-11ee-9113-c0e4349366ab', 'Samples', 'fine', '2023-10-17 16:30:00')" + | new IndicatorRepository(dbc) || "SELECT type FROM indicators WHERE id = ? AND type='blood, blood'" + "patient" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366bb", + "name": "Oleh", + "age": "21", + "phone": "380123451234"] | "insert into patients values ('bcbbcdb4-702c-11ee-9113-c0e4349366bb', 'Maybe Oleh', 22, '380123451234')" + | new PatientRepository(dbc) || "SELECT name FROM patients WHERE id = ? AND name='Oleh'" + "department" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366cb", + "name": "First department", + "location": "Stepan Bandera Street", + "phone": "380123451244"] | "insert into departments values ('bcbbcdb4-702c-11ee-9113-c0e4349366cb', 'Second department', 'Stepan Bandera Street', '380123451244')" + | new DepartmentRepository(dbc) || "SELECT name FROM departments WHERE id = ? AND name='First department'" + } + + def "Test delete commands"() { + given: "I have a command data" + CommandData data = new CommandData("remove", datatype, values) + UUID id = UUID.fromString(data.arguments.get("id")) + + and: "I have a fixture" + sql.execute(fixture) + + and: "I have factory for the command" + CommandFactory factory = new CommandFactory() + @Subject + CommandEntity command = factory.create(data, repository) + + when: "The command is executed" + def result = command.run() + + then: "checking that the insert was successful" + result + id + + and: "checking that the entry is added" + def isExist = sql.firstRow(verifyStatement, id) + !isExist + + cleanup: "remove the data" + sql.execute("delete from " + datatype + "s") + + where: + datatype | values | fixture | repository || verifyStatement + "indicator" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366ab"] | "insert into indicators values ('bcbbcdb4-702c-11ee-9113-c0e4349366ab', 'Samples', 'fine', '2023-10-17 16:30:00')" + | new IndicatorRepository(dbc) || "SELECT type FROM indicators WHERE id = ?" + "patient" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366bb"] | "insert into patients values ('bcbbcdb4-702c-11ee-9113-c0e4349366bb', 'Maybe Oleh', 22, '380123451234')" + | new PatientRepository(dbc) || "SELECT name FROM patients WHERE id = ?" + "department" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366cb"] | "insert into departments values ('bcbbcdb4-702c-11ee-9113-c0e4349366cb', 'Second department', 'Stepan Bandera Street', '380123451244')" + | new DepartmentRepository(dbc) || "SELECT name FROM departments WHERE id = ?" + } +}