Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic committed Jul 6, 2024
1 parent 94c8d52 commit 3df6fdd
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/test_bynderassetcopymixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import responses

from django.db import IntegrityError
from django.http import HttpRequest
from django.test import TestCase
from testapp.factories import CustomImageFactory
from wagtail.images import get_image_model
from wagtail_factories import ImageFactory

Expand All @@ -17,8 +20,13 @@
class BynderAssetCopyMixinTests(TestCase):
def setUp(self):
super().setUp()
request = HttpRequest()
request.method = ""
request.path = "/"

self.view = BynderAssetCopyMixin()
self.view.model = get_image_model()
self.view.setup(request)
# Mock out Bynder API calls
responses.add(
responses.GET,
Expand Down Expand Up @@ -94,3 +102,61 @@ def test_update_object_when_object_is_outdated(self):
is_up_to_date_mock.assert_called_once_with(TEST_ASSET_DATA)
update_from_asset_data_mock.assert_called_once_with(TEST_ASSET_DATA)
save_mock.assert_called_once()

@responses.activate
def test_create_object_clash_handling_after_update(self):
# Create an image with a matching bynder_id
existing_image = CustomImageFactory.create(bynder_id=TEST_ASSET_ID)

# Trigger the test target directly
with (
mock.patch(
self.view,
"testapp.models.CustomImage.update_from_asset_data",
) as update_from_asset_data_mock,
):
result = self.view.create_object(TEST_ASSET_ID)

# Check behavior and result
update_from_asset_data_mock.assert_called_once()
self.assertEqual(result, existing_image)

@responses.activate
def test_create_object_clash_handling_on_save(self):
def erroneous_save_method(*args):
# Create an image with a matching bynder_id
CustomImageFactory.create(bynder_id=TEST_ASSET_ID)
raise IntegrityError("bynder_id must be unique")

# Trigger the test target directly
with (
mock.patch(
"testapp.models.CustomImage.save",
side_effect=erroneous_save_method,
return_value=None,
) as update_from_asset_data_mock,
):
result = self.view.create_object(TEST_ASSET_ID)

# Check behavior and result
update_from_asset_data_mock.assert_called_once()
self.assertEqual(result.bynder_id, TEST_ASSET_ID)

@responses.activate
def test_create_object_clash_handling_on_save_when_bynder_id_match_not_found(self):
def erroneous_save_method():
raise IntegrityError("Some other field must be unique")

# Trigger the test target directly
with (
mock.patch(
"testapp.models.CustomImage.save",
side_effect=erroneous_save_method,
return_value=None,
) as update_from_asset_data_mock,
self.assertRaises(IntegrityError),
):
self.view.create_object(TEST_ASSET_ID)

# Check behavior
update_from_asset_data_mock.assert_called_once()

0 comments on commit 3df6fdd

Please sign in to comment.