|
| 1 | +import os |
| 2 | + |
| 3 | +from django.core.files.uploadedfile import SimpleUploadedFile |
| 4 | +from django.test import TestCase |
| 5 | + |
| 6 | +from cloudinary import api, CloudinaryResource |
| 7 | +from cloudinary.forms import CloudinaryFileField |
| 8 | +from django_tests.test_helper import SUFFIX |
| 9 | + |
| 10 | +API_TEST_ID = "dj_test_{}".format(SUFFIX) |
| 11 | + |
| 12 | +TEST_IMAGES_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "tests") |
| 13 | +TEST_IMAGE = os.path.join(TEST_IMAGES_PATH, "logo.png") |
| 14 | + |
| 15 | + |
| 16 | +class TestCloudinaryFileField(TestCase): |
| 17 | + def setUp(self): |
| 18 | + with open(TEST_IMAGE, 'rb') as test_image_file: |
| 19 | + self.test_file = SimpleUploadedFile("logo.png", test_image_file.read()) |
| 20 | + |
| 21 | + def test_to_python(self): |
| 22 | + cff_no_auto_save = CloudinaryFileField(autosave=False) |
| 23 | + res = cff_no_auto_save.to_python(None) |
| 24 | + self.assertIsNone(res) |
| 25 | + # without auto_save File is untouched |
| 26 | + res = cff_no_auto_save.to_python(self.test_file) |
| 27 | + self.assertIsInstance(res, SimpleUploadedFile) |
| 28 | + # when auto_save is used, resource is uploaded to Cloudinary and CloudinaryResource is returned |
| 29 | + cff_auto_save = CloudinaryFileField(autosave=True, options={"public_id": API_TEST_ID}) |
| 30 | + res = cff_auto_save.to_python(self.test_file) |
| 31 | + self.assertIsInstance(res, CloudinaryResource) |
| 32 | + self.assertEqual(API_TEST_ID, res.public_id) |
| 33 | + |
| 34 | + def tearDown(self): |
| 35 | + api.delete_resources([API_TEST_ID]) |
0 commit comments