# Path: tests/testapp/models.py
# class EventPage(Page):
#     date_from = models.DateField("Start date", null=True)
#     date_to = models.DateField(
#         "End date",
#         null=True,
#         blank=True,
#         help_text="Not required if event is on a single day",
#     )
#     time_from = models.TimeField("Start time", null=True, blank=True)
#     time_to = models.TimeField("End time", null=True, blank=True)
#     location = models.CharField(max_length=255)
#     body = RichTextField(blank=True)
#     cost = models.CharField(max_length=255)
#     signup_link = models.URLField(blank=True)
# 
# class EventPageRelatedMedia(Orderable):
#     page = ParentalKey(
#         "wagtailmedia_tests.EventPage",
#         related_name="related_media",
#         on_delete=models.CASCADE,
#     )
#     title = models.CharField(max_length=255, help_text="Link title")
#     link_media = models.ForeignKey(
#         "wagtailmedia.Media",
#         null=True,
#         blank=True,
#         related_name="+",
#         on_delete=models.CASCADE,
#     )
# 
#     @property
#     def link(self):
#         return self.link_media.url
# 
#     panels = [
#         FieldPanel("title"),
#         MediaChooserPanel("link_media"),
#     ]


import json
import os
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.core.files.base import ContentFile
from django.forms.utils import ErrorDict
from django.test import TestCase, modify_settings
from django.test.utils import override_settings
from django.urls import NoReverseMatch, reverse
from wagtail.core.models import Collection, GroupCollectionPermission, Page
from wagtail.tests.utils import WagtailTestUtils
from six import b
from tests.testapp.models import EventPage, EventPageRelatedMedia
from wagtailmedia import models
                "media-chooser-upload-title": "Test video",
                "media-chooser-upload-file": ContentFile(
                    b("A boring example"), name="video.avi"
                ),
                "media-chooser-upload-duration": "100",
            },
        )

        # Check response
        self.assertEqual(response.status_code, 200)

        # Check that the video was created
        self.assertTrue(models.Media.objects.filter(title="Test video").exists())


class TestUsageCount(TestCase, WagtailTestUtils):
    fixtures = ["test.json"]

    def setUp(self):
        self.login()

    @override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
    def test_unused_media_usage_count(self):
        media = models.Media.objects.get(id=1)
        self.assertEqual(media.get_usage().count(), 0)

    @override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
    def test_used_media_usage_count(self):
        media = models.Media.objects.get(id=1)
        page = EventPage.objects.get(id=3)

# Path: tests/testapp/models.py
# class EventPage(Page):
#     date_from = models.DateField("Start date", null=True)
#     date_to = models.DateField(
#         "End date",
#         null=True,
#         blank=True,
#         help_text="Not required if event is on a single day",
#     )
#     time_from = models.TimeField("Start time", null=True, blank=True)
#     time_to = models.TimeField("End time", null=True, blank=True)
#     location = models.CharField(max_length=255)
#     body = RichTextField(blank=True)
#     cost = models.CharField(max_length=255)
#     signup_link = models.URLField(blank=True)
# 
# class EventPageRelatedMedia(Orderable):
#     page = ParentalKey(
#         "wagtailmedia_tests.EventPage",
#         related_name="related_media",
#         on_delete=models.CASCADE,
#     )
#     title = models.CharField(max_length=255, help_text="Link title")
#     link_media = models.ForeignKey(
#         "wagtailmedia.Media",
#         null=True,
#         blank=True,
#         related_name="+",
#         on_delete=models.CASCADE,
#     )
# 
#     @property
#     def link(self):
#         return self.link_media.url
# 
#     panels = [
#         FieldPanel("title"),
#         MediaChooserPanel("link_media"),
#     ]


# Path: tests/test_views.py
import json
import os
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.core.files.base import ContentFile
from django.forms.utils import ErrorDict
from django.test import TestCase, modify_settings
from django.test.utils import override_settings
from django.urls import NoReverseMatch, reverse
from wagtail.core.models import Collection, GroupCollectionPermission, Page
from wagtail.tests.utils import WagtailTestUtils
from six import b
from tests.testapp.models import EventPage, EventPageRelatedMedia
from wagtailmedia import models

                "media-chooser-upload-title": "Test video",
                "media-chooser-upload-file": ContentFile(
                    b("A boring example"), name="video.avi"
                ),
                "media-chooser-upload-duration": "100",
            },
        )

        # Check response
        self.assertEqual(response.status_code, 200)

        # Check that the video was created
        self.assertTrue(models.Media.objects.filter(title="Test video").exists())


class TestUsageCount(TestCase, WagtailTestUtils):
    fixtures = ["test.json"]

    def setUp(self):
        self.login()

    @override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
    def test_unused_media_usage_count(self):
        media = models.Media.objects.get(id=1)
        self.assertEqual(media.get_usage().count(), 0)

    @override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
    def test_used_media_usage_count(self):
        media = models.Media.objects.get(id=1)
        page = EventPage.objects.get(id=3)

        event_page_related_link = EventPageRelatedMedia()