Skip to content

Add examples of mill + cask + scalasql #4924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a3db4d1
Add examples of mill + cask + scalasql
chaitanyawaikar Apr 12, 2025
5d3053f
[autofix.ci] apply automated fixes
autofix-ci[bot] Apr 12, 2025
6fd4641
Remove comment
chaitanyawaikar Apr 12, 2025
59f3845
Add documentation for example 10-todo-webapp-cask-scalasql
chaitanyawaikar Apr 12, 2025
f3a7d78
Introduce example for http4s + scalasql
chaitanyawaikar Apr 13, 2025
6e8c44c
[autofix.ci] apply automated fixes
autofix-ci[bot] Apr 13, 2025
dc6846e
Add documentation link for 11-todo-http4s-scalasql
chaitanyawaikar Apr 13, 2025
d39cc7d
Merge branch 'main' into feature/add_examples_with_scalasql
chaitanyawaikar Apr 13, 2025
cb409d3
Add tests and documentation for the module `11-todo-http-scalasql`
chaitanyawaikar Apr 13, 2025
0599fb4
[autofix.ci] apply automated fixes
autofix-ci[bot] Apr 13, 2025
0765a09
Merge branch 'main' into feature/add_examples_with_scalasql
chaitanyawaikar Apr 16, 2025
39a33ec
Add integration tests for `10-todo-webapp-cask-scalasql` module
chaitanyawaikar Apr 18, 2025
56eefc3
[autofix.ci] apply automated fixes
autofix-ci[bot] Apr 18, 2025
7d4049e
Changing build.mill to get rid of the CI error
chaitanyawaikar Apr 18, 2025
bcb32c5
[autofix.ci] apply automated fixes
autofix-ci[bot] Apr 18, 2025
750b540
Merge branch 'main' into feature/add_examples_with_scalasql
chaitanyawaikar Apr 27, 2025
ac52b64
Address review comment https://github.com/com-lihaoyi/mill/pull/4924/…
chaitanyawaikar Apr 27, 2025
6b4e6d3
Add integration tests and create a separate module
chaitanyawaikar Apr 27, 2025
5b7b8d8
[autofix.ci] apply automated fixes
autofix-ci[bot] Apr 27, 2025
1fb3288
Add integration tests for `10-todo-webapp-cask-scalasql` example
chaitanyawaikar Apr 27, 2025
afcaf55
Edit comment in build.mill file
chaitanyawaikar Apr 27, 2025
639a29b
Merge branch 'main' into feature/add_examples_with_scalasql
chaitanyawaikar Apr 28, 2025
0c53be7
Merge branch 'main' into feature/add_examples_with_scalasql
chaitanyawaikar May 2, 2025
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
50 changes: 50 additions & 0 deletions example/scalalib/web/10-todo-webapp-cask-scalasql/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package build

import mill._, scalalib._

object `package` extends RootModule with ScalaModule {
def scalaVersion = "2.13.8"

override def ivyDeps = Agg(
ivy"com.lihaoyi::cask:0.9.1",
ivy"com.lihaoyi::scalatags:0.13.1",
ivy"com.lihaoyi::upickle:3.1.0",
ivy"com.lihaoyi::scalasql:0.1.19",
ivy"com.h2database:h2:2.2.224"
Copy link
Member

Choose a reason for hiding this comment

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

Does the root module really need h2database when it is only used in the unit tests?

)

object test extends TestModule

object integration extends IntegrationTestModule

/** Common test setup */
trait TestBase extends ScalaModule {
def scalaVersion = `package`.scalaVersion
override def repositories = super.repositories ++ Seq(
coursier.MavenRepository("https://oss.sonatype.org/content/repositories/snapshots")
)
override def moduleDeps = Seq(`package`)
Comment on lines +22 to +26
Copy link
Member

Choose a reason for hiding this comment

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

None of these are necessary, why are they here?

}

/** Unit tests using H2 */
trait TestModule extends TestBase with ScalaTests {
Copy link
Member

Choose a reason for hiding this comment

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

No need separate traits, just put these definitions in the objects

def testFramework = "utest.runner.Framework"

override def ivyDeps = super.ivyDeps() ++ Agg(
ivy"com.lihaoyi::utest:0.8.5",
ivy"com.lihaoyi::requests:0.6.9"
)
}

/** Integration tests using PostgreSQL Testcontainers */
trait IntegrationTestModule extends TestBase with ScalaTests {
def testFramework = "utest.runner.Framework"

override def ivyDeps = super.ivyDeps() ++ Agg(
ivy"com.lihaoyi::utest:0.8.5",
ivy"com.dimafeng::testcontainers-scala-postgresql:0.43.0",
ivy"com.dimafeng::testcontainers-scala-scalatest:0.43.0",
ivy"org.testcontainers:postgresql:1.19.1"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package webapp

import utest._
import requests._
import com.dimafeng.testcontainers.PostgreSQLContainer
import org.testcontainers.utility.DockerImageName
import scala.concurrent.blocking
import scala.sys.process._
import scala.concurrent.duration._
import scala.util.Try

object WebAppIntegrationTests extends TestSuite {

val tests = Tests {
var serverPid: Option[Process] = None
val serverPort = 8080

val pgContainer = PostgreSQLContainer(
dockerImageNameOverride = DockerImageName.parse("postgres:15"),
databaseName = "testdb",
username = "postgres",
password = "password"
)

def waitForServer(port: Int, timeout: FiniteDuration = 100.seconds): Boolean = {
val deadline = timeout.fromNow
while (deadline.hasTimeLeft()) {
Try(requests.get(s"http://localhost:$port/")).toOption match {
case Some(response) if response.statusCode == 200 => return true
case _ =>
Thread.sleep(500)
}
}
false
}

test("start postgres container and app server") {
pgContainer.start()

val envVars = Seq(
"DB_URL" -> pgContainer.jdbcUrl,
"DB_USER" -> pgContainer.username,
"DB_PASS" -> pgContainer.password
)

val cmd = Seq("mill", "runBackground")
val pb = Process(cmd, None, envVars: _*)
serverPid = Some(pb.run())

val started = waitForServer(serverPort)
require(started, "Server did not start in time")
}

test("add and list todos") {
val response = requests.post(s"http://localhost:$serverPort/add/all", data = "")
assert(response.statusCode == 200)
assert(response.text.contains("What needs to be done"))

val response2 = requests.post(s"http://localhost:$serverPort/list/all", data = "")
assert(response2.text.contains("What needs to be done"))
}

test("toggle and delete todo") {
// Toggle doesn't change DB but should still return 200
val response = requests.post(s"http://localhost:$serverPort/toggle/all/1", data = "")
assert(response.statusCode == 200)

val deleteResp = requests.post(s"http://localhost:$serverPort/delete/all/1", data = "")
assert(deleteResp.statusCode == 200)
}

test("cleanup") {
serverPid.foreach(_.destroy())
pgContainer.stop()
}
}
}
Loading