~jan0sch/smederee

Showing details for patch 39a3754e5efeb83f87f94b98991162ea78e2d398.
2024-07-29 (Mon), 7:13 AM - Jens Grassel - 39a3754e5efeb83f87f94b98991162ea78e2d398

chore: migrate test to ScalaCheckEffect

Summary of changes
1 files modified with 66 lines added and 71 lines removed
  • modules/hub/src/test/scala/de/smederee/tickets/DoobieTicketServiceApiTest.scala with 66 added and 71 removed lines
diff -rN -u old-smederee/modules/hub/src/test/scala/de/smederee/tickets/DoobieTicketServiceApiTest.scala new-smederee/modules/hub/src/test/scala/de/smederee/tickets/DoobieTicketServiceApiTest.scala
--- old-smederee/modules/hub/src/test/scala/de/smederee/tickets/DoobieTicketServiceApiTest.scala	2025-01-10 21:04:48.394337872 +0000
+++ new-smederee/modules/hub/src/test/scala/de/smederee/tickets/DoobieTicketServiceApiTest.scala	2025-01-10 21:04:48.398337881 +0000
@@ -8,89 +8,84 @@
 
 import cats.effect.*
 import de.smederee.TestTags.*
-import de.smederee.tickets.Generators.*
+import de.smederee.tickets.Generators.given
 import doobie.*
 
+import org.scalacheck.effect.PropF
+
 final class DoobieTicketServiceApiTest extends BaseSpec {
-    test("createOrUpdateUser must create new users".tag(NeedsDatabase)) {
-        genTicketsUser.sample match {
-            case Some(user) =>
-                val dbConfig = configuration.database
-                val tx = Transactor.fromDriverManager[IO](
-                    driver = dbConfig.driver,
-                    url = dbConfig.url,
-                    user = dbConfig.user,
-                    password = dbConfig.pass,
-                    logHandler = None
-                )
-                val api = new DoobieTicketServiceApi[IO](tx)
-                val test = for {
-                    written   <- api.createOrUpdateUser(user)
-                    foundUser <- loadTicketsUser(user.uid)
-                } yield (written, foundUser)
-                test.map { result =>
-                    val (written, foundUser) = result
-                    assert(written > 0, "No rows written to database!")
-                    assertEquals(foundUser, Some(user))
-                }
+    override def scalaCheckTestParameters = super.scalaCheckTestParameters.withMinSuccessfulTests(1)
 
-            case _ => fail("Could not generate data samples!")
+    test("createOrUpdateUser must create new users".tag(NeedsDatabase)) {
+        PropF.forAllF { (user: TicketsUser) =>
+            val dbConfig = configuration.database
+            val tx = Transactor.fromDriverManager[IO](
+                driver = dbConfig.driver,
+                url = dbConfig.url,
+                user = dbConfig.user,
+                password = dbConfig.pass,
+                logHandler = None
+            )
+            val api = new DoobieTicketServiceApi[IO](tx)
+            val test = for {
+                written   <- api.createOrUpdateUser(user)
+                foundUser <- loadTicketsUser(user.uid)
+            } yield (written, foundUser)
+            test.start.flatMap(_.joinWithNever).map { result =>
+                val (written, foundUser) = result
+                assert(written > 0, "No rows written to database!")
+                assertEquals(foundUser, Some(user))
+            }
         }
     }
 
     test("createOrUpdateUser must update existing users".tag(NeedsDatabase)) {
-        (genTicketsUser.sample, genTicketsUser.sample) match {
-            case (Some(user), Some(anotherUser)) =>
-                val updatedUser = anotherUser.copy(uid = user.uid)
-                val dbConfig    = configuration.database
-                val tx = Transactor.fromDriverManager[IO](
-                    driver = dbConfig.driver,
-                    url = dbConfig.url,
-                    user = dbConfig.user,
-                    password = dbConfig.pass,
-                    logHandler = None
-                )
-                val api = new DoobieTicketServiceApi[IO](tx)
-                val test = for {
-                    created   <- api.createOrUpdateUser(user)
-                    updated   <- api.createOrUpdateUser(updatedUser)
-                    foundUser <- loadTicketsUser(user.uid)
-                } yield (created, updated, foundUser)
-                test.map { result =>
-                    val (created, updated, foundUser) = result
-                    assert(created > 0, "No rows written to database!")
-                    assert(updated > 0, "No rows updated in database!")
-                    assertEquals(foundUser, Some(updatedUser))
-                }
-
-            case _ => fail("Could not generate data samples!")
+        PropF.forAllF { (user: TicketsUser, anotherUser: TicketsUser) =>
+            val updatedUser = anotherUser.copy(uid = user.uid)
+            val dbConfig    = configuration.database
+            val tx = Transactor.fromDriverManager[IO](
+                driver = dbConfig.driver,
+                url = dbConfig.url,
+                user = dbConfig.user,
+                password = dbConfig.pass,
+                logHandler = None
+            )
+            val api = new DoobieTicketServiceApi[IO](tx)
+            val test = for {
+                created   <- api.createOrUpdateUser(user)
+                updated   <- api.createOrUpdateUser(updatedUser)
+                foundUser <- loadTicketsUser(user.uid)
+            } yield (created, updated, foundUser)
+            test.start.flatMap(_.joinWithNever).map { result =>
+                val (created, updated, foundUser) = result
+                assert(created > 0, "No rows written to database!")
+                assert(updated > 0, "No rows updated in database!")
+                assertEquals(foundUser, Some(updatedUser))
+            }
         }
     }
 
     test("deleteUser must delete existing users".tag(NeedsDatabase)) {
-        genTicketsUser.sample match {
-            case Some(user) =>
-                val dbConfig = configuration.database
-                val tx = Transactor.fromDriverManager[IO](
-                    driver = dbConfig.driver,
-                    url = dbConfig.url,
-                    user = dbConfig.user,
-                    password = dbConfig.pass,
-                    logHandler = None
-                )
-                val api = new DoobieTicketServiceApi[IO](tx)
-                val test = for {
-                    _         <- api.createOrUpdateUser(user)
-                    deleted   <- api.deleteUser(user.uid)
-                    foundUser <- loadTicketsUser(user.uid)
-                } yield (deleted, foundUser)
-                test.map { result =>
-                    val (deleted, foundUser) = result
-                    assert(deleted > 0, "No rows deleted from database!")
-                    assert(foundUser.isEmpty, "User not deleted from database!")
-                }
-
-            case _ => fail("Could not generate data samples!")
+        PropF.forAllF { (user: TicketsUser) =>
+            val dbConfig = configuration.database
+            val tx = Transactor.fromDriverManager[IO](
+                driver = dbConfig.driver,
+                url = dbConfig.url,
+                user = dbConfig.user,
+                password = dbConfig.pass,
+                logHandler = None
+            )
+            val api = new DoobieTicketServiceApi[IO](tx)
+            val test = for {
+                _         <- api.createOrUpdateUser(user)
+                deleted   <- api.deleteUser(user.uid)
+                foundUser <- loadTicketsUser(user.uid)
+            } yield (deleted, foundUser)
+            test.start.flatMap(_.joinWithNever).map { result =>
+                val (deleted, foundUser) = result
+                assert(deleted > 0, "No rows deleted from database!")
+                assert(foundUser.isEmpty, "User not deleted from database!")
+            }
         }
     }
 }