Skip to content

Commit 57ff935

Browse files
committed
🔴 Add actix web integration test
1 parent 7f31984 commit 57ff935

File tree

6 files changed

+88
-29
lines changed

6 files changed

+88
-29
lines changed

exercises/tiered_pricing/solutions/isaac1024_baby-steps/Cargo.lock

Lines changed: 42 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exercises/tiered_pricing/solutions/isaac1024_baby-steps/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ edition = "2021"
55

66
[dependencies]
77
actix-web = "4"
8+
serde = { version = "1.0", features = ["derive"] }
9+
serde_json = "1.0"
810

911
[dev-dependencies]
1012
fake = "2.4"

exercises/tiered_pricing/solutions/isaac1024_baby-steps/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Rust base skeleton for TDD katas
1+
# Tiered pricing kata with rust
22

33
## Run app
44

5-
`make up` default url: http://localhost:8000/health-check
5+
`make up` url example: http://localhost:8000/pricing?subscriptions=1
66

77
## Run tests
88

exercises/tiered_pricing/solutions/isaac1024_baby-steps/src/health_check.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ pub async fn health_check() -> impl Responder {
77
#[cfg(test)]
88
mod tests {
99
use super::*;
10-
use actix_web::{App, test, web};
10+
use actix_web::{test, web, App};
1111

1212
#[actix_web::test]
1313
async fn health_check_return_ok() {
14-
let app = test::init_service(
15-
App::new().route("/health-check", web::get().to(health_check))
16-
).await;
14+
let app =
15+
test::init_service(App::new().route("/health-check", web::get().to(health_check)))
16+
.await;
1717

1818
let request = test::TestRequest::get().uri("/health-check").to_request();
1919

2020
let response = test::call_service(&app, request).await;
2121

22-
println!("{:?}", response.status());
2322
assert!(response.status().is_success());
2423
}
2524
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use actix_web::web;
21
use crate::health_check::health_check;
2+
use crate::tiered_pricing::tiered_pricing;
3+
use actix_web::web;
34

45
pub fn routes(cfg: &mut web::ServiceConfig) {
56
cfg.route("/health-check", web::get().to(health_check));
7+
cfg.route("/pricing", web::get().to(tiered_pricing));
68
}

exercises/tiered_pricing/solutions/isaac1024_baby-steps/src/tiered_pricing.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
use actix_web::{HttpResponse, Responder};
2+
use serde::{Deserialize, Serialize};
13
use std::fmt;
24

5+
#[derive(Serialize, Deserialize)]
6+
struct Subscriptions {
7+
pricing: u32,
8+
}
9+
10+
pub async fn tiered_pricing() -> impl Responder {
11+
HttpResponse::Ok().json(Subscriptions { pricing: 0 })
12+
}
13+
314
type SubscriptionResult<T> = Result<T, NumberSubscriptionsError>;
415

516
#[derive(Debug)]
@@ -34,11 +45,12 @@ fn get_total_subscription_price(number_of_subscriptions: u32) -> SubscriptionRes
3445

3546
#[cfg(test)]
3647
mod tests {
37-
use crate::tiered_pricing::{get_total_subscription_price, NumberSubscriptionsError};
48+
use super::*;
49+
use actix_web::{test, web, App};
3850
use fake::Fake;
3951

4052
#[test]
41-
fn when_subscriptions_are_between_1_and_2_unit_price_is_299(
53+
async fn when_subscriptions_are_between_1_and_2_unit_price_is_299(
4254
) -> Result<(), NumberSubscriptionsError> {
4355
let number_of_subscriptions = (1..=2).fake();
4456

@@ -51,7 +63,7 @@ mod tests {
5163
}
5264

5365
#[test]
54-
fn when_subscriptions_are_between_3_and_10_unit_price_is_239(
66+
async fn when_subscriptions_are_between_3_and_10_unit_price_is_239(
5567
) -> Result<(), NumberSubscriptionsError> {
5668
let number_of_subscriptions = (3..=10).fake();
5769

@@ -64,7 +76,7 @@ mod tests {
6476
}
6577

6678
#[test]
67-
fn when_subscriptions_are_between_11_and_25_unit_price_is_219(
79+
async fn when_subscriptions_are_between_11_and_25_unit_price_is_219(
6880
) -> Result<(), NumberSubscriptionsError> {
6981
let number_of_subscriptions = (11..=25).fake();
7082

@@ -77,7 +89,7 @@ mod tests {
7789
}
7890

7991
#[test]
80-
fn when_subscriptions_are_between_26_and_50_unit_price_is_199(
92+
async fn when_subscriptions_are_between_26_and_50_unit_price_is_199(
8193
) -> Result<(), NumberSubscriptionsError> {
8294
let number_of_subscriptions = (26..=50).fake();
8395

@@ -90,7 +102,7 @@ mod tests {
90102
}
91103

92104
#[test]
93-
fn when_subscriptions_are_more_than_50_unit_price_is_149(
105+
async fn when_subscriptions_are_more_than_50_unit_price_is_149(
94106
) -> Result<(), NumberSubscriptionsError> {
95107
let number_of_subscriptions = (51..1000).fake();
96108

@@ -103,7 +115,23 @@ mod tests {
103115
}
104116

105117
#[test]
106-
fn when_get_0_subscriptions_return_error() {
118+
async fn when_get_0_subscriptions_return_error() {
107119
assert!(get_total_subscription_price(0).is_err());
108120
}
121+
122+
#[actix_web::test]
123+
async fn when_send_n_subscriptions_return_price() {
124+
let number_of_subscriptions: u32 = (51..1000).fake();
125+
126+
let app =
127+
test::init_service(App::new().route("/pricing", web::get().to(tiered_pricing))).await;
128+
129+
let request = test::TestRequest::get()
130+
.uri(format!("/pricing?subscriptions={}", number_of_subscriptions).as_str())
131+
.to_request();
132+
133+
let response: Subscriptions = test::call_and_read_body_json(&app, request).await;
134+
135+
assert_eq!(number_of_subscriptions * 149, response.pricing);
136+
}
109137
}

0 commit comments

Comments
 (0)