コード例 #1
0
 def test_del(self):
     with mock.patch('anki.storage.Collection') as anki_storage_Collection:
         col = anki_storage_Collection.return_value
         wrapper = CollectionWrapper(self.collection_path)
         wrapper.open()
         wrapper = None
         col.close.assert_called_with()
コード例 #2
0
    def test_setup_func(self):
        # Run it when the collection doesn't exist
        with mock.patch('anki.storage.Collection') as anki_storage_Collection:
            col = anki_storage_Collection.return_value
            setup_new_collection = MagicMock()
            self.assertFalse(os.path.exists(self.collection_path))
            wrapper = CollectionWrapper(self.collection_path, setup_new_collection)
            wrapper.open()
            anki_storage_Collection.assert_called_with(self.collection_path)
            setup_new_collection.assert_called_with(col)
            wrapper = None

        # Make sure that no collection was actually created
        self.assertFalse(os.path.exists(self.collection_path))
        
        # Create a faux collection file
        with file(self.collection_path, 'wt') as fd:
            fd.write('Collection!')

        # Run it when the collection does exist
        with mock.patch('anki.storage.Collection'):
            setup_new_collection = lambda col: self.fail("Setup function called when collection already exists!")
            self.assertTrue(os.path.exists(self.collection_path))
            wrapper = CollectionWrapper(self.collection_path, setup_new_collection)
            wrapper.open()
            anki_storage_Collection.assert_called_with(self.collection_path)
            wrapper = None
コード例 #3
0
    def test_lifecycle_real(self):
        """Testing common life-cycle with existing and non-existant collections. This
        test uses the real Anki objects and actually creates a new collection on disk."""

        wrapper = CollectionWrapper(self.collection_path)
        self.assertFalse(os.path.exists(self.collection_path))
        self.assertFalse(wrapper.opened())

        wrapper.open()
        self.assertTrue(os.path.exists(self.collection_path))
        self.assertTrue(wrapper.opened())

        # calling open twice shouldn't break anything
        wrapper.open()

        wrapper.close()
        self.assertTrue(os.path.exists(self.collection_path))
        self.assertFalse(wrapper.opened())

        # open the same collection again (not a creation)
        wrapper = CollectionWrapper(self.collection_path)
        self.assertFalse(wrapper.opened())
        wrapper.open()
        self.assertTrue(wrapper.opened())
        wrapper.close()
        self.assertFalse(wrapper.opened())
        self.assertTrue(os.path.exists(self.collection_path))