From 604c4cf50720ab41d5acf7f1341d9bfcb59f581a Mon Sep 17 00:00:00 2001 From: Andrea Brighi Date: Sun, 18 Jun 2023 19:51:22 +0200 Subject: [PATCH] chore(school): remove circular dependency in classes (schoolDomain) --- .../schooldata/adapter/ClassAdapter.kt | 4 +- .../schooldata/adapter/LessonAdapter.kt | 5 +- .../schooldata/adapter/ProfessorAdapter.kt | 1 - .../schooldata/adapter/StudentAdapter.kt | 4 +- .../schooldomain/entities/Class.kt | 9 +- .../entities/calendar/CalendarEventFactory.kt | 26 +++--- .../entities/calendar/SchoolCalendarImpl.kt | 49 +++++------ .../entities/implementation/ClassImpl.kt | 29 ++---- .../schooldomain/entities/person/Professor.kt | 63 +------------ .../schooldomain/entities/person/Student.kt | 26 +----- .../schooldomain/usecase/SchoolUseCase.kt | 16 ++-- .../schooldomain/CalendarTest.kt | 26 +----- .../schooldomain/CancelEventTest.kt | 9 +- .../schooldomain/NewEventTest.kt | 9 +- .../schooldomain/PersonTest.kt | 47 ++-------- .../schooldomain/RescheduleEventTest.kt | 9 +- .../schooldomain/SchoolTest.kt | 88 +++---------------- .../schooldomain/UseCaseTest.kt | 6 +- 18 files changed, 84 insertions(+), 342 deletions(-) diff --git a/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/ClassAdapter.kt b/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/ClassAdapter.kt index c8adaa6d..b9ebc301 100644 --- a/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/ClassAdapter.kt +++ b/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/ClassAdapter.kt @@ -1,15 +1,13 @@ package com.intelligentbackpack.schooldata.adapter -import com.intelligentbackpack.schooldomain.entities.School import com.intelligentbackpack.schooldata.db.entities.SchoolClass as DBClass import com.intelligentbackpack.schooldomain.entities.Class as DomainClass object ClassAdapter { - fun DBClass.fromDBToDomain(school: School): DomainClass { + fun DBClass.fromDBToDomain(): DomainClass { return DomainClass.create( name = name, - school = school, ) } } diff --git a/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/LessonAdapter.kt b/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/LessonAdapter.kt index c361aaca..2ae14aee 100644 --- a/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/LessonAdapter.kt +++ b/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/LessonAdapter.kt @@ -26,9 +26,10 @@ object LessonAdapter { ) } - fun DBLesson.fromDBToDomain(professor: Professor, schoolClass: Class): WeekLesson { + fun DBLesson.fromDBToDomain(professor: Professor, schoolClass: Class, subject: String): WeekLesson { return createWeekLesson( - subject = module, + subject = subject, + module = module, professor = professor, studentsClass = schoolClass, startTime = startTime, diff --git a/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/ProfessorAdapter.kt b/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/ProfessorAdapter.kt index ba399038..65248e35 100644 --- a/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/ProfessorAdapter.kt +++ b/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/ProfessorAdapter.kt @@ -19,7 +19,6 @@ object ProfessorAdapter { email = email, name = name, surname = surname, - professorClasses = emptyMap(), ) } } diff --git a/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/StudentAdapter.kt b/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/StudentAdapter.kt index daed7831..4ca01a50 100644 --- a/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/StudentAdapter.kt +++ b/schoolData/src/main/kotlin/com/intelligentbackpack/schooldata/adapter/StudentAdapter.kt @@ -1,17 +1,15 @@ package com.intelligentbackpack.schooldata.adapter import com.intelligentbackpack.accessdomain.entities.User -import com.intelligentbackpack.schooldomain.entities.Class import com.intelligentbackpack.schooldomain.entities.person.Student object StudentAdapter { - fun User.fromAccessToSchool(schoolClass: Class): Student { + fun User.fromAccessToSchool(): Student { return Student.create( email = email, name = name, surname = surname, - studentClass = schoolClass, ) } } diff --git a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/Class.kt b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/Class.kt index 27a8d859..689611cd 100644 --- a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/Class.kt +++ b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/Class.kt @@ -8,7 +8,6 @@ import com.intelligentbackpack.schooldomain.entities.person.Student * A class is a group of students that attend the same subjects taught by the same professors in the same school with the same schedule. * * @property name the name of the class - * @property school the school where the class is taught * @property students the students that attend the class * @property subjects the subjects taught in the class * @property professors the professors that teach in the class @@ -16,7 +15,6 @@ import com.intelligentbackpack.schooldomain.entities.person.Student */ interface Class { val name: String - val school: School val students: Set val subjects: Set val professors: Set @@ -35,7 +33,7 @@ interface Class { * @param professor the professor to add * @param subjects the subjects taught by the professor in the class */ - fun addProfessor(professor: Professor, subjects: Set): Pair + fun addProfessor(professor: Professor, subjects: Set): Class companion object { @@ -43,15 +41,14 @@ interface Class { * Creates a class. * * @param name the name of the class - * @param school the school where the class is taught * @return the created class * @throws IllegalArgumentException if the name is blank */ - fun create(name: String, school: School): Class { + fun create(name: String): Class { if (name.isBlank()) { throw IllegalArgumentException("name cannot be blank") } else { - return ClassImpl(name, school) + return ClassImpl(name) } } } diff --git a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/calendar/CalendarEventFactory.kt b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/calendar/CalendarEventFactory.kt index 25019095..002bbfc3 100644 --- a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/calendar/CalendarEventFactory.kt +++ b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/calendar/CalendarEventFactory.kt @@ -115,21 +115,17 @@ object CalendarEventFactory { } else if (fromDate.isAfter(toDate)) { throw IllegalArgumentException("fromDate cannot be after toDate") } else { - if (!professor.professorSubjects.contains(subject)) { - throw IllegalArgumentException("professor must teach subject") - } else { - return WeekLessonImpl( - subject, - module, - professor, - studentsClass, - startTime, - endTime, - day, - fromDate, - toDate, - ) - } + return WeekLessonImpl( + subject, + module, + professor, + studentsClass, + startTime, + endTime, + day, + fromDate, + toDate, + ) } } diff --git a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/calendar/SchoolCalendarImpl.kt b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/calendar/SchoolCalendarImpl.kt index a7f47ded..ea792515 100644 --- a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/calendar/SchoolCalendarImpl.kt +++ b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/calendar/SchoolCalendarImpl.kt @@ -63,39 +63,30 @@ internal data class SchoolCalendarImpl( } override fun addLessons(lessons: Set): SchoolCalendar { - if (lessons.any { lesson -> - lesson.professor.professorClasses.any { - it.school.calendar?.schoolYear != schoolYear - } - } - ) { - throw IllegalArgumentException("lessons must be in the same school year") + if (lessons.any { lesson -> this.lessons.any { it == lesson } }) { + throw IllegalArgumentException("lessons must not be in current lessons") } else { - if (lessons.any { lesson -> this.lessons.any { it == lesson } }) { - throw IllegalArgumentException("lessons must not be in current lessons") - } else { - val checkOverlappingStudents = lessons.any { lesson -> - areLessonsInTimeTableOverlapping( - lessons, - studentsTimeTableLesson[lesson.studentsClass] ?: emptyMap(), - ) { - it.studentsClass == lesson.studentsClass - } - } - val checkOverlappingProfessors = lessons.any { lesson -> - areLessonsInTimeTableOverlapping( - lessons, - professorsTimeTableLesson[lesson.professor] ?: emptyMap(), - ) { - it.professor == lesson.professor - } + val checkOverlappingStudents = lessons.any { lesson -> + areLessonsInTimeTableOverlapping( + lessons, + studentsTimeTableLesson[lesson.studentsClass] ?: emptyMap(), + ) { + it.studentsClass == lesson.studentsClass } - if (checkOverlappingStudents || checkOverlappingProfessors) { - throw EventOverlappingException() - } else { - return copy(lessons = this.lessons + lessons) + } + val checkOverlappingProfessors = lessons.any { lesson -> + areLessonsInTimeTableOverlapping( + lessons, + professorsTimeTableLesson[lesson.professor] ?: emptyMap(), + ) { + it.professor == lesson.professor } } + if (checkOverlappingStudents || checkOverlappingProfessors) { + throw EventOverlappingException() + } else { + return copy(lessons = this.lessons + lessons) + } } } diff --git a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/implementation/ClassImpl.kt b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/implementation/ClassImpl.kt index 66ffd37d..51175d98 100644 --- a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/implementation/ClassImpl.kt +++ b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/implementation/ClassImpl.kt @@ -1,7 +1,6 @@ package com.intelligentbackpack.schooldomain.entities.implementation 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 com.intelligentbackpack.schooldomain.entities.person.Student @@ -10,7 +9,6 @@ import com.intelligentbackpack.schooldomain.entities.person.Student * A class is a group of students that attend the same subjects taught by the same professors in the same school with the same schedule. * * @property name the name of the class - * @property school the school where the class is taught * @property students the students that attend the class * @property subjects the subjects taught in the class * @property professors the professors that teach in the class @@ -18,7 +16,6 @@ import com.intelligentbackpack.schooldomain.entities.person.Student */ internal data class ClassImpl( override val name: String, - override val school: School, ) : Class { override var students: Set = setOf() private set @@ -39,12 +36,8 @@ internal data class ClassImpl( if (students.contains(student)) { throw IllegalArgumentException("student is already in this class") } else { - if (student.studentClass != this) { - throw IllegalArgumentException("student is not in this class") - } else { - return copy().apply { - students = students + student - } + return copy().apply { + students = students + student } } } @@ -57,32 +50,22 @@ internal data class ClassImpl( * @param subjects the subjects taught by the professor in the class * @throws IllegalArgumentException if the professor is not in the class or if the professor doesn't teach the subjects in the class */ - override fun addProfessor(professor: Professor, subjects: Set): Pair { + override fun addProfessor(professor: Professor, subjects: Set): Class { if (subjects.isEmpty()) { throw IllegalArgumentException("subjects cannot be empty") - } else if (professor.professorClasses.any { it.school != school }) { - throw IllegalArgumentException("professor doesn't teach the subjects in this school") } else { - val newProfessor = - if (!professor.professorClasses.contains(this)) { - professor.addProfessorToClass(this, subjects) - } else if (professor.professorSubjectsInClasses[this]?.containsAll(subjects) == false) { - professor.addProfessorToClass(this, subjects) - } else { - professor - } val newClass = if (professors.contains(professor)) { val oldSubjects = professorTeachSubjects[professor]!! copy().apply { - professorTeachSubjects = professorTeachSubjects + (newProfessor to oldSubjects + subjects) + professorTeachSubjects = professorTeachSubjects + (professor to oldSubjects + subjects) } } else { copy().apply { professors = professors + professor - professorTeachSubjects = professorTeachSubjects + (newProfessor to subjects) + professorTeachSubjects = professorTeachSubjects + (professor to subjects) } } - return Pair(newClass, newProfessor) + return newClass } } } diff --git a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Professor.kt b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Professor.kt index f588541d..3f264c89 100644 --- a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Professor.kt +++ b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Professor.kt @@ -1,36 +1,13 @@ package com.intelligentbackpack.schooldomain.entities.person -import com.intelligentbackpack.schooldomain.entities.Class -import com.intelligentbackpack.schooldomain.entities.Subject - /** * A professor. * * @property name the name of the professor * @property surname the surname of the professor * @property email the email of the professor - * @property professorClasses the classes the professor teaches - * @property professorSubjects the subjects the professor teaches - * @property professorSubjectsInClasses the subjects the professor teaches in each class */ interface Professor : Person { - val subjects: Set - val professorSubjectsInClasses: Map> - val professorClasses: Set - get() = professorSubjectsInClasses.keys - - val professorSubjects: Set - get() = professorSubjectsInClasses.values.flatten().toSet() - - /** - * Adds the professor to a class. - * If the professor is already in the class, the subjects are added to the subjects the professor teaches in the class. - * - * @param professorClass the class to add the professor to - * @param subjects the subjects the professor teaches in the class - * @throws IllegalArgumentException if the subjects are empty - */ - fun addProfessorToClass(professorClass: Class, subjects: Set): Professor companion object { @@ -38,32 +15,7 @@ interface Professor : Person { override val email: String, override val name: String, override val surname: String, - ) : Professor { - override val subjects: Set - get() = professorSubjectsInClasses.values.flatten().toSet() - override var professorSubjectsInClasses: Map> = mapOf() - private set - - override fun addProfessorToClass(professorClass: Class, subjects: Set): Professor { - if (subjects.isEmpty()) { - throw IllegalArgumentException("subjects cannot be empty") - } else { - return if (professorSubjectsInClasses.containsKey(professorClass)) { - val oldSubjects = professorSubjectsInClasses[professorClass]!! - copy().apply { - professorSubjectsInClasses = - this@ProfessorImpl.professorSubjectsInClasses + - (professorClass to (oldSubjects + subjects)) - } - } else { - copy().apply { - professorSubjectsInClasses = - this@ProfessorImpl.professorSubjectsInClasses + (professorClass to subjects) - } - } - } - } - } + ) : Professor /** * Creates a professor. @@ -71,18 +23,15 @@ interface Professor : Person { * @param email the email of the professor * @param name the name of the professor * @param surname the surname of the professor - * @param professorClasses the classes the professor teaches * @return the created professor * @throws IllegalArgumentException if the email is blank * @throws IllegalArgumentException if the name is blank * @throws IllegalArgumentException if the surname is blank - * @throws IllegalArgumentException if the professorClasses are empty */ fun create( email: String, name: String, surname: String, - professorClasses: Map>, ): Professor { if (email.isBlank()) { throw IllegalArgumentException("email cannot be blank") @@ -91,15 +40,7 @@ interface Professor : Person { } else if (surname.isBlank()) { throw IllegalArgumentException("surname cannot be blank") } else { - if (professorClasses.any { it.value.isEmpty() }) { - throw IllegalArgumentException("professorClasses cannot be empty") - } else { - return ProfessorImpl(email, name, surname).let { - professorClasses.entries.fold(it as Professor) { professor, (professorClass, subjects) -> - professor.addProfessorToClass(professorClass, subjects) - } - } - } + return ProfessorImpl(email, name, surname) } } } diff --git a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Student.kt b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Student.kt index 734d9d4f..c81f059e 100644 --- a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Student.kt +++ b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Student.kt @@ -1,36 +1,20 @@ package com.intelligentbackpack.schooldomain.entities.person -import com.intelligentbackpack.schooldomain.entities.Class - /** * A student. * * @property email the email of the student * @property name the name of the student * @property surname the surname of the student - * @property studentClass the class the student attends */ interface Student : Person { - val studentClass: Class? - - fun changeClass(newClass: Class): Student companion object { private data class StudentImpl( override val email: String, override val name: String, override val surname: String, - ) : Student { - - override var studentClass: Class? = null - private set - - override fun changeClass(newClass: Class): Student { - return copy().apply { - studentClass = newClass - } - } - } + ) : Student /** * Creates a student. @@ -38,7 +22,6 @@ interface Student : Person { * @param email the email of the student * @param name the name of the student * @param surname the surname of the student - * @param studentClass the class the student attends * @throws IllegalArgumentException if the email or name or surname is blank * */ @@ -46,7 +29,6 @@ interface Student : Person { email: String, name: String, surname: String, - studentClass: Class? = null, ): Student { if (email.isBlank()) { throw IllegalArgumentException("email cannot be blank") @@ -55,11 +37,7 @@ interface Student : Person { } else if (surname.isBlank()) { throw IllegalArgumentException("surname cannot be blank") } else { - return StudentImpl(email, name, surname).let { student -> - studentClass?.let { - student.changeClass(it) - } ?: student - } + return StudentImpl(email, name, surname) } } } diff --git a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/usecase/SchoolUseCase.kt b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/usecase/SchoolUseCase.kt index d2a8823a..d2fdd6f5 100644 --- a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/usecase/SchoolUseCase.kt +++ b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/usecase/SchoolUseCase.kt @@ -55,7 +55,8 @@ class SchoolUseCase(private val accessUseCase: AccessUseCase, private val reposi if (user.role == Role.STUDENT || user.role == Role.PROFESSOR) { repository.getSchool(user).let { school -> if (user.role == Role.STUDENT) { - school.students.find { it.email == user.email }?.studentClass?.let { + val student = school.students.find { it.email == user.email } + school.classes.find { it.students.contains(student) }?.let { school.calendar?.getStudentsEvents( it, date, @@ -75,14 +76,6 @@ class SchoolUseCase(private val accessUseCase: AccessUseCase, private val reposi } } - /** - * Gets the calendar events of the user for today. - * - * @return the result of the operation with the calendar events. - */ - suspend fun getUserTodayCalendarEvents() = - getUserCalendarEventsForDate(LocalDate.now()) - /** * Adds an alteration event to the school calendar. * @@ -115,9 +108,10 @@ class SchoolUseCase(private val accessUseCase: AccessUseCase, private val reposi if (user.role == Role.STUDENT || user.role == Role.PROFESSOR) { repository.getSchool(user).let { school -> if (user.role == Role.STUDENT) { - school.students.find { it.email == user.email }?.studentClass?.let { studentClass -> + val student = school.students.find { it.email == user.email } + school.classes.find { it.students.contains(student) }?.let { studentClass -> school.calendar?.getAllStudentEvents(studentClass) ?: emptySet() - } + } ?: emptySet() } else { school.professors.find { it.email == user.email }?.let { professor -> school.calendar?.getAllProfessorEvents(professor) ?: emptySet() diff --git a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/CalendarTest.kt b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/CalendarTest.kt index 5cd986bc..f4551ce0 100644 --- a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/CalendarTest.kt +++ b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/CalendarTest.kt @@ -1,7 +1,6 @@ package com.intelligentbackpack.schooldomain import com.intelligentbackpack.schooldomain.entities.Class -import com.intelligentbackpack.schooldomain.entities.School import com.intelligentbackpack.schooldomain.entities.calendar.CalendarEventFactory.createWeekLesson import com.intelligentbackpack.schooldomain.entities.calendar.SchoolCalendar import com.intelligentbackpack.schooldomain.entities.person.Professor @@ -16,8 +15,6 @@ import java.time.LocalTime class CalendarTest : StringSpec({ val schoolYear = "2022-2023" - val schoolName = "ITI L. Da Vinci" - val schoolCity = "Rimini" val class1A = "1A" val email = "test@gmail.com" val name = "John" @@ -25,9 +22,8 @@ class CalendarTest : StringSpec({ val math = "Math" val physics = "Physics" val calendar = SchoolCalendar.create(schoolYear) - val school = School.create(schoolName, schoolCity).replaceCalendar(calendar) - val studentClass = Class.create(class1A, school) - val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf(math, physics))) + val studentClass = Class.create(class1A) + val professor = Professor.create(email, name, surname) val singleMondayLesson = createWeekLesson( day = DayOfWeek.MONDAY, subject = math, @@ -95,22 +91,6 @@ class CalendarTest : StringSpec({ newCalendar.professorsTimeTableLesson shouldBe mapOf(professor to mapOf(DayOfWeek.MONDAY to lessons.toList())) } - "should have an error if a professor doesn't teach a subject" { - val exception = shouldThrow { - createWeekLesson( - day = DayOfWeek.MONDAY, - subject = "Italian", - startTime = LocalTime.of(8, 30), - endTime = LocalTime.of(9, 30), - professor = professor, - fromDate = LocalDate.of(2022, 9, 12), - toDate = LocalDate.of(2022, 12, 23), - studentsClass = studentClass, - ) - } - exception.message shouldBe "professor must teach subject" - } - "should have an error if fromDate is after toDate" { val exception = shouldThrow { createWeekLesson( @@ -128,7 +108,7 @@ class CalendarTest : StringSpec({ } "should have an error if a professor teaches more than one subject at the same time" { - val studentClass2 = Class.create("2A", school) + val studentClass2 = Class.create("2A") shouldThrow { val lessons = setOf( singleMondayLesson, diff --git a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/CancelEventTest.kt b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/CancelEventTest.kt index 082e2783..45233482 100644 --- a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/CancelEventTest.kt +++ b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/CancelEventTest.kt @@ -1,7 +1,6 @@ package com.intelligentbackpack.schooldomain import com.intelligentbackpack.schooldomain.entities.Class -import com.intelligentbackpack.schooldomain.entities.School import com.intelligentbackpack.schooldomain.entities.calendar.CalendarEventFactory import com.intelligentbackpack.schooldomain.entities.calendar.SchoolCalendar import com.intelligentbackpack.schooldomain.entities.calendar.alteration.AlterationFactory @@ -19,8 +18,6 @@ import java.time.LocalTime class CancelEventTest : StringSpec({ val schoolYear = "2022-2023" - val schoolName = "ITI L. Da Vinci" - val schoolCity = "Rimini" val class1A = "1A" val email = "test@gmail.com" val name = "John" @@ -28,10 +25,8 @@ class CancelEventTest : StringSpec({ val math = "Math" val physics = "Physics" val calendar = SchoolCalendar.create(schoolYear) - val school = School.create(schoolName, schoolCity) - .replaceCalendar(calendar) - val studentClass = Class.create(class1A, school) - val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf(math, physics))) + val studentClass = Class.create(class1A) + val professor = Professor.create(email, name, surname) val mondayLesson1 = CalendarEventFactory.createWeekLesson( day = DayOfWeek.MONDAY, subject = math, diff --git a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/NewEventTest.kt b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/NewEventTest.kt index 61d31598..3adf99ff 100644 --- a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/NewEventTest.kt +++ b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/NewEventTest.kt @@ -1,7 +1,6 @@ package com.intelligentbackpack.schooldomain import com.intelligentbackpack.schooldomain.entities.Class -import com.intelligentbackpack.schooldomain.entities.School import com.intelligentbackpack.schooldomain.entities.calendar.CalendarEventFactory import com.intelligentbackpack.schooldomain.entities.calendar.SchoolCalendar import com.intelligentbackpack.schooldomain.entities.calendar.alteration.AlterationFactory @@ -18,8 +17,6 @@ import java.time.LocalTime class NewEventTest : StringSpec({ val schoolYear = "2022-2023" - val schoolName = "ITI L. Da Vinci" - val schoolCity = "Rimini" val class1A = "1A" val email = "test@gmail.com" val name = "John" @@ -27,10 +24,8 @@ class NewEventTest : StringSpec({ val math = "Math" val physics = "Physics" val calendar = SchoolCalendar.create(schoolYear) - val school = School.create(schoolName, schoolCity) - .replaceCalendar(calendar) - val studentClass = Class.create(class1A, school) - val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf(math, physics))) + val studentClass = Class.create(class1A) + val professor = Professor.create(email, name, surname) val mondayLesson1 = CalendarEventFactory.createWeekLesson( day = DayOfWeek.MONDAY, subject = math, diff --git a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/PersonTest.kt b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/PersonTest.kt index faf2892b..f858026c 100644 --- a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/PersonTest.kt +++ b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/PersonTest.kt @@ -1,8 +1,5 @@ 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 com.intelligentbackpack.schooldomain.entities.person.Student import io.kotest.assertions.throwables.shouldThrow @@ -11,91 +8,63 @@ 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) + val student = Student.create(email, name, surname) 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) + Student.create("", name, surname) } 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) + Student.create(email, "", surname) } 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) + Student.create(email, name, "") } 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))) + val professor = Professor.create(email, name, surname) 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))) + Professor.create("", name, surname) } 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))) + Professor.create(email, "", surname) } 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))) + Professor.create(email, name, "") } 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" - val newProfessor = professor.addProfessorToClass(studentClass, setOf(newSubject)) - newProfessor.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/RescheduleEventTest.kt b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/RescheduleEventTest.kt index 6f919d78..5b37366f 100644 --- a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/RescheduleEventTest.kt +++ b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/RescheduleEventTest.kt @@ -1,7 +1,6 @@ package com.intelligentbackpack.schooldomain import com.intelligentbackpack.schooldomain.entities.Class -import com.intelligentbackpack.schooldomain.entities.School import com.intelligentbackpack.schooldomain.entities.calendar.CalendarEventFactory import com.intelligentbackpack.schooldomain.entities.calendar.SchoolCalendar import com.intelligentbackpack.schooldomain.entities.calendar.alteration.AlterationFactory @@ -19,8 +18,6 @@ import java.time.LocalTime class RescheduleEventTest : StringSpec({ val schoolYear = "2022-2023" - val schoolName = "ITI L. Da Vinci" - val schoolCity = "Rimini" val class1A = "1A" val email = "test@gmail.com" val name = "John" @@ -28,10 +25,8 @@ class RescheduleEventTest : StringSpec({ val math = "Math" val physics = "Physics" val calendar = SchoolCalendar.create(schoolYear) - val school = School.create(schoolName, schoolCity) - .replaceCalendar(calendar) - val studentClass = Class.create(class1A, school) - val professor = Professor.create(email, name, surname, mapOf(studentClass to setOf(math, physics))) + val studentClass = Class.create(class1A) + val professor = Professor.create(email, name, surname) val mondayLesson1 = CalendarEventFactory.createWeekLesson( day = DayOfWeek.MONDAY, subject = math, diff --git a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/SchoolTest.kt b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/SchoolTest.kt index a377d04e..7a199374 100644 --- a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/SchoolTest.kt +++ b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/SchoolTest.kt @@ -38,44 +38,27 @@ class SchoolTest : StringSpec({ } "should be able to create a Class" { - val school = School.create(schoolName, schoolCity) - val studentClass = Class.create(class1A, school) + val studentClass = Class.create(class1A) 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) + Class.create("") } 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) + val studentClass = Class.create(class1A) + val student = Student.create(email, name, surname) val newStudentClass = studentClass.addStudent(student) - student.studentClass shouldBe studentClass newStudentClass.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) + val studentClass = Class.create(class1A) + val student = Student.create(email, name, surname) val newStudentClass = studentClass.addStudent(student) val exception = shouldThrow { newStudentClass.addStudent(student) @@ -83,69 +66,20 @@ class SchoolTest : StringSpec({ 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"))) - val result = studentClass.addProfessor(professor, setOf("Math", "Physics")) - val newStudentClass = result.first - val newProfessor = result.second + val studentClass = Class.create(class1A) + val professor = Professor.create(email, name, surname) + val newStudentClass = studentClass.addProfessor(professor, setOf("Math", "Physics")) newStudentClass.professors shouldBe setOf(professor) newStudentClass.professorTeachSubjects[professor] shouldBe setOf("Math", "Physics") - newProfessor.professorClasses shouldBe setOf(newStudentClass) - newProfessor.professorSubjectsInClasses shouldBe mapOf(newStudentClass to setOf("Math", "Physics")) - } - - "should be able to add a professor to a class even when the class isn't 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 result = studentClass2.addProfessor(professor, setOf("Math", "Physics")) - val newProfessor = result.second - newProfessor.professorClasses shouldBe setOf(studentClass, studentClass2) - } - - "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 doesn't teach the subjects in this school" } "should have a error when adding a professor to a class with no subjects" { - 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 studentClass = Class.create(class1A) + val professor = Professor.create(email, name, surname) val exception = shouldThrow { studentClass.addProfessor(professor, setOf()) } exception.message shouldBe "subjects cannot be empty" } - - "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")) - } }) diff --git a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/UseCaseTest.kt b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/UseCaseTest.kt index 45f9156b..e215b0bc 100644 --- a/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/UseCaseTest.kt +++ b/schoolDomain/src/test/kotlin/com/intelligentbackpack/schooldomain/UseCaseTest.kt @@ -249,16 +249,15 @@ class UseCaseTest : StringSpec({ private val calendar = SchoolCalendar.create(schoolYear) private val school = School.create("School", "City") .replaceCalendar(calendar) - val studentClass = Class.create("1A", school) + val studentClass = Class.create("1A") val date: LocalDate = LocalDate.of(2022, 9, 12) const val math = "Math" - const val physics = "Physics" + private const val physics = "Physics" val professor = Professor.create( email = professorUser.email, name = professorUser.name, surname = professorUser.surname, - professorClasses = mapOf(studentClass to setOf(math, physics)), ) private val singleMondayLesson = CalendarEventFactory.createWeekLesson( day = DayOfWeek.MONDAY, @@ -302,7 +301,6 @@ class UseCaseTest : StringSpec({ email = studentUser.email, name = studentUser.name, surname = studentUser.surname, - studentClass = studentClass, ) val lessons = mondayLessons + tuesday return school