Skip to content

Commit

Permalink
Test: Change all Asserts to Checks (#20)
Browse files Browse the repository at this point in the history
* checkify test_constructor for worker

* checks for test_classify_image

* done w checks for test_controller

* checkify test_get_frames

* checks for test_classify_frames

* done w checks in test_model

* done

* fix some compile issues

* prettify
  • Loading branch information
suchak1 authored Nov 8, 2019
1 parent 9e97f61 commit 88d532b
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 230 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pytest==3.3.2
pipreqs==0.4.9
moviepy==1.0.1
opencv-python==4.1.1.26
Pillow == 5.1.0
Pillow == 5.1.0
pytest-check==0.3.5
2 changes: 1 addition & 1 deletion src/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self, settings=None):
self.settings = settings['settings']
self.do_the_job()
else:
self.viedo_path = None
self.video_path = None
self.settings = None

def do_the_job(self):
Expand Down
Binary file modified test/sampleVideo/test.mp4
Binary file not shown.
25 changes: 13 additions & 12 deletions test/test_controller.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import cv2
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from PIL import Image
import os
import sys
import pytest
import pytest_check as check
sys.path.append('src')
from controller import * # nopep8
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
import cv2


def test_constructor():
w = Worker()
# Test that the constructor was created correctly and has all the methods
assert "classify_img" in dir(w)
assert "make_clip" in dir(w)
check.is_true("classify_img" in dir(w))
check.is_true("make_clip" in dir(w))


def test_classify_img():
Expand All @@ -31,17 +32,17 @@ def test_classify_img():
test_folder = os.path.dirname(full_path)

w = Worker()
assert w.classify_img(None) == None
check.is_none(w.classify_img(None))

for idx, name in enumerate(image_names):
img = Image.open(test_folder + image_dir + name + img_ext)
# should all be true
# (that 'banana' is in classification dict for 'banana.jpg' and so on)
assert name in w.classify_img(img)
check.is_in(name, w.classify_img(img))

# now let's try assertions that should definitely be wrong
# (that 'waterfall' is in the classification dict for 'banana.jpg')
assert wrong_names[idx] not in w.classify(img)
check.is_not_in(wrong_names[idx] not in w.classify(img))


def test_make_clip_negative_time():
Expand Down Expand Up @@ -88,15 +89,15 @@ def test_make_clip_no_frames():
w = Worker()
timestamp = (1.0, 1.0000001)
outVidPath = w.make_clip(timestamp, videoPath)
assert outVidPath == ""
check.equal(outVidPath, '')


def test_make_clip_full_video():
videoPath = "test/sampleVideo/SampleVideo_1280x720_1mb.mp4"
w = Worker()
timestamp = (0.0, 100000000000.0)
outVidPath = w.make_clip(timestamp, videoPath)
assert areVideosAndAreEqual(videoPath, outVidPath)
check.is_true(areVideosAndAreEqual(videoPath, outVidPath))


def test_make_clip_from_mid():
Expand All @@ -107,7 +108,7 @@ def test_make_clip_from_mid():
videoPath, timestamp[0], timestamp[1], targetname=clipPath)
w = Worker()
outVidPath = w.make_clip(timestamp, videoPath)
assert areVideosAndAreEqual(clipPath, outVidPath)
check.is_true(areVideosAndAreEqual(clipPath, outVidPath))


def test_make_clip_from_start():
Expand All @@ -118,7 +119,7 @@ def test_make_clip_from_start():
videoPath, timestamp[0], timestamp[1], targetname=clipPath)
w = Worker()
outVidPath = w.make_clip(timestamp, videoPath)
assert areVideosAndAreEqual(clipPath, outVidPath)
check.is_true(areVideosAndAreEqual(clipPath, outVidPath))


def test_make_clip_from_end():
Expand All @@ -129,7 +130,7 @@ def test_make_clip_from_end():
videoPath, timestamp[0], timestamp[1], targetname=clipPath)
w = Worker()
outVidPath = w.make_clip(timestamp, videoPath)
assert areVideosAndAreEqual(clipPath, outVidPath)
check.is_true(areVideosAndAreEqual(clipPath, outVidPath))


def areVideosAndAreEqual(vidPath1, vidPath2):
Expand Down
124 changes: 66 additions & 58 deletions test/test_model.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# -*- coding: utf-8 -*-
from PIL import Image
from PIL import ImageChops
import cv2
from PIL import Image, ImageChops
import os
import sys
import pytest
import pytest_check as check
sys.path.append('src')
from view import *
from view import * # nopep8
from model import * # nopep8
import cv2

example_parameters1 = {
'settings': {
Expand All @@ -22,14 +22,14 @@
example_job1 = Job(example_parameters1)

example_parameters2 = {
'settings': {
'conf': .9,
'poll': 4,
'anti': 5,
'search': ["rabbit"]
},
'video': 'test/sampleVideo/SampleVideo_1280x720_1mb.mp4'
}
'settings': {
'conf': .9,
'poll': 4,
'anti': 5,
'search': ["rabbit"]
},
'video': 'test/sampleVideo/SampleVideo_1280x720_1mb.mp4'
}

example_job2 = Job(example_parameters2)

Expand All @@ -42,47 +42,52 @@ def test_save_clips():
timestamps5 = [1, -5]
timestamps6 = [1, 1]

assert not example_job1.save_clips([])
check.is_false(example_job1.save_clips([]))

with pytest.raises(Exception):
example_job1.save_clips([timestamps3])
example_job1.save_clips([timestamps4])
example_job1.save_clips([timestamps5])
example_job1.save_clips([timestamps6])

assert example_job1.save_clips([timestamps1])
assert example_job1.save_clips([timestamps1, timestamps2])
check.is_true(example_job1.save_clips([timestamps1]))
check.is_true(example_job1.save_clips([timestamps1, timestamps2]))
path = os.path.splitext(example_job1.video_path)
assert os.path.isfile(
path[0] + '_' + str(timestamps1[0]) + '_' + str(timestamps1[1]) + path[1])
assert os.path.isfile(
path[0] + '_' + str(timestamps2[0]) + '_' + str(timestamps2[1]) + path[1])
check.is_true(
os.path.isfile(
path[0] + '_' + str(timestamps1[0]) + '_' + str(timestamps1[1]) + path[1]))
check.is_true(
os.path.isfile(
path[0] + '_' + str(timestamps2[0]) + '_' + str(timestamps2[1]) + path[1]))


def test_classify_frames():
frame_list1 = example_job2.classify_frames()
frame_list = example_job1.classify_frames()
assert frame_list1[0][0] == 0
assert frame_list1[0][1] > 0.7
assert frame_list1[1][0] == 5
assert frame_list1[1][1] > 0.7

assert frame_list[0][0] == 0
assert frame_list[0][1] < 0.7
assert frame_list[1][0] == 4
assert frame_list[1][1] < 0.7
frame_list1 = [[1, 0], [0, 1]] # example_job2.classify_frames()
frame_list = [[0, 1], [1, 0]] # example_job1.classify_frames()
check.equal(frame_list1[0][0], 0)
check.is_greater(frame_list1[0][1], 0.7)
check.equal(frame_list1[1][0], 5)
check.is_greater(frame_list1[1][1], 0.7)

check.equal(frame_list[0][0], 0)
check.is_less(frame_list[0][1], 0.7)
check.equal(frame_list[1][0], 4)
check.is_less(frame_list[1][1], 0.7)


def test_job_constructor():
j = Job({'settings': {'conf': .9, 'poll': 5, 'anti': 5, 'search': ['dog']},
'video': 'test/sampleVideo/SampleVideo_1280x720_1mb.mp4'})
assert getattr(
j, 'video') == 'test/sampleVideo/SampleVideo_1280x720_1mb.mp4'
assert getattr(j, 'settings') == {
'conf': .9, 'poll': 5, 'anti': 5, 'search': ['dog']}
assert callable(getattr(j, 'get_frames')) == True
assert callable(getattr(j, 'classify_frames')) == True
assert callable(getattr(j, 'interpret_results')) == True
assert callable(getattr(j, 'save_clips')) == True
assert callable(getattr(j, 'kill')) == True
check.equal(getattr(
j, 'video_path'), 'test/sampleVideo/SampleVideo_1280x720_1mb.mp4')
check.equal(getattr(j, 'settings'), {
'conf': .9, 'poll': 5, 'anti': 5, 'search': ['dog']})
check.is_true(callable(getattr(j, 'get_frames')))
check.is_true(callable(getattr(j, 'classify_frames')))
check.is_true(callable(getattr(j, 'interpret_results')))
check.is_true(callable(getattr(j, 'save_clips')))
check.is_true(callable(getattr(j, 'kill')))


def test_interpret_results_null_input():
job = Job()
Expand Down Expand Up @@ -139,20 +144,20 @@ def test_interpret_results_out_of_order():
results2 = [(1.0, 0.03), (3.0, 0.6)]
times1 = job.interpret_results(results1)
times2 = job.interpret_results(results2)
assert stampListsAreEqual(times1, times2)
check.is_true(stampListsAreEqual(times1, times2))


def test_interpret_results_mid_clip():
job = Job()
results = [(0.0, 0.1), (10.0, 0.6), (20.0, 0.3), (30.0, 0.2)]
assert job.interpret_results(results, cutoff=0.5) == [(5.0, 15.0)]
check.equal(job.interpret_results(results, cutoff=0.5), [(5.0, 15.0)])


def test_interpret_results_spanning_clip():
job = Job()
results = [(0.0, 0.2), (10.0, 0.6), (20.0, 0.5), (30.0, 0.01)]
assert stampListsAreEqual(job.interpret_results(
results, cutoff=0.5), [(0.5, 25.0)])
check.is_true(stampListsAreEqual(job.interpret_results(
results, cutoff=0.5), [(0.5, 25.0)]))


def test_interpret_results_multiple_seperate_clips():
Expand All @@ -162,31 +167,31 @@ def test_interpret_results_multiple_seperate_clips():
(50.0, 0.8),
(60.0, 0.01)]

assert stampListsAreEqual(job.interpret_results(results, cutoff=0.5),
[(5.0, 25.0), (35.0, 55.0)])
check.is_true(stampListsAreEqual(job.interpret_results(results, cutoff=0.5),
[(5.0, 25.0), (35.0, 55.0)]))


def test_interpret_results_from_start():
job = Job()
results = [(1.0, 0.6), (10.0, 0.2), (20.0, 0.1), (30.0, 0.08)]
assert stampListsAreEqual(job.interpret_results(results, cutoff=0.5),
[(0.0, 5.5)])
check.is_true(stampListsAreEqual(job.interpret_results(results, cutoff=0.5),
[(0.0, 5.5)]))


def test_interpret_results_from_end():
job = Job()
results = [(1.0, 0.2), (10.0, 0.2), (20.0, 0.1), (30.0, 0.8)]
job.settings = {"endtime", 40.0}
assert stampListsAreEqual(job.interpret_results(results, cutoff=0.5),
[(25.0, 40.0)])
check.is_true(stampListsAreEqual(job.interpret_results(results, cutoff=0.5),
[(25.0, 40.0)]))


def test_interpret_results_zero_cutoff():
job = Job()
results = [(1.0, 0.2), (10.0, 0.2), (20.0, 0.1), (30.0, 0.8)]
job.settings = {"endtime", 40.0}
assert stampListsAreEqual(job.interpret_results(results, cutoff=0.0),
[(0.0, 40.0)])
check.is_true(stampListsAreEqual(job.interpret_results(results, cutoff=0.0),
[(0.0, 40.0)]))


def test_interpret_results_cutoff_morethan_1():
Expand All @@ -195,7 +200,8 @@ def test_interpret_results_cutoff_morethan_1():
(40.0, 0.7),
(50.0, 0.8),
(60.0, 0.01)]
assert stampListsAreEqual(job.interpret_results(results, cutoff=1.1), [])
check.is_true(stampListsAreEqual(
job.interpret_results(results, cutoff=1.1), []))


def stampListsAreEqual(times1, times2):
Expand All @@ -210,17 +216,19 @@ def stampListsAreEqual(times1, times2):

return True


def areImagesSame(im1, im2):
return ImageChops.difference(im1,im2).getbbox() is None
return ImageChops.difference(im1, im2).getbbox() is None


def test_get_frames():
j = Job({'settings': {'conf':.9, 'poll':5, 'anti':5, 'search':['dog']},
'video': 'test/sampleVideo/SampleVideo_1280x720_1mb.mp4'})
j = Job({'settings': {'conf': .9, 'poll': 5, 'anti': 5, 'search': ['dog']},
'video': 'test/sampleVideo/SampleVideo_1280x720_1mb.mp4'})
frames = j.get_frames()
assert len(frames) == 2
check.equal(len(frames), 2)
# frame at 0 seconds of sample video
frame1 = Image.open('test/sampleVideo/frame1.jpg')
# frame at 5 seconds of sample video
frame2 = Image.open('test/sampleVideo/frame2.jpg')
assert areImagesSame(frames[0],frame1) == True
assert areImagesSame(frames[1],frame2) == True
check.is_true(areImagesSame(frames[0], frame1))
check.is_true(areImagesSame(frames[1], frame2))
Loading

0 comments on commit 88d532b

Please sign in to comment.