Skip to content

Commit

Permalink
Reducing line width to 79, #105
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Aug 2, 2018
1 parent fc3cfd2 commit 45cc9d0
Showing 1 changed file with 60 additions and 19 deletions.
79 changes: 60 additions & 19 deletions sqlalchemy_media/tests/test_file.py
@@ -1,14 +1,14 @@

import unittest
from io import BytesIO
from os.path import join, exists

from sqlalchemy import Column, Integer

from sqlalchemy_media import File, StoreManager, ContentTypeValidator, MagicAnalyzer
from sqlalchemy_media import File, StoreManager, ContentTypeValidator, \
MagicAnalyzer
from sqlalchemy_media.exceptions import MaximumLengthIsReachedError, \
MinimumLengthIsNotReachedError, ContentTypeValidationError
from sqlalchemy_media.tests.helpers import Json, TempStoreTestCase
from sqlalchemy_media.exceptions import MaximumLengthIsReachedError, MinimumLengthIsNotReachedError, \
ContentTypeValidationError


class FileTestCase(TempStoreTestCase):
Expand All @@ -34,10 +34,15 @@ class Person(self.Base):
with StoreManager(session):

# First file before commit
person1.cv = File.create_from(BytesIO(sample_content), content_type='text/plain', extension='.txt')
person1.cv = File.create_from(
BytesIO(sample_content),
content_type='text/plain',
extension='.txt'
)
self.assertIsInstance(person1.cv, File)
self.assertEqual(person1.cv.locate(), '%s/%s?_ts=%s' % (
self.base_url, person1.cv.path, person1.cv.timestamp))
self.base_url, person1.cv.path, person1.cv.timestamp)
)
self.assertDictEqual(person1.cv, {
'content_type': 'text/plain',
'key': person1.cv.key,
Expand All @@ -51,20 +56,31 @@ class Person(self.Base):
self.assertEqual(person1.cv.length, len(sample_content))

# Second file before commit
person1.cv.attach(BytesIO(sample_content), content_type='text/plain', extension='.txt')
person1.cv.attach(
BytesIO(sample_content),
content_type='text/plain',
extension='.txt'
)
second_filename = join(self.temp_path, person1.cv.path)
self.assertTrue(exists(second_filename))

# Adding object to session, the new life-cycle of the person1 just began.
# Adding object to session, the new life-cycle of the person1 just
# began.
session.add(person1)
session.commit()
self.assertFalse(exists(first_filename))
self.assertTrue(exists(second_filename))

# Loading again
sample_content = b'Lorem ipsum dolor sit amet'
person1 = session.query(Person).filter(Person.id == person1.id).one()
person1.cv.attach(BytesIO(sample_content), content_type='text/plain', extension='.txt')
person1 = session.query(Person) \
.filter(Person.id == person1.id) \
.one()
person1.cv.attach(
BytesIO(sample_content),
content_type='text/plain',
extension='.txt'
)
self.assertIsInstance(person1.cv, File)
self.assertDictEqual(person1.cv, {
'content_type': 'text/plain',
Expand All @@ -84,15 +100,21 @@ class Person(self.Base):
self.assertTrue(exists(third_filename))

# Rollback
person1.cv.attach(BytesIO(sample_content), content_type='text/plain', extension='.txt')
person1.cv.attach(
BytesIO(sample_content),
content_type='text/plain',
extension='.txt'
)
forth_filename = join(self.temp_path, person1.cv.path)
self.assertTrue(exists(forth_filename))
session.rollback()
self.assertTrue(exists(third_filename))
self.assertFalse(exists(forth_filename))

# Delete file after object deletion
person1 = session.query(Person).filter(Person.id == person1.id).one()
person1 = session.query(Person) \
.filter(Person.id == person1.id) \
.one()
session.delete(person1)
session.commit()
self.assertFalse(exists(third_filename))
Expand All @@ -101,7 +123,11 @@ class Person(self.Base):
person1 = Person()
self.assertIsNone(person1.cv)
person1.cv = File()
person1.cv.attach(BytesIO(sample_content), content_type='text/plain', extension='.txt')
person1.cv.attach(
BytesIO(sample_content),
content_type='text/plain',
extension='.txt'
)
fifth_filename = join(self.temp_path, person1.cv.path)
person1.cv = None
session.add(person1)
Expand All @@ -111,7 +137,10 @@ class Person(self.Base):
self.assertTrue(exists(fifth_filename))

# storing a file on separate store:
person1.cv = File.create_from(BytesIO(sample_content), store_id='temp_fs')
person1.cv = File.create_from(
BytesIO(sample_content),
store_id='temp_fs'
)
fifth_filename = join(self.sys_temp_path, person1.cv.path)
session.commit()
self.assertTrue(exists(fifth_filename))
Expand All @@ -135,9 +164,15 @@ class Person(self.Base):
with StoreManager(session):

# MaximumLengthIsReachedError, MinimumLengthIsNotReachedError
self.assertRaises(MinimumLengthIsNotReachedError, person1.cv.attach, BytesIO(b'less than 20 chars!'))
self.assertRaises(MaximumLengthIsReachedError, person1.cv.attach,
BytesIO(b'more than 30 chars!............'))
self.assertRaises(
MinimumLengthIsNotReachedError,
person1.cv.attach, BytesIO(b'less than 20 chars!')
)
self.assertRaises(
MaximumLengthIsReachedError,
person1.cv.attach,
BytesIO(b'more than 30 chars!............')
)

def test_attribute_type_assertion(self):
class MyAttachmentType(File):
Expand Down Expand Up @@ -213,7 +248,9 @@ class Person(self.Base):
session.commit()

# Now overwriting the file
person1 = session.query(Person).filter(Person.id == person1.id).one()
person1 = session.query(Person) \
.filter(Person.id == person1.id) \
.one()
person1.cv.attach(BytesIO(b'Another simple text'), overwrite=True)
self.assertTrue(exists(cv_filename))
session.commit()
Expand All @@ -236,7 +273,11 @@ class Person(self.Base):
person1 = Person(cv=PDFFile())
with StoreManager(session):
self.assertIsNotNone(person1.cv.attach(self.cat_jpeg))
self.assertRaises(ContentTypeValidationError, person1.cv.attach, BytesIO(b'Simple text'))
self.assertRaises(
ContentTypeValidationError,
person1.cv.attach,
BytesIO(b'Simple text')
)


if __name__ == '__main__': # pragma: no cover
Expand Down

0 comments on commit 45cc9d0

Please sign in to comment.