aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/resources/tests/test_models.py
blob: 56416f4a6f0351b4b53d6adec9242e5f180b30ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import unittest
from unittest.mock import patch

import pytest
from django.test import TestCase

from pyblackbird_cc.resources.factories import PDFPageSnapshotModelFactory
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

# TODO - convert this test into the test of a function
# that takes a Resource object and returns all the PDF snapshots
# file names


@pytest.mark.django_db()
def test_resource_model(resources):
    # Create a resource
    r1 = resources[0]
    # Get the first pdf resource on the resource
    pdf_on_rw = r1.pdf_resources.first()
    # Create a single pdf_page_shapshot and associate it with the pdf resource on the resource
    pdf_page_snapshot = PDFPageSnapshotModelFactory(pdf_file=pdf_on_rw)
    assert Resource.objects.filter(name="Default Resource 1").exists()
    assert "test_0.pdf" in r1.get_pdf_file_names()
    assert "test_1.pdf" in r1.get_pdf_file_names()
    assert "test_2.pdf" in r1.get_pdf_file_names()
    assert "test_3.pdf" not in r1.get_pdf_file_names()
    assert "pdf_page_snapshot_0.jpg" in r1.get_pdf_snapshot_file_names()


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