Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(flask): backend flask server tests #56

Merged
merged 17 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/back-end/video-processing/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ def handle_request():
file = request.data.decode("utf-8") # expects the filename, in the form <uid>-<file name>-<epoch time> such as "23-yeehaw-1698360721.mp4"
if os.path.isfile(f"{input_path}/{file}"): # check if the file exists
final = f"{out_path}/{file[:-4]}-processed{file[-4:]}"
process = mp.Process(target=vp.process_INTERPOLATE, args=(f"{input_path}/{file}", final, )) # define a new process pointing to process_INTERPOLATE
process.start() # start the process on another thread
print(f"Process started on {file}")
if not app.testing: # if we're running Flask unit tests, don't run the video processing method
process = mp.Process(target=vp.process_INTERPOLATE, args=(f"{input_path}/{file}", final, )) # define a new process pointing to process_INTERPOLATE
tthvo marked this conversation as resolved.
Show resolved Hide resolved
tthvo marked this conversation as resolved.
Show resolved Hide resolved
process.start() # start the process on another thread
print(f"Process started on {file}")
return "Success: file exists"
else:
return "Error: file not found"
Expand Down
Binary file added tests/back-end/1.mp4
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/back-end/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Testing the back-end video processing server

## Requirements
- Python 3.8
MyStackOverflows marked this conversation as resolved.
Show resolved Hide resolved

## Running tests
Navigate to the repository's root directory and run:
```bash
python3 -m unittest discover -s ./tests/back-end
```
50 changes: 50 additions & 0 deletions tests/back-end/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import unittest, sys, os
from flask import Flask, request

# add server.py's directory to sys path and import it
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../app/back-end/video-processing")))

# set temporary environment variables (will be automatically deleted when the session ends)
os.environ["PRIVACYPAL_INPUT_VIDEO_DIR"] = f"{os.getcwd()}/tests/back-end"
os.environ["PRIVACYPAL_OUT_VIDEO_DIR"] = f"{os.getcwd()}/tests/back-end"

from server import app # finally, import our flask server

class ServerTest(unittest.TestCase):
app: Flask
method_not_allowed = b'<!doctype html>\n<html lang=en>\n<title>405 Method Not Allowed</title>\n<h1>Method Not Allowed</h1>\n<p>The method is not allowed for the requested URL.</p>\n'
MyStackOverflows marked this conversation as resolved.
Show resolved Hide resolved

def setUp(self):
self.app = app
self.app.testing = True

def test_process_video(self):
route = "/process_video"
with self.app.test_client() as c:
MyStackOverflows marked this conversation as resolved.
Show resolved Hide resolved
for method in [c.get, c.put, c.delete, c.trace, c.patch]:
response = method(route)
self.assertEqual(self.method_not_allowed, response.data)
self.assertEqual(405, response.status_code)

response = c.post(route, data="blahblahblah invalid file")
MyStackOverflows marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(b"Error: file not found", response.data)
self.assertEqual(200, response.status_code)

response = c.post(route, data="1.mp4")
self.assertEqual(b"Success: file exists", response.data)
self.assertEqual(200, response.status_code)

def test_health(self):
route = "/health"
with self.app.test_client() as c:
for method in [c.put, c.post, c.delete, c.trace, c.patch]:
MyStackOverflows marked this conversation as resolved.
Show resolved Hide resolved
response = method(route)
self.assertEqual(self.method_not_allowed, response.data)
self.assertEqual(405, response.status_code)

response = c.get(route)
self.assertEqual(b"{}\n", response.data)
MyStackOverflows marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(200, response.status_code)

if __name__ == "__main__":
unittest.main()
16 changes: 2 additions & 14 deletions tests/back-end/test_video_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ class VideoProcessorTest(unittest.TestCase):

def setUp(self):
self.vp = VideoProcessor()
self.video_path = "app/back-end/video-processing/videos/1.mp4"
self.video_path = "tests/back-end/1.mp4"
MyStackOverflows marked this conversation as resolved.
Show resolved Hide resolved
# hard-coded test values i retrieved from ffprobe for this ^ video
self.fps = 25
self.width = 1280
self.height = 720
self.num_frames = 132

def test_blur_frame(self):
pass

def test_get_frames(self):
frames = self.vp.get_frames(self.video_path)
self.assertEqual(len(frames), self.num_frames, "incorrect number of frames in get_frames")
Expand All @@ -42,22 +39,13 @@ def test_calc_vector_size(self):
self.assertEqual(vector, answer, "incorrect calculations in calc_vector")

def test_img_to_bytes(self):
file_path = "tests/back-end/test.jpg"
file_path = "test.jpg"
MyStackOverflows marked this conversation as resolved.
Show resolved Hide resolved
img = np.zeros((64, 64, 3), np.uint8) # 64x64 black opencv image
cv.imwrite(file_path, img) # write out a black 64x64 image to disk
f = open(file_path, "rb")
self.assertEqual(self.vp.img_to_bytes(img), f.read()) # compare converted bytes to direct read-from-disk bytes
f.close()

def test_get_face(self):
pass

def server_test_unfinished(self):
url = "http://127.0.0.1:5000"
response = requests.post(url, "test")
content = response.content.decode("utf-8")
self.assertEqual(content, "Error: file not found")

if __name__ == "__main__":
unittest.main()