Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/core/shared/src/main/scala/data/Completion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ object Completion {
case object DropPolicy extends Completion
case object Comment extends Completion
case object Analyze extends Completion
case object AlterDefaultPrivileges extends Completion
case object GrantRole extends Completion
case object RevokeRole extends Completion

// more ...

// weird Redshift variations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ object CommandComplete {
case "ALTER POLICY" => apply(Completion.AlterPolicy)
case "DROP POLICY" => apply(Completion.DropPolicy)
case "ANALYZE" => apply(Completion.Analyze)
case "ALTER DEFAULT PRIVILEGES" => apply(Completion.AlterDefaultPrivileges)
case "GRANT ROLE" => apply(Completion.GrantRole)
case "REVOKE ROLE" => apply(Completion.RevokeRole)
// more .. fill in as we hit them

// weird Redshift variations
Expand Down
19 changes: 19 additions & 0 deletions modules/tests/shared/src/test/scala/CommandTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -664,4 +664,23 @@ class CommandTest extends SkunkTest {
} yield "ok"
}

sessionTestWithCleanup("grant role, revoke role") { s =>
for {
_ <- s.execute(createRole)
_ <- s.execute(sql"""CREATE ROLE skunk_role2""".command)
c <- s.execute(sql"""GRANT skunk_role2 TO skunk_role""".command)
_ <- assertEqual("completion", c, Completion.GrantRole)
c <- s.execute(sql"""REVOKE skunk_role2 FROM skunk_role""".command)
_ <- assertEqual("completion", c, Completion.RevokeRole)
} yield "ok"
}(sql"""DROP ROLE IF EXISTS skunk_role2""".command, dropRole)

sessionTest("alter default privileges") { s =>
for {
c <- s.execute(sql"""ALTER DEFAULT PRIVILEGES GRANT SELECT ON TABLES TO PUBLIC""".command)
_ <- assertEqual("completion", c, Completion.AlterDefaultPrivileges)
_ <- s.assertHealthy
} yield "ok"
}

}
25 changes: 19 additions & 6 deletions modules/tests/shared/src/test/scala/SkunkTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

package tests

import cats.effect.{ IO, Resource }
import cats.syntax.all._
import skunk.Session
import skunk.data._
import skunk.codec.all._
import skunk.implicits._
import cats.effect.{IO, Resource}
import cats.syntax.all.*
import skunk.{Command, Session, Void}
import skunk.data.*
import skunk.codec.all.*
import skunk.implicits.*
import skunk.util.Typer
import natchez.Trace.Implicits.noop
import munit.Location

import scala.concurrent.duration.Duration

abstract class SkunkTest(debug: Boolean = false, strategy: Typer.Strategy = Typer.Strategy.BuiltinsOnly) extends ffstest.FTest {
Expand All @@ -34,6 +35,18 @@ abstract class SkunkTest(debug: Boolean = false, strategy: Typer.Strategy = Type
def sessionTest[A](name: String, readTimeout: Duration = Duration.Inf)(fa: Session[IO] => IO[A])(implicit loc: Location): Unit =
test(name)(session(readTimeout).use(fa))

def sessionTestWithCleanup[A](name: String,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got annoyed that the DROP ROLE commands that clean up many of the command tests don't run if the test fails, so I added this new method to run the cleanup steps in a guarantee block. If we like this, we can move others to this style over time.

readTimeout: Duration = Duration.Inf)
(fa: Session[IO] => IO[A])
(cleanup: Command[Void]*)
(implicit loc: Location): Unit =
test(name) {
session(readTimeout).use { s =>
(fa(s) >> s.assertHealthy)
.guarantee(cleanup.toList.traverse_(s.execute))
}
}

def pooled(readTimeout: Duration): Resource[IO, Resource[IO, Session[IO]]] =
Session.pooled(
host = "localhost",
Expand Down