Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Reorganize test modules
Browse files Browse the repository at this point in the history
  • Loading branch information
0rzech committed Feb 27, 2024
1 parent 4de3c51 commit 65dd9f8
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/domain/subscriber_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl AsRef<str> 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]
Expand Down Expand Up @@ -74,7 +74,7 @@ mod tests {
assert_err!(result);
}

mod valid_emails {
mod helpers {
use fake::{
faker::internet::en::{FreeEmail, SafeEmail},
Fake,
Expand Down
3 changes: 1 addition & 2 deletions src/domain/subscriber_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ impl AsRef<str> 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
Expand Down
28 changes: 12 additions & 16 deletions src/email_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions tests/health_check.rs → tests/api/health_check.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions tests/api/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod health_check;
mod helpers;
mod subscriptions;
14 changes: 7 additions & 7 deletions tests/subscriptions.rs → tests/api/subscriptions.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"),
(
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 65dd9f8

Please sign in to comment.