~jan0sch/smederee

Showing details for patch c40a532e67069c21163d7a2e2044f8d6646d2375.
2024-07-22 (Mon), 9:14 AM - Jens Grassel - c40a532e67069c21163d7a2e2044f8d6646d2375

chore: migrate test to ScalaCheckEffect

Summary of changes
1 files modified with 110 lines added and 116 lines removed
  • modules/hub/src/test/scala/de/smederee/hub/DoobieSignupRepositoryTest.scala with 110 added and 116 removed lines
diff -rN -u old-smederee/modules/hub/src/test/scala/de/smederee/hub/DoobieSignupRepositoryTest.scala new-smederee/modules/hub/src/test/scala/de/smederee/hub/DoobieSignupRepositoryTest.scala
--- old-smederee/modules/hub/src/test/scala/de/smederee/hub/DoobieSignupRepositoryTest.scala	2025-01-11 00:13:21.074603997 +0000
+++ new-smederee/modules/hub/src/test/scala/de/smederee/hub/DoobieSignupRepositoryTest.scala	2025-01-11 00:13:21.074603997 +0000
@@ -9,143 +9,137 @@
 import cats.effect.*
 import cats.syntax.all.*
 import de.smederee.TestTags.*
-import de.smederee.hub.Generators.*
+import de.smederee.hub.Generators.given
 import de.smederee.security.*
 import doobie.*
 
+import org.scalacheck.effect.PropF
+
 final class DoobieSignupRepositoryTest extends BaseSpec {
+    override def scalaCheckTestParameters = super.scalaCheckTestParameters.withMinSuccessfulTests(1)
+
     test("createAccount must create a new account".tag(NeedsDatabase)) {
-        genValidAccount.sample match {
-            case None => fail("Could not generate data samples!")
-            case Some(account) =>
-                val dbConfig = configuration.database
-                val tx = Transactor.fromDriverManager[IO](
-                    driver = dbConfig.driver,
-                    url = dbConfig.url,
-                    user = dbConfig.user,
-                    password = dbConfig.pass,
-                    logHandler = None
-                )
-                val repo = new DoobieSignupRepository[IO](tx)
-                val test = for {
-                    w <- repo.createAccount(account, PasswordHash("I am not a password hash!"))
-                    o <- loadAccount(account.uid)
-                } yield (w, o)
-                test.map { result =>
-                    val (written, loadedAccount) = result
-                    assert(written === 1, "1 database row must have been written!")
-                    assert(loadedAccount === Option(account), "Saved account differs from expected one!")
-                }
+        PropF.forAllF { (account: Account) =>
+            val dbConfig = configuration.database
+            val tx = Transactor.fromDriverManager[IO](
+                driver = dbConfig.driver,
+                url = dbConfig.url,
+                user = dbConfig.user,
+                password = dbConfig.pass,
+                logHandler = None
+            )
+            val repo = new DoobieSignupRepository[IO](tx)
+            val test = for {
+                w <- repo.createAccount(account, PasswordHash("I am not a password hash!"))
+                o <- loadAccount(account.uid)
+            } yield (w, o)
+            test.start.flatMap(_.joinWithNever).map { result =>
+                val (written, loadedAccount) = result
+                assert(written === 1, "1 database row must have been written!")
+                assert(loadedAccount === Option(account), "Saved account differs from expected one!")
+            }
         }
     }
 
     test("createAccount must fail if the email already exists".tag(NeedsDatabase)) {
-        (genValidAccount.sample, genValidAccount.sample) match {
-            case (Some(a), Some(b)) =>
-                val existingAccount = a
-                val newAccount      = b.copy(email = a.email)
-                val dbConfig        = configuration.database
-                val tx = Transactor.fromDriverManager[IO](
-                    driver = dbConfig.driver,
-                    url = dbConfig.url,
-                    user = dbConfig.user,
-                    password = dbConfig.pass,
-                    logHandler = None
-                )
-                val repo = new DoobieSignupRepository[IO](tx)
-                val test = for {
-                    c <- createAccount(existingAccount, PasswordHash("I am not a password hash!"), None, None)
-                    w <- repo.createAccount(newAccount, PasswordHash("I am not a password hash!"))
-                } yield (c, w)
-                test.attempt.map {
-                    case Left(error) =>
-                        assert(
-                            error.getMessage.contains("accounts_unique_email"),
-                            "Error must be triggered by accounts_unique_email constraint!"
-                        )
-                    case Right(_) => fail("Creating accounts with already used emails must fail!")
-                }
-            case _ => fail("Could not generate data samples!")
+        PropF.forAllF { (a: Account, b: Account) =>
+            val existingAccount = a
+            val newAccount      = b.copy(email = a.email)
+            val dbConfig        = configuration.database
+            val tx = Transactor.fromDriverManager[IO](
+                driver = dbConfig.driver,
+                url = dbConfig.url,
+                user = dbConfig.user,
+                password = dbConfig.pass,
+                logHandler = None
+            )
+            val repo = new DoobieSignupRepository[IO](tx)
+            val test = for {
+                c <- createAccount(existingAccount, PasswordHash("I am not a password hash!"), None, None)
+                w <- repo.createAccount(newAccount, PasswordHash("I am not a password hash!"))
+            } yield (c, w)
+            test.attempt.map {
+                case Left(error) =>
+                    assert(
+                        error.getMessage.contains("accounts_unique_email"),
+                        "Error must be triggered by accounts_unique_email constraint!"
+                    )
+                case Right(_) => fail("Creating accounts with already used emails must fail!")
+            }
         }
     }
 
     test("createAccount must fail if the username already exists".tag(NeedsDatabase)) {
-        (genValidAccount.sample, genValidAccount.sample) match {
-            case (Some(a), Some(b)) =>
-                val existingAccount = a
-                val newAccount      = b.copy(name = a.name)
-                val dbConfig        = configuration.database
-                val tx = Transactor.fromDriverManager[IO](
-                    driver = dbConfig.driver,
-                    url = dbConfig.url,
-                    user = dbConfig.user,
-                    password = dbConfig.pass,
-                    logHandler = None
-                )
-                val repo = new DoobieSignupRepository[IO](tx)
-                val test = for {
-                    c <- createAccount(existingAccount, PasswordHash("I am not a password hash!"), None, None)
-                    w <- repo.createAccount(newAccount, PasswordHash("I am not a password hash!"))
-                } yield (c, w)
-                test.attempt.map {
-                    case Left(error) =>
-                        assert(
-                            error.getMessage.contains("accounts_unique_name"),
-                            "Error must be triggered by accounts_unique_name constraint!"
-                        )
-                    case Right(_) => fail("Creating accounts with already used names must fail!")
-                }
-            case _ => fail("Could not generate data samples!")
+        PropF.forAllF { (a: Account, b: Account) =>
+            val existingAccount = a
+            val newAccount      = b.copy(name = a.name)
+            val dbConfig        = configuration.database
+            val tx = Transactor.fromDriverManager[IO](
+                driver = dbConfig.driver,
+                url = dbConfig.url,
+                user = dbConfig.user,
+                password = dbConfig.pass,
+                logHandler = None
+            )
+            val repo = new DoobieSignupRepository[IO](tx)
+            val test = for {
+                c <- createAccount(existingAccount, PasswordHash("I am not a password hash!"), None, None)
+                w <- repo.createAccount(newAccount, PasswordHash("I am not a password hash!"))
+            } yield (c, w)
+            test.attempt.map {
+                case Left(error) =>
+                    assert(
+                        error.getMessage.contains("accounts_unique_name"),
+                        "Error must be triggered by accounts_unique_name constraint!"
+                    )
+                case Right(_) => fail("Creating accounts with already used names must fail!")
+            }
         }
     }
 
     test("findEmail must return an existing email".tag(NeedsDatabase)) {
-        genValidAccount.sample match {
-            case None => fail("Could not generate data samples!")
-            case Some(account) =>
-                val dbConfig = configuration.database
-                val tx = Transactor.fromDriverManager[IO](
-                    driver = dbConfig.driver,
-                    url = dbConfig.url,
-                    user = dbConfig.user,
-                    password = dbConfig.pass,
-                    logHandler = None
-                )
-                val repo = new DoobieSignupRepository[IO](tx)
-                val test = for {
-                    c <- repo.createAccount(account, PasswordHash("I am not a password hash!"))
-                    e <- repo.findEmail(account.email)
-                } yield (c, e)
-                test.map { result =>
-                    val (created, email) = result
-                    assert(created === 1, "Test account not created!")
-                    assert(email === Option(account.email), "Expected email not found!")
-                }
+        PropF.forAllF { (account: Account) =>
+            val dbConfig = configuration.database
+            val tx = Transactor.fromDriverManager[IO](
+                driver = dbConfig.driver,
+                url = dbConfig.url,
+                user = dbConfig.user,
+                password = dbConfig.pass,
+                logHandler = None
+            )
+            val repo = new DoobieSignupRepository[IO](tx)
+            val test = for {
+                c <- repo.createAccount(account, PasswordHash("I am not a password hash!"))
+                e <- repo.findEmail(account.email)
+            } yield (c, e)
+            test.start.flatMap(_.joinWithNever).map { result =>
+                val (created, email) = result
+                assert(created === 1, "Test account not created!")
+                assert(email === Option(account.email), "Expected email not found!")
+            }
         }
     }
 
     test("findUsername must return an existing name".tag(NeedsDatabase)) {
-        genValidAccount.sample match {
-            case None => fail("Could not generate data samples!")
-            case Some(account) =>
-                val dbConfig = configuration.database
-                val tx = Transactor.fromDriverManager[IO](
-                    driver = dbConfig.driver,
-                    url = dbConfig.url,
-                    user = dbConfig.user,
-                    password = dbConfig.pass,
-                    logHandler = None
-                )
-                val repo = new DoobieSignupRepository[IO](tx)
-                val test = for {
-                    c <- repo.createAccount(account, PasswordHash("I am not a password hash!"))
-                    n <- repo.findUsername(account.name)
-                } yield (c, n)
-                test.map { result =>
-                    val (created, name) = result
-                    assert(created === 1, "Test account not created!")
-                    assert(name === Option(account.name), "Expected name not found!")
-                }
+        PropF.forAllF { (account: Account) =>
+            val dbConfig = configuration.database
+            val tx = Transactor.fromDriverManager[IO](
+                driver = dbConfig.driver,
+                url = dbConfig.url,
+                user = dbConfig.user,
+                password = dbConfig.pass,
+                logHandler = None
+            )
+            val repo = new DoobieSignupRepository[IO](tx)
+            val test = for {
+                c <- repo.createAccount(account, PasswordHash("I am not a password hash!"))
+                n <- repo.findUsername(account.name)
+            } yield (c, n)
+            test.start.flatMap(_.joinWithNever).map { result =>
+                val (created, name) = result
+                assert(created === 1, "Test account not created!")
+                assert(name === Option(account.name), "Expected name not found!")
+            }
         }
     }
 }