Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
chungg committed Jan 31, 2024
1 parent 552846c commit 9de2fd9
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 25 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ jobs:
- name: check style
run: ./biome check src/static/js

run-unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: setup rust
uses: actions-rust-lang/setup-rust-toolchain@v1
- name: run unit tests
run: cargo test

build:
runs-on: ubuntu-latest
needs: [check-style, analyze]
needs: [check-style, analyze, run-unit-tests]
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
Expand Down
162 changes: 146 additions & 16 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ serde_json = "1.0.111"
tokio = { version = "1.35.1", features = ["full"] }
tower-http = { version = "0.5.1", features = ["fs"] }
uuid = { version = "1.7.0", features = ["v4"] }

[dev-dependencies]
pretty_assertions = "1.4.0"
rstest = "0.18.2"
18 changes: 18 additions & 0 deletions src/core/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,21 @@ pub async fn get_prices(ticker: &str) -> Value {
});
data["chart"]["result"][0].clone()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_random_endpoint() {
let data = random_data();
assert!(data.as_object().unwrap()["datasets"][0]["data"].is_array());
assert_eq!(
data.as_object().unwrap()["datasets"][0]["data"]
.as_array()
.unwrap()
.len(),
6
);
}
}
18 changes: 10 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ pub struct AppState {

#[tokio::main]
async fn main() {
//// setup jinja
// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app()).await.unwrap();
}

fn app() -> Router {
// setup jinja
let mut minijinja = Environment::new();
minijinja.set_loader(path_loader("src/templates"));

// application routes
let app = Router::new()
//.route("/", get(|| async { "Hello, World!" }))
Router::new()
.route("/health", get(|| async { "¯\\_(ツ)_/¯" }))
.route("/api/v1/data/random", get(v1::routes::random_data))
.route("/api/v1/data/sales", get(v1::routes::sales_data))
.route("/api/v1/market/prices", get(v1::routes::price_data))
Expand All @@ -35,9 +41,5 @@ async fn main() {
.route("/analytics", get(pages::analytics))
.route("/yahoo", get(pages::yahoo))
.nest_service("/static/js", ServeDir::new("src/static/js"))
.with_state(Arc::new(AppState { jinja: minijinja }));

// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
.with_state(Arc::new(AppState { jinja: minijinja }))
}

0 comments on commit 9de2fd9

Please sign in to comment.