def test_delete_orphan_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, 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)
            second_filename = join(self.temp_path, person1.files['1'].path)

            person1.files = FileDict({
                str(i): File.create_from(BytesIO(b'New Text 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_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)
 class Person(self.Base):
     __tablename__ = 'person'
     id = Column(Integer, primary_key=True)
     files = Column(FileDict.as_mutable(Json))
    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)