✅ (tests): Adapta la suite al nuevo framework web
- Sustituye `service::test::*` por `web::test::*` (migración de actix-web a
axum).
- Extrae `setup()` en los módulos que sólo renderizan componentes,
evitando levantar un router completo en cada test.
- Elimina los `env::set_var("PAGETOP_RUN_MODE", "test")` manuales, ya
cubiertos por la *feature* `testing`.
This commit is contained in:
parent
87e4eac27c
commit
eb18690a5c
6 changed files with 45 additions and 53 deletions
|
|
@ -46,18 +46,20 @@ async fn component_html_default_renders_empty_markup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn component_html_can_access_http_method() {
|
async fn component_html_can_access_request_path() {
|
||||||
let req = service::test::TestRequest::with_uri("/").to_http_request();
|
let req = web::test::TestRequest::get()
|
||||||
|
.uri("/hello/world")
|
||||||
|
.to_http_request();
|
||||||
let mut cx = Context::new(Some(req));
|
let mut cx = Context::new(Some(req));
|
||||||
|
|
||||||
let mut component = Html::with(|cx| {
|
let mut component = Html::with(|cx| {
|
||||||
let method = cx
|
let path = cx
|
||||||
.request()
|
.request()
|
||||||
.map(|r| r.method().to_string())
|
.map(|r| r.path().to_string())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
html! { span { (method) } }
|
html! { span { (path) } }
|
||||||
});
|
});
|
||||||
|
|
||||||
let markup = component.render(&mut cx);
|
let markup = component.render(&mut cx);
|
||||||
assert_eq!(markup.0, "<span>GET</span>");
|
assert_eq!(markup.0, "<span>/hello/world</span>");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,16 @@
|
||||||
use pagetop::prelude::*;
|
use pagetop::prelude::*;
|
||||||
|
|
||||||
|
/// Inicializa PageTop (locale, extensiones…) una sola vez para toda la suite.
|
||||||
|
///
|
||||||
|
/// Los tests de este módulo renderizan componentes directamente con `Context::default()`, por lo
|
||||||
|
/// que sólo necesitan el subsistema de localización y las extensiones registradas, no un router.
|
||||||
|
fn setup() {
|
||||||
|
let _ = Application::new();
|
||||||
|
}
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn poweredby_default_shows_only_pagetop_recognition() {
|
async fn poweredby_default_shows_only_pagetop_recognition() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
setup();
|
||||||
|
|
||||||
let mut p = PoweredBy::default();
|
let mut p = PoweredBy::default();
|
||||||
let html = p.render(&mut Context::default());
|
let html = p.render(&mut Context::default());
|
||||||
|
|
@ -16,7 +24,7 @@ async fn poweredby_default_shows_only_pagetop_recognition() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn poweredby_new_includes_current_year_and_app_name() {
|
async fn poweredby_new_includes_current_year_and_app_name() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
setup();
|
||||||
|
|
||||||
let mut p = PoweredBy::new();
|
let mut p = PoweredBy::new();
|
||||||
let html = p.render(&mut Context::default());
|
let html = p.render(&mut Context::default());
|
||||||
|
|
@ -40,7 +48,7 @@ async fn poweredby_new_includes_current_year_and_app_name() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn poweredby_with_copyright_overrides_text() {
|
async fn poweredby_with_copyright_overrides_text() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
setup();
|
||||||
|
|
||||||
let custom = "2001 © FooBar Inc.";
|
let custom = "2001 © FooBar Inc.";
|
||||||
let mut p = PoweredBy::default().with_copyright(Some(custom));
|
let mut p = PoweredBy::default().with_copyright(Some(custom));
|
||||||
|
|
@ -52,7 +60,7 @@ async fn poweredby_with_copyright_overrides_text() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn poweredby_with_copyright_none_hides_text() {
|
async fn poweredby_with_copyright_none_hides_text() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
setup();
|
||||||
|
|
||||||
let mut p = PoweredBy::new().with_copyright(None::<String>);
|
let mut p = PoweredBy::new().with_copyright(None::<String>);
|
||||||
let html = p.render(&mut Context::default());
|
let html = p.render(&mut Context::default());
|
||||||
|
|
@ -64,7 +72,7 @@ async fn poweredby_with_copyright_none_hides_text() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn poweredby_link_points_to_crates_io() {
|
async fn poweredby_link_points_to_crates_io() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
setup();
|
||||||
|
|
||||||
let mut p = PoweredBy::default();
|
let mut p = PoweredBy::default();
|
||||||
let html = p.render(&mut Context::default());
|
let html = p.render(&mut Context::default());
|
||||||
|
|
@ -77,7 +85,7 @@ async fn poweredby_link_points_to_crates_io() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn poweredby_getter_reflects_internal_state() {
|
async fn poweredby_getter_reflects_internal_state() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
setup();
|
||||||
|
|
||||||
// Por defecto no hay copyright.
|
// Por defecto no hay copyright.
|
||||||
let p0 = PoweredBy::default();
|
let p0 = PoweredBy::default();
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ use pagetop::prelude::*;
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
include_config!(SETTINGS: Settings => [
|
include_config!(SETTINGS: Settings => [
|
||||||
"test.string_value" => "Test String",
|
"test.string_value" => "Test String",
|
||||||
"test.int_value" => -321,
|
"test.int_value" => -321,
|
||||||
|
|
@ -22,10 +20,12 @@ pub struct Test {
|
||||||
pub float_value: f32,
|
pub float_value: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// La *feature* `testing` (activo con `cargo ts` / `cargo tw`) fija el modo "test" en tiempo de
|
||||||
|
// compilación dentro de `config::CONFIG_VALUES`, de forma que `global::SETTINGS` y cualquier
|
||||||
|
// `include_config!` local cargan automáticamente la configuración del modo "test".
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn check_global_config() {
|
async fn check_global_config() {
|
||||||
env::set_var("PAGETOP_RUN_MODE", "test");
|
|
||||||
|
|
||||||
assert_eq!(global::SETTINGS.app.run_mode, "test");
|
assert_eq!(global::SETTINGS.app.run_mode, "test");
|
||||||
assert_eq!(global::SETTINGS.app.name, "Testing");
|
assert_eq!(global::SETTINGS.app.name, "Testing");
|
||||||
assert_eq!(global::SETTINGS.server.bind_port, 9000);
|
assert_eq!(global::SETTINGS.server.bind_port, 9000);
|
||||||
|
|
@ -33,8 +33,6 @@ async fn check_global_config() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn check_local_config() {
|
async fn check_local_config() {
|
||||||
env::set_var("PAGETOP_RUN_MODE", "test");
|
|
||||||
|
|
||||||
assert_eq!(SETTINGS.test.string_value, "Modified value");
|
assert_eq!(SETTINGS.test.string_value, "Modified value");
|
||||||
assert_eq!(SETTINGS.test.int_value, -321);
|
assert_eq!(SETTINGS.test.int_value, -321);
|
||||||
assert_eq!(SETTINGS.test.float_value, 8.7654);
|
assert_eq!(SETTINGS.test.float_value, 8.7654);
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use pagetop::prelude::*;
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn literal_text() {
|
async fn literal_text() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let l10n = L10n::n("© 2025 PageTop");
|
let l10n = L10n::n("© 2025 PageTop");
|
||||||
assert_eq!(l10n.get(), Some("© 2025 PageTop".to_string()));
|
assert_eq!(l10n.get(), Some("© 2025 PageTop".to_string()));
|
||||||
|
|
@ -10,7 +10,7 @@ async fn literal_text() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn translation_without_args() {
|
async fn translation_without_args() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let l10n = L10n::l("test_hello_world");
|
let l10n = L10n::l("test_hello_world");
|
||||||
let translation = l10n.lookup(&Locale::resolve("es-ES"));
|
let translation = l10n.lookup(&Locale::resolve("es-ES"));
|
||||||
|
|
@ -19,7 +19,7 @@ async fn translation_without_args() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn translation_with_args() {
|
async fn translation_with_args() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let l10n = L10n::l("test_hello_user").with_arg("userName", "Manuel");
|
let l10n = L10n::l("test_hello_user").with_arg("userName", "Manuel");
|
||||||
let translation = l10n.lookup(&Locale::resolve("es-ES"));
|
let translation = l10n.lookup(&Locale::resolve("es-ES"));
|
||||||
|
|
@ -28,7 +28,7 @@ async fn translation_with_args() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn translation_with_plural_and_select() {
|
async fn translation_with_plural_and_select() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let l10n = L10n::l("test_shared_photos").with_args(vec![
|
let l10n = L10n::l("test_shared_photos").with_args(vec![
|
||||||
("userName", "Roberto"),
|
("userName", "Roberto"),
|
||||||
|
|
@ -41,7 +41,7 @@ async fn translation_with_plural_and_select() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn check_fallback_language() {
|
async fn check_fallback_language() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let l10n = L10n::l("test_hello_world");
|
let l10n = L10n::l("test_hello_world");
|
||||||
let translation = l10n.lookup(&Locale::resolve("xx-YY")); // Retrocede a "en-US".
|
let translation = l10n.lookup(&Locale::resolve("xx-YY")); // Retrocede a "en-US".
|
||||||
|
|
@ -50,7 +50,7 @@ async fn check_fallback_language() {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn check_unknown_key() {
|
async fn check_unknown_key() {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let l10n = L10n::l("non-existent-key");
|
let l10n = L10n::l("non-existent-key");
|
||||||
let translation = l10n.lookup(&Locale::resolve("en-US"));
|
let translation = l10n.lookup(&Locale::resolve("en-US"));
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,11 @@ use pagetop::prelude::*;
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn homepage_returns_404() {
|
async fn homepage_returns_404() {
|
||||||
let app = service::test::init_service(Application::new().test()).await;
|
let app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let req = service::test::TestRequest::get().uri("/").to_request();
|
let req = web::test::TestRequest::get().uri("/").to_request();
|
||||||
let resp = service::test::call_service(&app, req).await;
|
let resp = web::test::send_request(&app, req).await;
|
||||||
|
|
||||||
// Comprueba el acceso a la ruta de inicio.
|
// Comprueba el acceso a la ruta de inicio.
|
||||||
assert_eq!(resp.status(), service::http::StatusCode::OK);
|
assert_eq!(resp.status(), web::http::StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use pagetop::prelude::*;
|
use pagetop::prelude::*;
|
||||||
|
|
||||||
use std::{borrow::Cow, env, fs, io};
|
use std::{borrow::Cow, fs, io};
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
// **< Testing normalize_ascii() >******************************************************************
|
// **< Testing normalize_ascii() >******************************************************************
|
||||||
|
|
@ -265,7 +265,7 @@ mod unix {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn ok_absolute_dir() -> io::Result<()> {
|
async fn ok_absolute_dir() -> io::Result<()> {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
// /tmp/<rand>/sub
|
// /tmp/<rand>/sub
|
||||||
let td = TempDir::new()?;
|
let td = TempDir::new()?;
|
||||||
|
|
@ -279,21 +279,13 @@ mod unix {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn ok_relative_dir_with_manifest() -> io::Result<()> {
|
async fn ok_relative_dir_with_manifest() -> io::Result<()> {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let td = TempDir::new()?;
|
let td = TempDir::new()?;
|
||||||
let sub = td.path().join("sub");
|
let sub = td.path().join("sub");
|
||||||
fs::create_dir(&sub)?;
|
fs::create_dir(&sub)?;
|
||||||
|
|
||||||
// Fija CARGO_MANIFEST_DIR para que "sub" se resuelva contra td.path()
|
let res = util::resolve_absolute_dir_with_base("sub", Some(td.path().to_path_buf()));
|
||||||
let prev_manifest_dir = env::var_os("CARGO_MANIFEST_DIR");
|
|
||||||
env::set_var("CARGO_MANIFEST_DIR", td.path());
|
|
||||||
let res = util::resolve_absolute_dir("sub");
|
|
||||||
// Restaura entorno.
|
|
||||||
match prev_manifest_dir {
|
|
||||||
Some(v) => env::set_var("CARGO_MANIFEST_DIR", v),
|
|
||||||
None => env::remove_var("CARGO_MANIFEST_DIR"),
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(res?, std::fs::canonicalize(&sub)?);
|
assert_eq!(res?, std::fs::canonicalize(&sub)?);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -301,7 +293,7 @@ mod unix {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn error_not_a_directory() -> io::Result<()> {
|
async fn error_not_a_directory() -> io::Result<()> {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let td = TempDir::new()?;
|
let td = TempDir::new()?;
|
||||||
let file = td.path().join("foo.txt");
|
let file = td.path().join("foo.txt");
|
||||||
|
|
@ -319,7 +311,7 @@ mod windows {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn ok_absolute_dir() -> io::Result<()> {
|
async fn ok_absolute_dir() -> io::Result<()> {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
// C:\Users\...\Temp\...
|
// C:\Users\...\Temp\...
|
||||||
let td = TempDir::new()?;
|
let td = TempDir::new()?;
|
||||||
|
|
@ -333,21 +325,13 @@ mod windows {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn ok_relative_dir_with_manifest() -> io::Result<()> {
|
async fn ok_relative_dir_with_manifest() -> io::Result<()> {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let td = TempDir::new()?;
|
let td = TempDir::new()?;
|
||||||
let sub = td.path().join("sub");
|
let sub = td.path().join("sub");
|
||||||
fs::create_dir(&sub)?;
|
fs::create_dir(&sub)?;
|
||||||
|
|
||||||
// Fija CARGO_MANIFEST_DIR para que "sub" se resuelva contra td.path()
|
let res = resolve_absolute_dir_with_base("sub", Some(td.path().to_path_buf()));
|
||||||
let prev_manifest_dir = env::var_os("CARGO_MANIFEST_DIR");
|
|
||||||
env::set_var("CARGO_MANIFEST_DIR", td.path());
|
|
||||||
let res = util::resolve_absolute_dir("sub");
|
|
||||||
// Restaura entorno.
|
|
||||||
match prev_manifest_dir {
|
|
||||||
Some(v) => env::set_var("CARGO_MANIFEST_DIR", v),
|
|
||||||
None => env::remove_var("CARGO_MANIFEST_DIR"),
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(res?, std::fs::canonicalize(&sub)?);
|
assert_eq!(res?, std::fs::canonicalize(&sub)?);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -355,7 +339,7 @@ mod windows {
|
||||||
|
|
||||||
#[pagetop::test]
|
#[pagetop::test]
|
||||||
async fn error_not_a_directory() -> io::Result<()> {
|
async fn error_not_a_directory() -> io::Result<()> {
|
||||||
let _app = service::test::init_service(Application::new().test()).await;
|
let _app = web::test::init_router(Application::new().test());
|
||||||
|
|
||||||
let td = TempDir::new()?;
|
let td = TempDir::new()?;
|
||||||
let file = td.path().join("foo.txt");
|
let file = td.path().join("foo.txt");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue