コード例 #1
0
    def test_delete_orphan_list(self):
        class Person(self.Base):
            __tablename__ = 'person'
            id = Column(Integer, primary_key=True)
            files = Column(FileList.as_mutable(Json))

        session = self.create_all_and_get_session()

        with StoreManager(session, delete_orphan=True):
            person1 = Person()
            person1.files = FileList([
                File.create_from(BytesIO(b'simple text %d' % i))
                for i in range(2)
            ])

            # Removing the first file
            first_filename = join(self.temp_path, person1.files[0].path)
            second_filename = join(self.temp_path, person1.files[1].path)

            person1.files = FileList([
                File.create_from(BytesIO(b'New test file: %d' % i))
                for i in range(2)
            ])

            session.add(person1)
            session.commit()
            self.assertFalse(exists(first_filename))
            self.assertFalse(exists(second_filename))

            first_filename = join(self.temp_path, person1.files[0].path)
            second_filename = join(self.temp_path, person1.files[1].path)
            self.assertTrue(exists(first_filename))
            self.assertTrue(exists(second_filename))
コード例 #2
0
    def test_delete_orphan(self):
        class Person(self.Base):
            __tablename__ = 'person'
            id = Column(Integer, primary_key=True)
            cv = Column(File.as_mutable(Json), nullable=True)

        session = self.create_all_and_get_session()
        person1 = Person()
        self.assertIsNone(person1.cv)

        with StoreManager(session, delete_orphan=True):
            # First file before commit
            person1.cv = File.create_from(BytesIO(b'Simple text.'),
                                          content_type='text/plain',
                                          extension='.txt')
            self.assertIsInstance(person1.cv, File)
            first_filename = join(self.temp_path, person1.cv.path)
            self.assertTrue(exists(first_filename))
            person1.cv = File.create_from(BytesIO(b'Second simple text.'))
            second_filename = join(self.temp_path, person1.cv.path)
            self.assertTrue(exists(first_filename))
            self.assertTrue(exists(second_filename))
            session.add(person1)
            session.commit()
            self.assertFalse(exists(first_filename))
            self.assertTrue(exists(second_filename))
コード例 #3
0
    def test_delete_orphan_list_item(self):
        class Person(self.Base):
            __tablename__ = 'person'
            id = Column(Integer, primary_key=True)
            files = Column(FileList.as_mutable(Json))

        session = self.create_all_and_get_session()

        with StoreManager(session, delete_orphan=True):
            person1 = Person()
            person1.files = FileList()
            person1.files.append(File.create_from(BytesIO(b'simple text 1')))
            person1.files.append(File.create_from(BytesIO(b'simple text 2')))
            person1.files.append(File.create_from(BytesIO(b'simple text 3')))

            # Removing the first file
            first_filename = join(self.temp_path, person1.files[0].path)
            person1.files.remove(person1.files[0])
            session.add(person1)
            session.commit()
            self.assertFalse(exists(first_filename))
            # noinspection PyTypeChecker
            self.assertEqual(len(person1.files), 2)

            # Loading from db
            person1 = session.query(Person).one()
            # Preserving the first file's path
            first_filename = join(self.temp_path, person1.files[0].path)

            # remove from orphan list
            f = person1.files[1]
            person1.files.remove(f)
            person1.files.insert(1, f)
            self.assertEqual(len(person1.files), 2)

            # Removing the first file
            del person1.files[0]
            session.commit()
            self.assertFalse(exists(first_filename))
            self.assertEqual(len(person1.files), 1)

            old_attachment_filename = join(self.temp_path,
                                           person1.files[0].path)
            attachment = person1.files[0].attach(
                BytesIO(b'Changed inside nested mutable!'))
            attachment_filename = join(self.temp_path, attachment.path)
            self.assertTrue(exists(old_attachment_filename))
            self.assertTrue(exists(attachment_filename))
            session.commit()
            self.assertFalse(exists(old_attachment_filename))
            self.assertTrue(exists(attachment_filename))
コード例 #4
0
    def test_cdn_url_with_prefix(self):
        prefix = 'media'
        cdn_url = 'http//test.sqlalchemy-media.com'
        with mockup_s3_server(TEST_BUCKET) as (server, uri):
            StoreManager.register(
                's3',
                functools.partial(
                    create_s3_store,
                    prefix=prefix,
                    base_url=uri,
                    cdn_url=cdn_url
                ),
                default=True
            )

            class Person(self.Base):
                __tablename__ = 'person'
                id = Column(Integer, primary_key=True)
                file = Column(File.as_mutable(Json))

            session = self.create_all_and_get_session()

            person1 = Person()
            self.assertIsNone(person1.file)
            sample_content = b'Simple text.'

            with StoreManager(session):
                person1 = Person()
                person1.file = File.create_from(io.BytesIO(sample_content),
                                                content_type='text/plain',
                                                extension='.txt')
                self.assertIsInstance(person1.file, File)
                self.assertEqual(person1.file.locate(), '%s/%s/%s?_ts=%s' % (
                    cdn_url, prefix, person1.file.path, person1.file.timestamp)
                )
コード例 #5
0
    def test_public_base_url_strip(self):
        with mockup_s3_server(TEST_BUCKET) as (server, uri):
            base_url = '%s/' % uri
            StoreManager.register(
                's3',
                functools.partial(create_s3_store, base_url=base_url),
                default=True
            )

            class Person(self.Base):
                __tablename__ = 'person'
                id = Column(Integer, primary_key=True)
                file = Column(File.as_mutable(Json))

            session = self.create_all_and_get_session()

            person1 = Person()
            self.assertIsNone(person1.file)
            sample_content = b'Simple text.'

            with StoreManager(session):
                person1 = Person()
                person1.file = File.create_from(io.BytesIO(sample_content),
                                                content_type='text/plain',
                                                extension='.txt')
                self.assertIsInstance(person1.file, File)
                self.assertEqual(person1.file.locate(), '%s%s?_ts=%s' % (
                    base_url, person1.file.path, person1.file.timestamp))
コード例 #6
0
    def test_delete_orphan_dict_item(self):
        class Person(self.Base):
            __tablename__ = 'person'
            id = Column(Integer, primary_key=True)
            files = Column(FileDict.as_mutable(Json))

        session = self.create_all_and_get_session()

        with StoreManager(session, delete_orphan=True):
            person1 = Person()
            person1.files = FileDict({
                str(i): File.create_from(BytesIO(b'simple text %d' % i))
                for i in range(2)
            })

            # Removing the first file
            first_filename = join(self.temp_path, person1.files['0'].path)
            del person1.files['0']
            session.add(person1)
            session.commit()
            self.assertFalse(exists(first_filename))
            # noinspection PyTypeChecker
            self.assertEqual(len(person1.files), 1)

            # Loading from db
            person1 = session.query(Person).one()
            # Preserving the first file's path
            first_filename = join(self.temp_path, person1.files['1'].path)

            # Clearing
            person1.files.clear()
            session.commit()
            self.assertFalse(exists(first_filename))
            self.assertEqual(len(person1.files), 0)
コード例 #7
0
    def test_public_base_url_strip(self):
        public_base_url = 'http://test.sqlalchemy.media/'
        StoreManager.register(
            's3',
            lambda: _get_s3_store(public_base_url=public_base_url),
            default=True)

        class Person(self.Base):
            __tablename__ = 'person'
            id = Column(Integer, primary_key=True)
            file = Column(File.as_mutable(Json))

        session = self.create_all_and_get_session()

        person1 = Person()
        self.assertIsNone(person1.file)
        sample_content = b'Simple text.'

        with StoreManager(session):
            person1 = Person()
            person1.file = File.create_from(io.BytesIO(sample_content),
                                            content_type='text/plain',
                                            extension='.txt')
            self.assertIsInstance(person1.file, File)
            self.assertEqual(
                person1.file.locate(),
                '%s/%s?_ts=%s' % (public_base_url.rstrip('/'),
                                  person1.file.path, person1.file.timestamp))
コード例 #8
0
    def test_prefix(self):
        prefix = 'test'
        StoreManager.register('s3',
                              lambda: _get_s3_store(prefix=prefix),
                              default=True)

        class Person(self.Base):
            __tablename__ = 'person'
            id = Column(Integer, primary_key=True)
            file = Column(File.as_mutable(Json))

        session = self.create_all_and_get_session()

        person1 = Person()
        self.assertIsNone(person1.file)
        sample_content = b'Simple text.'

        with StoreManager(session):
            person1 = Person()
            person1.file = File.create_from(io.BytesIO(sample_content),
                                            content_type='text/plain',
                                            extension='.txt')
            self.assertIsInstance(person1.file, File)
            self.assertEqual(
                person1.file.locate(), '%s/%s/%s?_ts=%s' %
                (TEST_SERVER_URL, prefix, person1.file.path,
                 person1.file.timestamp))
コード例 #9
0
 class Person(self.Base):
     __tablename__ = 'person'
     id = Column(Integer, primary_key=True)
     file = Column(File.as_mutable(Json))
コード例 #10
0
    def test_file_dict(self):

        class Person(self.Base):
            __tablename__ = 'person'
            id = Column(Integer, primary_key=True)
            files = Column(FileDict.as_mutable(Json))

        session = self.create_all_and_get_session()

        with StoreManager(session):
            person1 = Person()
            person1.files = FileDict()
            person1.files['first'] = File.create_from(BytesIO(b'simple text 1'))
            person1.files['second'] = File.create_from(BytesIO(b'simple text 2'))
            person1.files['third'] = File.create_from(BytesIO(b'simple text 3'))
            session.add(person1)
            session.commit()

            person1 = session.query(Person).one()
            self.assertEqual(len(person1.files), 3)
            for f in person1.files.values():
                self.assertIsInstance(f, File)
                filename = join(self.temp_path, f.path)
                self.assertTrue(exists(filename))

            # Overwriting the first file
            first_filename = join(self.temp_path, person1.files['first'].path)
            person1.files['first'].attach(BytesIO(b'Another simple text.'))
            first_new_filename = join(self.temp_path, person1.files['first'].path)
            session.commit()
            self.assertFalse(exists(first_filename))
            self.assertTrue(exists(first_new_filename))

            # setdefault
            person1.files.setdefault('default', File.create_from(BytesIO(b'Default file')))
            self.assertIn('default', person1.files)

            # update
            person1.files.update(dict(
                edit1=File.create_from(BytesIO(b'Updated file 1')),
                edit2=File.create_from(BytesIO(b'Updated file 2'))
            ))
            self.assertIn('edit1', person1.files)
            self.assertIn('edit2', person1.files)

            # pop
            self.assertEqual(len(person1.files), 6)
            self.assertIsNotNone(person1.files.pop('first'))
            self.assertEqual(len(person1.files), 5)

            # popitem
            self.assertEqual(len(person1.files), 5)
            self.assertIsNotNone(person1.files.popitem())
            self.assertEqual(len(person1.files), 4)

            # setitem
            person1.files['setitem'] = File.create_from(BytesIO(b'setitem file'))
            self.assertIn('setitem', person1.files)
            self.assertEqual(len(person1.files), 5)

            # delitem
            del person1.files['setitem']
            self.assertEqual(len(person1.files), 4)

            # clear
            person1.files.clear()
            self.assertEqual(len(person1.files), 0)
コード例 #11
0
    def test_attachment_list(self):

        class Person(self.Base):
            __tablename__ = 'person'
            id = Column(Integer, primary_key=True)
            files = Column(FileList.as_mutable(Json))

        session = self.create_all_and_get_session()

        with StoreManager(session):
            person1 = Person()
            person1.files = FileList()
            person1.files.append(File.create_from(BytesIO(b'simple text 1')))
            person1.files.append(File.create_from(BytesIO(b'simple text 2')))
            person1.files.append(File.create_from(BytesIO(b'simple text 3')))
            session.add(person1)
            session.commit()

            person1 = session.query(Person).one()
            self.assertEqual(len(person1.files), 3)
            for f in person1.files:
                self.assertIsInstance(f, File)
                filename = join(self.temp_path, f.path)
                self.assertTrue(exists(filename))
                self.assertEqual(f.locate(), '%s/%s?_ts=%s' % (self.base_url, f.path, f.timestamp))

            # Overwriting the first file
            first_filename = join(self.temp_path, person1.files[0].path)
            person1.files[0].attach(BytesIO(b'Another simple text.'))
            first_new_filename = join(self.temp_path, person1.files[0].path)
            session.commit()
            self.assertFalse(exists(first_filename))
            self.assertTrue(exists(first_new_filename))

            # pop
            self.assertIsNotNone(person1.files.pop())

            # extend
            person1.files.extend([
                File.create_from(BytesIO(b'simple text 4')),
                File.create_from(BytesIO(b'simple text 5'))
            ])
            self.assertEqual(len(person1.files), 4)

            # insert
            person1.files.insert(2, File.create_from(BytesIO(b'simple text 3 # restored')))
            self.assertEqual(len(person1.files), 5)

            # __setitem__
            old_key = person1.files[3].key
            person1.files[3] = File.create_from(BytesIO(b'simple text 4 # replaced'))
            self.assertEqual(len(person1.files), 5)
            self.assertNotEqual(person1.files[3].key, old_key)

            # __setslice__
            old_keys = [a.key for a in person1.files[2:4]]
            person1.files[2:4] = [
                File.create_from(BytesIO(b'simple text 4')),
                File.create_from(BytesIO(b'simple text 5'))
            ]
            self.assertEqual(len(person1.files), 5)
            for i, a in enumerate(person1.files[2:4]):
                self.assertNotEqual(a.key, old_keys[i])

            # clear
            person1.files.clear()
            self.assertEqual(len(person1.files), 0)