Skip to content

Commit

Permalink
Merge branch 'master' into cargo-fmt-nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad authored and NexVeridian committed Dec 3, 2024
2 parents c54aa4b + 721755d commit f25add0
Show file tree
Hide file tree
Showing 83 changed files with 1,873 additions and 1,314 deletions.
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

## Unreleased

* fix: guard jwt error behind feature flag. [https://github.com/loco-rs/loco/pull/1032](https://github.com/loco-rs/loco/pull/1032)
* fix: logger file_appender not using the seperated format setting. [https://github.com/loco-rs/loco/pull/1036](https://github.com/loco-rs/loco/pull/1036)
* seed cli command. [https://github.com/loco-rs/loco/pull/1046](https://github.com/loco-rs/loco/pull/1046)
* Updated validator to 0.19. [https://github.com/loco-rs/loco/pull/993](https://github.com/loco-rs/loco/pull/993)
* Testing helpers: simplified function calls + adding html selector. [https://github.com/loco-rs/loco/pull/1047](https://github.com/loco-rs/loco/pull/1047)
### Breaking Changes
#### Updated Import Paths
The testing module import path has been updated. To adapt your code, update imports from:
```rust
use loco_rs::testing;
```
to:
```rust
use testing::prelude::*;
```
#### Simplified Function Calls
Function calls within the testing module no longer require the testing:: prefix. Update your code accordingly. For example:

Before:
```rust
let boot = testing::boot_test::<App>().await.unwrap();
```

After:
```rust
let boot = boot_test::<App>().await.unwrap();
```

## v0.13.2

* static fallback now returns 200 and not 404 [https://github.com/loco-rs/loco/pull/991](https://github.com/loco-rs/loco/pull/991)
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ default = [
]
auth_jwt = ["dep:jsonwebtoken"]
cli = ["dep:clap"]
testing = ["dep:axum-test"]
testing = ["dep:axum-test", "dep:scraper"]
with-db = ["dep:sea-orm", "dep:sea-orm-migration", "loco-gen/with-db"]
# Storage features
all_storage = ["storage_aws_s3", "storage_azure", "storage_gcp"]
Expand Down Expand Up @@ -104,7 +104,7 @@ byte-unit = "4.0.19"
argon2 = { version = "0.5.2", features = ["std"] }
rand = { version = "0.8.5", features = ["std"] }
jsonwebtoken = { version = "9.3.0", optional = true }
validator = { version = "0.18.1", features = ["derive"] }
validator = { version = "0.19.0", features = ["derive"] }
futures-util = "0.3"
tower = { workspace = true }
hyper = "1.1"
Expand Down Expand Up @@ -143,6 +143,8 @@ ulid = { version = "1", optional = true }
rusty-sidekiq = { version = "0.11.0", default-features = false, optional = true }
bb8 = { version = "0.8.1", optional = true }

scraper = { version = "0.21.0", optional = true }

[workspace.dependencies]

chrono = { version = "0.4", features = ["serde"] }
Expand Down
4 changes: 3 additions & 1 deletion docs-site/content/docs/infrastructure/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,12 @@ async fn upload_file(
By testing file storage in your controller you can follow this example:

```rust
use loco_rs::testing::prelude::*;

#[tokio::test]
#[serial]
async fn can_register() {
testing::request::<App, _, _>(|request, ctx| async move {
request::<App, _, _>(|request, ctx| async move {
let file_content = "loco file upload";
let file_part = Part::bytes(file_content.as_bytes()).file_name("loco.txt");

Expand Down
5 changes: 3 additions & 2 deletions docs-site/content/docs/processing/mailers.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,19 @@ Test Description:
- Retrieve the mailer instance from the context and call the deliveries() function, which contains information about the number of sent emails and their content.

```rust
use loco_rs::testing::prelude::*;
#[tokio::test]
#[serial]
async fn can_register() {
configure_insta!();
testing::request::<App, Migrator, _, _>(|request, ctx| async move {
request::<App, Migrator, _, _>(|request, ctx| async move {
// Create a request for user registration.
// Now you can call the context mailer and use the deliveries function.
with_settings!({
filters => testing::cleanup_email()
filters => cleanup_email()
}, {
assert_debug_snapshot!(ctx.mailer.unwrap().deliveries());
});
Expand Down
4 changes: 3 additions & 1 deletion docs-site/content/docs/processing/workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,13 @@ Here's an example of how the test should be structured:


```rust
use loco_rs::testing::prelude::*;
#[tokio::test]
#[serial]
async fn test_run_report_worker_worker() {
// Set up the test environment
let boot = testing::boot_test::<App, Migrator>().await.unwrap();
let boot = boot_test::<App, Migrator>().await.unwrap();
// Execute the worker in 'ForegroundBlocking' mode, preventing it from running asynchronously
assert!(
Expand Down
5 changes: 3 additions & 2 deletions docs-site/content/docs/the-app/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -858,18 +858,19 @@ impl PaginationResponse {
# Testing
When testing controllers, the goal is to call the router's controller endpoint and verify the HTTP response, including the status code, response content, headers, and more.

To initialize a test request, use `testing::request`, which prepares your app routers, providing the request instance and the application context.
To initialize a test request, use `use loco_rs::testing::prelude::*;`, which prepares your app routers, providing the request instance and the application context.

In the following example, we have a POST endpoint that returns the data sent in the POST request.

```rust
use loco_rs::testing::prelude::*;

#[tokio::test]
#[serial]
async fn can_print_echo() {
configure_insta!();

testing::request::<App, _, _>(|request, _ctx| async move {
request::<App, _, _>(|request, _ctx| async move {
let response = request
.post("/example")
.json(&serde_json::json!({"site": "Loco"}))
Expand Down
23 changes: 15 additions & 8 deletions docs-site/content/docs/the-app/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,14 @@ impl Task for SeedData {
2. In your test section, follow the example below:

```rust
use loco_rs::testing::prelude::*;
#[tokio::test]
#[serial]
async fn handle_create_with_password_with_duplicate() {
let boot = testing::boot_test::<App, Migrator>().await;
testing::seed::<App>(&boot.app_context.db).await.unwrap();
let boot = boot_test::<App, Migrator>().await;
seed::<App>(&boot.app_context.db).await.unwrap();
assert!(get_user_by_id(1).ok());
}
```
Expand Down Expand Up @@ -695,11 +697,13 @@ If you used the generator to crate a model migration, you should also have an au
A typical test contains everything you need to set up test data, boot the app, and reset the database automatically before the testing code runs. It looks like this:

```rust
use loco_rs::testing::prelude::*;
async fn can_find_by_pid() {
configure_insta!();
let boot = testing::boot_test::<App, Migrator>().await;
testing::seed::<App>(&boot.app_context.db).await.unwrap();
let boot = boot_test::<App, Migrator>().await;
seed::<App>(&boot.app_context.db).await.unwrap();
let existing_user =
Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111").await;
Expand Down Expand Up @@ -747,13 +751,15 @@ impl Hooks for App {
## Seeding

```rust
use loco_rs::testing::prelude::*;
#[tokio::test]
#[serial]
async fn is_user_exists() {
configure_insta!();
let boot = testing::boot_test::<App, Migrator>().await;
testing::seed::<App>(&boot.app_context.db).await.unwrap();
let boot = boot_test::<App, Migrator>().await;
seed::<App>(&boot.app_context.db).await.unwrap();
assert!(get_user_by_id(1).ok());
}
Expand All @@ -770,14 +776,15 @@ Example using [insta](https://crates.io/crates/insta) for snapshots.
in the following example you can use `cleanup_user_model` which clean all user model data.

```rust
use loco_rs::testing::prelude::*;
#[tokio::test]
#[serial]
async fn can_create_user() {
testing::request::<App, Migrator, _, _>(|request, _ctx| async move {
request::<App, Migrator, _, _>(|request, _ctx| async move {
// create user test
with_settings!({
filters => testing::cleanup_user_model()
filters => cleanup_user_model()
}, {
assert_debug_snapshot!(current_user_request.text());
});
Expand Down
Loading

0 comments on commit f25add0

Please sign in to comment.