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.

123 lines
3.6 KiB

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