From c25f2e08102d6af3611724920cf09e96026a0b9c Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Wed, 5 May 2021 22:53:19 +0530 Subject: [PATCH] make logback stfu + minor edits --- .../main/scala/wow/doge/MonixBioSuite.scala | 20 +++----- src/it/resources/logback-test.xml | 10 ++++ .../doge/http4sdemo/LibraryServiceSpec.scala | 4 +- .../wow/doge/http4sdemo/dto/Library.scala | 12 ----- .../http4sdemo/services/LibraryService.scala | 49 +++++++++++-------- src/test/resources/logback-test.xml | 10 ++++ .../http4sdemo/LibraryControllerSpec.scala | 13 ----- .../doge/http4sdemo/LoggerFixtureSpec.scala | 12 ----- 8 files changed, 56 insertions(+), 74 deletions(-) create mode 100644 src/it/resources/logback-test.xml create mode 100644 src/test/resources/logback-test.xml delete mode 100644 src/test/scala/wow/doge/http4sdemo/LoggerFixtureSpec.scala diff --git a/modules/test-common/src/main/scala/wow/doge/MonixBioSuite.scala b/modules/test-common/src/main/scala/wow/doge/MonixBioSuite.scala index eacb7b4..6fd792b 100644 --- a/modules/test-common/src/main/scala/wow/doge/MonixBioSuite.scala +++ b/modules/test-common/src/main/scala/wow/doge/MonixBioSuite.scala @@ -4,12 +4,12 @@ import scala.concurrent.Future import cats.syntax.all._ import io.odin.Logger -import io.odin.consoleLogger import io.odin.fileLogger import io.odin.syntax._ import monix.bio.Task import monix.execution.Scheduler import munit.TestOptions +import java.time.LocalDateTime trait MonixBioSuite extends munit.TaglessFinalSuite[Task] { override protected def toFuture[A](f: Task[A]): Future[A] = { @@ -17,18 +17,10 @@ trait MonixBioSuite extends munit.TaglessFinalSuite[Task] { f.runToFuture } - def loggerFixture(fileName: Option[String] = None)(implicit - enc: sourcecode.Enclosing - ) = - ResourceFixture( - consoleLogger[Task]().withAsync() |+| fileLogger[Task]( - fileName.getOrElse(enc.value.split("#").head + ".log") - ), - ( - options: TestOptions, - value: Logger[Task] - ) => Task.unit, - (_: Logger[Task]) => Task.unit - ) + val date = LocalDateTime.now() + + val noopLogger = Logger.noop[Task] + + val consoleLogger = io.odin.consoleLogger[Task]() } diff --git a/src/it/resources/logback-test.xml b/src/it/resources/logback-test.xml new file mode 100644 index 0000000..c2ac424 --- /dev/null +++ b/src/it/resources/logback-test.xml @@ -0,0 +1,10 @@ + + + + %msg%n + + + + + + \ No newline at end of file diff --git a/src/it/scala/wow/doge/http4sdemo/LibraryServiceSpec.scala b/src/it/scala/wow/doge/http4sdemo/LibraryServiceSpec.scala index a909d4b..9753080 100644 --- a/src/it/scala/wow/doge/http4sdemo/LibraryServiceSpec.scala +++ b/src/it/scala/wow/doge/http4sdemo/LibraryServiceSpec.scala @@ -1,6 +1,5 @@ package wow.doge.http4sdemo -import cats.syntax.all._ import com.dimafeng.testcontainers.PostgreSQLContainer import monix.bio.UIO import wow.doge.http4sdemo.dto.BookSearchMode @@ -33,7 +32,6 @@ class LibraryServiceSpec extends DatabaseIntegrationTestBase { book <- service.insertBook(NewBook("blah", "Segehwe", id)) _ <- service .getBookById(book.bookId) - .flatTap(r => UIO(println(r))) .assertEquals(Some(book)) } yield () ) @@ -109,7 +107,7 @@ class LibraryServiceSpec extends DatabaseIntegrationTestBase { book1 <- service.insertBook(NewBook("blah3", "aeaega", id)) book2 <- service.insertBook(NewBook("blah4", "afgegg", id)) _ <- service - .searchBook(BookSearchMode.AuthorName, id.toString) + .searchBook(BookSearchMode.AuthorName, "bar") .toListL .toIO .attempt diff --git a/src/main/scala/wow/doge/http4sdemo/dto/Library.scala b/src/main/scala/wow/doge/http4sdemo/dto/Library.scala index 74431c3..2b5e34b 100644 --- a/src/main/scala/wow/doge/http4sdemo/dto/Library.scala +++ b/src/main/scala/wow/doge/http4sdemo/dto/Library.scala @@ -5,13 +5,10 @@ import java.time.LocalDateTime import cats.syntax.either._ import enumeratum.EnumEntry import enumeratum._ -import io.circe.Printer import io.circe.generic.semiauto._ import io.scalaland.chimney.dsl._ -import org.http4s.EntityEncoder import org.http4s.ParseFailure import org.http4s.QueryParamDecoder -import org.http4s.circe.streamJsonArrayEncoderWithPrinterOf import org.http4s.dsl.impl.QueryParamDecoderMatcher import slick.jdbc.JdbcProfile import wow.doge.http4sdemo.slickcodegen.Tables @@ -26,9 +23,6 @@ final case class Book( object Book { def tupled = (apply _).tupled implicit val codec = deriveCodec[Book] - // implicit def streamEntityEncoder[F[_]] - // : EntityEncoder[F, fs2.Stream[F, Book]] = - // streamJsonArrayEncoderWithPrinterOf(Printer.noSpaces) def fromBooksRow(row: Tables.BooksRow) = row.transformInto[Book] def fromBooksTableFn(implicit profile: JdbcProfile) = { import profile.api._ @@ -68,9 +62,6 @@ final case class Author(authorId: Int, authorName: String) object Author { def tupled = (apply _).tupled implicit val codec = deriveCodec[Author] - implicit def streamEntityEncoder[F[_]] - : EntityEncoder[F, fs2.Stream[F, Author]] = - streamJsonArrayEncoderWithPrinterOf(Printer.noSpaces) def fromAuthorsRow(row: Tables.AuthorsRow) = row.transformInto[Author] def fromAuthorsTableFn(implicit profile: JdbcProfile) = { import profile.api._ @@ -97,9 +88,6 @@ final case class BookWithAuthor( object BookWithAuthor { def tupled = (apply _).tupled implicit val codec = deriveCodec[BookWithAuthor] - implicit def streamEntityEncoder[F[_]] - : EntityEncoder[F, fs2.Stream[F, BookWithAuthor]] = - streamJsonArrayEncoderWithPrinterOf(Printer.noSpaces) } sealed trait BookSearchMode extends EnumEntry diff --git a/src/main/scala/wow/doge/http4sdemo/services/LibraryService.scala b/src/main/scala/wow/doge/http4sdemo/services/LibraryService.scala index 7068a29..2b0cca6 100755 --- a/src/main/scala/wow/doge/http4sdemo/services/LibraryService.scala +++ b/src/main/scala/wow/doge/http4sdemo/services/LibraryService.scala @@ -66,7 +66,7 @@ class LibraryServiceImpl( def getBooks = db.streamO(dbio.getBooks.transactionally) - def getBookById(id: Int) = db.runL(dbio.getBook(id)) + def getBookById(id: Int) = db.runL(dbio.getBookById(id)) def searchBook(mode: BookSearchMode, value: String): Observable[Book] = mode match { @@ -76,21 +76,20 @@ class LibraryServiceImpl( case AuthorName => Observable .fromTask((for { - _ <- IO.unit - id <- IO(value.toInt) - author <- db.runL(dbio.getAuthor(id)).flatMap { + author <- db.runL(dbio.getAuthorByName(value)).flatMap { case None => IO.raiseError( - new EntityDoesNotExist(s"Author with id=$id does not exist") + new EntityDoesNotExist( + s"Author with name=$value does not exist" + ) ) case Some(value) => IO.pure(value) } books = db - .streamO(dbio.getBooksForAuthor(id)) + .streamO(dbio.getBooksForAuthor(author.authorId)) .map(Book.fromBooksRow) } yield books).toTask) .flatten - } def insertAuthor(a: NewAuthor): Task[Int] = db.runL(dbio.insertAuthor(a)) @@ -99,7 +98,7 @@ class LibraryServiceImpl( for { action <- UIO.deferAction(implicit s => UIO(for { - mbRow <- dbio.selectBook(id).result.headOption + mbRow <- Tables.Books.filter(_.bookId === id).result.headOption updatedRow <- mbRow match { case Some(value) => println(s"Original value -> $value") @@ -110,7 +109,7 @@ class LibraryServiceImpl( EntityDoesNotExist(s"Book with id=$id does not exist") ) } - updateAction = dbio.selectBook(id).update(updatedRow) + updateAction = Tables.Books.filter(_.bookId === id).update(updatedRow) _ = println(s"SQL = ${updateAction.statements}") _ <- updateAction } yield ()) @@ -128,8 +127,8 @@ class LibraryServiceImpl( IO.deferAction { implicit s => for { action <- UIO(for { - _ <- dbio - .selectBookByIsbn(newBook.isbn) + _ <- Tables.Books + .filter(_.isbn === newBook.isbn) .map(Book.fromBooksTableFn) .result .headOption @@ -183,24 +182,30 @@ class LibraryDbio(val profile: JdbcProfile) { def insertAuthor(newAuthor: NewAuthor): DBIO[Int] = Query.insertAuthorGetId += newAuthor - def selectBook(id: Int) = Tables.Books.filter(_.bookId === id) - - def getAuthor(id: Int) = + def getAuthor(id: Int): DBIO[Option[Author]] = Query.selectAuthor(id).map(Author.fromAuthorsTableFn).result.headOption - def deleteBook(id: Int) = selectBook(id).delete + def getAuthorByName(name: String): DBIO[Option[Author]] = + Tables.Authors + .filter(_.authorName === name) + .map(Author.fromAuthorsTableFn) + .result + .headOption + + def deleteBook(id: Int): DBIO[Int] = Query.selectBookById(id).delete - def getBook(id: Int) = selectBook(id) + def getBookById(id: Int): DBIO[Option[Book]] = Query + .selectBookById(id) .map(Book.fromBooksTableFn) .result .headOption - def selectBookByIsbn(isbn: String) = Tables.Books.filter(_.isbn === isbn) - - def getBooksByTitle(title: String) = + def getBooksByTitle(title: String): StreamingDBIO[Seq[Book], Book] = Tables.Books.filter(_.bookTitle === title).map(Book.fromBooksTableFn).result - def getBooksForAuthor(authorId: Int) = + def getBooksForAuthor( + authorId: Int + ): StreamingDBIO[Seq[Tables.BooksRow], Tables.BooksRow] = Query.booksForAuthorInner(authorId).result private object Query { @@ -227,6 +232,10 @@ class LibraryDbio(val profile: JdbcProfile) { def selectAuthor(authorId: Int) = Tables.Authors.filter(_.authorId === authorId) + + def selectBookById(id: Int) = Tables.Books.filter(_.bookId === id) + + def selectBookByIsbn(isbn: String) = Tables.Books.filter(_.isbn === isbn) } } diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 0000000..f52e03f --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,10 @@ + + + + %msg%n + + + + + + \ No newline at end of file diff --git a/src/test/scala/wow/doge/http4sdemo/LibraryControllerSpec.scala b/src/test/scala/wow/doge/http4sdemo/LibraryControllerSpec.scala index 06b2167..bdf32eb 100644 --- a/src/test/scala/wow/doge/http4sdemo/LibraryControllerSpec.scala +++ b/src/test/scala/wow/doge/http4sdemo/LibraryControllerSpec.scala @@ -1,7 +1,5 @@ package wow.doge.http4sdemo -import java.time.LocalDateTime - import cats.syntax.all._ import monix.bio.IO import monix.bio.Task @@ -20,17 +18,6 @@ import wow.doge.http4sdemo.services.NoopLibraryService class LibraryControllerSpec extends MonixBioSuite { - // "libraryControllerSpec" - // val fixture = loggerFixture() - // ResourceFixture - - // override def munitFixtures = List(myFixture) - // override def munitFixtures: Seq[Fixture[_]] = ??? - - val date = LocalDateTime.now() - - // val logger = consoleLogger[Task]() - val Root = Uri(path = "") test("get books success") { diff --git a/src/test/scala/wow/doge/http4sdemo/LoggerFixtureSpec.scala b/src/test/scala/wow/doge/http4sdemo/LoggerFixtureSpec.scala deleted file mode 100644 index 36d5002..0000000 --- a/src/test/scala/wow/doge/http4sdemo/LoggerFixtureSpec.scala +++ /dev/null @@ -1,12 +0,0 @@ -package wow.doge.http4sdemo - -// import sourcecode.File - -class LoggerFixtureSpec extends MonixBioSuite { - // "LoggerFixtureSpec" - val fixture = loggerFixture() - - loggerFixture().test("blah blah") { logger => - logger.debug("blah blah blah") - } -}