def test_delete_from_path(): mox = Mox() http_request, model_base, manager, model, queryset = get_mocks(mox) fs_mock = mox.CreateMockAnything() fs_mock.join("test_web_root", "some_path/index.html").AndReturn("test_web_root/some_path") fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root") fs_mock.exists("test_web_root/some_path").AndReturn(True) fs_mock.remove("test_web_root/some_path") fs_mock.rmdir("test_web_root") settings = CustomSettings(WEB_ROOT="test_web_root") mox.ReplayAll() instance = StaticGenerator(http_request=http_request, model_base=model_base, manager=manager, model=model, queryset=queryset, settings=settings, fs=fs_mock) instance.delete_from_path("some_path") mox.VerifyAll()
def test_delete_raises_when_unable_to_delete_file(): mox = Mox() http_request, model_base, manager, model, queryset = get_mocks(mox) fs_mock = mox.CreateMockAnything() fs_mock.join("test_web_root", "some_path/index.html").AndReturn("test_web_root/some_path") fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root") fs_mock.exists("test_web_root/some_path").AndReturn(True) fs_mock.remove("test_web_root/some_path").AndRaise(ValueError()) settings = CustomSettings(WEB_ROOT="test_web_root") mox.ReplayAll() instance = StaticGenerator(http_request=http_request, model_base=model_base, manager=manager, model=model, queryset=queryset, settings=settings, fs=fs_mock) try: instance.delete_from_path("some_path") except StaticGeneratorException, e: assert str(e) == 'Could not delete file: test_web_root/some_path' mox.VerifyAll() return
def test_delete_ignores_folder_delete_when_unable_to_delete_folder(): mox = Mox() http_request, model_base, manager, model, queryset = get_mocks(mox) fs_mock = mox.CreateMockAnything() fs_mock.join("test_web_root", "some_path/index.html").AndReturn("test_web_root/some_path") fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root") fs_mock.exists("test_web_root/some_path").AndReturn(True) fs_mock.remove("test_web_root/some_path") fs_mock.rmdir("test_web_root").AndRaise(OSError()) settings = CustomSettings(WEB_ROOT="test_web_root") mox.ReplayAll() instance = StaticGenerator(http_request=http_request, model_base=model_base, manager=manager, model=model, queryset=queryset, settings=settings, fs=fs_mock) instance.delete_from_path("some_path") assert True, "Should work even when raising OSError"
def test_delete_ignores_folder_delete_when_unable_to_delete_folder(): mox = Mox() http_request, model_base, manager, model, queryset = get_mocks(mox) fs_mock = mox.CreateMockAnything() fs_mock.join("test_web_root", "some_path").AndReturn("test_web_root/some_path") fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root") fs_mock.exists("test_web_root/some_path").AndReturn(True) fs_mock.remove("test_web_root/some_path") fs_mock.rmdir("test_web_root").AndRaise(OSError()) settings = CustomSettings(WEB_ROOT="test_web_root") mox.ReplayAll() instance = StaticGenerator(http_request=http_request, model_base=model_base, manager=manager, model=model, queryset=queryset, settings=settings, fs=fs_mock) instance.delete_from_path("some_path") assert True, "Should work even when raising OSError"
def test_delete_from_path(): mox = Mox() http_request, model_base, manager, model, queryset = get_mocks(mox) fs_mock = mox.CreateMockAnything() fs_mock.join("test_web_root", "some_path").AndReturn("test_web_root/some_path") fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root") fs_mock.exists("test_web_root/some_path").AndReturn(True) fs_mock.remove("test_web_root/some_path") fs_mock.rmdir("test_web_root") settings = CustomSettings(WEB_ROOT="test_web_root") mox.ReplayAll() instance = StaticGenerator(http_request=http_request, model_base=model_base, manager=manager, model=model, queryset=queryset, settings=settings, fs=fs_mock) instance.delete_from_path("some_path") mox.VerifyAll()
def test_delete_raises_when_unable_to_delete_file(): mox = Mox() http_request, model_base, manager, model, queryset = get_mocks(mox) fs_mock = mox.CreateMockAnything() fs_mock.join("test_web_root", "some_path").AndReturn("test_web_root/some_path") fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root") fs_mock.exists("test_web_root/some_path").AndReturn(True) fs_mock.remove("test_web_root/some_path").AndRaise(ValueError()) settings = CustomSettings(WEB_ROOT="test_web_root") mox.ReplayAll() instance = StaticGenerator(http_request=http_request, model_base=model_base, manager=manager, model=model, queryset=queryset, settings=settings, fs=fs_mock) try: instance.delete_from_path("some_path") except StaticGeneratorException, e: assert str(e) == 'Could not delete file: test_web_root/some_path' mox.VerifyAll() return
def test_delete_from_path_deletes_current_file(self): instance = StaticGenerator() remove = Mock() with nested(patch('os.path.exists', Mock(return_value=True)), patch('os.remove', remove)): instance.delete_from_path('/some_path') remove.assert_called_once_with('test_web_root/fresh/some_path')
def test_delete_from_path_does_not_delete_stale_file(self): instance = StaticGenerator() remove = Mock() with nested(patch('os.path.exists', Mock(return_value=True)), patch('os.remove', remove)): instance.delete_from_path('/some_path') self.assertNotIn(call('test_web_root/stale/some_path'), remove.call_args_list)
def test_delete_ignores_folder_delete_when_unable_to_delete_folder(self): instance = StaticGenerator() rmdir = Mock(side_effect=OSError) with nested(patch('os.path.exists', Mock(return_value=True)), patch('os.remove'), patch('os.rmdir', rmdir)): instance.delete_from_path('/some_path') rmdir.assert_called_once_with('test_web_root/fresh')
def test_delete_from_path_deletes_current_file(self): instance = StaticGenerator() remove = Mock() with nested(patch('os.path.exists', Mock(return_value=True)), patch('os.remove', remove)): instance.delete_from_path('/some_path') remove.assert_has_calls([call('test_web_root/fresh/some_path'), call('test_web_root/fresh/some_path.gz')])
def test_delete_raises_when_unable_to_delete_file(self): instance = StaticGenerator() with nested(patch('os.path.exists'), patch('os.remove'), self.assertRaises(StaticGeneratorException) ) as (exists, remove, cm): exists.return_value = True remove.side_effect = ValueError instance.delete_from_path('/some_path') self.assertEqual('Could not delete file', str(cm.exception)) self.assertEqual('test_web_root/fresh/some_path', cm.exception.filename)