minjibir commented on code in PR #949: URL: https://github.com/apache/pekko-connectors/pull/949#discussion_r1928044154
########## slick/src/test/scala/docs/scaladsl/SlickWithTryResultSpec.scala: ########## @@ -0,0 +1,355 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * license agreements; and to You under the Apache License, version 2.0: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * This file is part of the Apache Pekko project, which was derived from Akka. + */ + +/* + * Copyright (C) since 2016 Lightbend Inc. <https://www.lightbend.com> + */ + +package docs.scaladsl + +import org.apache.pekko +import pekko.Done +import pekko.actor.ActorSystem +import pekko.stream.connectors.slick.scaladsl.{ Slick, SlickSession, SlickWithTryResult } +import pekko.stream.connectors.testkit.scaladsl.LogCapturing +import pekko.stream.scaladsl._ +import pekko.testkit.TestKit + +import org.scalatest._ +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.matchers.must.Matchers +import org.scalatest.wordspec.AnyWordSpec + +import slick.dbio.DBIOAction +import slick.jdbc.GetResult + +import scala.concurrent.duration._ +import scala.concurrent.{ Await, ExecutionContext, Future } +import scala.util.{ Failure, Success } + +class SlickWithTryResultSpec extends AnyWordSpec + with ScalaFutures + with BeforeAndAfterEach + with BeforeAndAfterAll + with Matchers + with LogCapturing { + // #init-mat + implicit val system: ActorSystem = ActorSystem() + // #init-mat + + // #init-session + implicit val session: SlickSession = SlickSession.forConfig("slick-h2") + // #init-session + + import session.profile.api._ + + case class User(id: Int, name: String) + class Users(tag: Tag) extends Table[(Int, String)](tag, "PEKKO_CONNECTORS_SLICK_SCALADSL_TEST_USERS") { + def id = column[Int]("ID", O.PrimaryKey) + def name = column[String]("NAME") + def * = (id, name) + } + + implicit val ec: ExecutionContext = system.dispatcher + implicit val defaultPatience: PatienceConfig = PatienceConfig(timeout = 3.seconds, interval = 50.millis) + implicit val getUserResult: GetResult[User] = GetResult(r => User(r.nextInt(), r.nextString())) + + val users = (1 to 40).map(i => User(i, s"Name$i")).toSet + val duplicateUser = scala.collection.immutable.Seq(users.head, users.head) + + val createTable = + sqlu"""CREATE TABLE PEKKO_CONNECTORS_SLICK_SCALADSL_TEST_USERS(ID INTEGER PRIMARY KEY, NAME VARCHAR(50))""" + val dropTable = sqlu"""DROP TABLE PEKKO_CONNECTORS_SLICK_SCALADSL_TEST_USERS""" + val selectAllUsers = sql"SELECT ID, NAME FROM PEKKO_CONNECTORS_SLICK_SCALADSL_TEST_USERS".as[User] + val typedSelectAllUsers = TableQuery[Users].result + + def insertUser(user: User): DBIO[Int] = + sqlu"INSERT INTO PEKKO_CONNECTORS_SLICK_SCALADSL_TEST_USERS VALUES(${user.id}, ${user.name})" + + def getAllUsersFromDb: Future[Set[User]] = Slick.source(selectAllUsers).runWith(Sink.seq).map(_.toSet) + def populate(): Unit = { + val actions = users.map(insertUser) + + // This uses the standard Slick API exposed by the Slick session + // on purpose, just to double-check that inserting data through + // our Pekko connectors is equivalent to inserting it the Slick way. + session.db.run(DBIO.seq(actions.toList: _*)).futureValue + } + + override def beforeEach(): Unit = session.db.run(createTable).futureValue + override def afterEach(): Unit = session.db.run(dropTable).futureValue + + override def afterAll(): Unit = { + // #close-session + system.registerOnTermination(() => session.close()) + // #close-session + + TestKit.shutdownActorSystem(system) + } + + "SlickSession.forDbAndProfile" must { + "create a slick session able to talk to the db" in { + // #init-session-from-db-and-profile + val db = Database.forConfig("slick-h2.db") + val profile = slick.jdbc.H2Profile + val slickSessionCreatedForDbAndProfile: SlickSession = SlickSession.forDbAndProfile(db, profile) + // #init-session-from-db-and-profile + try { + val q = sql"select true".as[Boolean] + val result = Slick Review Comment: Ok, I will do so. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@pekko.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@pekko.apache.org For additional commands, e-mail: notifications-h...@pekko.apache.org