-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
567 lines (525 loc) · 22.7 KB
/
test.py
File metadata and controls
567 lines (525 loc) · 22.7 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import unittest
import lib
import pandas as pd
from pandas.testing import assert_frame_equal
from lib import checkStream
import testlib
from pathlib import Path
from functools import partial
import logging
# TestCases
# Switch off logging
logging.getLogger("lib").disabled = True
class TestCheckStream(unittest.TestCase):
"""Tests the checkStream function"""
def test_checkStream_noContainer(self):
# test whether nothing happens if container is none
mockframe = testlib.mockFrame()
# monkey patch onupdate
lib.onUpdate = lambda x: 1
lib.createStatusWidget(mockframe)
lib.checkStream(mockframe)
self.assertEqual(mockframe.status.get(), "yellow")
self.assertEqual(mockframe.streamActive, False)
def test_checkStream_containerRunningEarly(self):
"""Tests whether the reaction to a running container that
has no valid bitrate output is correct."""
# test whether nothing happens if container is none
mockframe = testlib.mockFrame()
mockframe.container = testlib.mockContainer(status="running", log=b"asdf")
# monkey patch onupdate
lib.onUpdate = lambda x: 1
lib.createStatusWidget(mockframe)
lib.checkStream(mockframe)
self.assertEqual(mockframe.status.get(), "green")
self.assertEqual(mockframe.streamActive, False)
self.assertEqual(mockframe.lbl_StreamSpeed["text"], "-/-")
def test_checkStream_containerRunningLate(self):
"""Tests whether the reaction to a running container that
has no valid bitrate output is correct."""
# test whether nothing happens if container is none
mockframe = testlib.mockFrame()
mockframe.container = testlib.mockContainer(
status="running",
log=b"\rframe 918.3kbits/s 38.8image 25 fps \frame 920.3kbits/s"
b"\rframe 918.3kbits/s 38.8image 25 fps \frame 920.3kbits/s"
b"\rframe 918.3kbits/s 38.8image 25 fps \frame 920.3kbits/s"
b"\rframe 918.3kbits/s 38.8image 25 fps \frame 920.3kbits/s",
)
# monkey patch onupdate
lib.onUpdate = lambda x: 1
lib.createStatusWidget(mockframe)
lib.checkStream(mockframe)
self.assertEqual(mockframe.status.get(), "green")
self.assertEqual(mockframe.streamActive, False)
self.assertEqual(mockframe.lbl_StreamSpeed["text"], "920.3kbits/s")
def test_checkStream_containerCreated_inList(self):
"""Tests whether the reaction to a created container that
is then the client.containers.list is correct"""
mockframe = testlib.mockFrame()
mockframe.container = testlib.mockContainer(status="created", log=b"asdf")
# monkey patch onupdate
oldOnUpdate = lib.onUpdate
lib.onUpdate = lambda x: 1
# monkeypatch client
oldClient = lib.docker.from_env
lib.docker.from_env = lambda: testlib.mockEngine(
testlib.mockImages(), containers=[mockframe.container]
)
lib.createStatusWidget(mockframe)
lib.checkStream(mockframe)
self.assertEqual(mockframe.status.get(), "green")
self.assertEqual(mockframe.streamActive, False)
self.assertEqual(mockframe.lbl_StreamSpeed["text"], "-/-")
# undo monkeypatch
lib.onUpdate = oldOnUpdate
lib.docker.from_env = oldClient
def test_checkStream_containerCreated_finished(self):
"""Check whether reaction to a container that has been created and
finished succesfully is correct."""
mockframe = testlib.mockFrame()
mockframe.container = testlib.mockContainer(status="created", log=b"asdf")
# monkey patch onupdate
oldOnUpdate = lib.onUpdate
lib.onUpdate = lambda x: 1
# monkeypatch client
oldClient = lib.docker.from_env
lib.docker.from_env = lambda: testlib.mockEngine(testlib.mockImages())
lib.createStatusWidget(mockframe)
lib.checkStream(mockframe)
self.assertEqual(mockframe.status.get(), "yellow")
self.assertEqual(mockframe.streamActive, False)
self.assertEqual(mockframe.lbl_StreamSpeed["text"], "Inactive")
self.assertEqual(mockframe.container, None)
# undo monkeypatch
lib.onUpdate = oldOnUpdate
lib.docker.from_env = oldClient
def test_checkStream_containerCreated_crashed(self):
"""Check whether reaction to a container that has been created and
finished succesfully is correct."""
mockframe = testlib.mockFrame()
mockframe.container = testlib.mockContainer(status="created", log=b"error")
# monkey patch onupdate
oldOnUpdate = lib.onUpdate
lib.onUpdate = lambda x: 1
# monkeypatch client
oldClient = lib.docker.from_env
lib.docker.from_env = lambda: testlib.mockEngine(testlib.mockImages())
lib.showerror = testlib.raiseAssertion
lib.createStatusWidget(mockframe)
badcall = partial(checkStream, mockframe)
self.assertRaises(AssertionError, badcall)
self.assertEqual(mockframe.status.get(), "yellow")
self.assertEqual(mockframe.streamActive, False)
self.assertEqual(mockframe.lbl_StreamSpeed["text"], "Inactive")
self.assertEqual(mockframe.container, None)
# undo monkeypatch
lib.onUpdate = oldOnUpdate
lib.docker.from_env = oldClient
class TestCheckRightTime(unittest.TestCase):
"""Tests the checkRightTime function."""
def test_checkRightTime_noSchedule(self):
mockframe = testlib.mockFrame()
# monkeypatch dispatch stream
oldDispatch = lib.dispatch_stream
lib.dispatch_stream = testlib.raiseAssertion
lib.checkRightTime(mockframe) # should not raise AssertionError
self.assertEqual(mockframe.container, None)
self.assertEqual(mockframe.streamActive, False)
# undo monkeypatch
lib.dispatch_stream = oldDispatch
def test_checkRightTime_StreamInFuture(self):
mockframe = testlib.mockFrame()
# add current time
mockframe.nowDT = pd.Timestamp("2010-06-21 12:00:00")
# add schedule
mockframe.schedule = pd.DataFrame(
{
"File": [
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test.mp4",
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test2.mp4",
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test4.mp4",
],
"Date/Time": [
pd.Timestamp("2025-06-21 12:00:00"),
pd.Timestamp("2025-06-22 13:00:00"),
pd.Timestamp("2025-07-23 14:00:00"),
],
}
)
# monkeypatch dispatch stream
oldDispatch = lib.dispatch_stream
lib.dispatch_stream = testlib.raiseAssertion
lib.checkRightTime(mockframe) # should not raise AssertionError
self.assertEqual(mockframe.container, None)
self.assertEqual(mockframe.streamActive, False)
# undo monkeypatch
lib.dispatch_stream = oldDispatch
def test_checkRightTime_StreamInPast(self):
mockframe = testlib.mockFrame()
# add current time
mockframe.nowDT = pd.Timestamp("2010-06-21 12:00:00")
# add schedule
mockframe.schedule = pd.DataFrame(
{
"File": [
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test.mp4",
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test2.mp4",
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test4.mp4",
],
"Date/Time": [
pd.Timestamp("2009-06-21 12:00:00"),
pd.Timestamp("2009-06-22 13:00:00"),
pd.Timestamp("2009-07-23 14:00:00"),
],
}
)
# monkeypatch dispatch stream
oldDispatch = lib.dispatch_stream
lib.dispatch_stream = testlib.raiseAssertion
lib.checkRightTime(mockframe) # should not raise AssertionError
self.assertEqual(mockframe.container, None)
self.assertEqual(mockframe.streamActive, False)
# undo monkeypatch
lib.dispatch_stream = oldDispatch
def test_checkRightTime_RightTime(self):
mockframe = testlib.mockFrame()
# add current time
mockframe.nowDT = pd.Timestamp("2010-06-21 12:00:00")
# add schedule
mockframe.schedule = pd.DataFrame(
{
"File": [
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test.mp4",
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test2.mp4",
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test4.mp4",
],
"Date/Time": [
pd.Timestamp("2010-06-21 12:00:01"),
pd.Timestamp("2009-06-22 13:00:00"),
pd.Timestamp("2009-07-23 14:00:00"),
],
}
)
# monkeypatch dispatch stream
oldDispatch = lib.dispatch_stream
lib.dispatch_stream = testlib.raiseAssertion
goodCall = partial(lib.checkRightTime, mockframe)
self.assertRaises(AssertionError, goodCall)
# second monkeypatch to check whether events afterwards happen correctly
lib.dispatch_stream = lambda x, y, z: None
mockframe.after = (
testlib.raiseAssertion
) # this is called by the error dispatching functions
oldDrawConfig = lib.draw_config
lib.draw_config = lambda x, y: None
goodCall = partial(lib.checkRightTime, mockframe)
self.assertRaises(AssertionError, goodCall)
# call it again to see whether the effects are correct
mockframe.after = (
lambda x, y, z, a: None
) # this is called by the error dispatching functions
lib.checkRightTime(
mockframe
) # before the call only goes through to frame.after because assertion is raised there
self.assertEqual(len(mockframe.schedule), 2)
self.assertEqual(mockframe.streamActive, False)
# undo monkeypatch
lib.dispatch_stream = oldDispatch
lib.draw_config = oldDrawConfig
class TestGui(unittest.TestCase):
def test_createTimeWidget(self):
mockframe = testlib.mockFrame()
# monkey patch onupdate
oldOnUpdate = lib.onUpdate
lib.onUpdate = lambda x: 1
lib.createTimeWidget(mockframe)
# check whether it is there
self.assertTrue(hasattr(mockframe, "time_label"))
self.assertTrue(hasattr(mockframe, "time_title"))
self.assertTrue(hasattr(mockframe, "now"))
# undo monkey patch
lib.onUpdate = oldOnUpdate
def test_createStatusWidget(self):
mockframe = testlib.mockFrame()
# monkey patch onupdate
oldOnUpdate = lib.onUpdate
lib.onUpdate = lambda x: 1
lib.createStatusWidget(mockframe)
# check whether it is there
self.assertTrue(hasattr(mockframe, "status"))
self.assertTrue(hasattr(mockframe, "streamSpeed"))
self.assertTrue(hasattr(mockframe, "status_title"))
self.assertTrue(hasattr(mockframe, "rectl_status"))
self.assertTrue(hasattr(mockframe, "lbl_StreamSpeed"))
# undo monkey patch
lib.onUpdate = oldOnUpdate
def test_setStream(self):
mockframe = testlib.mockFrame()
lib.createStatusWidget(mockframe)
lib.setStream(mockframe, "green", "1234")
self.assertEqual(mockframe.status.get(), "green")
self.assertEqual(mockframe.streamSpeed.get(), "1234")
def test_checkPastStream(self):
mockframe = testlib.mockFrame()
mockframe.streamActive = True
mockframe.nowDT = pd.Timestamp("2009-07-23 14:00:00")
# monkey patch draw config and on update
oldDrawConfig = lib.draw_config
lib.draw_config = lambda x, y: None
oldOnUpdate = lib.onUpdate
lib.onUpdate = lambda: None
# only present/future events
goodDf = pd.DataFrame(
{
"File": [
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test.mp4",
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test2.mp4",
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test4.mp4",
],
"Date/Time": [
pd.Timestamp("2015-06-21 12:00:01"),
pd.Timestamp("2015-06-22 13:00:00"),
pd.Timestamp("2015-07-23 14:00:00"),
],
}
)
mockframe.schedule = goodDf
lib.checkPastStream(mockframe)
assert_frame_equal(mockframe.schedule, goodDf)
# one past event
badDf = pd.DataFrame(
{
"File": [
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test.mp4",
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test2.mp4",
"C:\\Users\\michael.mitter\\Documents\\streamscheduler\\test_files\\vids\\test4.mp4",
],
"Date/Time": [
pd.Timestamp("2008-06-21 12:00:01"),
pd.Timestamp("2012-06-22 13:00:00"),
pd.Timestamp("2012-07-23 14:00:00"),
],
}
)
mockframe.schedule = badDf
lib.checkPastStream(mockframe)
assert_frame_equal(mockframe.schedule, badDf.iloc[1:, :])
# restore old draw config
lib.draw_config = oldDrawConfig
lib.onUpdate = oldOnUpdate
def test_drawconfigGrid(self):
mockframe = testlib.mockFrame()
lib.drawConfigGrid(mockframe)
self.assertEqual(list(mockframe.grid["Names"].keys()), ["File", "Date/Time"])
self.assertEqual(len(mockframe.grid["grid"]), 10)
for i in range(10):
self.assertEqual(len(mockframe.grid["grid"][i]), 2)
def test_askExit(self):
# make mockfraem
mockframe = testlib.mockFrame()
mockroot = testlib.mockRoot()
# save old askyesno
oldAskYesNo = lib.askyesno
# monkey patch in alwasy true
lib.askyesno = lambda x, y: True
# set stream to active
mockframe.streamActive = True
destroyCall = partial(lib.askExit, frame=mockframe, root=mockroot)
self.assertRaises(testlib.RootDestroyedException, destroyCall)
# set stream to inactive
lib.askyesno = lambda x, y: False
# set stream to active
mockframe.streamActive = True
destroyCall = partial(lib.askExit, frame=mockframe, root=mockroot)
# restore old function
lib.askyesno = oldAskYesNo
class TestParse(unittest.TestCase):
def setUp(self) -> None:
self.GoodContainer = testlib.mockContainer(
b"\rframe 918.3kbits/s 38.8image 25 fps \frame 920.3kbits/s"
b"\rframe 918.3kbits/s 38.8image 25 fps \frame 920.3kbits/s"
b"\rframe 918.3kbits/s 38.8image 25 fps \frame 920.3kbits/s"
b"\rframe 918.3kbits/s 38.8image 25 fps \frame 920.3kbits/s"
)
self.badContainer = testlib.mockContainer(b"press [q] press [h]")
self.goodConfig = pd.DataFrame(
{
"File": [
"test_files\\vids\\test.mp4",
"test_files\\vids\\test2.mp4",
"test_files\\vids\\test4.mp4",
],
"Date/Time": [
pd.Timestamp("2025-06-21 12:00:00"),
pd.Timestamp("2025-06-22 13:00:00"),
pd.Timestamp("2025-07-23 14:00:00"),
],
}
)
self.goodCredentials = {
"User": 12345,
"Password": 678910,
"rtmp-URL": "rtmp://i.amagood.server",
"playpath": "dclive_0_1@2345",
}
self.mockframe = testlib.mockFrame()
# draw grid onto mockFrame
lib.drawConfigGrid(self.mockframe)
# monkey patch lib.showerror so that I can catch to result
lib.showerror = testlib.raiseAssertion
def test_parseContainerOutput(self):
# test no bitrate
result = next(lib.parseContainerOutput(self.badContainer))
self.assertIsNone(result)
# test bitrate
result = next(lib.parseContainerOutput(self.GoodContainer))
self.assertEqual(result, "920.3kbits/s")
def test_loadconfig(self):
lib.load_config(
self.mockframe, filepath="./test_files/test_schedule_1_mock_good.xlsx"
)
assert_frame_equal(self.mockframe.schedule, self.goodConfig)
self.assertEqual(self.mockframe.credentials, self.goodCredentials)
def test_drawconfig(self):
lib.load_config(
self.mockframe, filepath="./test_files/test_schedule_1_mock_good.xlsx"
)
i = 0
for i in range(len(self.mockframe.schedule)):
tempFile = self.mockframe.grid["grid"][i][0].get()
self.assertEqual(Path(self.mockframe.schedule.iloc[i, 0]).name, tempFile)
tempDateTime = pd.Timestamp(self.mockframe.grid["grid"][i][1].get())
self.assertEqual(self.mockframe.schedule.iloc[i, 1], tempDateTime)
# Test that the other lines contain ----
for index in range(i + 1, 10):
temp = self.mockframe.grid["grid"][index][0].get()
self.assertEqual(temp, " ".join(["-"] * 20))
def test_checkConfigTiming(self):
# bad example
badCall = partial(
lib.load_config,
frame=self.mockframe,
filepath="./test_files/test_schedule_1_mock_badTiming.xlsx",
)
self.assertRaises(AssertionError, badCall)
# good example
goodCall = partial(
lib.load_config,
frame=self.mockframe,
filepath="./test_files/test_schedule_1_mock_good.xlsx",
)
goodCall()
def test_config_format(self):
# bad example - bad datatypes
badCall = partial(
lib.load_config,
frame=self.mockframe,
filepath="./test_files/test_schedule_1_mock_badDtypes.xlsx",
)
self.assertRaises(AssertionError, badCall)
# bad example - badfiles
badCall = partial(
lib.load_config,
frame=self.mockframe,
filepath="./test_files/test_schedule_1_mock_badfiles.xlsx",
)
self.assertRaises(AssertionError, badCall)
# bad example - bad videodirs
badCall = partial(
lib.load_config,
frame=self.mockframe,
filepath="./test_files/test_schedule_1_mock_badVideoDir.xlsx",
)
self.assertRaises(AssertionError, badCall)
def test_parseFailure(self):
badContainer1 = testlib.mockContainer(b"I am a failure!")
self.assertTrue(lib.parseFailure(badContainer1))
badContainer2 = testlib.mockContainer(b"Everything is an error..")
self.assertTrue(lib.parseFailure(badContainer2))
badContainer3 = testlib.mockContainer(b"Not found...")
self.assertTrue(lib.parseFailure(badContainer3))
class TestDockers(unittest.TestCase):
def makeGood(self):
# setup things in a way that all tests of checkDocker will pass
lib.shutil.which = lambda x: "asdf" # this will make the call return something
lib.docker.from_env = lambda: testlib.mockEngine(testlib.mockImages())
lib.countImages = lambda x: 0
def test_checkDocker(self):
# save all functions that will be monkey patched here
oldWhich = lib.shutil.which
oldClient = lib.docker.from_env
oldCountImages = lib.countImages
oldShowerror = lib.showerror
# check if everything passes
lib.showerror = testlib.raiseAssertion
self.makeGood()
lib.checkDocker("ffmpeg:1.0")
# simulate docker not installed
lib.shutil.which = lambda x: None
badCall = partial(lib.checkDocker, "asdf")
self.assertRaises(AssertionError, badCall)
# reset to good version
self.makeGood()
# simulate version is not ok
lib.docker.from_env = lambda: testlib.mockEngine(
testlib.mockImages(), version="Bad"
)
badCall = partial(lib.checkDocker, "asdf")
self.assertRaises(AssertionError, badCall)
# reset to good version
self.makeGood()
# simulate image is not installed
lib.docker.from_env = lambda: testlib.mockEngine(testlib.mockImages(good=False))
badCall = partial(lib.checkDocker, "asdf")
self.assertRaises(AssertionError, badCall)
# simulate count of image is not right
self.makeGood()
lib.countImages = lambda x: 1
badCall = partial(lib.checkDocker, "asdf")
self.assertRaises(AssertionError, badCall)
# restore old funcitons
lib.countImages = oldCountImages
lib.docker.from_env = oldClient
lib.shutil.which = oldWhich
lib.showerror = oldShowerror
def test_countImages(self):
rightContainer = testlib.mockContainer(name="asdf")
# 0 images
oldClient = lib.docker.from_env
lib.docker.from_env = lambda: testlib.mockEngine(containers=[])
self.assertEqual(lib.countImages("asdf"), 0)
# 2 images
lib.docker.from_env = lambda: testlib.mockEngine(
containers=[rightContainer, rightContainer]
)
self.assertEqual(lib.countImages("asdf"), 2)
# restore old client
lib.docker.from_env = oldClient
class TestStream(unittest.TestCase):
def setUp(self):
self.credentials = {
"User": 12345,
"Password": 678910,
"rtmp-URL": "rtmp://i.amagood.server",
"playpath": "dclive_0_1@2345",
}
self.engine = testlib.mockEngine()
self.mockframe = testlib.mockFrame()
self.mockframe.credentials = self.credentials
def tearDown(self) -> None:
self.engine = testlib.mockEngine()
def test_dispatch_test_stream(self):
lib.dispatch_test_stream(self.credentials, self.engine)
self.assertEqual(len(self.engine.containers), 1)
def test_dispatch_Stream(self):
# test whether container is dispatched
lib.dispatch_stream(
"mockfile",
credentials=self.credentials,
pathmap="dummy",
engine=self.engine,
)
self.assertEqual(len(self.engine.containers), 1)
if __name__ == "__main__":
res = unittest.main(verbosity=3, exit=False)