|
|
import unittest
from unittest.mock import patch
import pytest
from django.db import IntegrityError
from django.test import TestCase
from alphabetlearning.resources.factories import PDFPageSnapshotModelFactory
from alphabetlearning.resources.factories import ResourceModelFactory
from alphabetlearning.resources.models import PDFPageSnapshot
from alphabetlearning.resources.models import PDFResource
from alphabetlearning.resources.models import Resource
from alphabetlearning.resources.models import ResourceCategory
from alphabetlearning.resources.models import ResourceType
from alphabetlearning.resources.views import ResourceInfo
from alphabetlearning.resources.views import _extract_metadata_from_resource
@pytest.mark.django_db()
def test_resource_model(resources):
r1 = resources[0]
pdf_on_rw = r1.pdf_resources.first()
pdf_page_snapshot = PDFPageSnapshotModelFactory(pdf_file=pdf_on_rw)
assert Resource.objects.filter(id=r1.id).exists()
assert any("test_" in name for name in r1.get_pdf_file_names())
assert len(r1.get_pdf_file_names()) == 3
assert "pdf_page_snapshot_" in r1.get_pdf_snapshot_file_names()[0]
@pytest.mark.django_db()
def test_can_add_feature_slots_to_resource():
r = ResourceModelFactory()
r.feature_slot = 1
r.save()
assert r.feature_slot == 1
@pytest.mark.django_db()
def test_resource_slot_int_must_be_unique():
"""
Test that a resource feature slot must be unique.
"""
r1 = ResourceModelFactory()
r2 = ResourceModelFactory()
r1.feature_slot = 1
r1.save()
r2.feature_slot = 1
with pytest.raises(IntegrityError):
r2.save()
class ResourceModelTest(TestCase):
def test_string_representation(self):
resource = Resource(name="Test Resource")
assert str(resource) == "Test Resource"
@unittest.skip("Skipping this test for now as it is broken")
class TestExtractMetadata(TestCase):
@patch("resources.views.get_presigned_obj_url")
def test_extract_metadata_from_resource(self, mock_get_url):
# Create mock instances of ResourceType and ResourceCategory
mock_resource_type = ResourceType.objects.create(name="Test Type")
mock_main_category = ResourceCategory.objects.create(name="Test Main Category")
mock_resource = Resource(
name="Test Resource",
thumbnail_filenames=["thumb.jpg", "thumb2.jpg"],
created_at="2022-01-01",
updated_at="2022-01-02",
resource_type=mock_resource_type,
main_resource_category=mock_main_category,
age_range="5-7",
)
mock_get_url.return_value = "https://example.com/url"
result = _extract_metadata_from_resource(mock_resource)
self.assertIsInstance(result, ResourceInfo)
assert result.name == "Test Resource"
assert result.pdf_filenames == "test.pdf"
assert result.thumbnail_filenames in ["thumb.jpg", "thumb2.jpg"]
assert result.created == "2022-01-01"
assert result.updated == "2022-01-02"
@unittest.skip("These tests will not run because they rely upon the view to get file size, etc.")
class TestPDFResourceModel(TestCase):
def setUp(self):
self.resource_type = ResourceType.objects.create(name="Test Resource Type")
self.resource_category = ResourceCategory.objects.create(name="Test Resource Category")
self.resource = Resource.objects.create(
name="Test Resource",
resource_type=self.resource_type,
main_resource_category=self.resource_category,
age_range="5-7",
curriculum="English",
description="Test Description",
)
self.pdf_resource = PDFResource.objects.create(
resource=self.resource,
file_name="resources/tests/testdata/test_small_file.pdf",
)
self.pdf_page_snapshot = PDFPageSnapshot.objects.create(
name="Test Thumbnail Image",
file_name="test_resource_1.jpg",
pdf_file=self.pdf_resource,
)
def test_pdf_resource_string_representation(self):
self.assertEqual(str(self.resource), "Test Resource")
def test_get_pdf_snapshot_filenames(self):
self.assertEqual(self.pdf_resource.snapshot_file_names(), ["test_resource_1.jpg"])
@pytest.mark.django_db()
def test_get_urls_of_resource_snapshot_images(resource):
assert len(resource.thumbnail_filenames) == 3
# crude but it does the job; concatenating a list of URLS into one long sting...
assert "https://ams3.digitaloceanspaces.com" in "".join(resource.thumbnail_urls())
|