-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathintegration_test_data_objects.py
More file actions
146 lines (121 loc) · 5.03 KB
/
integration_test_data_objects.py
File metadata and controls
146 lines (121 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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