Skip to content

Commit

Permalink
unit test + pydoc
Browse files Browse the repository at this point in the history
  • Loading branch information
vsabatie committed Nov 27, 2023
1 parent 4e3fd61 commit 02a598a
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 4 deletions.
1 change: 1 addition & 0 deletions pod/dressing/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class DressingAdmin(admin.ModelAdmin):
"""Dressing admin page."""
form = DressingForm
list_display = ("title", "watermark", "opacity", "position",
"opening_credits", "ending_credits")
Expand Down
4 changes: 4 additions & 0 deletions pod/dressing/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ class AddVideoHoldWidget(s2forms.ModelSelect2Widget):


class DressingForm(forms.ModelForm):
"""Form to add or edit a dressing."""
is_staff = True
is_superuser = False
admin_form = True
site = forms.ModelChoiceField(Site.objects.all(), required=False)

def __init__(self, *args, **kwargs):
"""Init method."""
self.is_staff = (
kwargs.pop("is_staff") if "is_staff" in kwargs.keys() else self.is_staff
)
Expand Down Expand Up @@ -91,6 +93,7 @@ def __init__(self, *args, **kwargs):
self.fields["ending_credits"].queryset = query_videos.all()

class Meta(object):
"""Meta class."""
model = Dressing
fields = "__all__"
exclude = ['videos']
Expand All @@ -104,6 +107,7 @@ class Meta(object):


class DressingDeleteForm(forms.Form):
"""Form to delete a dressing."""
agree = forms.BooleanField(
label=_("I agree"),
help_text=_("Delete dressing cannot be undo"),
Expand Down
2 changes: 2 additions & 0 deletions pod/dressing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ class Dressing(models.Model):
)

class Meta:
"""Metadata for Dressing model."""
verbose_name = _("Video dressing")
verbose_name_plural = _("Video dressings")

def to_json(self):
"""Convert to json format for encoding logs"""
return {
"id": self.id,
"title": self.title,
Expand Down
3 changes: 0 additions & 3 deletions pod/dressing/test.py

This file was deleted.

Empty file added pod/dressing/tests/__init__.py
Empty file.
104 changes: 104 additions & 0 deletions pod/dressing/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""Unit tests for dressing models."""

import os
from django.test import TestCase
from django.contrib.auth.models import User
from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from pod.video.models import Type, Video
from pod.dressing.models import Dressing

if getattr(settings, "USE_PODFILE", False):
from pod.podfile.models import CustomImageModel
from pod.podfile.models import UserFolder

FILEPICKER = True
else:
FILEPICKER = False
from pod.main.models import CustomImageModel


class DressingModelTest(TestCase):
"""Test case for Pod dressing models."""
def setUp(self):
owner = User.objects.create(username="pod")
currentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
simplefile = SimpleUploadedFile(
name="testimage.jpg",
content=open(os.path.join(currentdir, "tests", "testimage.jpg"), "rb").read(),
content_type="image/jpeg",
)

home = UserFolder.objects.get(name="home", owner=owner)
if FILEPICKER:
home = UserFolder.objects.get(name="home", owner=owner)
customImage = CustomImageModel.objects.create(
name="testimage",
description="testimage",
created_by=owner,
folder=home,
file=simplefile,
)
else:
customImage = CustomImageModel.objects.create(file=simplefile)
videotype = Type.objects.create(title="others")
video = Video.objects.create(
title="video",
type=videotype,
owner=owner,
video="test.mp4",
duration=20,
)
video2 = Video.objects.create(
title="video2",
type=videotype,
owner=owner,
video="test2.mp4",
duration=20,
)
dressing = Dressing.objects.create(
title='Test Dressing',
watermark=customImage,
position=Dressing.TOP_RIGHT,
opacity=50,
opening_credits=video,
ending_credits=video2,
)
dressing.owners.set([owner])
dressing.users.set([owner])

def test_attributs_full(self):
dressing = Dressing.objects.get(id=1)
owner = User.objects.get(username="pod")
video = Video.objects.get(id=1)
video2 = Video.objects.get(id=2)
self.assertEqual(dressing.title, 'Test Dressing')
self.assertEqual(list(dressing.owners.all()), [owner])
self.assertEqual(list(dressing.users.all()), [owner])
self.assertEqual(list(dressing.allow_to_groups.all()), [])
self.assertTrue("testimage" in dressing.watermark.name)
self.assertEqual(dressing.position, Dressing.TOP_RIGHT)
self.assertEqual(dressing.opacity, 50)
self.assertEqual(dressing.opening_credits, video)
self.assertEqual(dressing.ending_credits, video2)

def test_dressing_to_json(self):
dressing = Dressing.objects.get(id=1)
dressing_json = dressing.to_json()
owner = User.objects.get(username="pod")
video = Video.objects.get(id=1)
video2 = Video.objects.get(id=2)

expected_json = {
"id": dressing.id,
"title": "Test Dressing",
"allow_to_groups": [],
"owners": [owner.id],
"users": [owner.id],
"watermark": dressing.watermark.file.url,
"position": "En haut à droite",
"opacity": 50,
"opening_credits": video.slug,
"ending_credits": video2.slug,
}
self.assertEqual(dressing_json, expected_json)
1 change: 1 addition & 0 deletions pod/dressing/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Unit tests for dressing views."""
Binary file added pod/dressing/tests/testimage.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions pod/dressing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


def get_position_value(position, height):
"""Obtain dimensions proportional to the video format."""
height = str(float(height) * 0.05)
if position == 'top_right':
return 'overlay=main_w-overlay_w-' + height + ':' + height
Expand All @@ -16,6 +17,7 @@ def get_position_value(position, height):


def get_dressing_input(dressing, FFMPEG_DRESSING_INPUT):
"""Function to obtain the files necessary for encoding a dressed video"""
command = ''
if dressing.watermark:
command += FFMPEG_DRESSING_INPUT % {
Expand Down
4 changes: 3 additions & 1 deletion pod/dressing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def video_dressing(request, slug):
@csrf_protect
@login_required(redirect_field_name="referrer")
def dressing_edit(request, dressing_id):
"""Edit a dressing object."""
dressing_edit = get_object_or_404(Dressing, id=dressing_id)
form_dressing = DressingForm(
instance=dressing_edit,
Expand Down Expand Up @@ -86,6 +87,7 @@ def dressing_edit(request, dressing_id):
@csrf_protect
@login_required(redirect_field_name="referrer")
def dressing_create(request):
"""Create a dressing object."""
if request.method == 'POST':
form_dressing = DressingForm(request.POST, user=request.user)
if form_dressing.is_valid():
Expand Down Expand Up @@ -129,7 +131,7 @@ def dressing_delete(request, dressing_id):
@csrf_protect
@login_required(redirect_field_name="referrer")
def my_dressings(request):
"""Render the logged user's dressing"""
"""Render the logged user's dressings."""
if in_maintenance():
return redirect(reverse("maintenance"))
user = request.user
Expand Down
3 changes: 3 additions & 0 deletions pod/video_encode_transcript/Encoding_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,14 @@ def get_hls_command(self):
return hls_command

def get_dressing_file(self):
"""Create or replace the dressed video file."""
dirname = os.path.dirname(self.video_file)
filename, ext = os.path.splitext(os.path.basename(self.video_file))
output_file = os.path.join(dirname, filename + "_dressing" + ext)
return output_file

def get_dressing_command(self):
"""Get the command based on the dressing object parameters"""
height = str(list(self.list_video_track.items())[0][1]["height"])
order_opening_credits = 0
dressing_command_params = '[vid][0:a]'
Expand Down Expand Up @@ -462,6 +464,7 @@ def get_dressing_command(self):
return dressing_command

def encode_video_dressing(self):
"""Encode the dressed video."""
dressing_command = self.get_dressing_command()
return_value, return_msg = launch_cmd(dressing_command)
self.add_encoding_log("dressing_command", dressing_command, return_value,
Expand Down

0 comments on commit 02a598a

Please sign in to comment.