Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed Feb 5, 2025
1 parent a24a9e7 commit 2fec316
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
39 changes: 39 additions & 0 deletions test/helper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import sys
import time
import traceback
import unittest
from contextlib import contextmanager
from datetime import timedelta, tzinfo
from functools import wraps

import six
from urllib3 import HTTPResponse
from urllib3._collections import HTTPHeaderDict
from collections import defaultdict

from cloudinary import utils, logger, api
from cloudinary.exceptions import NotFound
Expand Down Expand Up @@ -249,3 +251,40 @@ def should_test_addon(addon):
return True
cld_test_addons_list = [addon_name.strip() for addon_name in cld_test_addons.split(',')]
return addon in cld_test_addons_list


class CldTestCase(unittest.TestCase):
"""
A custom test case class that extends unittest.TestCase.
It provides the assertCountEqual method for Python 2.7 compatibility,
handling unhashable elements by serializing them.
"""

if six.PY2:
def assertCountEqual(self, list1, list2, msg=None):
"""
Fail if two sequences do not contain the same elements the same number of times,
regardless of their order. Handles unhashable elements by serializing them.
This is a compatibility method for Python 2.7.
"""

def serialize_item(item):
try:
# Attempt to serialize the item to a JSON string
return json.dumps(item, sort_keys=True)
except (TypeError, ValueError):
# Fallback: use the string representation
return str(item)

def count_elements(lst):
counts = defaultdict(int)
for item in lst:
serialized = serialize_item(item)
counts[serialized] += 1
return counts

count1 = count_elements(list1)
count2 = count_elements(list2)
if count1 != count2:
standard_msg = '%s != %s' % (count1, count2)
self.fail(self._formatMessage(msg, standard_msg))
10 changes: 5 additions & 5 deletions test/test_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from test.helper_test import uploader_response_mock, SUFFIX, TEST_IMAGE, get_params, get_headers, TEST_ICON, TEST_DOC, \
REMOTE_TEST_IMAGE, UTC, populate_large_file, TEST_UNICODE_IMAGE, get_uri, get_method, get_param, \
cleanup_test_resources_by_tag, cleanup_test_transformation, cleanup_test_resources, EVAL_STR, ON_SUCCESS_STR, \
URLLIB3_REQUEST, patch, retry_assertion
URLLIB3_REQUEST, patch, retry_assertion, CldTestCase
from test.test_utils import TEST_ID, TEST_FOLDER

MOCK_RESPONSE = uploader_response_mock()
Expand Down Expand Up @@ -77,7 +77,7 @@
disable_warnings()


class UploaderTest(unittest.TestCase):
class UploaderTest(CldTestCase):
rbp_trans = {"angle": 45, "crop": "scale"}
rbp_format = "png"
rbp_values = [206, 50]
Expand Down Expand Up @@ -757,15 +757,15 @@ def test_upload_large(self):
resource = uploader.upload_large(temp_file_name, chunk_size=LARGE_CHUNK_SIZE,
tags=["upload_large_tag", UNIQUE_TAG])

self.assertEqual(resource["tags"], ["upload_large_tag", UNIQUE_TAG])
self.assertCountEqual(resource["tags"], ["upload_large_tag", UNIQUE_TAG])
self.assertEqual(resource["resource_type"], "raw")
self.assertEqual(resource["original_filename"], temp_file_filename)

resource2 = uploader.upload_large(temp_file_name, chunk_size=LARGE_CHUNK_SIZE,
tags=["upload_large_tag", UNIQUE_TAG], resource_type="image",
use_filename=True, unique_filename=False, filename=filename)

self.assertEqual(resource2["tags"], ["upload_large_tag", UNIQUE_TAG])
self.assertCountEqual(resource2["tags"], ["upload_large_tag", UNIQUE_TAG])
self.assertEqual(resource2["resource_type"], "image")
self.assertEqual(resource2["original_filename"], filename)
self.assertEqual(resource2["original_filename"], resource2["public_id"])
Expand All @@ -775,7 +775,7 @@ def test_upload_large(self):
resource3 = uploader.upload_large(temp_file_name, chunk_size=LARGE_FILE_SIZE,
tags=["upload_large_tag", UNIQUE_TAG])

self.assertEqual(resource3["tags"], ["upload_large_tag", UNIQUE_TAG])
self.assertCountEqual(resource3["tags"], ["upload_large_tag", UNIQUE_TAG])
self.assertEqual(resource3["resource_type"], "raw")

@unittest.skipUnless(cloudinary.config().api_secret, "requires api_key/api_secret")
Expand Down

0 comments on commit 2fec316

Please sign in to comment.