/
UT_FilerFunctionality.py
325 lines (283 loc) · 11.3 KB
/
UT_FilerFunctionality.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import sys
import io
from io import StringIO
from io import BytesIO
import unittest
from unittest.mock import Mock
from unittest.mock import patch
import os
from os.path import isfile
import builtins
import pickle
import random
thePaths = sys.argv[1:]
for path in thePaths:
sys.path.append(path)
sys.argv = sys.argv[:1]
import RunMain
import TestView
from Controller import Controller
from RecordCollection import RecordCollection
class TestFilerFunctionClass(unittest.TestCase):
def _addSomeRecords(self, theRC, number):
# auto id, default attrs
for x in range(number):
theRC.addRecord("", "m", None, None, None, None, True, False)
def _addSomeRecordsViaController(self, theCont, number):
# auto id, default attrs
theCont.do_select_option("AUTOID")
theCont.do_on("")
for x in range(number):
theCont.do_add_rec(" m ")
@patch('builtins.open')
def test_SerialLoad_01(self, mockMeth):
"""
Can only load an instance of RecordCollection
- Reject other class object
"""
TestView.clearLog()
object = set()
thePickle = BytesIO()
pickle.dump(object, thePickle)
theDump = thePickle.getvalue()
# builtins.open
mockMeth.return_value = BytesIO(theDump)
# Set up Controller via RunMain module
# order important for list of string-codes
# "coll:" triggers serial load
# "view:TestView" will store uses of TestView() to TestView.py module
RunMain.main(False, ["coll:", "view:TestView"])
expectedShow = "Failed to do serial load: No instance of record \
collection"
actualShow = TestView.theLog[0]
self.assertEqual(expectedShow, actualShow)
@patch('builtins.open')
def test_SerialLoad_02(self, mockMeth):
"""
Handles IOError
- Because the IOError message is expected to be determined by the
IOError constructor, the message will be randomised
"""
message = str(random.randrange(100))
TestView.clearLog()
mockMeth.side_effect = IOError(message)
RunMain.main(False, ["coll:", "view:TestView"])
expectedShow = "Failed to do serial load: {}".format(message)
actualShow = TestView.theLog[0]
self.assertEqual(expectedShow, actualShow)
@patch('pickle.load')
@patch('builtins.open')
def test_SerialLoad_03(self, mock_open, mock_load):
"""
Handles AtrributeError
- Because the AttributeError message is expected to be determined by
the AttributeError constructor, the message will be randomised
"""
message = str(random.randrange(100))
TestView.clearLog()
mock_open.return_value = BytesIO()
mock_load.side_effect = AttributeError(message)
RunMain.main(False, ["coll:", "view:TestView"])
expectedShow = "Failed to do serial load: {}".format(message)
actualShow = TestView.theLog[0]
self.assertEqual(expectedShow, actualShow)
@patch('builtins.open')
def test_SerialLoad_04(self, mockMeth):
"""
Successful loading of RecordCollection
- Because Controller will end up with an instance of RecordCollection
regardless of the outcome of a serial load, the object loaded will
come with 10 Records
"""
theRC = RecordCollection()
self._addSomeRecords(theRC, 10)
theDumpStream = BytesIO()
pickle.dump(theRC, theDumpStream)
theDump = theDumpStream.getvalue()
mockMeth.return_value = BytesIO(theDump)
TestView.clearLog()
RunMain.main(False, ["coll:", "view:TestView"])
expected_1 = "Serial load of record collection successful"
expected_2 = "Records in ERP: 10"
self.assertEqual(expected_1, TestView.theLog[0])
TestView.clearLog()
RunMain.theController.do_neutral("")
self.assertEqual(expected_2, TestView.theLog[0])
@patch('builtins.open')
def test_TextLoad_01(self, mockMeth):
"""
Load a record from text, test for correct attribute parsing
- This test does not add coverage if test_TextLoad_02 is exercised
- Asserted by expecting TestView().show() to get a message,
representing the record in the same string format
"""
expID = "A001"
expGender = "F"
expAge = 36
expSales = 92
expBMI = "Normal"
expIncome = 700
theData = "{} {} {} {} {} {}".format(expID, expGender, expAge,
expSales, expBMI, expIncome)
mockMeth.return_value = StringIO(theData)
theController = Controller(TestView.TestView(), None)
theController.do_text_load("")
TestView.clearLog()
expectedMessage = theData + "\n"
theController.do_view_records("")
actualMessage = TestView.theLog[0]
self.assertEqual(expectedMessage, actualMessage)
@patch('builtins.open')
def test_TextLoad_02(self, mockMeth):
"""
Can handle bad lines in text load
- Line 2 in textData (between \n\n) is bad as nothing can be
interpreted
- Also 2 records will be added
"""
textData = "a001 f 36 92 normal 700\n\nb222 m 1 1 normal 9"
mockMeth.return_value = StringIO(textData)
theController = Controller(TestView.TestView(), None)
TestView.clearLog()
theController.do_text_load("")
expectedShow = "Records Added: 2\nProblems: \nBAD LINE 2: 'Not Enough \
Arguments Provided'\n\n"
actualShow = TestView.theLog[0]
self.assertEqual(expectedShow, actualShow)
@patch('builtins.open')
def test_TextLoad_03(self, mockMeth):
"""
Handles IOError
- Because the IOError message is expected to be determined by the
IOError constructor, the message will be randomised
"""
message = str(random.randrange(100))
mockMeth.side_effect = IOError(message)
theController = Controller(TestView.TestView(), None)
TestView.clearLog()
theController.do_text_load("")
expectedShow = "EXCEPTION: {}\n".format(message)
actualShow = TestView.theLog[0]
self.assertEqual(expectedShow, actualShow)
@patch('builtins.open')
@patch('os.path.isfile')
def test_SerialSave_01(self, mock_isfile, mock_open):
"""
Saving a RecordCollection with 10 Records
"""
def doDump():
doDump.theDump = theDumpStream.getvalue()
theDumpStream = BytesIO()
theDumpStream.close = Mock(side_effect=doDump)
# So that it is know that file does not exist
mock_isfile.return_value = False
mock_open.return_value = theDumpStream
theController = Controller(TestView.TestView(), None)
# add 10 Records, reset view log, then serial save
self._addSomeRecordsViaController(theController, 10)
TestView.clearLog()
theController.do_serial_save("")
# Part 1: Check contents of theDumpStream
# theDump = theDumpStream.getvalue()
expectedRecords = 10
fromDump = pickle.load(BytesIO(doDump.theDump))
actualRecords = len(fromDump.getAllRecords())
self.assertEqual(expectedRecords, actualRecords)
# Part 2: Check no show() messages on theLog
expectedLog = 0
actualLog = len(TestView.theLog)
self.assertEqual(expectedLog, actualLog)
@patch('builtins.open')
@patch('os.path.isfile')
def test_SerialSave_02(self, mock_isfile, mock_open):
"""
Ensure that a serial save does not happen when file exists
- isfile will return true
- Ensure open does not get called: If it does an error will be raised
"""
mock_isfile.return_value = True
mock_open.side_effect = AssertionError()
theController = Controller(TestView.TestView(), None)
TestView.clearLog()
theController.do_serial_save("")
expectedShow = "Will not overwrite an existing file\nPlease, enter a \
new file when using serial_save\n"
actualShow = TestView.theLog[0]
self.assertEqual(expectedShow, actualShow)
@patch('builtins.open')
@patch('os.path.isfile')
def test_SerialSave_03(self, mock_isfile, mock_open):
"""
Handles IOError
- Because the IOError message is expected to be determined by the
IOError constructor, the message will be randomised
"""
message = str(random.randrange(100))
mock_isfile.return_value = False
mock_open.side_effect = IOError(message)
theController = Controller(TestView.TestView(), None)
TestView.clearLog()
theController.do_serial_save("")
expectedShow = "EXCEPTION: {}\n".format(message)
actualShow = TestView.theLog[0]
self.assertEqual(expectedShow, actualShow)
@patch('builtins.open')
@patch('os.path.isfile')
def test_TextSave_01(self, mock_isfile, mock_open):
"""
Ensure that a text save does not happen when file exists
- isfile will return true
- Ensure open does not get called: If it does an error will be raised
"""
mock_isfile.return_value = True
mock_open.side_effect = AssertionError()
theController = Controller(TestView.TestView(), None)
TestView.clearLog()
theController.do_text_save("")
expectedShow = "Will not overwrite an existing file\nPlease, enter a \
new file when using text_save\n"
actualShow = TestView.theLog[0]
self.assertEqual(expectedShow, actualShow)
@patch('builtins.open')
@patch('os.path.isfile')
def test_TextSave_02(self, mock_isfile, mock_open):
"""
Handles IOError
- Because the IOError message is expected to be determined by the
IOError constructor, the message will be randomised
"""
message = str(random.randrange(100))
mock_isfile.return_value = False
mock_open.side_effect = IOError(message)
theController = Controller(TestView.TestView(), None)
TestView.clearLog()
theController.do_text_save("")
expectedShow = "EXCEPTION: {}\n".format(message)
actualShow = TestView.theLog[0]
self.assertEqual(expectedShow, actualShow)
@patch('builtins.open')
@patch('os.path.isfile')
def test_TextSave_03(self, mock_isfile, mock_open):
"""
Add 2 Records, then expect the text save to produce 2 text lines
"""
def doText():
doText.theText = theTextStream.getvalue()
theTextStream = StringIO()
theTextStream.close = Mock(side_effect=doText)
mock_isfile.return_value = False
mock_open.return_value = theTextStream
theController = Controller(TestView.TestView(), None)
self._addSomeRecordsViaController(theController, 2)
TestView.clearLog()
theController.do_text_save("")
# Part 1: Text data
expectedData = "A000 M 0 0 Normal 0\nA001 M 0 0 Normal 0"
actualData = doText.theText
self.assertEqual(expectedData, actualData)
# Part 2: Show
expectedShow = "Saved As Text"
actualShow = TestView.theLog[0]
self.assertEqual(expectedShow, actualShow)
if __name__ == "__main__":
unittest.main()