def test_lifecycle(self): with mock.patch('AnkiServer.collection.CollectionManager.collection_wrapper') as CollectionWrapper: wrapper = MagicMock() CollectionWrapper.return_value = wrapper manager = CollectionManager() # check getting a new collection ret = manager.get_collection('path1') CollectionWrapper.assert_called_with(os.path.realpath('path1'), None) self.assertEqual(ret, wrapper) # change the return value, so that it would return a new object new_wrapper = MagicMock() CollectionWrapper.return_value = new_wrapper CollectionWrapper.reset_mock() # get the new wrapper ret = manager.get_collection('path2') CollectionWrapper.assert_called_with(os.path.realpath('path2'), None) self.assertEqual(ret, new_wrapper) # make sure the wrapper and new_wrapper are different self.assertNotEqual(wrapper, new_wrapper) # assert that calling with the first path again, returns the first wrapper ret = manager.get_collection('path1') self.assertEqual(ret, wrapper) manager.shutdown() wrapper.close.assert_called_with() new_wrapper.close.assert_called_with()