def test_fs_plugin_sync_all(): class SyncPlugin(Plugin): sync_order = [] def push(self, response): self._push_response = response self.sync_order.append("plugin_push") return response def sync_merge(self, state, response, fs_path=None, pootle_path=None, update=None): self._merged = (state, response, fs_path, pootle_path) self.sync_order.append("merge") def sync_pull(self, state, response, fs_path=None, pootle_path=None): self._pulled = (state, response, fs_path, pootle_path) self.sync_order.append("pull") def sync_push(self, state, response, fs_path=None, pootle_path=None): self._pushed = (state, response, fs_path, pootle_path) self.sync_order.append("push") def sync_rm(self, state, response, fs_path=None, pootle_path=None): self._rmed = (state, response, fs_path, pootle_path) self.sync_order.append("rm") project = Project.objects.get(code="project0") plugin = SyncPlugin(project) state = State("dummy") response = Response(state) class DummyResources(object): file_hashes = {} pootle_revisions = {} state.resources = DummyResources() plugin.sync(state, response, fs_path="FOO", pootle_path="BAR") for result in [ plugin._merged, plugin._pushed, plugin._rmed, plugin._pulled ]: assert result[0] is state assert result[1] is response assert result[2] == "FOO" assert result[3] == "BAR" assert plugin._push_response is response assert plugin.sync_order == ["rm", "merge", "pull", "push", "plugin_push"]
def test_fs_plugin_sync_all(): class SyncPlugin(Plugin): sync_order = [] def push(self, response): self._push_response = response self.sync_order.append("plugin_push") return response def sync_merge(self, state, response, fs_path=None, pootle_path=None, update=None): self._merged = (state, response, fs_path, pootle_path) self.sync_order.append("merge") def sync_pull(self, state, response, fs_path=None, pootle_path=None): self._pulled = (state, response, fs_path, pootle_path) self.sync_order.append("pull") def sync_push(self, state, response, fs_path=None, pootle_path=None): self._pushed = (state, response, fs_path, pootle_path) self.sync_order.append("push") def sync_rm(self, state, response, fs_path=None, pootle_path=None): self._rmed = (state, response, fs_path, pootle_path) self.sync_order.append("rm") project = Project.objects.get(code="project0") plugin = SyncPlugin(project) state = State("dummy") response = Response(state) class DummyResources(object): file_hashes = {} pootle_revisions = {} state.resources = DummyResources() plugin.sync(state, response, fs_path="FOO", pootle_path="BAR") for result in [plugin._merged, plugin._pushed, plugin._rmed, plugin._pulled]: assert result[0] is state assert result[1] is response assert result[2] == "FOO" assert result[3] == "BAR" assert plugin._push_response is response assert plugin.sync_order == ["rm", "merge", "pull", "push", "plugin_push"]
def test_state_item_instance(): class DummyContext(object): def __str__(self): return "<DummyContext object>" context = DummyContext() state = State(context) item = ItemState(state, "foo") assert item.state == state assert item.state_type == "foo" assert str(item) == ("<ItemState(<DummyContext object>): foo {}>") assert item == ItemState(state, "foo")
def test_state_instance(): context = DummyContext() state = State(context) assert state.context == context assert state.__state__ == {} assert state.prefix == "state" assert state.has_changed is False assert state.states == [] assert "x" not in state assert list(state) == [] assert state.item_state_class == ItemState assert str(state) == ("<State(<DummyContext object>): Nothing to report>")
def dummy_cmd_response(): from pootle.core.plugin import provider from pootle.core.state import State from pootle_fs.delegate import fs_plugins from pootle_fs.utils import FSPlugin from pootle_project.models import Project DummyResponse, DummyCommandPlugin = _get_dummy_api_plugin() @provider(fs_plugins, sender=Project, weak=False) def plugins_provider_(**kwargs_): return dict(dummy_cmd=DummyCommandPlugin) project = Project.objects.get(code="project0") project.config["pootle_fs.fs_type"] = "dummy_cmd" plugin = FSPlugin(project) dummy_response = DummyResponse(State(plugin)) return dummy_response, add_dummy_api_call
def test_state_bad(): # requires a context with pytest.raises(TypeError): State() class ContextualState(State): states = 3 # context.states must be iterable if set with pytest.raises(TypeError): ContextualState(DummyContext()) class ContextualState(State): def state_foo(self, **kwargs): yield [] # context.state_* methods should yield dict-like object with pytest.raises(TypeError): ContextualState(DummyContext())
def dummy_response(self): return DummyResponse(State(self))