Skip to content

Commit

Permalink
Merge pull request #251 from Syncano/release-v5.4.4
Browse files Browse the repository at this point in the history
[Release v5.4.4]
  • Loading branch information
opalczynski authored Sep 27, 2016
2 parents e7d1f40 + 70313a3 commit 73d11ea
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 1 deletion.
2 changes: 1 addition & 1 deletion syncano/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os

__title__ = 'Syncano Python'
__version__ = '5.4.3'
__version__ = '5.4.4'
__author__ = "Daniel Kopka, Michal Kobus and Sebastian Opalczynski"
__credits__ = ["Daniel Kopka",
"Michal Kobus",
Expand Down
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.

0 comments on commit 73d11ea

Please sign in to comment.