From 65dd9f8848ed15edc796519fcf0398864cc26a3f Mon Sep 17 00:00:00 2001 From: Piotr Orzechowski Date: Tue, 27 Feb 2024 12:27:24 +0100 Subject: [PATCH] Reorganize test modules --- src/domain/subscriber_email.rs | 4 ++-- src/domain/subscriber_name.rs | 3 +-- src/email_client.rs | 28 ++++++++++++--------------- tests/{ => api}/health_check.rs | 6 +++--- tests/{util/mod.rs => api/helpers.rs} | 0 tests/api/main.rs | 3 +++ tests/{ => api}/subscriptions.rs | 14 +++++++------- 7 files changed, 28 insertions(+), 30 deletions(-) rename tests/{ => api}/health_check.rs (74%) rename tests/{util/mod.rs => api/helpers.rs} (100%) create mode 100644 tests/api/main.rs rename tests/{ => api}/subscriptions.rs (90%) diff --git a/src/domain/subscriber_email.rs b/src/domain/subscriber_email.rs index 9b99d87..1d1ac13 100644 --- a/src/domain/subscriber_email.rs +++ b/src/domain/subscriber_email.rs @@ -24,8 +24,8 @@ impl AsRef for SubscriberEmail { mod tests { use super::SubscriberEmail; use claims::{assert_err, assert_ok}; + use helpers::valid_emails; use proptest::prelude::proptest; - use valid_emails::valid_emails; proptest! { #[test] @@ -74,7 +74,7 @@ mod tests { assert_err!(result); } - mod valid_emails { + mod helpers { use fake::{ faker::internet::en::{FreeEmail, SafeEmail}, Fake, diff --git a/src/domain/subscriber_name.rs b/src/domain/subscriber_name.rs index f9304bc..5d370ef 100644 --- a/src/domain/subscriber_name.rs +++ b/src/domain/subscriber_name.rs @@ -34,11 +34,10 @@ impl AsRef for SubscriberName { #[cfg(test)] mod tests { + use super::FORBIDDEN_CHARS; use crate::domain::SubscriberName; use claims::{assert_err, assert_ok}; - use super::FORBIDDEN_CHARS; - #[test] fn a_valid_name_is_parsed_successfully() { // given diff --git a/src/email_client.rs b/src/email_client.rs index 65c4672..ff28850 100644 --- a/src/email_client.rs +++ b/src/email_client.rs @@ -73,9 +73,8 @@ struct SendEmailRequest<'a> { #[cfg(test)] mod tests { use claims::{assert_err, assert_ok}; - use matcher::SendEmailBodyMatcher; + use helpers::{content, email, email_client, subject, SendEmailBodyMatcher}; use std::time::Duration; - use util::*; use wiremock::{ matchers::{any, header, header_exists, method, path}, Mock, MockServer, ResponseTemplate, @@ -148,8 +147,18 @@ mod tests { assert_err!(response); } - mod matcher { + mod helpers { + use crate::{domain::SubscriberEmail, email_client::EmailClient}; + use fake::{ + faker::{ + internet::en::SafeEmail, + lorem::en::{Paragraph, Sentence}, + }, + Fake, Faker, + }; + use secrecy::Secret; use serde_json::{from_slice, Value}; + use std::time::Duration; use wiremock::{Match, Request}; pub struct SendEmailBodyMatcher; @@ -167,19 +176,6 @@ mod tests { } } } - } - - mod util { - use crate::{domain::SubscriberEmail, email_client::EmailClient}; - use fake::{ - faker::{ - internet::en::SafeEmail, - lorem::en::{Paragraph, Sentence}, - }, - Fake, Faker, - }; - use secrecy::Secret; - use std::time::Duration; pub fn email_client(base_url: String) -> EmailClient { EmailClient::new( diff --git a/tests/health_check.rs b/tests/api/health_check.rs similarity index 74% rename from tests/health_check.rs rename to tests/api/health_check.rs index 4be2d83..70cec18 100644 --- a/tests/health_check.rs +++ b/tests/api/health_check.rs @@ -1,11 +1,11 @@ -mod util; +use crate::helpers::{spawn_app, url}; #[tokio::test] async fn health_check_works() { // given let client = reqwest::Client::new(); - let app = util::spawn_app().await; - let url = util::url(app.address, "health_check"); + let app = spawn_app().await; + let url = url(app.address, "health_check"); // when let response = client diff --git a/tests/util/mod.rs b/tests/api/helpers.rs similarity index 100% rename from tests/util/mod.rs rename to tests/api/helpers.rs diff --git a/tests/api/main.rs b/tests/api/main.rs new file mode 100644 index 0000000..3b9c227 --- /dev/null +++ b/tests/api/main.rs @@ -0,0 +1,3 @@ +mod health_check; +mod helpers; +mod subscriptions; diff --git a/tests/subscriptions.rs b/tests/api/subscriptions.rs similarity index 90% rename from tests/subscriptions.rs rename to tests/api/subscriptions.rs index c4047c7..a78a572 100644 --- a/tests/subscriptions.rs +++ b/tests/api/subscriptions.rs @@ -1,12 +1,12 @@ -mod util; +use crate::helpers::{spawn_app, url}; #[tokio::test] async fn subscribe_returns_a_200_for_valid_form_data() { // given let client = reqwest::Client::new(); let body = "name=Imi%C4%99%20Nazwisko&email=imie.nazwisko%40example.com"; - let app = util::spawn_app().await; - let url = util::url(app.address, "subscriptions"); + let app = spawn_app().await; + let url = url(app.address, "subscriptions"); // when let response = client @@ -31,8 +31,8 @@ async fn subscribe_returns_a_200_for_valid_form_data() { async fn subscribe_returns_a_400_when_fields_are_present_but_empty() { // given let client = reqwest::Client::new(); - let app = util::spawn_app().await; - let url = util::url(app.address, "subscriptions"); + let app = spawn_app().await; + let url = url(app.address, "subscriptions"); let test_cases = vec![ ("name=Imi%C4%99%20Nazwisko&email=", "empty email"), ( @@ -67,8 +67,8 @@ async fn subscribe_returns_a_400_when_fields_are_present_but_empty() { async fn subscribe_returns_a_400_when_data_is_missing() { // given let client = reqwest::Client::new(); - let app = util::spawn_app().await; - let url = util::url(app.address, "subscriptions"); + let app = spawn_app().await; + let url = url(app.address, "subscriptions"); let test_cases = vec![ ("name=Imi%C4%99%20Nazwisko", "missing the email"), ("email=imie.nazwisko%40example.com", "missing the name"),