Spaces:
Runtime error
Runtime error
Commit
·
bcee635
1
Parent(s):
1db942a
resolve test
Browse files- app/routers/friend_request.py +15 -5
- app/routers/me.py +0 -5
- app/routers/video.py +6 -5
- tests/test_video.py +79 -63
app/routers/friend_request.py
CHANGED
@@ -68,15 +68,21 @@ async def acceptRequest(RequestId: str, user=Depends(get_current_user)):
|
|
68 |
fr = fr_ref.get()
|
69 |
|
70 |
if not fr.exists:
|
71 |
-
raise HTTPException(
|
|
|
|
|
72 |
|
73 |
fr = fr.to_dict()
|
74 |
|
75 |
if isRequestExpired(fr):
|
76 |
-
raise HTTPException(
|
|
|
|
|
77 |
|
78 |
if isRequestDone(fr):
|
79 |
-
raise HTTPException(
|
|
|
|
|
80 |
|
81 |
if isInviter(user, fr):
|
82 |
if isInviteeEmpty(fr):
|
@@ -113,13 +119,17 @@ def deleteRequest(RequestId: str, user=Depends(get_current_user)):
|
|
113 |
Request = Request_ref.get()
|
114 |
|
115 |
if not Request.exists:
|
116 |
-
raise HTTPException(
|
|
|
|
|
117 |
Request = Request.to_dict()
|
118 |
if isInviter(user, Request):
|
119 |
Request_ref.delete()
|
120 |
return {"status": "OK"}
|
121 |
else:
|
122 |
-
raise HTTPException(
|
|
|
|
|
123 |
|
124 |
|
125 |
def isRequestExpired(request):
|
|
|
68 |
fr = fr_ref.get()
|
69 |
|
70 |
if not fr.exists:
|
71 |
+
raise HTTPException(
|
72 |
+
status_code=status.HTTP_404_NOT_FOUND, detail="Friend request not found"
|
73 |
+
)
|
74 |
|
75 |
fr = fr.to_dict()
|
76 |
|
77 |
if isRequestExpired(fr):
|
78 |
+
raise HTTPException(
|
79 |
+
status_code=status.HTTP_409_CONFLICT, detail="Friend request expired"
|
80 |
+
)
|
81 |
|
82 |
if isRequestDone(fr):
|
83 |
+
raise HTTPException(
|
84 |
+
status_code=status.HTTP_409_CONFLICT, detail="Friend request already done"
|
85 |
+
)
|
86 |
|
87 |
if isInviter(user, fr):
|
88 |
if isInviteeEmpty(fr):
|
|
|
119 |
Request = Request_ref.get()
|
120 |
|
121 |
if not Request.exists:
|
122 |
+
raise HTTPException(
|
123 |
+
status_code=status.HTTP_404_NOT_FOUND, detail="Friend request not found"
|
124 |
+
)
|
125 |
Request = Request.to_dict()
|
126 |
if isInviter(user, Request):
|
127 |
Request_ref.delete()
|
128 |
return {"status": "OK"}
|
129 |
else:
|
130 |
+
raise HTTPException(
|
131 |
+
status_code=status.HTTP_403_FORBIDDEN, detail="You are not inviter"
|
132 |
+
)
|
133 |
|
134 |
|
135 |
def isRequestExpired(request):
|
app/routers/me.py
CHANGED
@@ -9,8 +9,3 @@ router = APIRouter(prefix="/me", tags=["Me"])
|
|
9 |
@router.get("")
|
10 |
def getProfile(current_user=Depends(get_current_user)):
|
11 |
return current_user
|
12 |
-
|
13 |
-
|
14 |
-
@router.get("/friends")
|
15 |
-
def getFriends(current_user=Depends(get_current_user)):
|
16 |
-
raise NotImplementedError()
|
|
|
9 |
@router.get("")
|
10 |
def getProfile(current_user=Depends(get_current_user)):
|
11 |
return current_user
|
|
|
|
|
|
|
|
|
|
app/routers/video.py
CHANGED
@@ -11,6 +11,7 @@ from fastapi import (
|
|
11 |
APIRouter,
|
12 |
Depends,
|
13 |
HTTPException,
|
|
|
14 |
UploadFile,
|
15 |
BackgroundTasks,
|
16 |
status,
|
@@ -26,12 +27,11 @@ from app import logger
|
|
26 |
router = APIRouter(prefix="/video", tags=["Video"])
|
27 |
|
28 |
|
29 |
-
@router.post("")
|
30 |
async def handleVideoRequest(
|
31 |
file: UploadFile,
|
32 |
background_tasks: BackgroundTasks,
|
33 |
threshold: float = 0.3,
|
34 |
-
user=Depends(get_current_user),
|
35 |
):
|
36 |
if re.search("^video\/", file.content_type) is None:
|
37 |
raise HTTPException(
|
@@ -46,7 +46,7 @@ async def handleVideoRequest(
|
|
46 |
)
|
47 |
os.mkdir(id)
|
48 |
async with aiofiles.open(os.path.join(id, "input.mp4"), "wb") as out_file:
|
49 |
-
while content := await file.read(
|
50 |
await out_file.write(content)
|
51 |
background_tasks.add_task(inferenceVideo, artifact_ref.id, id, threshold)
|
52 |
return id + ".mp4"
|
@@ -66,7 +66,7 @@ def createThumbnail(thumbnail, inputDir):
|
|
66 |
cv2.imwrite(os.path.join(inputDir, "thumbnail.jpg"), thumbnail)
|
67 |
|
68 |
|
69 |
-
def
|
70 |
cap = cv2.VideoCapture(
|
71 |
filename=os.path.join(inputDir, "input.mp4"), apiPreference=cv2.CAP_FFMPEG
|
72 |
)
|
@@ -103,9 +103,10 @@ def inference_frame(inputDir, threshold: float = 0.3):
|
|
103 |
|
104 |
|
105 |
async def inferenceVideo(artifactId: str, inputDir: str, threshold: float):
|
|
|
106 |
try:
|
107 |
Process(updateArtifact(artifactId, {"status": "processing"})).start()
|
108 |
-
thumbnail =
|
109 |
createThumbnail(thumbnail, inputDir)
|
110 |
|
111 |
async def uploadVideo():
|
|
|
11 |
APIRouter,
|
12 |
Depends,
|
13 |
HTTPException,
|
14 |
+
Request,
|
15 |
UploadFile,
|
16 |
BackgroundTasks,
|
17 |
status,
|
|
|
27 |
router = APIRouter(prefix="/video", tags=["Video"])
|
28 |
|
29 |
|
30 |
+
@router.post("", dependencies=[Depends(get_current_user)])
|
31 |
async def handleVideoRequest(
|
32 |
file: UploadFile,
|
33 |
background_tasks: BackgroundTasks,
|
34 |
threshold: float = 0.3,
|
|
|
35 |
):
|
36 |
if re.search("^video\/", file.content_type) is None:
|
37 |
raise HTTPException(
|
|
|
46 |
)
|
47 |
os.mkdir(id)
|
48 |
async with aiofiles.open(os.path.join(id, "input.mp4"), "wb") as out_file:
|
49 |
+
while content := await file.read(102400):
|
50 |
await out_file.write(content)
|
51 |
background_tasks.add_task(inferenceVideo, artifact_ref.id, id, threshold)
|
52 |
return id + ".mp4"
|
|
|
66 |
cv2.imwrite(os.path.join(inputDir, "thumbnail.jpg"), thumbnail)
|
67 |
|
68 |
|
69 |
+
def inferenceFrame(inputDir, threshold: float = 0.3):
|
70 |
cap = cv2.VideoCapture(
|
71 |
filename=os.path.join(inputDir, "input.mp4"), apiPreference=cv2.CAP_FFMPEG
|
72 |
)
|
|
|
103 |
|
104 |
|
105 |
async def inferenceVideo(artifactId: str, inputDir: str, threshold: float):
|
106 |
+
logger.info("Start inference video")
|
107 |
try:
|
108 |
Process(updateArtifact(artifactId, {"status": "processing"})).start()
|
109 |
+
thumbnail = inferenceFrame(inputDir, threshold=threshold)
|
110 |
createThumbnail(thumbnail, inputDir)
|
111 |
|
112 |
async def uploadVideo():
|
tests/test_video.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
from fastapi.testclient import TestClient
|
2 |
from fastapi.routing import APIRoute
|
3 |
-
from app.routers.video import updateArtifact, createThumbnail,
|
4 |
from app.main import app
|
5 |
from app.constants import deviceId
|
6 |
from app import db
|
7 |
-
import platform
|
8 |
import mmcv
|
9 |
import os
|
10 |
import pytest
|
@@ -14,120 +13,137 @@ import cv2
|
|
14 |
import shutil
|
15 |
from google.cloud.firestore_v1.base_query import FieldFilter
|
16 |
|
|
|
17 |
def endpoints():
|
18 |
endpoints = []
|
19 |
for route in app.routes:
|
20 |
if isinstance(route, APIRoute):
|
21 |
endpoints.append(route.path)
|
22 |
return endpoints
|
|
|
|
|
23 |
@pytest.fixture
|
24 |
def client():
|
25 |
client = TestClient(app, "http://0.0.0.0:3000")
|
26 |
yield client
|
|
|
|
|
27 |
@pytest.fixture
|
28 |
def user():
|
29 |
-
url =
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
39 |
response = requests.request("POST", url, headers=headers, data=payload)
|
40 |
data = response.json()
|
41 |
-
user = {"id": data[
|
42 |
-
db.collection("user").document(user[
|
43 |
yield user
|
44 |
-
db.collection("user").document(user[
|
|
|
|
|
45 |
class TestVideoAPI:
|
46 |
-
@pytest.mark.skipif("/video" not in endpoints(),reason="Route not defined")
|
47 |
def test_video_API(self, user, client):
|
48 |
# Test when no token is pass to route
|
49 |
payload = {}
|
50 |
-
files=[
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
response = client.request("POST", 'video', headers=headers, data=payload, files=files)
|
56 |
assert response.status_code == 403
|
57 |
# Test when a dummy (not valid) token passed
|
58 |
payload = {}
|
59 |
-
files=[
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
}
|
65 |
-
response = client.request("POST", 'video', headers=headers, data=payload, files=files)
|
66 |
assert response.status_code == 401
|
67 |
# Test when sent file is not a video
|
68 |
payload = {}
|
69 |
-
files=[
|
70 |
-
|
71 |
]
|
72 |
-
headers = {
|
73 |
-
'Authorization': "Bearer " + user['token']
|
74 |
-
}
|
75 |
while True:
|
76 |
-
response = client.request(
|
|
|
|
|
77 |
if response.status_code != 401:
|
78 |
break
|
79 |
assert response.status_code == 400
|
80 |
# Test when all requirements have been fulfilled
|
81 |
payload = {}
|
82 |
-
files=[
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
}
|
88 |
-
response = client.request("POST", 'video', headers=headers, data=payload, files=files)
|
89 |
assert response.status_code == 200
|
90 |
artifactName = response.text
|
91 |
-
docs =
|
|
|
|
|
|
|
|
|
92 |
index = 0
|
93 |
for doc in docs:
|
94 |
# For each document in docs. Verify name and status of the artifact
|
95 |
index += 1
|
96 |
data = doc.get().to_dict()
|
97 |
-
assert data[
|
98 |
-
assert data[
|
99 |
assert index == 1
|
100 |
doc.delete()
|
|
|
101 |
def test_update_artifact(self):
|
102 |
# Check and preprocess test data before testing
|
103 |
-
test_artifact = db.collection("artifacts").document(
|
104 |
if not test_artifact.get().exists:
|
105 |
-
db.collection("artifacts").document(
|
106 |
-
|
|
|
|
|
107 |
else:
|
108 |
-
test_artifact.update({"status": "testing",
|
109 |
# Testing update on each field
|
110 |
-
updateArtifact(test_artifact.id,{"status": "test_done"})
|
111 |
-
assert
|
112 |
-
|
|
|
|
|
|
|
113 |
test_artifact.delete()
|
|
|
114 |
def test_inference_frame(self):
|
115 |
-
if not os.path.exists(
|
116 |
-
os.mkdir(
|
117 |
-
shutil.copyfile(
|
118 |
-
thumbnail =
|
119 |
-
assert os.path.exists("test_vid/out.mp4") and os.path.isfile(
|
120 |
-
vidcap = cv2.VideoCapture(
|
121 |
success, image = vidcap.read()
|
122 |
if success:
|
123 |
-
assert
|
124 |
vidcap.release()
|
125 |
del vidcap
|
126 |
-
shutil.rmtree(
|
|
|
127 |
def test_create_thumbnal(self):
|
128 |
-
vidcap = cv2.VideoCapture(
|
129 |
success, image = vidcap.read()
|
130 |
if success:
|
131 |
createThumbnail(image, "")
|
132 |
-
result = mmcv.imread(
|
133 |
-
assert
|
|
|
1 |
from fastapi.testclient import TestClient
|
2 |
from fastapi.routing import APIRoute
|
3 |
+
from app.routers.video import updateArtifact, createThumbnail, inferenceFrame
|
4 |
from app.main import app
|
5 |
from app.constants import deviceId
|
6 |
from app import db
|
|
|
7 |
import mmcv
|
8 |
import os
|
9 |
import pytest
|
|
|
13 |
import shutil
|
14 |
from google.cloud.firestore_v1.base_query import FieldFilter
|
15 |
|
16 |
+
|
17 |
def endpoints():
|
18 |
endpoints = []
|
19 |
for route in app.routes:
|
20 |
if isinstance(route, APIRoute):
|
21 |
endpoints.append(route.path)
|
22 |
return endpoints
|
23 |
+
|
24 |
+
|
25 |
@pytest.fixture
|
26 |
def client():
|
27 |
client = TestClient(app, "http://0.0.0.0:3000")
|
28 |
yield client
|
29 |
+
|
30 |
+
|
31 |
@pytest.fixture
|
32 |
def user():
|
33 |
+
url = (
|
34 |
+
"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key="
|
35 |
+
+ os.environ.get("FIREBASE_API_KEY")
|
36 |
+
)
|
37 |
+
|
38 |
+
payload = json.dumps(
|
39 |
+
{
|
40 |
+
"email": "[email protected]",
|
41 |
+
"password": "testing",
|
42 |
+
"returnSecureToken": True,
|
43 |
+
}
|
44 |
+
)
|
45 |
+
headers = {"Content-Type": "application/json"}
|
46 |
response = requests.request("POST", url, headers=headers, data=payload)
|
47 |
data = response.json()
|
48 |
+
user = {"id": data["localId"], "token": data["idToken"]}
|
49 |
+
db.collection("user").document(user["id"]).set({"deviceId": deviceId})
|
50 |
yield user
|
51 |
+
db.collection("user").document(user["id"]).delete()
|
52 |
+
|
53 |
+
|
54 |
class TestVideoAPI:
|
55 |
+
@pytest.mark.skipif("/video" not in endpoints(), reason="Route not defined")
|
56 |
def test_video_API(self, user, client):
|
57 |
# Test when no token is pass to route
|
58 |
payload = {}
|
59 |
+
files = [("file", ("demo.mp4", open("demo.mp4", "rb"), "video/mp4"))]
|
60 |
+
headers = {}
|
61 |
+
response = client.request(
|
62 |
+
"POST", "video", headers=headers, data=payload, files=files
|
63 |
+
)
|
|
|
64 |
assert response.status_code == 403
|
65 |
# Test when a dummy (not valid) token passed
|
66 |
payload = {}
|
67 |
+
files = [("file", ("demo.mp4", open("demo.mp4", "rb"), "video/mp4"))]
|
68 |
+
headers = {"Authorization": "Bearer saikoljncaskljnfckjnasckjna"}
|
69 |
+
response = client.request(
|
70 |
+
"POST", "video", headers=headers, data=payload, files=files
|
71 |
+
)
|
|
|
|
|
72 |
assert response.status_code == 401
|
73 |
# Test when sent file is not a video
|
74 |
payload = {}
|
75 |
+
files = [
|
76 |
+
("file", ("demo.jpg", open("demo.jpg", "rb"), "application/octet-stream"))
|
77 |
]
|
78 |
+
headers = {"Authorization": "Bearer " + user["token"]}
|
|
|
|
|
79 |
while True:
|
80 |
+
response = client.request(
|
81 |
+
"POST", "video", headers=headers, data=payload, files=files
|
82 |
+
)
|
83 |
if response.status_code != 401:
|
84 |
break
|
85 |
assert response.status_code == 400
|
86 |
# Test when all requirements have been fulfilled
|
87 |
payload = {}
|
88 |
+
files = [("file", ("demo.mp4", open("demo.mp4", "rb"), "video/mp4"))]
|
89 |
+
headers = {"Authorization": "Bearer " + user["token"]}
|
90 |
+
response = client.request(
|
91 |
+
"POST", "video", headers=headers, data=payload, files=files
|
92 |
+
)
|
|
|
|
|
93 |
assert response.status_code == 200
|
94 |
artifactName = response.text
|
95 |
+
docs = (
|
96 |
+
db.collection("artifacts")
|
97 |
+
.where(filter=FieldFilter("name", "==", artifactName))
|
98 |
+
.stream()
|
99 |
+
)
|
100 |
index = 0
|
101 |
for doc in docs:
|
102 |
# For each document in docs. Verify name and status of the artifact
|
103 |
index += 1
|
104 |
data = doc.get().to_dict()
|
105 |
+
assert data["name"] == artifactName
|
106 |
+
assert data["status"] == "pending"
|
107 |
assert index == 1
|
108 |
doc.delete()
|
109 |
+
|
110 |
def test_update_artifact(self):
|
111 |
# Check and preprocess test data before testing
|
112 |
+
test_artifact = db.collection("artifacts").document("test")
|
113 |
if not test_artifact.get().exists:
|
114 |
+
db.collection("artifacts").document("test").set(
|
115 |
+
{"name": "test", "path": "", "status": "testing", "thumbnailURL": ""}
|
116 |
+
)
|
117 |
+
test_artifact = db.collection("artifacts").document("test")
|
118 |
else:
|
119 |
+
test_artifact.update({"status": "testing", "path": "", "thumbnailURL": ""})
|
120 |
# Testing update on each field
|
121 |
+
updateArtifact(test_artifact.id, {"status": "test_done"})
|
122 |
+
assert (
|
123 |
+
db.collection("artifacts").document("test").get().to_dict()["status"]
|
124 |
+
== "test_done"
|
125 |
+
)
|
126 |
+
# Delete data for next time test
|
127 |
test_artifact.delete()
|
128 |
+
|
129 |
def test_inference_frame(self):
|
130 |
+
if not os.path.exists("test_vid"):
|
131 |
+
os.mkdir("test_vid")
|
132 |
+
shutil.copyfile("demo.mp4", "test_vid/input.mp4")
|
133 |
+
thumbnail = inferenceFrame("test_vid")
|
134 |
+
assert os.path.exists("test_vid/out.mp4") and os.path.isfile("test_vid/out.mp4")
|
135 |
+
vidcap = cv2.VideoCapture("test_vid/input.mp4")
|
136 |
success, image = vidcap.read()
|
137 |
if success:
|
138 |
+
assert image.shape == thumbnail.shape
|
139 |
vidcap.release()
|
140 |
del vidcap
|
141 |
+
shutil.rmtree("test_vid")
|
142 |
+
|
143 |
def test_create_thumbnal(self):
|
144 |
+
vidcap = cv2.VideoCapture("demo.mp4")
|
145 |
success, image = vidcap.read()
|
146 |
if success:
|
147 |
createThumbnail(image, "")
|
148 |
+
result = mmcv.imread("thumbnail.jpg", channel_order="rgb")
|
149 |
+
assert result.shape == (160, 160, 3)
|