Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions syncano/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ class FileField(WritableField):
param_name = 'files'

def to_native(self, value):
if isinstance(value, six.string_types):
return None
return {self.name: value}


Expand Down
146 changes: 146 additions & 0 deletions tests/integration_test_data_objects.py
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
Binary file added tests/test_files/python-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.