Skip to content

Commit d71786a

Browse files
committed
feat: using pytest
1 parent acf3d07 commit d71786a

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

src/auction/domain.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@
33

44
class User:
55

6-
def __init__(self, name):
6+
def __init__(self, name, wallet=500.0):
77
self.__name = name
8+
self.__wallet = wallet
89

910
@property
1011
def name(self):
1112
return self.__name
1213

14+
@property
15+
def wallet(self):
16+
return self.__wallet
17+
18+
def propose_bid(self, auction, value):
19+
if value > self.__wallet:
20+
raise ValueError(f'User tried to propose R${value} having only R${self.__wallet}.')
21+
22+
bid = Bid(self, value)
23+
auction.propose(bid)
24+
25+
self.__wallet -= value
26+
1327

1428
class Bid:
1529

@@ -28,7 +42,7 @@ def __init__(self, description):
2842

2943
def propose(self, bid: Bid):
3044
if not self.__bids or self.__bids[-1].user.name != bid.user.name and bid.value > self.__bids[
31-
-1].value:
45+
-1].value:
3246
if bid.value > self.highest_bid:
3347
self.highest_bid = bid.value
3448

tests/__init__.py

Whitespace-only changes.
File renamed without changes.

tests/test_usuario.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
from pytest import raises
3+
4+
from src.auction.domain import User, Auction
5+
6+
7+
@pytest.fixture
8+
def generic_user():
9+
return User('Generic User', 100.0)
10+
11+
12+
@pytest.fixture
13+
def auction():
14+
return Auction('Generic Auction')
15+
16+
17+
def test_should_subtract_value_from_user_wallet_when_proposing_a_bid(generic_user, auction):
18+
generic_user.propose_bid(auction, 50.0)
19+
assert generic_user.wallet == 50.0
20+
21+
22+
def test_should_allow_propose_bid_when_the_value_is_equal_to_the_value_on_wallet(generic_user, auction):
23+
generic_user.propose_bid(auction, 100.0)
24+
assert generic_user.wallet == 0.0
25+
26+
27+
def test_should_not_allow_propose_bid_when_the_value_is_higher_than_the_value_on_wallet(generic_user, auction):
28+
with raises(ValueError):
29+
generic_user.propose_bid(auction, 150.0)

0 commit comments

Comments
 (0)