-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_command.py
50 lines (36 loc) · 1.23 KB
/
test_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import unittest
from enum import Enum
class Command:
class Action(Enum):
DEPOSIT = 0
WITHDRAW = 1
def __init__(self, action, amount):
self.action = action
self.amount = amount
self.success = False
class Account:
def __init__(self, balance=0):
self.balance = balance
def process(self, command):
if command.action == Command.Action.DEPOSIT:
self.balance += command.amount
command.success = True
elif command.action == Command.Action.WITHDRAW:
command.success = self.balance >= command.amount
if command.success:
self.balance -= command.amount
class Evaluate(unittest.TestCase):
def test(self):
a = Account()
cmd = Command(Command.Action.DEPOSIT, 100)
a.process(cmd)
self.assertEqual(100, a.balance)
self.assertTrue(cmd.success)
cmd = Command(Command.Action.WITHDRAW, 50)
a.process(cmd)
self.assertEqual(50, a.balance)
self.assertTrue(cmd.success)
cmd.amount = 150
a.process(cmd)
self.assertEqual(50, a.balance)
self.assertFalse(cmd.success)