Compare commits
2 Commits
060e8ff795
...
f97b9bb51b
| Author | SHA1 | Date | |
|---|---|---|---|
| f97b9bb51b | |||
| ebeeac2091 |
@@ -0,0 +1,40 @@
|
||||
name: Tests
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
|
||||
- name: Cache dependencies
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.cargo/registry
|
||||
~/.cargo/git
|
||||
target
|
||||
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-cargo-
|
||||
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
|
||||
- name: Check formatting
|
||||
run: cargo fmt --check
|
||||
|
||||
- name: Run clippy
|
||||
run: cargo clippy -- -D warnings
|
||||
@@ -9,3 +9,8 @@ log = "0.4.28"
|
||||
extended_rust = { git = "https://github.com/AlexIndustrial/extended_rust.git" }
|
||||
axum = "0.8"
|
||||
desert-framework-macros = { path = "../desert-framework-macros" }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1.47.1", features = ["full"] }
|
||||
tower = { version = "0.5", features = ["util"] }
|
||||
tower-http = { version = "0.6", features = ["util"] }
|
||||
|
||||
+243
-94
@@ -1,102 +1,251 @@
|
||||
use std::sync::Arc;
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use extended_rust::string;
|
||||
use axum::body::Body;
|
||||
use axum::http::{Request, StatusCode};
|
||||
use tower::ServiceExt;
|
||||
|
||||
use crate::{
|
||||
dependency::{dep, Deps},
|
||||
manager::DependencyManager,
|
||||
service::Service,
|
||||
};
|
||||
use crate::{controller, get, impl_routes, post};
|
||||
use crate::dependency::{dep, Deps};
|
||||
use crate::manager::DependencyManager;
|
||||
use crate::service::Service;
|
||||
use crate::{controller, get, impl_routes, post};
|
||||
|
||||
pub struct TestService1 {}
|
||||
struct TestService1;
|
||||
|
||||
impl Service for TestService1 {
|
||||
async fn new(_manager: Arc<DependencyManager>) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Self {}
|
||||
impl Service for TestService1 {
|
||||
fn name() -> String { "TestService1".into() }
|
||||
|
||||
async fn new(_manager: Arc<DependencyManager>) -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
fn name() -> String {
|
||||
string!("1")
|
||||
impl TestService1 {
|
||||
fn get_hello(&self) -> &str { "hello from service 1" }
|
||||
}
|
||||
|
||||
struct TestService2;
|
||||
|
||||
impl Service for TestService2 {
|
||||
fn name() -> String { "TestService2".into() }
|
||||
|
||||
fn deps() -> Deps {
|
||||
vec![dep::<TestService1>()]
|
||||
}
|
||||
|
||||
async fn new(manager: Arc<DependencyManager>) -> Self {
|
||||
let _s1 = manager.get::<TestService1>("TestService2").await.unwrap();
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
struct NoDepsService;
|
||||
|
||||
impl Service for NoDepsService {
|
||||
fn name() -> String { "NoDepsService".into() }
|
||||
|
||||
async fn new(_manager: Arc<DependencyManager>) -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
// === Dependency Manager Tests ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn register_service_without_deps() {
|
||||
let manager = DependencyManager::new();
|
||||
let _svc = manager.register::<NoDepsService>().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn register_service_with_deps() {
|
||||
let manager = DependencyManager::new();
|
||||
let _s1 = manager.register::<TestService1>().await;
|
||||
let _s2 = manager.register::<TestService2>().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_registered_service() {
|
||||
let manager = DependencyManager::new();
|
||||
let _s1 = manager.register::<TestService1>().await;
|
||||
let result = manager.get::<TestService1>("test").await;
|
||||
assert!(result.is_some());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_unregistered_service_returns_none() {
|
||||
let manager = DependencyManager::new();
|
||||
let result = manager.get::<TestService1>("test").await;
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn service_returns_correct_data() {
|
||||
let manager = DependencyManager::new();
|
||||
let s1 = manager.register::<TestService1>().await;
|
||||
assert_eq!(s1.get_hello(), "hello from service 1");
|
||||
}
|
||||
|
||||
// === Controller Tests ===
|
||||
|
||||
#[controller(path = "/api")]
|
||||
struct TestController;
|
||||
|
||||
impl TestController {
|
||||
#[get("/hello")]
|
||||
async fn hello(&self) -> &'static str {
|
||||
"hello world"
|
||||
}
|
||||
|
||||
#[get("/items/{id}")]
|
||||
async fn get_item(
|
||||
&self,
|
||||
axum::extract::Path(id): axum::extract::Path<String>,
|
||||
) -> String {
|
||||
format!("item: {}", id)
|
||||
}
|
||||
|
||||
#[post("/items")]
|
||||
async fn add_item(
|
||||
&self,
|
||||
axum::extract::Json(body): axum::extract::Json<String>,
|
||||
) -> String {
|
||||
format!("added: {}", body)
|
||||
}
|
||||
}
|
||||
|
||||
impl_routes!(TestController, [hello, get_item, add_item]);
|
||||
|
||||
#[tokio::test]
|
||||
async fn controller_get_hello() {
|
||||
let controller = TestController;
|
||||
let app = controller.get_router();
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/api/hello")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
|
||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
assert_eq!(body, "hello world");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn controller_get_item() {
|
||||
let controller = TestController;
|
||||
let app = controller.get_router();
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/api/items/42")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
|
||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
assert_eq!(body, "item: 42");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn controller_post_item() {
|
||||
let controller = TestController;
|
||||
let app = controller.get_router();
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/items")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(r#""test item""#))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
|
||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
assert_eq!(body, "added: test item");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn controller_404_on_unknown_route() {
|
||||
let controller = TestController;
|
||||
let app = controller.get_router();
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/api/unknown")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[controller(path = "/api/other")]
|
||||
struct AnotherController;
|
||||
|
||||
impl AnotherController {
|
||||
#[get("/test")]
|
||||
async fn test(&self) -> &'static str {
|
||||
"from another"
|
||||
}
|
||||
}
|
||||
|
||||
impl_routes!(AnotherController, [test]);
|
||||
|
||||
#[tokio::test]
|
||||
async fn merge_different_controllers() {
|
||||
let c1 = TestController;
|
||||
let c2 = AnotherController;
|
||||
|
||||
let app = axum::Router::new()
|
||||
.merge(c1.get_router())
|
||||
.merge(c2.get_router());
|
||||
|
||||
let response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/api/hello")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/api/other/test")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
assert_eq!(body, "from another");
|
||||
}
|
||||
}
|
||||
|
||||
impl TestService1 {
|
||||
pub async fn get_hello_from_1(&self) -> String {
|
||||
string!("hello from service 1")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TestService2 {}
|
||||
|
||||
impl Service for TestService2 {
|
||||
fn deps() -> Deps {
|
||||
vec![dep::<TestService1>()]
|
||||
}
|
||||
|
||||
async fn new(manager: Arc<DependencyManager>) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let service1 = manager.get::<TestService1>(&Self::name()).await.unwrap();
|
||||
|
||||
println!("{}", service1.get_hello_from_1().await);
|
||||
Self {}
|
||||
}
|
||||
|
||||
fn name() -> String {
|
||||
string!("2")
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn test() {
|
||||
let manager = DependencyManager::new();
|
||||
|
||||
let _service1 = manager.register::<TestService1>().await;
|
||||
let _service2 = manager.register::<TestService2>().await;
|
||||
}
|
||||
|
||||
#[controller(path = "/api")]
|
||||
pub struct AppState {}
|
||||
|
||||
impl AppState {
|
||||
#[get("/hello")]
|
||||
pub async fn hello(&self) -> &'static str {
|
||||
"hello world"
|
||||
}
|
||||
|
||||
#[get("/items/{id}")]
|
||||
pub async fn get_item(
|
||||
&self,
|
||||
axum::extract::Path(id): axum::extract::Path<String>,
|
||||
) -> String {
|
||||
format!("item: {}", id)
|
||||
}
|
||||
|
||||
#[post("/items")]
|
||||
pub async fn add_item(
|
||||
&self,
|
||||
axum::Json(body): axum::Json<String>,
|
||||
) -> String {
|
||||
format!("added: {}", body)
|
||||
}
|
||||
}
|
||||
|
||||
impl_routes!(AppState, [hello, get_item, add_item]);
|
||||
|
||||
pub async fn test_controller() {
|
||||
let controller = AppState {};
|
||||
|
||||
let _router: axum::Router = controller.get_router();
|
||||
}
|
||||
|
||||
pub async fn test_merge_controllers() {
|
||||
let c1 = AppState {};
|
||||
let router = axum::Router::new()
|
||||
.merge(c1.get_router());
|
||||
|
||||
let _: axum::Router = router;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user