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))
    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))
 class Person(self.Base):
     __tablename__ = 'person'
     id = Column(Integer, primary_key=True)
     files = Column(FileList.as_mutable(Json))
    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)