Skip to content

Commit f1bc891

Browse files
committed
Update chapter 11 per revisions
1 parent da94a59 commit f1bc891

File tree

5 files changed

+43
-40
lines changed

5 files changed

+43
-40
lines changed

Chapter11/ABQ_Data_Entry/abq_data_entry/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def get_all_records(self):
8888

8989
with open(self.file, 'r', encoding='utf-8') as fh:
9090
# Casting to list is necessary for unit tests to work
91-
csvreader = csv.DictReader(list(fh.readlines()))
91+
csvreader = csv.DictReader(fh)
9292
missing_fields = set(self.fields.keys()) - set(csvreader.fieldnames)
9393
if len(missing_fields) > 0:
9494
fields_string = ', '.join(missing_fields)

Chapter11/ABQ_Data_Entry/abq_data_entry/test/test_application.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ class TestApplication(TestCase):
3232
def setUp(self):
3333
# can be parenthesized in python 3.10+
3434
with \
35-
patch('abq_data_entry.application.m.CSVModel') as csvmodel,\
36-
patch('abq_data_entry.application.m.SettingsModel') as settingsmodel,\
37-
patch('abq_data_entry.application.Application._show_login') as show_login,\
35+
patch(
36+
'abq_data_entry.application.m.CSVModel'
37+
) as csvmodel,\
38+
patch(
39+
'abq_data_entry.application.m.SettingsModel'
40+
) as settingsmodel,\
41+
patch(
42+
'abq_data_entry.application.Application._show_login'
43+
) as show_login,\
3844
patch('abq_data_entry.application.v.DataRecordForm'),\
3945
patch('abq_data_entry.application.v.RecordList'),\
4046
patch('abq_data_entry.application.ttk.Notebook'),\
@@ -52,7 +58,6 @@ def tearDown(self):
5258

5359
def test_show_recordlist(self):
5460
self.app._show_recordlist()
55-
self.app.update()
5661
self.app.notebook.select.assert_called_with(self.app.recordlist)
5762

5863
def test_populate_recordlist(self):

Chapter11/ABQ_Data_Entry/abq_data_entry/test/test_widgets.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ def type_in_widget(self, widget, string):
2828
widget.focus_force()
2929
for char in string:
3030
char = self.keysyms.get(char, char)
31-
self.root.update()
32-
widget.event_generate('<KeyPress-{}>'.format(char))
33-
self.root.update()
31+
widget.event_generate(f'<KeyPress-{char}>')
32+
self.root.update_idletasks()
3433

3534
def click_on_widget(self, widget, x, y, button=1):
3635
widget.focus_force()
37-
self.root.update()
38-
widget.event_generate("<ButtonPress-{}>".format(button), x=x, y=y)
39-
self.root.update()
36+
widget.event_generate(f'<ButtonPress-{button}>', x=x, y=y)
37+
self.root.update_idletasks()
4038

4139
@staticmethod
4240
def find_element(widget, element):
@@ -47,7 +45,7 @@ def find_element(widget, element):
4745
for x in x_coords:
4846
for y in y_coords:
4947
if widget.identify(x, y) == element:
50-
return (x, y)
48+
return (x + 1, y + 1)
5149
raise Exception(f'{element} was not found in widget')
5250

5351

@@ -166,11 +164,10 @@ def click_arrow(self, arrow, times=1):
166164
for _ in range(times):
167165
self.click_on_widget(self.vsb, x=x, y=y)
168166

169-
def test__key_validate(self):
167+
def test_key_validate(self):
170168
###################
171169
# Unit-test Style #
172170
###################
173-
174171
# test valid input
175172
for x in range(10):
176173
x = str(x)
@@ -179,19 +176,19 @@ def test__key_validate(self):
179176
self.assertTrue(p_valid)
180177
self.assertTrue(n_valid)
181178

182-
# test letters
179+
def test_key_validate_letters(self):
183180
valid = self.key_validate('a')
184181
self.assertFalse(valid)
185182

186-
# test non-increment number
183+
def test_key_validate_increment(self):
187184
valid = self.key_validate('1', '0.')
188185
self.assertFalse(valid)
189186

190-
# test too high number
187+
def test_key_validate_high(self):
191188
valid = self.key_validate('0', '10')
192189
self.assertFalse(valid)
193190

194-
def test__key_validate_integration(self):
191+
def test_key_validate_integration(self):
195192
##########################
196193
# Integration test style #
197194
##########################
@@ -208,7 +205,7 @@ def test__key_validate_integration(self):
208205
self.type_in_widget(self.vsb, '200')
209206
self.assertEqual(self.vsb.get(), '2')
210207

211-
def test__focusout_validate(self):
208+
def test_focusout_validate(self):
212209

213210
# test valid
214211
for x in range(10):
@@ -233,6 +230,7 @@ def test__focusout_validate(self):
233230

234231
def test_arrows(self):
235232
self.value.set(0)
233+
self.vsb.update()
236234
self.click_arrow('up', times=1)
237235
self.assertEqual(self.vsb.get(), '1')
238236

Chapter11/unittest_demo/mycalc.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
class MyCalc:
44

5-
def __init__(self, a, b):
6-
self.a = a
7-
self.b = b
5+
def __init__(self, a, b):
6+
self.a = a
7+
self.b = b
88

9-
def add(self):
10-
return self.a + self.b
9+
def add(self):
10+
return self.a + self.b
1111

12-
def mod_divide(self):
13-
if self.b == 0:
14-
raise ValueError("Cannot divide by zero")
15-
return (int(self.a / self.b), self.a % self.b)
12+
def mod_divide(self):
13+
if self.b == 0:
14+
raise ValueError("Cannot divide by zero")
15+
return (int(self.a / self.b), self.a % self.b)
1616

17-
def mod_divide2(self):
18-
if type(self.a) is not int or type(self.b) is not int:
19-
raise ValueError("Method only valid for ints")
20-
if self.b == 0:
21-
raise ValueError("Cannot divide by zero")
22-
return (self.a // self.b, self.a % self.b)
17+
def mod_divide2(self):
18+
if type(self.a) is not int or type(self.b) is not int:
19+
raise ValueError("Method only valid for ints")
20+
if self.b == 0:
21+
raise ValueError("Cannot divide by zero")
22+
return (self.a // self.b, self.a % self.b)
2323

24-
def rand_between(self):
25-
return (
26-
(random.random() * abs(self.a - self.b)) +
27-
min(self.a, self.b))
24+
def rand_between(self):
25+
return (
26+
(random.random() * abs(self.a - self.b)) +
27+
min(self.a, self.b))

Chapter11/unittest_demo/test_mycalc_badly.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from mycalc import MyCalc
1+
import mycalc
22
import unittest
33

44

55
class TestMyCalc(unittest.TestCase):
66

77
def test_add(self):
8-
mc = MyCalc(1, 10)
8+
mc = mycalc.MyCalc(1, 10)
99
assert mc.add() == 11
1010

1111
# much better error output

0 commit comments

Comments
 (0)