Skip to content

Commit

Permalink
Add simple test with sqlx integration
Browse files Browse the repository at this point in the history
  • Loading branch information
raffomania committed Dec 26, 2023
1 parent c20b329 commit f2880ee
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 15 deletions.
23 changes: 20 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ jobs:
name: CI
runs-on: ubuntu-latest

services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- name: Checkout code
uses: actions/checkout@v3
Expand All @@ -19,12 +32,16 @@ jobs:

- name: cargo build
run: cargo build

- name: cargo test
run: cargo test
env:
SQLX_OFFLINE: true

- name: cargo fmt
run: cargo fmt --all -- --check

- name: cargo clippy
run: cargo clippy -- -D warnings

- name: cargo test
run: cargo test
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/linkblocks
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
[build-dependencies]
railwind = "0.1.5"
walkdir = "2"

[dev-dependencies]
tower = { version = "0.4.13", features = ["util"] }
19 changes: 13 additions & 6 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@ start-database:
exit
fi

podman run \
--name linkblocks_postgres --detach \
--health-cmd pg_isready --health-interval 10s \
-e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=linkblocks \
-p ${DATABASE_PORT}:5432 docker.io/postgres:16 \
postgres
if ! podman inspect linkblocks_postgres &> /dev/null; then
podman create \
--name linkblocks_postgres --detach \
--health-cmd pg_isready --health-interval 10s \
-e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=linkblocks \
-p ${DATABASE_PORT}:5432 docker.io/postgres:16 \
postgres
fi

podman start linkblocks_postgres

for i in {1..20}; do
pg_isready -h localhost -p $DATABASE_PORT && break
sleep 1
done

stop-database:
podman stop linkblocks_postgres

test:
cargo test

Expand Down
3 changes: 2 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ pub async fn run() -> Result<()> {
} => {
let pool = db::pool(&database_url).await?;
db::migrate(&pool).await?;
server::start(listen_address, pool).await
let app = server::app(pool);
server::start(listen_address, app).await
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod app_error;
pub mod cli;
mod db;
mod routes;
mod server;
pub mod server;
10 changes: 6 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use axum::{debug_handler, routing::get, Router};
use listenfd::ListenFd;
use tower_http::trace::TraceLayer;

pub async fn start(listen: ListenArgs, pool: sqlx::PgPool) -> anyhow::Result<()> {
let app = Router::new()
pub fn app(pool: sqlx::PgPool) -> Router {
Router::new()
.route("/", get(hello))
.route("/htmx-fragment", get(htmx_fragment))
.route(
Expand All @@ -16,8 +16,10 @@ pub async fn start(listen: ListenArgs, pool: sqlx::PgPool) -> anyhow::Result<()>
)
.route("/assets/*path", get(routes::assets::assets))
.layer(TraceLayer::new_for_http())
.with_state(pool);
.with_state(pool)
}

pub async fn start(listen: ListenArgs, app: Router) -> anyhow::Result<()> {
let listener = if let Some(listen_address) = listen.listen {
tokio::net::TcpListener::bind(format!("{listen_address}")).await?
} else {
Expand All @@ -31,7 +33,7 @@ pub async fn start(listen: ListenArgs, pool: sqlx::PgPool) -> anyhow::Result<()>
let listening_on = listener.local_addr()?;
tracing::info!("Listening on http://{listening_on}");

axum::serve(listener, app).await?;
axum::serve(listener, app);

Ok(())
}
Expand Down
17 changes: 17 additions & 0 deletions tests/index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use axum::{
body::Body,
http::{Request, StatusCode},
};
use sqlx::{Pool, Postgres};
use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
#[sqlx::test]
async fn index(pool: Pool<Postgres>) {
let app = linkblocks::server::app(pool);

let response = app
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
.await
.unwrap();

assert_eq!(response.status(), StatusCode::OK);
}

0 comments on commit f2880ee

Please sign in to comment.