You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.5 KiB

3 years ago
3 years ago
  1. package wow.doge.http4sdemo
  2. import com.dimafeng.testcontainers.PostgreSQLContainer
  3. import monix.bio.UIO
  4. import wow.doge.http4sdemo.dto.BookSearchMode
  5. import wow.doge.http4sdemo.dto.NewAuthor
  6. import wow.doge.http4sdemo.dto.NewBook
  7. import wow.doge.http4sdemo.implicits._
  8. import wow.doge.http4sdemo.services.LibraryDbio
  9. import wow.doge.http4sdemo.services.LibraryService
  10. import wow.doge.http4sdemo.services.LibraryServiceImpl
  11. class LibraryServiceSpec extends DatabaseIntegrationTestBase {
  12. override def afterContainersStart(containers: Containers): Unit = {
  13. super.afterContainersStart(containers)
  14. createSchema(containers)
  15. }
  16. test("insert and retrieve book") {
  17. withContainersIO { case container: PostgreSQLContainer =>
  18. val io =
  19. withDb(container.jdbcUrl)(db =>
  20. for {
  21. _ <- UIO.unit
  22. service: LibraryService = new LibraryServiceImpl(
  23. profile,
  24. new LibraryDbio(profile),
  25. db
  26. )
  27. id <- service.insertAuthor(NewAuthor("author1"))
  28. book <- service.insertBook(NewBook("blah", "Segehwe", id))
  29. _ <- service
  30. .getBookById(book.bookId)
  31. .assertEquals(Some(book))
  32. } yield ()
  33. )
  34. io
  35. }
  36. }
  37. test("author does not exist error on book insertion") {
  38. withContainersIO { case container: PostgreSQLContainer =>
  39. val io =
  40. withDb(container.jdbcUrl)(db =>
  41. for {
  42. _ <- UIO.unit
  43. service: LibraryService = new LibraryServiceImpl(
  44. profile,
  45. new LibraryDbio(profile),
  46. db
  47. )
  48. _ <- service
  49. .insertBook(NewBook("blah2", "agege", 23))
  50. .attempt
  51. .assertEquals(
  52. Left(
  53. LibraryService
  54. .EntityDoesNotExist("Author with id=23 does not exist")
  55. )
  56. )
  57. } yield ()
  58. )
  59. io
  60. }
  61. }
  62. test("books with isbn already exists error on book insertion") {
  63. withContainersIO { case container: PostgreSQLContainer =>
  64. val io =
  65. withDb(container.jdbcUrl)(db =>
  66. for {
  67. _ <- UIO.unit
  68. service: LibraryService = new LibraryServiceImpl(
  69. profile,
  70. new LibraryDbio(profile),
  71. db
  72. )
  73. _ <- service.insertBook(NewBook("blah2", "agege", 1))
  74. _ <- service
  75. .insertBook(NewBook("blah3", "agege", 1))
  76. .attempt
  77. .assertEquals(
  78. Left(
  79. LibraryService
  80. .EntityAlreadyExists("Book with isbn=agege already exists")
  81. )
  82. )
  83. } yield ()
  84. )
  85. io
  86. }
  87. }
  88. test("search books by author id") {
  89. withContainersIO { case container: PostgreSQLContainer =>
  90. val io =
  91. withDb(container.jdbcUrl)(db =>
  92. for {
  93. _ <- UIO.unit
  94. service: LibraryService = new LibraryServiceImpl(
  95. profile,
  96. new LibraryDbio(profile),
  97. db
  98. )
  99. id <- service.insertAuthor(NewAuthor("bar"))
  100. book1 <- service.insertBook(NewBook("blah3", "aeaega", id))
  101. book2 <- service.insertBook(NewBook("blah4", "afgegg", id))
  102. _ <- service
  103. .searchBook(BookSearchMode.AuthorName, "bar")
  104. .toListL
  105. .toIO
  106. .attempt
  107. .assertEquals(Right(List(book1, book2)))
  108. } yield ()
  109. )
  110. io
  111. }
  112. }
  113. }