def test_create_file(running_maya_engine, bootstrapped_project_with_data, empty_file): """ We start from an empty file, so file will be created from template """ project, project_data = bootstrapped_project_with_data view_callback = ViewCallbackMixin() # start engine manager = file_manager.FileManager(view_callback) context = Context( project=project_data["project"], entity=project_data["Hank"], step=project_data["Hank_modelling"]["step"], task=project_data["Hank_modelling"], ) workfile = manager.create(context) # make sure file exists on disk path = "M:/Projekte/2018/Finding_Dory/Assets/character/Hank/Hank_Maya/Hank_modelling_modelling_v001.mb" assert os.path.exists(path) # make sure file is opened in maya assert os.path.normpath(pm.sceneName()) == os.path.normpath(path) # make sure context is set correctly file_context = kttk.engines.current_engine().context assert file_context == context.copy_context(workfile=workfile) assert file_context.workfile
def test_context_is_valid(file_creation_helper, project_dict, shot_dict, task_dict): # valid context context = Context(project=project_dict, entity=shot_dict, task=task_dict, step="anim") assert file_creation_helper._context_is_valid_for_file_creation(context) # invalid contexts with pytest.raises(ValueError): context = Context(project=project_dict, entity=shot_dict, task=task_dict) assert (file_creation_helper._context_is_valid_for_file_creation( context) == False) with pytest.raises(ValueError): context = Context(project=project_dict, entity=shot_dict) assert (file_creation_helper._context_is_valid_for_file_creation( context) == False) with pytest.raises(ValueError): context = Context(project=project_dict) assert (file_creation_helper._context_is_valid_for_file_creation( context) == False) with pytest.raises(ValueError): context = Context() assert (file_creation_helper._context_is_valid_for_file_creation( context) == False)
def test_context_immutable(): """ Tests that Context can not be changed after construction :return: """ context = Context() # test project immutable with pytest.raises(AttributeError): context.project = {"type": "project", "id": 123} # test entity immutable with pytest.raises(AttributeError): context.entity = {"type": "project", "id": 123} # test step immutable with pytest.raises(AttributeError): context.project = {"type": "project", "id": 123} # test task immutable with pytest.raises(AttributeError): context.task = {"type": "project", "id": 123} # test workfile immutable with pytest.raises(AttributeError): context.workfile = {"type": "project", "id": 123} # test user immutable with pytest.raises(AttributeError): context.user = {"type": "project", "id": 123}
def change_file(self, new_file): # type: (dict) -> None """ Changes context workfile to new file. Called when open or save_as change the DCC workfile. Will NOT load the file into the DCC!!! :param new_file: workfile which will be opened in the DCC :return: """ # extract contest from the workfile # load full workfile kt = ktrack_api.get_ktrack() new_file = kt.find_one("workfile", new_file["id"]) project = new_file["project"] task = kt.find_one("task", new_file["entity"]["id"]) step = task["step"] entity = task["entity"] # entity here means shot / asset... # todo what to do with user? usermanager.restore_user() can lead to issues in travis ci self.context = Context( project=project, entity=entity, step=step, task=task, workfile=new_file, user=None, ) # todo add context changed signal / callback
def test_current_workfile(abstract_engine, workfile_dict): abstract_engine.context = Context(workfile=workfile_dict) assert abstract_engine.current_workfile == { "type": "workfile", "id": workfile_dict["id"], }
def populated_context(ktrack_instance): kt = ktrack_instance project = kt.create("project", {"name": "my_project"}) entity = kt.create("asset", { "code": "my_entity", "asset_type": "prop", "project": project }) task = kt.create("task", { "name": "task", "step": "anim", "entity": entity, "project": project }) workfile = kt.create( "workfile", { "entity": task, "name": "workfile", "path": "some_path", "comment": "awesome", "version_number": 1, "project": project, }, ) user = kt.create("user", {"name": "user"}) return Context( project=project, entity=entity, step="anim", task=task, workfile=workfile, user=user, )
def test_print_context_valid_path(mock_print_result): # test registered path with mock.patch( "kttk.path_cache_manager.context_from_path") as mock_from_path: mock_from_path.return_value = Context() ktrack_command.print_context("some_path") mock_print_result.assert_called_once_with(mock_from_path.return_value)
def test_create_new_workfile(file_creation_helper): # mock create from exisitng mock_create_from_existing = MagicMock() file_creation_helper._create_workfile_from = mock_create_from_existing file_creation_helper._create_new_workfile(Context()) assert file_creation_helper._create_workfile_from.called
def _context_from_task(self, task): # todo add tests context = Context( project=self._data_retriver.project_from_project_entity(task), entity=self._data_retriver.entity_from_task(task), task=task, step=task["step"], user=self._data_retriver.get_user(), ) return context
def test_serialize_deserialize(populated_context): # type: (Context) -> None context_serialized = populated_context.serialize() context = Context.deserialize(context_serialized) assert context.project == populated_context.project assert context.entity == populated_context.entity assert context.step == populated_context.step assert context.task == populated_context.task assert context.workfile == populated_context.workfile assert context.user == populated_context.user
def test_context__ne__(populated_context): context_left = Context() context_right = Context() # test empty context assert not context_left != context_right # test not matching project assert populated_context != populated_context.copy_context(project={ "type": "project", "id": 123 }) # test not matching entity assert populated_context != populated_context.copy_context(entity={ "type": "asset", "id": 123 }) # test not matching step assert populated_context != populated_context.copy_context(step="test") # test not matching task assert populated_context != populated_context.copy_context(task={ "type": "project", "id": 123 }) # test not matching workfile assert populated_context != populated_context.copy_context(workfile={ "type": "project", "id": 123 }) # test not matching user assert populated_context != populated_context.copy_context(user={ "type": "project", "id": 123 })
def test_entity_dicts_equal(): context = Context() # both None, match assert context._entity_dicts_equal(None, None) # one None assert not context._entity_dicts_equal(None, {}) assert not context._entity_dicts_equal({}, None) # both are equal, id and type match assert context._entity_dicts_equal({ "type": "project", "id": "123" }, { "type": "project", "id": "123" }) # not equal because of id assert not context._entity_dicts_equal({ "type": "project", "id": "13" }, { "type": "project", "id": "123" }) # not equal because of type assert not context._entity_dicts_equal({ "type": "projeect", "id": "123" }, { "type": "project", "id": "123" }) # not equal because of id and type assert not context._entity_dicts_equal({ "type": "projeect", "id": "13" }, { "type": "project", "id": "123" })
def test_context_from_dict(): context_dict = {} context_dict["project"] = {"type": "project", "id": 123} context_dict["entity"] = {"type": "shot", "id": 123} context_dict["step"] = "step" context_dict["task"] = {"type": "task", "id": 123} context_dict["workfile"] = {"type": "workfile", "id": 123} context_dict["user"] = {"type": "user", "id": 123} context = Context.from_dict(context_dict) assert context.project == {"type": "project", "id": 123} assert context.entity == {"type": "shot", "id": 123} assert context.step == "step" assert context.task == {"type": "task", "id": 123} assert context.workfile == {"type": "workfile", "id": 123} assert context.user == {"type": "user", "id": 123}
def test_render_context_empty(qtbot, fully_populated_context): context_widget = ContextWidget(None) qtbot.add_widget(context_widget) # test renders correctly with empty context context_widget.context = Context() assert context_widget._project_name_label.text() == "<i>None</i>" assert context_widget._entity_name_label.text() == "<i>None</i>" assert context_widget._task_name_label.text() == "<i>None</i>" assert context_widget._workfile_name_label.text() == "<i>None</i>" assert context_widget._user_name_label.text() == "<i>None</i>" # test correctly with None context context_widget.context = None assert context_widget._project_name_label.text() == "<i>None</i>" assert context_widget._entity_name_label.text() == "<i>None</i>" assert context_widget._task_name_label.text() == "<i>None</i>" assert context_widget._workfile_name_label.text() == "<i>None</i>" assert context_widget._user_name_label.text() == "<i>None</i>"
def context_from_path(path): # type: (str) -> kttk.context.Context """ Extracts context by path from database :param path: path for context :return: stored context if exists, else None """ # make path beautifull path = __good_path(path) kt = ktrack_api.get_ktrack() context_dicts = kt.find("path_entry", [["path", "is", path]]) context_found = len(context_dicts) > 0 if context_found: context = Context.from_dict(context_dicts[0]["context"]) return context else: return None
def test_context_serialize_deserialize(running_maya_engine, bootstrapped_project_with_data, empty_file): project, project_data = bootstrapped_project_with_data context = Context( project=project_data["project"], entity=project_data["Hank"], step=project_data["Hank_modelling"]["step"], task=project_data["Hank_modelling"], ) # change context kttk.engines.current_engine().context = context # serialize context kttk.engines.current_engine().serialize_context_to_file() # deserialze context and make sure they are the same assert kttk.engines.current_engine().deserialize_context_from_file( ) == context
def test_get_highest_workfile_existing_workfiles(file_creation_helper, ktrack_instance): # mock database with patch("ktrack_api.get_ktrack") as mock_get_ktrack: mock_get_ktrack.return_value = ktrack_instance # create task and some workfiles task = ktrack_instance.create( "task", { "project": { "type": "project", "id": "some_id" }, "entity": { "code": "some_entity" }, }, ) for i in range(5): ktrack_instance.create( "workfile", { "entity": task, "version_number": i, "path": "some_path", "project": { "type": "project", "id": "some_id" }, }, ) highest_workfile = file_creation_helper._get_highest_workfile( Context(task=task)) assert highest_workfile["version_number"] == 4
def test_creation(project_dict, shot_dict, task_dict, workfile_dict, user_dict): context = Context( project=project_dict, entity=shot_dict, step="anim", task=task_dict, workfile=workfile_dict, user=user_dict, ) # check everything was set correctly assert context.project["id"] == "1" assert context.entity["id"] == "2" assert context.step == "anim" assert context.task["id"] == "4" assert context.workfile["id"] == "5" assert context.user["id"] == "6" # check for entity dicts assert _is_entity_id_dict(context.project) assert _is_entity_id_dict(context.entity) assert _is_entity_id_dict(context.task) assert _is_entity_id_dict(context.workfile) assert _is_entity_id_dict(context.user)
def test_get_highest_workfile_no_workfiles(file_creation_helper, ktrack_instance): # mock database with patch("ktrack_api.get_ktrack") as mock_get_ktrack: mock_get_ktrack.return_value = ktrack_instance # create task and some workfiles task = ktrack_instance.create( "task", { "project": { "type": "project", "id": "some_id" }, "entity": { "code": "some_entity" }, }, ) highest_workfile = file_creation_helper._get_highest_workfile( Context(task=task)) assert highest_workfile is None
self._layout.addWidget(self._task_label, 2, 0) self._layout.addWidget(self._task_name_label, 2, 1) # workfile self._workfile_label = QtWidgets.QLabel("Workfile:") self._workfile_name_label = QtWidgets.QLabel("blabla_v001.mb") self._layout.addWidget(self._workfile_label, 3, 0) self._layout.addWidget(self._workfile_name_label, 3, 1) # user self._user_label = QtWidgets.QLabel("User:"******"Jan") self._layout.addWidget(self._user_label, 4, 0) self._layout.addWidget(self._user_name_label, 4, 1) if __name__ == "__main__": app = QtWidgets.QApplication([]) context = Context( project={"type": "project", "name": None}, entity={"type": "shot", "code": "shot010"}, task={"type": "task", "name": "Anim", "step": "anim"}, workfile={"type": "workfile", "name": "shot010_anim_v002.mb"}, ) widget = ContextWidget(context) widget.show() app.exec_()
def test_valid_path(bootstrapped_project, mock_print_result): ktrack_command.print_context(FINDING_DORY_PATH) mock_print_result.assert_called_with( Context(project=bootstrapped_project))
def deserialize_context_from_file(self): return Context.deserialize(pm.fileInfo[KTTK_CONTEXT])
def test_validate_entity_dict(): context = Context() # test None context._validate_entity_dict(None) # test valid dict context._validate_entity_dict({"type": "project", "id": "123"}) # test missing id with pytest.raises(ValueError): context._validate_entity_dict({"type": "project"}) # test missing type with pytest.raises(ValueError): context._validate_entity_dict({"id": "123"}) # test missing type and id with pytest.raises(ValueError): context._validate_entity_dict({})
def context_for_testing(ktrack_instance): project = ktrack_instance.create("project", {"name": "my_lovely_project"}) return Context(project=project)
def test_valid_step(): # test _validate_step method assert Context._validate_step(None) with pytest.raises(ValueError): assert Context._validate_step("") assert Context._validate_step(u"") with pytest.raises(ValueError): assert Context._validate_step(dict) assert Context._validate_step(list) assert Context._validate_step("step") assert Context._validate_step(u"step") # test Context construction context = Context(step=None) with pytest.raises(ValueError): context = Context(step="") context = Context(step=u"") with pytest.raises(ValueError): context = Context(step=dict) context = Context(step=list) context = Context(step="step") context = Context(step=u"step")