From 3c1c31b9069b3f82f909bdb6d7bec0a72fd4abce Mon Sep 17 00:00:00 2001 From: Andrea Brighi Date: Sat, 27 May 2023 02:25:43 +0200 Subject: [PATCH] test(school): create test for school, class, professor and student --- .../schooldomain/PersonTest.kt | 101 ++++++++++++ .../schooldomain/SchoolTest.kt | 150 ++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/PersonTest.kt create mode 100644 schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/SchoolTest.kt diff --git a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/PersonTest.kt b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/PersonTest.kt new file mode 100644 index 00000000..283c7590 --- /dev/null +++ b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/PersonTest.kt @@ -0,0 +1,101 @@ +package com.intelligentbackpack.schooldomain + +import com.intelligentbackpack.schooldomain.entities.Class +import com.intelligentbackpack.schooldomain.entities.School +import com.intelligentbackpack.schooldomain.entities.Subject +import com.intelligentbackpack.schooldomain.entities.person.Professor +import io.kotest.core.spec.style.StringSpec +import com.intelligentbackpack.schooldomain.entities.person.Student +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.matchers.shouldBe + +class PersonTest : StringSpec({ + + val school = School.create("ITI L. Da Vinci", "Rimini") + val studentClass = Class.create("1A", school) + val email = "test@gmail.com" + val name = "John" + val surname = "Doe" + val math: Subject = "Math" + + "should be able to create a Student" { + val student = Student.create(email, name, surname, studentClass) + student.email shouldBe email + student.name shouldBe name + student.surname shouldBe surname + student.studentClass.name shouldBe studentClass.name + } + + "should have a error when creating a Student with blank email" { + val exception = shouldThrow { + Student.create("", name, surname, studentClass) + } + exception.message shouldBe "email cannot be blank" + } + + "should have a error when creating a Student with blank name" { + val exception = shouldThrow { + Student.create(email, "", surname, studentClass) + } + exception.message shouldBe "name cannot be blank" + } + + "should have a error when creating a Student with blank surname" { + val exception = shouldThrow { + Student.create(email, name, "", studentClass) + } + exception.message shouldBe "surname cannot be blank" + } + + "should be able to create a Professor" { + val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf(math))) + professor.email shouldBe email + professor.name shouldBe name + professor.surname shouldBe surname + professor.professorClasses shouldBe setOf(studentClass) + professor.professorSubjects shouldBe setOf(math) + } + + "should have a error when creating a Professor with blank email" { + val exception = shouldThrow { + Professor.create("", name, surname, mapOf(studentClass to setOf(math))) + } + exception.message shouldBe "email cannot be blank" + } + + "should have a error when creating a Professor with blank name" { + val exception = shouldThrow { + Professor.create(email, "", surname, mapOf(studentClass to setOf(math))) + } + exception.message shouldBe "name cannot be blank" + } + + "should have a error when creating a Professor with blank surname" { + val exception = shouldThrow { + Professor.create(email, name, "", mapOf(studentClass to setOf(math))) + } + exception.message shouldBe "surname cannot be blank" + } + + "should have a error when creating a Professor with a class without subjects" { + val exception = shouldThrow { + Professor.create(email, name, surname, mapOf(studentClass to setOf())) + } + exception.message shouldBe "professorClasses cannot be empty" + } + + "should be able to add a subject to a Professor" { + val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf(math))) + val newSubject: Subject = "English" + professor.addProfessorToClass(studentClass, setOf(newSubject)) + professor.professorSubjects shouldBe setOf(math, newSubject) + } + + "should have a error when adding an empty set of subject to a professor in class" { + val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf(math))) + val exception = shouldThrow { + professor.addProfessorToClass(studentClass, setOf()) + } + exception.message shouldBe "subjects cannot be empty" + } +}) diff --git a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/SchoolTest.kt b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/SchoolTest.kt new file mode 100644 index 00000000..3fb04bd8 --- /dev/null +++ b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/SchoolTest.kt @@ -0,0 +1,150 @@ +package com.intelligentbackpack.schooldomain + +import com.intelligentbackpack.schooldomain.entities.School +import com.intelligentbackpack.schooldomain.entities.Class +import com.intelligentbackpack.schooldomain.entities.person.Professor +import com.intelligentbackpack.schooldomain.entities.person.Student +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class SchoolTest : StringSpec({ + + val schoolName = "ITI L. Da Vinci" + val schoolCity = "Rimini" + val class1A = "1A" + val email = "test@gmail.com" + val name = "John" + val surname = "Doe" + + "should be able to create a School" { + val school = School.create(schoolName, schoolCity) + school.name shouldBe schoolName + school.city shouldBe schoolCity + } + + "should have a error when creating a School with blank name" { + val exception = shouldThrow { + School.create("", schoolCity) + } + exception.message shouldBe "name cannot be blank" + } + + "should have a error when creating a School with blank city" { + val exception = shouldThrow { + School.create(schoolName, "") + } + exception.message shouldBe "city cannot be blank" + } + + "should be able to create a Class" { + val school = School.create(schoolName, schoolCity) + val studentClass = Class.create(class1A, school) + studentClass.name shouldBe class1A + studentClass.school shouldBe school + } + + "should have a error when creating a Class with blank name" { + val school = School.create(schoolName, schoolCity) + val exception = shouldThrow { + Class.create("", school) + } + exception.message shouldBe "name cannot be blank" + } + + "should add a student to a class" { + val school = School.create(schoolName, schoolCity) + val studentClass = Class.create(class1A, school) + val student = Student.create(email, name, surname, studentClass) + studentClass.addStudent(student) + student.studentClass shouldBe studentClass + studentClass.students shouldBe setOf(student) + } + + "should have a error when adding a student to a class that is not his" { + val school = School.create(schoolName, schoolCity) + val studentClass = Class.create(class1A, school) + val student = Student.create(email, name, surname, studentClass) + val studentClass2 = Class.create("2A", school) + val exception = shouldThrow { + studentClass2.addStudent(student) + } + exception.message shouldBe "student is not in this class" + } + + "should have a error when adding a student to a class in which he is already in" { + val school = School.create(schoolName, schoolCity) + val studentClass = Class.create(class1A, school) + val student = Student.create(email, name, surname, studentClass) + studentClass.addStudent(student) + val exception = shouldThrow { + studentClass.addStudent(student) + } + exception.message shouldBe "student is already in this class" + } + + "should have a error when adding a student to a class that is not in the same school" { + val school = School.create(schoolName, schoolCity) + val studentClass = Class.create(class1A, school) + val student = Student.create(email, name, surname, studentClass) + val school2 = School.create("Liceo A. Einstein", "Rimini") + val studentClass2 = Class.create(class1A, school2) + val exception = shouldThrow { + studentClass2.addStudent(student) + } + exception.message shouldBe "student is not in this class" + } + + "should add a professor to a class" { + val school = School.create(schoolName, schoolCity) + val studentClass = Class.create(class1A, school) + val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf("Math", "Physics"))) + studentClass.addProfessor(professor, setOf("Math", "Physics")) + studentClass.professors shouldBe setOf(professor) + studentClass.professorTeachSubjects[professor] shouldBe setOf("Math", "Physics") + professor.professorClasses shouldBe setOf(studentClass) + professor.professorSubjectsInClasses shouldBe mapOf(studentClass to setOf("Math", "Physics")) + } + + "should have a error when adding a professor to a class that is not his" { + val school = School.create(schoolName, schoolCity) + val studentClass = Class.create(class1A, school) + val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf("Math", "Physics"))) + val studentClass2 = Class.create("2A", school) + val exception = shouldThrow { + studentClass2.addProfessor(professor, setOf("Math", "Physics")) + } + exception.message shouldBe "professor is not in this class" + } + + "should have a error when adding a professor to a class that is not in the same school" { + val school = School.create(schoolName, schoolCity) + val studentClass = Class.create(class1A, school) + val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf("Math", "Physics"))) + val school2 = School.create("Liceo A. Einstein", "Rimini") + val studentClass2 = Class.create(class1A, school2) + val exception = shouldThrow { + studentClass2.addProfessor(professor, setOf("Math", "Physics")) + } + exception.message shouldBe "professor is not in this class" + } + + "should have a error when adding a professor to a class with a subject that he doesn't teach" { + val school = School.create(schoolName, schoolCity) + val studentClass = Class.create(class1A, school) + val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf("Math"))) + val exception = shouldThrow { + studentClass.addProfessor(professor, setOf("Math", "Physics")) + } + exception.message shouldBe "professor doesn't teach this subjects in this class" + } + + "should update subject when adding a professor to a class in which he is already in" { + val school = School.create(schoolName, schoolCity) + val studentClass = Class.create(class1A, school) + val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf("Math"))) + studentClass.addProfessor(professor, setOf("Math")) + professor.addProfessorToClass(studentClass, setOf("Physics")) + studentClass.addProfessor(professor, setOf("Physics")) + } +})