|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from hashlib import md5 |
| 3 | + |
| 4 | +import requests |
| 5 | +import six |
| 6 | +from syncano.models import Object |
| 7 | +from tests.integration_test import InstanceMixin, IntegrationTest |
| 8 | + |
| 9 | +try: |
| 10 | + # python2 |
| 11 | + from StringIO import StringIO |
| 12 | +except ImportError: |
| 13 | + # python3 |
| 14 | + from io import StringIO |
| 15 | + |
| 16 | + |
| 17 | +class DataObjectFileTest(InstanceMixin, IntegrationTest): |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def setUpClass(cls): |
| 21 | + super(DataObjectFileTest, cls).setUpClass() |
| 22 | + |
| 23 | + cls.schema = [ |
| 24 | + {'name': 'test_field_a', 'type': 'string'}, |
| 25 | + {'name': 'test_field_file', 'type': 'file'}, |
| 26 | + ] |
| 27 | + cls.class_name = 'test_object_file' |
| 28 | + cls.initial_field_a = 'some_string' |
| 29 | + cls.file_path = 'tests/test_files/python-logo.png' |
| 30 | + cls.instance.classes.create( |
| 31 | + name=cls.class_name, |
| 32 | + schema=cls.schema |
| 33 | + ) |
| 34 | + with open(cls.file_path, 'rb') as f: |
| 35 | + cls.file_md5 = cls.get_file_md5(f) |
| 36 | + |
| 37 | + def test_creating_file_object(self): |
| 38 | + data_object = self._create_object_with_file() |
| 39 | + self.assertEqual(data_object.test_field_a, self.initial_field_a) |
| 40 | + self.assert_file_md5(data_object) |
| 41 | + |
| 42 | + def test_updating_another_field(self): |
| 43 | + data_object = self._create_object_with_file() |
| 44 | + file_url = data_object.test_field_file |
| 45 | + |
| 46 | + # no changes made to the file; |
| 47 | + update_string = 'some_other_string' |
| 48 | + data_object.test_field_a = update_string |
| 49 | + data_object.save() |
| 50 | + |
| 51 | + self.assertEqual(data_object.test_field_file, file_url) |
| 52 | + self.assertEqual(data_object.test_field_a, update_string) |
| 53 | + self.assert_file_md5(data_object) |
| 54 | + |
| 55 | + def test_updating_file_field(self): |
| 56 | + data_object = self._create_object_with_file() |
| 57 | + file_url = data_object.test_field_file |
| 58 | + |
| 59 | + update_string = 'updating also field a' |
| 60 | + file_content = 'some example text file' |
| 61 | + new_file = StringIO(file_content) |
| 62 | + data_object.test_field_file = new_file |
| 63 | + data_object.test_field_a = update_string |
| 64 | + data_object.save() |
| 65 | + |
| 66 | + self.assertEqual(data_object.test_field_a, update_string) |
| 67 | + self.assertNotEqual(data_object.test_field_file, file_url) |
| 68 | + |
| 69 | + # check file content; |
| 70 | + file_content_s3 = self.get_s3_file(data_object.test_field_file) |
| 71 | + self.assertEqual(file_content_s3, file_content) |
| 72 | + |
| 73 | + def test_manager_update(self): |
| 74 | + data_object = self._create_object_with_file() |
| 75 | + file_url = data_object.test_field_file |
| 76 | + # update only string field; |
| 77 | + update_string = 'manager updating' |
| 78 | + Object.please.update( |
| 79 | + id=data_object.id, |
| 80 | + class_name=self.class_name, |
| 81 | + test_field_a=update_string |
| 82 | + ) |
| 83 | + |
| 84 | + data_object = Object.please.get(id=data_object.id, class_name=self.class_name) |
| 85 | + self.assertEqual(data_object.test_field_a, update_string) |
| 86 | + # shouldn't change; |
| 87 | + self.assertEqual(data_object.test_field_file, file_url) |
| 88 | + |
| 89 | + # update also a file; |
| 90 | + new_update_string = 'manager with file update' |
| 91 | + file_content = 'manager file update' |
| 92 | + new_file = StringIO(file_content) |
| 93 | + Object.please.update( |
| 94 | + id=data_object.id, |
| 95 | + class_name=self.class_name, |
| 96 | + test_field_a=new_update_string, |
| 97 | + test_field_file=new_file |
| 98 | + ) |
| 99 | + |
| 100 | + data_object = Object.please.get(id=data_object.id, class_name=self.class_name) |
| 101 | + self.assertEqual(data_object.test_field_a, new_update_string) |
| 102 | + # should change; |
| 103 | + self.assertNotEqual(data_object.test_field_file, file_url) |
| 104 | + |
| 105 | + # check file content; |
| 106 | + file_content_s3 = self.get_s3_file(data_object.test_field_file) |
| 107 | + self.assertEqual(file_content_s3, file_content) |
| 108 | + |
| 109 | + def test_manager_create(self): |
| 110 | + create_string = 'manager create' |
| 111 | + with open(self.file_path, 'rb') as f: |
| 112 | + data_object = Object.please.create( |
| 113 | + class_name=self.class_name, |
| 114 | + test_field_a=create_string, |
| 115 | + test_field_file=f |
| 116 | + ) |
| 117 | + |
| 118 | + self.assertEqual(data_object.test_field_a, create_string) |
| 119 | + self.assert_file_md5(data_object) |
| 120 | + |
| 121 | + @classmethod |
| 122 | + def get_file_md5(cls, file_content): |
| 123 | + if not isinstance(file_content, (six.string_types, six.binary_type)): |
| 124 | + file_content = file_content.read() |
| 125 | + return md5(file_content).hexdigest() |
| 126 | + |
| 127 | + def assert_file_md5(self, data_object): |
| 128 | + file_content = requests.get(data_object.test_field_file).content |
| 129 | + file_md5 = self.get_file_md5(file_content) |
| 130 | + self.assertEqual(self.file_md5, file_md5) |
| 131 | + |
| 132 | + @classmethod |
| 133 | + def get_s3_file(cls, url): |
| 134 | + file_content_s3 = requests.get(url).content |
| 135 | + if hasattr(file_content_s3, 'decode'): |
| 136 | + file_content_s3 = file_content_s3.decode('utf-8') |
| 137 | + return file_content_s3 |
| 138 | + |
| 139 | + def _create_object_with_file(self): |
| 140 | + with open('tests/test_files/python-logo.png', 'rb') as f: |
| 141 | + data_object = Object.please.create( |
| 142 | + class_name=self.class_name, |
| 143 | + test_field_a=self.initial_field_a, |
| 144 | + test_field_file=f, |
| 145 | + ) |
| 146 | + return data_object |
0 commit comments