Skip to content

Commit 7f31984

Browse files
committed
🔵 Refactor test to implement fake data
1 parent d204f2c commit 7f31984

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed

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

Lines changed: 11 additions & 0 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
actix-web = "4"
8+
9+
[dev-dependencies]
10+
fake = "2.4"
11+
rand = "0.8"

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

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn get_unit_price(number_of_subscriptions: u32) -> SubscriptionResult<u32> {
2020

2121
match number_of_subscriptions {
2222
0 => Err(NumberSubscriptionsError),
23-
1|2 => Ok(FIRST_UNIT_PRICE),
23+
1 | 2 => Ok(FIRST_UNIT_PRICE),
2424
3..=10 => Ok(SECOND_UNIT_PRICE),
2525
11..=25 => Ok(THIRD_UNIT_PRICE),
2626
26..=50 => Ok(FOURTH_UNIT_PRICE),
@@ -35,64 +35,70 @@ fn get_total_subscription_price(number_of_subscriptions: u32) -> SubscriptionRes
3535
#[cfg(test)]
3636
mod tests {
3737
use crate::tiered_pricing::{get_total_subscription_price, NumberSubscriptionsError};
38+
use fake::Fake;
3839

3940
#[test]
40-
fn when_get_a_subscription_return_299_euros() -> Result<(), NumberSubscriptionsError> {
41-
assert_eq!(299, get_total_subscription_price(1)?);
42-
Ok(())
43-
}
41+
fn when_subscriptions_are_between_1_and_2_unit_price_is_299(
42+
) -> Result<(), NumberSubscriptionsError> {
43+
let number_of_subscriptions = (1..=2).fake();
4444

45-
#[test]
46-
fn when_get_2_subscriptions_return_598_euros() -> Result<(), NumberSubscriptionsError> {
47-
assert_eq!(598, get_total_subscription_price(2)?);
48-
Ok(())
49-
}
45+
assert_eq!(
46+
number_of_subscriptions * 299,
47+
get_total_subscription_price(number_of_subscriptions)?
48+
);
5049

51-
#[test]
52-
fn when_get_3_subscriptions_return_717_euros() -> Result<(), NumberSubscriptionsError> {
53-
assert_eq!(717, get_total_subscription_price(3)?);
5450
Ok(())
5551
}
5652

5753
#[test]
58-
fn when_get_10_subscriptions_return_2390_euros() -> Result<(), NumberSubscriptionsError> {
59-
assert_eq!(2390, get_total_subscription_price(10)?);
60-
Ok(())
61-
}
54+
fn when_subscriptions_are_between_3_and_10_unit_price_is_239(
55+
) -> Result<(), NumberSubscriptionsError> {
56+
let number_of_subscriptions = (3..=10).fake();
6257

63-
#[test]
64-
fn when_get_11_subscriptions_return_2409_euros() -> Result<(), NumberSubscriptionsError> {
65-
assert_eq!(2409, get_total_subscription_price(11)?);
66-
Ok(())
67-
}
58+
assert_eq!(
59+
number_of_subscriptions * 239,
60+
get_total_subscription_price(number_of_subscriptions)?
61+
);
6862

69-
#[test]
70-
fn when_get_25_subscriptions_return_5475_euros() -> Result<(), NumberSubscriptionsError> {
71-
assert_eq!(5475, get_total_subscription_price(25)?);
7263
Ok(())
7364
}
7465

7566
#[test]
76-
fn when_get_26_subscriptions_return_5174_euros() -> Result<(), NumberSubscriptionsError> {
77-
assert_eq!(5174, get_total_subscription_price(26)?);
78-
Ok(())
79-
}
67+
fn when_subscriptions_are_between_11_and_25_unit_price_is_219(
68+
) -> Result<(), NumberSubscriptionsError> {
69+
let number_of_subscriptions = (11..=25).fake();
70+
71+
assert_eq!(
72+
number_of_subscriptions * 219,
73+
get_total_subscription_price(number_of_subscriptions)?
74+
);
8075

81-
#[test]
82-
fn when_get_50_subscriptions_return_9950_euros() -> Result<(), NumberSubscriptionsError> {
83-
assert_eq!(9950, get_total_subscription_price(50)?);
8476
Ok(())
8577
}
8678

8779
#[test]
88-
fn when_get_51_subscriptions_return_7599_euros() -> Result<(), NumberSubscriptionsError> {
89-
assert_eq!(7599, get_total_subscription_price(51)?);
80+
fn when_subscriptions_are_between_26_and_50_unit_price_is_199(
81+
) -> Result<(), NumberSubscriptionsError> {
82+
let number_of_subscriptions = (26..=50).fake();
83+
84+
assert_eq!(
85+
number_of_subscriptions * 199,
86+
get_total_subscription_price(number_of_subscriptions)?
87+
);
88+
9089
Ok(())
9190
}
9291

9392
#[test]
94-
fn when_get_52_subscriptions_return_7748_euros() -> Result<(), NumberSubscriptionsError> {
95-
assert_eq!(7748, get_total_subscription_price(52)?);
93+
fn when_subscriptions_are_more_than_50_unit_price_is_149(
94+
) -> Result<(), NumberSubscriptionsError> {
95+
let number_of_subscriptions = (51..1000).fake();
96+
97+
assert_eq!(
98+
number_of_subscriptions * 149,
99+
get_total_subscription_price(number_of_subscriptions)?
100+
);
101+
96102
Ok(())
97103
}
98104

0 commit comments

Comments
 (0)