Skip to content

Commit 4397670

Browse files
committed
Add exmaple for testing context-related issues with extend suite
1 parent 075aebb commit 4397670

6 files changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Why another bank_account example?
2+
This is a variation of the bank account, where it was divided to several files, so it won't be as easy to get
3+
the context.
4+
Specifically, it's for testing extend-suite, which currently doesn't know how to bring in context for things that the
5+
CUT uses - so we need to add the context manually.
6+
7+
The functionality has also been greatly reduced, so we'll always get relevant tests.
8+
Specifically - the premium account has 3 attributes - commision size, max deposit and max balance.
9+
And the values of all these attributes are in the separate file - so no matter what, we'll always
10+
have tests that check for them and have the wrong values if we don't manually add the context.
11+
12+
There's a top-level-functions test file and a class-test file, so we can test both.

examples/bank_account_3_context_games/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from examples.bank_account_3_context_games.bank_account_3_helpers import calc_commission_rate, is_deposit_over_balance, is_more_than_max_deposit
2+
3+
4+
class BankAccount3:
5+
""" Create a new bank account """
6+
def __init__(self, name, is_premium_account):
7+
self._name = name
8+
self._is_premium_account = is_premium_account
9+
self._balance = 0
10+
11+
def deposit(self, amount):
12+
""" deposit money """
13+
if amount <= 0:
14+
raise IOError("deposit amount must be larger than 0")
15+
16+
if is_more_than_max_deposit(self._is_premium_account, amount):
17+
raise ValueError("deposit amount exceeds maximum allowed")
18+
19+
if is_deposit_over_balance(self._is_premium_account, amount, self._balance):
20+
raise ValueError("deposit amount will exceed maximum allowed balance")
21+
22+
self._balance += amount - calc_commission_rate(self._is_premium_account)
23+
24+
def balance(self):
25+
return self._balance
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
MAX_DEPOSIT_PREMIUM = 1_000_000
2+
MAX_DEPOSIT_NON_PREMIUM = 10_000
3+
4+
MAX_BALANCE_PREMIUM = 10_000_000
5+
MAX_BALANCE_NON_PREMIUM = 100_000
6+
7+
def calc_commission_rate(is_premium_account):
8+
if is_premium_account:
9+
return 1.5
10+
else:
11+
return 8
12+
13+
14+
def is_more_than_max_deposit(is_premium_account, deposit_amount):
15+
if is_premium_account:
16+
return deposit_amount > MAX_DEPOSIT_PREMIUM
17+
else:
18+
return deposit_amount > MAX_DEPOSIT_NON_PREMIUM
19+
20+
21+
def is_deposit_over_balance(is_premium_account, deposit_amount, balance):
22+
expected_balance = balance + deposit_amount - calc_commission_rate(is_premium_account)
23+
24+
if is_premium_account:
25+
return expected_balance > MAX_BALANCE_PREMIUM
26+
else:
27+
return expected_balance > MAX_BALANCE_NON_PREMIUM
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
from examples.bank_account_3_context_games.bank_account_3 import BankAccount3
3+
4+
5+
class TestBankAccountSimpson:
6+
def setup_method(self):
7+
self.homer = BankAccount3("Homer", True)
8+
self.marge = BankAccount3("Marge", False)
9+
10+
def test_deposit_with_commission_discount_true(self):
11+
initial_balance = self.homer.balance()
12+
deposit_amount = 100
13+
expected_commission = 1.5
14+
expected_balance = initial_balance + deposit_amount - expected_commission
15+
16+
self.homer.deposit(deposit_amount)
17+
18+
assert self.homer.balance() == expected_balance
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from examples.bank_account_3_context_games.bank_account_3 import BankAccount3
2+
3+
def test_deposit_with_premium_account_true():
4+
homer = BankAccount3("Homer", True)
5+
initial_balance = homer.balance()
6+
deposit_amount = 100
7+
expected_commission = 1.5
8+
expected_balance = initial_balance + deposit_amount - expected_commission
9+
10+
homer.deposit(deposit_amount)
11+
12+
assert homer.balance() == expected_balance

0 commit comments

Comments
 (0)