def test_form_project_tag_resources_paths_for_select(resources_mock, project_a): # tests that selected paths are correct filtered resources_mock.configure_mock( **{ "filter.return_value.values_list.return_value": 23, "values_list.return_value": 17, }) form = LinkTagResourcesAdminForm(project=project_a) # no search filter set, all resources returned form.cleaned_data = dict(search="") assert form._clean_paths_for_select() == 17 assert not resources_mock.filter.called # search set, resources filtered form.cleaned_data = dict(search="search query") assert form._clean_paths_for_select() == 23 assert list(resources_mock.filter.call_args) == [ (), { "path__contains": "search query" }, ]
def test_form_project_tag_resources_paths_for_select( glob_mock, resources_mock, project_a ): # tests that selected paths are correct filtered resources_mock.configure_mock(**{ 'filter.return_value.values_list.return_value': 23, 'values_list.return_value': 17, }) glob_mock.return_value = 7 form = LinkTagResourcesAdminForm(project=project_a) # no search filter set, all resources returned form.cleaned_data = dict(search='') assert form._clean_paths_for_select() == 17 assert not resources_mock.filter.called assert not glob_mock.called # search set, resources filtered form.cleaned_data = dict(search='*') assert form._clean_paths_for_select() == 23 assert ( list(resources_mock.filter.call_args) == [(), {'path__regex': 7}] ) assert ( list(glob_mock.call_args) == [('*',), {}] )
def test_form_project_tag_resources_paths_for_select( glob_mock, resources_mock, project_a ): # tests that selected paths are correct filtered resources_mock.configure_mock(**{ 'filter.return_value.values_list.return_value': 23, 'values_list.return_value': 17, }) glob_mock.return_value = 7 form = LinkTagResourcesAdminForm(project=project_a) # no search filter set, all resources returned form.cleaned_data = dict(search='') assert form._clean_paths_for_select() == 17 assert not resources_mock.filter.called assert not glob_mock.called # search set, resources filtered form.cleaned_data = dict(search='*') assert form._clean_paths_for_select() == 23 assert ( list(resources_mock.filter.call_args) == [(), {'path__regex': 7}] ) assert ( list(glob_mock.call_args) == [('*',), {}] )
def test_form_project_tag_resources_resources(tag_mock, project_a): # tests that linked/linkable resources are correctly found tag_mock.configure_mock(**{"linkable_resources": 7, "linked_resources": 23}) form = LinkTagResourcesAdminForm(project=project_a) form.cleaned_data = dict(type="nonassoc") assert form.resources == 7 form.cleaned_data = dict(type="assoc") assert form.resources == 23
def test_form_project_tag_resources_action_type(project_a): # tests that the form displays the correct action_type # name for error messages form = LinkTagResourcesAdminForm(project=project_a) form.cleaned_data = dict() assert not form.action_type form.cleaned_data = dict(type='assoc') assert form.action_type == 'unlink' form.cleaned_data = dict(type='nonassoc') assert form.action_type == 'link'
def test_form_project_tag_resources_action_type(project_a): # tests that the form displays the correct action_type # name for error messages form = LinkTagResourcesAdminForm(project=project_a) form.cleaned_data = dict() assert not form.action_type form.cleaned_data = dict(type='assoc') assert form.action_type == 'unlink' form.cleaned_data = dict(type='nonassoc') assert form.action_type == 'link'
def test_form_project_tag_resources_resources(tag_mock, project_a): # tests that linked/linkable resources are correctly found tag_mock.configure_mock(**{ 'linkable_resources': 7, 'linked_resources': 23, }) form = LinkTagResourcesAdminForm(project=project_a) form.cleaned_data = dict(type='nonassoc') assert form.resources == 7 form.cleaned_data = dict(type='assoc') assert form.resources == 23
def test_form_project_tag_resources_save(paths_mock, project_a): # tests the form.save method # # if cleaned_data['action'] was set this will # first call the associated un/link action with the paths # # it returns a list of selectable paths after any action is # complete # return a generator to ensure conversion to a list paths_mock.return_value = (x for x in [23]) form = LinkTagResourcesAdminForm(project=project_a) # call with generator to ensure conversion to list form.cleaned_data = dict(action=None, data=(x for x in [7])) # no action, returns list(cleaned_data['data']) assert form.save() == [7] # data was not generated again after validation assert not paths_mock.called # action is set, and will be called with paths action = MagicMock() form.cleaned_data['action'] = action assert form.save() == [23] assert (list(action.call_args) == [(form.cleaned_data['data'], ), {}])
def test_form_project_tag_resources_save(paths_mock, project_a): # tests the form.save method # # if cleaned_data['action'] was set this will # first call the associated un/link action with the paths # # it returns a list of selectable paths after any action is # complete # return a generator to ensure conversion to a list paths_mock.return_value = (x for x in [23]) form = LinkTagResourcesAdminForm(project=project_a) # call with generator to ensure conversion to list form.cleaned_data = dict(action=None, data=(x for x in [7])) # no action, returns list(cleaned_data['data']) assert form.save() == [7] # data was not generated again after validation assert not paths_mock.called # action is set, and will be called with paths action = MagicMock() form.cleaned_data['action'] = action assert form.save() == [23] assert ( list(action.call_args) == [(form.cleaned_data['data'], ), {}])
def test_form_project_tag_resources_tag_tool(paths_mock, project0): # tests form.tag_tool is created correctly paths_mock.configure_mock(**{'return_value.get.return_value': 23}) form = LinkTagResourcesAdminForm(project=project0) form.cleaned_data = dict(tag='FOO') assert form.tag_tool == 23 assert (list(paths_mock.call_args) == [(), {'projects': [project0]}]) assert (list(paths_mock.return_value.get.call_args) == [('FOO', ), {}])
def test_form_project_tag_resources_tag_tool(paths_mock, project_a): # tests form.tag_tool is created correctly paths_mock.configure_mock(**{"return_value.get.return_value": 23}) form = LinkTagResourcesAdminForm(project=project_a) form.cleaned_data = dict(tag="FOO") assert form.tag_tool == 23 assert list(paths_mock.call_args) == [(), {"projects": [project_a]}] assert list(paths_mock.return_value.get.call_args) == [("FOO",), {}]
def test_form_project_tag_resources_tag_tool(paths_mock, project_a): # tests form.tag_tool is created correctly paths_mock.configure_mock(**{ 'return_value.get.return_value': 23, }) form = LinkTagResourcesAdminForm(project=project_a) form.cleaned_data = dict(tag='FOO') assert form.tag_tool == 23 assert ( list(paths_mock.call_args) == [(), {'projects': [project_a]}] ) assert ( list(paths_mock.return_value.get.call_args) == [('FOO',), {}] )
def test_form_project_tag_resources_paths_for_submit(resources_mock, project_a): # tests that submitted paths are correctly validated resources_mock.filter.return_value = [1, 2, 3] form = LinkTagResourcesAdminForm(project=project_a) form.cleaned_data = dict(action=True, type="assoc", data=[4, 5, 6]) # _clean_paths_for_submit returns filtered resources assert form._clean_paths_for_submit() == [1, 2, 3] # filter was called with list of paths assert list(resources_mock.filter.call_args) == [(), {"path__in": [4, 5, 6]}] # the number of paths is different so validation fails resources_mock.filter.return_value = [1, 2] with pytest.raises(forms.ValidationError): form._clean_paths_for_submit()
def test_form_project_tag_resources_paths_for_submit(resources_mock, project_a): # tests that submitted paths are correctly validated resources_mock.filter.return_value = [1, 2, 3] form = LinkTagResourcesAdminForm(project=project_a) form.cleaned_data = dict(action=True, type='assoc', data=[4, 5, 6]) # _clean_paths_for_submit returns filtered resources assert form._clean_paths_for_submit() == [1, 2, 3] # filter was called with list of paths assert ( list(resources_mock.filter.call_args) == [(), {'path__in': [4, 5, 6]}] ) # the number of paths is different so validation fails resources_mock.filter.return_value = [1, 2] with pytest.raises(forms.ValidationError): form._clean_paths_for_submit()