-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearner_test.py
115 lines (85 loc) · 3.18 KB
/
learner_test.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
import contextlib
import mock
import unittest
import learner
class LearningStateTest(unittest.TestCase):
def testInit(self):
state = learner.LearningState(generation=5, individuals=['ubiety'])
self.assertEquals(5, state._generation)
self.assertEquals(['ubiety'], state._individuals)
def testGetGeneration(self):
state = learner.LearningState()
# Forcibly set the private member to keep the test hermetic.
state._generation = 30
self.assertEquals(30, state.generation)
def testGetIndividuals(self):
state = learner.LearningState()
# Forcibly set the private member to keep the test hermetic.
state._individuals = ['nihility']
self.assertEquals(['nihility'], state.individuals)
def testSetGeneration(self):
state = learner.LearningState()
state.generation = 30
# Check the private member to keep the test hermetic.
self.assertEquals(30, state._generation)
def testSetIndividuals(self):
state = learner.LearningState()
state.individuals = ['nihility']
# Check the private member to keep the test hermetic.
self.assertEquals(['nihility'], state._individuals)
class _FakeState(object):
"""Simple fake implementation of LearningState."""
def __init__(self, *unused_args, **unused_kwargs):
self.generation = 0
def __eq__(self, other):
if isinstance(other, type(self)):
return True
return NotImplemented
@mock.patch.object(learner, 'LearningState', new=_FakeState)
class LearnerTest(unittest.TestCase):
def setUp(self):
def _wilt_function(iterations):
wilt_state = {'iterations': iterations}
def _wilt(*unused_args, **unused_kwargs):
wilt_state['iterations'] -= 1
return wilt_state['iterations'] < 0
return _wilt
@contextlib.contextmanager
def _mock_context(unused_state):
yield
self._seed_mock = mock.create_autospec(lambda: None)
self._wilt = _wilt_function(5)
self._reproduce_mock = mock.create_autospec(lambda unused_state: None)
self._learner = learner.Learner(
seed=self._seed_mock,
wilt=self._wilt,
reproduce=self._reproduce_mock)
def testEpoch(self):
self._learner.epoch(3)
# TODO: This test can be more precise, checking that the _FakeState's
# generation member was correct at the time it was called. Hard to do that
# since mock_calls just gets a shallow copy.
self.assertItemsEqual(
[
mock.call(_FakeState()),
mock.call(_FakeState()),
mock.call(_FakeState())],
self._reproduce_mock.mock_calls)
def testChatter(self):
self._learner.chatter()
# Five calls because self._wilt terminates after five iterations.
#
# TODO: This test can be more precise, checking that the _FakeState's
# generation member was correct at the time it was called. Hard to do that
# since mock_calls just gets a shallow copy.
self.assertItemsEqual(
[
mock.call(_FakeState()),
mock.call(_FakeState()),
mock.call(_FakeState()),
mock.call(_FakeState()),
mock.call(_FakeState())],
self._reproduce_mock.mock_calls)
if __name__ == '__main__':
unittest.main()