Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
test(reminder): create test for reminder
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaBrighi committed Jun 6, 2023
1 parent 86825ea commit 8c8f681
Showing 1 changed file with 289 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
package com.intelligentbackpack.reminderdomain

import com.intelligentbackpack.reminderdomain.adapter.EventAdapter
import com.intelligentbackpack.reminderdomain.entitites.Reminder
import com.intelligentbackpack.reminderdomain.entitites.ReminderForLessonDate
import com.intelligentbackpack.reminderdomain.entitites.ReminderForLessonIntervalPeriod
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.LocalTime

class ReminderTest : StringSpec({
val className = "1A"
val subject = "math"
val isbn = "9788843025343"
val reminder = Reminder.create()
val lesson = EventAdapter.WeekLessonImpl(
startTime = LocalTime.of(10, 0),
endTime = LocalTime.of(11, 0),
dayOfWeek = DayOfWeek.MONDAY,
fromData = LocalDate.of(2021, 1, 1),
toDate = LocalDate.of(2021, 1, 31),
className = className,
subject = subject,
)

"should be able to create a reminder for a lesson date" {
val reminderForLesson = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
)

reminderForLesson.isInInterval(LocalDate.of(2021, 1, 1)) shouldBe true
reminderForLesson.isInInterval(LocalDate.of(2021, 1, 2)) shouldBe false
}

"should be able to create a reminder for a lesson week" {
val reminderForLesson = ReminderForLessonIntervalPeriod.create(
isbn = isbn,
lesson = lesson,
startDate = LocalDate.of(2021, 1, 1),
endDate = LocalDate.of(2021, 1, 31),
)
reminderForLesson.isInInterval(LocalDate.of(2021, 1, 1)) shouldBe true
reminderForLesson.isInInterval(LocalDate.of(2021, 1, 31)) shouldBe true
reminderForLesson.isInInterval(LocalDate.of(2021, 2, 1)) shouldBe false
}

"should throw an exception when creating a reminder for a lesson date with a wrong date" {
shouldThrow<IllegalArgumentException> {
ReminderForLessonDate.create(
isbn = isbn,
lesson = EventAdapter.DateLessonImpl(
startTime = LocalTime.of(10, 0),
endTime = LocalTime.of(11, 0),
className = className,
subject = subject,
date = LocalDate.of(2021, 2, 1),
),
date = LocalDate.of(2021, 1, 1),
)
}
}

"should throw an exception when creating a reminder for a lesson week with a wrong date" {
shouldThrow<IllegalArgumentException> {
ReminderForLessonIntervalPeriod.create(
isbn = isbn,
lesson = lesson,
startDate = LocalDate.of(2021, 1, 1),
endDate = LocalDate.of(2021, 2, 1),
)
}
}

"should be able to add a book to a lesson" {
val reminderForLesson = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
)
val newReminder = reminder.addBookForLesson(reminderForLesson)
newReminder.getLessonsForBook(isbn) shouldBe setOf(lesson)
newReminder.getBooksForLesson(lesson) shouldBe setOf(isbn)
}

"should be able to have more than one element" {
val oneElementReminder = Reminder.create(
setOf(
ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
),
),
)
val newReminder = oneElementReminder.addBookForLesson(
ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 2),
),
)
newReminder.getLessonsForBook(isbn) shouldBe setOf(lesson)
newReminder.getBooksForLesson(lesson) shouldBe setOf(isbn)
newReminder.getBooksForLessonInDate(lesson, LocalDate.of(2021, 1, 1)) shouldBe setOf(isbn)
newReminder.getBooksForLessonInDate(lesson, LocalDate.of(2021, 1, 2)) shouldBe setOf(isbn)
newReminder.getBooksForLessonInDate(lesson, LocalDate.of(2021, 1, 3)) shouldBe setOf()
}

"should be able to have more than one book" {
val newIsbn = "9788843025344"
val oneElementReminder = Reminder.create(
setOf(
ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
),
),
)
val newReminder = oneElementReminder.addBookForLesson(
ReminderForLessonDate.create(
isbn = newIsbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
),
)
newReminder.getLessonsForBook(isbn) shouldBe setOf(lesson)
newReminder.getLessonsForBook(newIsbn) shouldBe setOf(lesson)
newReminder.getBooksForLesson(lesson) shouldBe setOf(isbn, newIsbn)
newReminder.getBooksForLessonInDate(lesson, LocalDate.of(2021, 1, 1)) shouldBe setOf(isbn, newIsbn)
newReminder.getBooksForLessonInDate(lesson, LocalDate.of(2021, 1, 2)) shouldBe setOf()
}

"should be able to remove a book from a lesson" {
val reminderForLesson = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
)
val newReminder = reminder.addBookForLesson(reminderForLesson)
newReminder.getLessonsForBook(isbn) shouldBe setOf(lesson)
newReminder.getBooksForLesson(lesson) shouldBe setOf(isbn)
val reminderWithoutBook = newReminder.removeBookForLesson(reminderForLesson)
reminderWithoutBook.getLessonsForBook(isbn) shouldBe setOf()
reminderWithoutBook.getBooksForLesson(lesson) shouldBe setOf()
}

"should have a error when removing a book from a lesson that is not in the reminder of the book" {
val reminderForLesson = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
)
val newReminder = reminder.addBookForLesson(reminderForLesson)
newReminder.getLessonsForBook(isbn) shouldBe setOf(lesson)
newReminder.getBooksForLesson(lesson) shouldBe setOf(isbn)
shouldThrow<IllegalArgumentException> {
newReminder.removeBookForLesson(
ReminderForLessonDate.create(
isbn = "9788843025344",
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
),
)
}
}

"should have a error when removing a book from a lesson that is not in the reminder of the lesson" {
val reminderForLesson = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
)
val newReminder = reminder.addBookForLesson(reminderForLesson)
newReminder.getLessonsForBook(isbn) shouldBe setOf(lesson)
newReminder.getBooksForLesson(lesson) shouldBe setOf(isbn)
shouldThrow<IllegalArgumentException> {
newReminder.removeBookForLesson(
ReminderForLessonDate.create(
isbn = isbn,
lesson = EventAdapter.DateLessonImpl(
startTime = LocalTime.of(10, 0),
endTime = LocalTime.of(11, 0),
className = className,
subject = subject,
date = LocalDate.of(2021, 1, 2),
),
date = LocalDate.of(2021, 1, 1),
),
)
}
}

"should be able to change the book for a lesson" {
val reminderForLesson = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
)
val newReminder = reminder.addBookForLesson(reminderForLesson)
newReminder.getLessonsForBook(isbn) shouldBe setOf(lesson)
newReminder.getBooksForLesson(lesson) shouldBe setOf(isbn)
val reminderForLessonUpdated = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 2),
)
val reminderUpdated =
newReminder.changePeriodOfBookForLesson(reminderForLesson, reminderForLessonUpdated)
reminderUpdated.getLessonsForBook(isbn) shouldBe setOf(lesson)
reminderUpdated.getBooksForLessonInDate(lesson, LocalDate.of(2021, 1, 2)) shouldBe setOf(isbn)
reminderUpdated.getBooksForLessonInDate(lesson, LocalDate.of(2021, 1, 1)) shouldBe setOf()
}

"should have an error if the old reminder is not in the reminder" {
val reminderForLesson = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
)
val newReminder = reminder.addBookForLesson(reminderForLesson)
newReminder.getLessonsForBook(isbn) shouldBe setOf(lesson)
newReminder.getBooksForLesson(lesson) shouldBe setOf(isbn)
val reminderForLessonUpdated = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 2),
)
shouldThrow<IllegalArgumentException> {
newReminder.changePeriodOfBookForLesson(
ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 3),
),
reminderForLessonUpdated,
)
}
}

"should have an error if the new reminder has a different lesson" {
val reminderForLesson = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
)
val newReminder = reminder.addBookForLesson(reminderForLesson)
newReminder.getLessonsForBook(isbn) shouldBe setOf(lesson)
newReminder.getBooksForLesson(lesson) shouldBe setOf(isbn)
val reminderForLessonUpdated = ReminderForLessonDate.create(
isbn = isbn,
lesson = EventAdapter.DateLessonImpl(
startTime = LocalTime.of(10, 0),
endTime = LocalTime.of(11, 0),
className = className,
subject = subject,
date = LocalDate.of(2021, 1, 2),
),
date = LocalDate.of(2021, 1, 2),
)
shouldThrow<IllegalArgumentException> {
newReminder.changePeriodOfBookForLesson(reminderForLesson, reminderForLessonUpdated)
}
}

"should have an error if the new reminder has a different book" {
val reminderForLesson = ReminderForLessonDate.create(
isbn = isbn,
lesson = lesson,
date = LocalDate.of(2021, 1, 1),
)
val newReminder = reminder.addBookForLesson(reminderForLesson)
newReminder.getLessonsForBook(isbn) shouldBe setOf(lesson)
newReminder.getBooksForLesson(lesson) shouldBe setOf(isbn)
val reminderForLessonUpdated = ReminderForLessonDate.create(
isbn = "9788843025344",
lesson = lesson,
date = LocalDate.of(2021, 1, 2),
)
shouldThrow<IllegalArgumentException> {
newReminder.changePeriodOfBookForLesson(reminderForLesson, reminderForLessonUpdated)
}
}
})

0 comments on commit 8c8f681

Please sign in to comment.