|
| 1 | +import unittest |
| 2 | +import io |
| 3 | +from os.path import join, exists |
| 4 | + |
| 5 | +from sqlalchemy import Column, Integer |
| 6 | + |
| 7 | +from sqlalchemy_media.attachments import File |
| 8 | +from sqlalchemy_media.stores import StoreManager |
| 9 | +from sqlalchemy_media.tests.helpers import Json, TempStoreTestCase |
| 10 | + |
| 11 | + |
| 12 | +class AutoCoerceFile(File): |
| 13 | + __auto_coercion__ = True |
| 14 | + |
| 15 | + |
| 16 | +class AutoCoercionTestCase(TempStoreTestCase): |
| 17 | + |
| 18 | + def test_file_assignment(self): |
| 19 | + |
| 20 | + class Person(self.Base): |
| 21 | + __tablename__ = 'person' |
| 22 | + id = Column(Integer, primary_key=True) |
| 23 | + cv = Column(AutoCoerceFile.as_mutable(Json)) |
| 24 | + |
| 25 | + session = self.create_all_and_get_session() |
| 26 | + person1 = Person() |
| 27 | + resume = io.BytesIO(b'This is my resume') |
| 28 | + with StoreManager(session): |
| 29 | + person1.cv = resume |
| 30 | + self.assertIsNone(person1.cv.content_type) |
| 31 | + self.assertIsNone(person1.cv.extension) |
| 32 | + self.assertTrue(exists(join(self.temp_path, person1.cv.path))) |
| 33 | + |
| 34 | + person1.cv = resume, 'text/plain' |
| 35 | + self.assertEqual(person1.cv.content_type, 'text/plain') |
| 36 | + self.assertEqual(person1.cv.extension, '.txt') |
| 37 | + self.assertTrue(exists(join(self.temp_path, person1.cv.path))) |
| 38 | + |
| 39 | + person1.cv = resume, 'text/plain', 'myfile.note' |
| 40 | + self.assertEqual(person1.cv.content_type, 'text/plain') |
| 41 | + self.assertEqual(person1.cv.extension, '.note') |
| 42 | + self.assertTrue(exists(join(self.temp_path, person1.cv.path))) |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == '__main__': # pragma: no cover |
| 47 | + unittest.main() |
| 48 | + |
0 commit comments