-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto coercion, now you can assign the file directly to the model's at…
…tribute, closes #98
- Loading branch information
Showing
2 changed files
with
84 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import unittest | ||
import io | ||
from os.path import join, exists | ||
|
||
from sqlalchemy import Column, Integer | ||
|
||
from sqlalchemy_media.attachments import File | ||
from sqlalchemy_media.stores import StoreManager | ||
from sqlalchemy_media.tests.helpers import Json, TempStoreTestCase | ||
|
||
|
||
class AutoCoerceFile(File): | ||
__auto_coercion__ = True | ||
|
||
|
||
class GenericAssignmentTestCase(TempStoreTestCase): | ||
|
||
def test_file_assignment(self): | ||
|
||
class Person(self.Base): | ||
__tablename__ = 'person' | ||
id = Column(Integer, primary_key=True) | ||
cv = Column(AutoCoerceFile.as_mutable(Json)) | ||
|
||
session = self.create_all_and_get_session() | ||
person1 = Person() | ||
resume = io.BytesIO(b'This is my resume') | ||
with StoreManager(session): | ||
person1.cv = resume | ||
self.assertIsNone(person1.cv.content_type) | ||
self.assertIsNone(person1.cv.extension) | ||
self.assertTrue(exists(join(self.temp_path, person1.cv.path))) | ||
|
||
person1.cv = resume, 'text/plain' | ||
self.assertEqual(person1.cv.content_type, 'text/plain') | ||
self.assertEqual(person1.cv.extension, '.txt') | ||
self.assertTrue(exists(join(self.temp_path, person1.cv.path))) | ||
|
||
person1.cv = resume, 'text/plain', 'myfile.note' | ||
self.assertEqual(person1.cv.content_type, 'text/plain') | ||
self.assertEqual(person1.cv.extension, '.note') | ||
self.assertTrue(exists(join(self.temp_path, person1.cv.path))) | ||
|
||
|
||
|
||
if __name__ == '__main__': # pragma: no cover | ||
unittest.main() | ||
|