-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_task_1.py
95 lines (73 loc) · 2.84 KB
/
test_task_1.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Tests for task 1."""
from task_1 import ChildMoocher, Getter, HelicopterParent, RichParent, Setter
class TestHelicopterParent:
"""Test for sub-class registration."""
def test_new_subclass(self):
"""The subclass should show in in the list."""
class SubClass(HelicopterParent):
pass
assert SubClass in HelicopterParent.children
class TestGetter:
"""Tests for dynamic attributes."""
def test_c_updates(self):
"""Ensure c updates when a or b are changed."""
get = Getter(a=2, b=2)
assert get.c == 4
# ensure changing a also updates c
get.a = 3
assert get.c == 5
# and b too
get.b = 10
assert get.c == 13
class TestSetter:
"""Test previous values stored."""
def test_list_changes_size(self):
"""Ensure the logging list changes size."""
set = Setter(a=10)
assert len(set.old_values_of_a) == 0
# now update a
set.a = 12
assert set.old_values_of_a == [10]
# do it again
set.a = 22
assert set.old_values_of_a == [10, 12]
class TestSpoiledKids:
"""Test inheritance between rich parent and moocher child."""
def sum_assets(self, obj):
"""Sum up asset value."""
out = []
for name in dir(obj):
if name.startswith("asset_"):
out.append(getattr(obj, name))
return sum(out)
def test_parent_get_net_worth(self):
"""Test that the parent networth is determined."""
parent = RichParent()
assert parent.get_net_worth() > 0
assert parent.get_net_worth() == self.sum_assets(parent)
def test_child_net_worth(self):
"""Ensure the child's net worth is calculated as expected."""
child = ChildMoocher()
parent = RichParent()
assert child.get_net_worth() == parent.get_net_worth() / child.siblings
# change siblings and it should still be true
child.siblings = 12
assert child.get_net_worth() == parent.get_net_worth() / child.siblings
# but adding parent siblings shouldnt do anyting.
parent.siblings = 3
assert child.get_net_worth() == parent.get_net_worth() / child.siblings
def test_child_networth_actually_calls_parent_net_worth(self, monkeypatch):
"""Test child.get_net_worth actually calls parent.get_net_worth."""
state = {"call_count": 0}
def instrument(func):
def _new_func(*args, **kwargs):
state["call_count"] += 1
return func(*args, **kwargs)
return _new_func
monkeypatch.setattr(
RichParent, "get_net_worth", instrument(RichParent.get_net_worth)
)
child = ChildMoocher()
assert state["call_count"] == 0
child.get_net_worth()
assert state["call_count"] == 1