-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from Syncano/release-v5.4.4
[Release v5.4.4]
- Loading branch information
Showing
4 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# -*- coding: utf-8 -*- | ||
from hashlib import md5 | ||
|
||
import requests | ||
import six | ||
from syncano.models import Object | ||
from tests.integration_test import InstanceMixin, IntegrationTest | ||
|
||
try: | ||
# python2 | ||
from StringIO import StringIO | ||
except ImportError: | ||
# python3 | ||
from io import StringIO | ||
|
||
|
||
class DataObjectFileTest(InstanceMixin, IntegrationTest): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super(DataObjectFileTest, cls).setUpClass() | ||
|
||
cls.schema = [ | ||
{'name': 'test_field_a', 'type': 'string'}, | ||
{'name': 'test_field_file', 'type': 'file'}, | ||
] | ||
cls.class_name = 'test_object_file' | ||
cls.initial_field_a = 'some_string' | ||
cls.file_path = 'tests/test_files/python-logo.png' | ||
cls.instance.classes.create( | ||
name=cls.class_name, | ||
schema=cls.schema | ||
) | ||
with open(cls.file_path, 'rb') as f: | ||
cls.file_md5 = cls.get_file_md5(f) | ||
|
||
def test_creating_file_object(self): | ||
data_object = self._create_object_with_file() | ||
self.assertEqual(data_object.test_field_a, self.initial_field_a) | ||
self.assert_file_md5(data_object) | ||
|
||
def test_updating_another_field(self): | ||
data_object = self._create_object_with_file() | ||
file_url = data_object.test_field_file | ||
|
||
# no changes made to the file; | ||
update_string = 'some_other_string' | ||
data_object.test_field_a = update_string | ||
data_object.save() | ||
|
||
self.assertEqual(data_object.test_field_file, file_url) | ||
self.assertEqual(data_object.test_field_a, update_string) | ||
self.assert_file_md5(data_object) | ||
|
||
def test_updating_file_field(self): | ||
data_object = self._create_object_with_file() | ||
file_url = data_object.test_field_file | ||
|
||
update_string = 'updating also field a' | ||
file_content = 'some example text file' | ||
new_file = StringIO(file_content) | ||
data_object.test_field_file = new_file | ||
data_object.test_field_a = update_string | ||
data_object.save() | ||
|
||
self.assertEqual(data_object.test_field_a, update_string) | ||
self.assertNotEqual(data_object.test_field_file, file_url) | ||
|
||
# check file content; | ||
file_content_s3 = self.get_s3_file(data_object.test_field_file) | ||
self.assertEqual(file_content_s3, file_content) | ||
|
||
def test_manager_update(self): | ||
data_object = self._create_object_with_file() | ||
file_url = data_object.test_field_file | ||
# update only string field; | ||
update_string = 'manager updating' | ||
Object.please.update( | ||
id=data_object.id, | ||
class_name=self.class_name, | ||
test_field_a=update_string | ||
) | ||
|
||
data_object = Object.please.get(id=data_object.id, class_name=self.class_name) | ||
self.assertEqual(data_object.test_field_a, update_string) | ||
# shouldn't change; | ||
self.assertEqual(data_object.test_field_file, file_url) | ||
|
||
# update also a file; | ||
new_update_string = 'manager with file update' | ||
file_content = 'manager file update' | ||
new_file = StringIO(file_content) | ||
Object.please.update( | ||
id=data_object.id, | ||
class_name=self.class_name, | ||
test_field_a=new_update_string, | ||
test_field_file=new_file | ||
) | ||
|
||
data_object = Object.please.get(id=data_object.id, class_name=self.class_name) | ||
self.assertEqual(data_object.test_field_a, new_update_string) | ||
# should change; | ||
self.assertNotEqual(data_object.test_field_file, file_url) | ||
|
||
# check file content; | ||
file_content_s3 = self.get_s3_file(data_object.test_field_file) | ||
self.assertEqual(file_content_s3, file_content) | ||
|
||
def test_manager_create(self): | ||
create_string = 'manager create' | ||
with open(self.file_path, 'rb') as f: | ||
data_object = Object.please.create( | ||
class_name=self.class_name, | ||
test_field_a=create_string, | ||
test_field_file=f | ||
) | ||
|
||
self.assertEqual(data_object.test_field_a, create_string) | ||
self.assert_file_md5(data_object) | ||
|
||
@classmethod | ||
def get_file_md5(cls, file_content): | ||
if not isinstance(file_content, (six.string_types, six.binary_type)): | ||
file_content = file_content.read() | ||
return md5(file_content).hexdigest() | ||
|
||
def assert_file_md5(self, data_object): | ||
file_content = requests.get(data_object.test_field_file).content | ||
file_md5 = self.get_file_md5(file_content) | ||
self.assertEqual(self.file_md5, file_md5) | ||
|
||
@classmethod | ||
def get_s3_file(cls, url): | ||
file_content_s3 = requests.get(url).content | ||
if hasattr(file_content_s3, 'decode'): | ||
file_content_s3 = file_content_s3.decode('utf-8') | ||
return file_content_s3 | ||
|
||
def _create_object_with_file(self): | ||
with open('tests/test_files/python-logo.png', 'rb') as f: | ||
data_object = Object.please.create( | ||
class_name=self.class_name, | ||
test_field_a=self.initial_field_a, | ||
test_field_file=f, | ||
) | ||
return data_object |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.