aboutsummaryrefslogblamecommitdiffstats
path: root/pyblackbird_cc/resources/tests/test_models.py
blob: 5adcd1457ee9335316c1575ef2b802333a055417 (plain) (tree)
1
2
3
4
5
6


                               
             

                                






                                                                          

 
                        



                                                                      


                                                 
                                               





















                                                                                       
                                             
                                                 


                                                                        





























                                                                                                 
import unittest
from unittest.mock import patch

import pytest
from django.test import TestCase

from pyblackbird_cc.resources.models import PDFPageSnapshot
from pyblackbird_cc.resources.models import PDFResource
from pyblackbird_cc.resources.models import Resource
from pyblackbird_cc.resources.models import ResourceCategory
from pyblackbird_cc.resources.models import ResourceType
from pyblackbird_cc.resources.views import ResourceInfo
from pyblackbird_cc.resources.views import _extract_metadata_from_resource


@pytest.mark.django_db()
def test_resource_model(resources):
    assert Resource.objects.filter(name="Default Resource 1").exists()


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"])