def test_datarequest_delete(self, organization_id, accepted_dataset_id): # Configure the mock datarequest = test_data._generate_basic_datarequest() datarequest.organization_id = organization_id datarequest.accepted_dataset_id = accepted_dataset_id actions.db.DataRequest.get.return_value = [datarequest] default_pkg = {"pkg": 1} default_org = {"org": 2} default_user = {"user": 3} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Call the function expected_data_dict = test_data.delete_request_data.copy() result = actions.datarequest_delete(self.context, test_data.delete_request_data) # Assertions actions.db.init_db.assert_called_once_with(self.context["model"]) actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_DELETE, self.context, expected_data_dict) self.context["session"].delete.assert_called_once_with(datarequest) self.context["session"].commit.assert_called_once_with() org = default_org if organization_id else None pkg = default_pkg if accepted_dataset_id else None self._check_basic_response(datarequest, result, default_user, org, pkg)
def _test_show_datarequest_found(self, datarequest, org_checked, pkg_checked): # Configure mock actions.db.DataRequest.get.return_value = [datarequest] # Mock actions default_pkg = {'pkg': 1} default_org = {'org': 2} default_user = {'user': 3} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Call the function result = actions.show_datarequest(self.context, test_data.show_request_data) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.SHOW_DATAREQUEST, self.context, test_data.show_request_data) actions.db.DataRequest.get.assert_called_once_with(id=test_data.show_request_data['id']) org = default_org if org_checked else None pkg = default_pkg if pkg_checked else None self._check_basic_response(datarequest, result, default_user, org, pkg)
def test_comment(self): # Configure the mocks current_time = self._datetime.datetime.now() actions.datetime.datetime.now = MagicMock(return_value=current_time) # User default_user = {'user': '******'} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function result = actions.datarequest_comment(self.context, test_data.comment_request_data) # Assertions comment = actions.db.Comment.return_value actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access(constants.DATAREQUEST_COMMENT, self.context, test_data.comment_request_data) actions.validator.validate_comment.assert_called_once_with(self.context, test_data.comment_request_data) actions.db.Comment.assert_called_once() self.context['session'].add.assert_called_once_with(comment) self.context['session'].commit.assert_called_once() # Check the object stored in the database self.assertEquals(self.context['auth_user_obj'].id, comment.user_id) self.assertEquals(test_data.comment_request_data['comment'], comment.comment) self.assertEquals(test_data.comment_request_data['datarequest_id'], comment.datarequest_id) self.assertEquals(current_time, comment.time) # Check that the response is OK self._check_comment(comment, result, default_user)
def test_comment(self): # Configure the mocks current_time = self._datetime.datetime.now() actions.datetime.datetime.now = MagicMock(return_value=current_time) # User default_user = {"user": "******"} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function result = actions.datarequest_comment(self.context, test_data.comment_request_data) # Assertions comment = actions.db.Comment.return_value actions.db.init_db.assert_called_once_with(self.context["model"]) actions.tk.check_access(constants.DATAREQUEST_COMMENT, self.context, test_data.comment_request_data) actions.validator.validate_comment.assert_called_once_with(self.context, test_data.comment_request_data) actions.db.Comment.assert_called_once() self.context["session"].add.assert_called_once_with(comment) self.context["session"].commit.assert_called_once() # Check the object stored in the database self.assertEquals(self.context["auth_user_obj"].id, comment.user_id) self.assertEquals(test_data.comment_request_data["comment"], comment.comment) self.assertEquals(test_data.comment_request_data["datarequest_id"], comment.datarequest_id) self.assertEquals(current_time, comment.time) # Check that the response is OK self._check_comment(comment, result, default_user)
def test_datarequest_create_valid(self): # Configure the mocks current_time = self._datetime.datetime.now() actions.datetime.datetime.now = MagicMock(return_value=current_time) # Mock actions default_user = {'user': 1} default_org = {'org': 2} default_pkg = None # Accepted dataset cannot be different from None at this time test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Call the function result = actions.datarequest_create(self.context, test_data.create_request_data) # Assertions datarequest = actions.db.DataRequest.return_value actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_CREATE, self.context, test_data.create_request_data) actions.validator.validate_datarequest.assert_called_once_with(self.context, test_data.create_request_data) actions.db.DataRequest.assert_called_once() self.context['session'].add.assert_called_once_with(datarequest) self.context['session'].commit.assert_called_once() # Check the object stored in the database self.assertEquals(self.context['auth_user_obj'].id, datarequest.user_id) self.assertEquals(test_data.create_request_data['title'], datarequest.title) self.assertEquals(test_data.create_request_data['description'], datarequest.description) self.assertEquals(test_data.create_request_data['organization_id'], datarequest.organization_id) self.assertEquals(current_time, datarequest.open_time) # Check the returned object self._check_basic_response(datarequest, result, default_user, default_org, default_pkg)
def test_comment_list(self, sort=None, desc=False): # Configure mock comments = [] for i in range(0, 5): comments.append(test_data._generate_basic_comment()) actions.db.Comment.get_ordered_by_date.return_value = comments # User default_user = {'user': '******'} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function params = test_data.comment_show_request_data.copy() params.pop('id') if sort: params['sort'] = sort results = actions.datarequest_comment_list(self.context, params) # Check that the DB has been called appropriately actions.db.Comment.get_ordered_by_date.assert_called_once_with( datarequest_id=test_data. comment_show_request_data['datarequest_id'], desc=desc) # Check that the response is OK for i in range(0, len(results)): self._check_comment(comments[i], results[i], default_user)
def test_comment_list(self, sort=None, desc=False): # Configure mock comments = [] for i in range(0, 5): comments.append(test_data._generate_basic_comment()) actions.db.Comment.get_ordered_by_date.return_value = comments # User default_user = {'user': '******'} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function params = test_data.comment_show_request_data.copy() params.pop('id') if sort: params['sort'] = sort results = actions.list_datarequest_comments(self.context, params) # Check that the DB has been called appropriately actions.db.Comment.get_ordered_by_date.assert_called_once_with(datarequest_id=test_data.comment_show_request_data['datarequest_id'], desc=desc) # Check that the response is OK for i in range(0, len(results)): self._check_comment(comments[i], results[i], default_user)
def test_create_datarequest_valid(self, send_mail_mock): # Configure the mocks current_time = self._datetime.datetime.now() actions.datetime.datetime.now = MagicMock(return_value=current_time) # Mock actions default_user = {'user': 1} default_org = {'org': 2, 'users': [{'id': 'user_1'}, {'id': 'user_2'}]} default_pkg = None # Accepted dataset cannot be different from None at this time test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Call the function result = actions.create_datarequest(self.context, test_data.create_request_data) # Assertions datarequest = actions.db.DataRequest.return_value actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.CREATE_DATAREQUEST, self.context, test_data.create_request_data) actions.validator.validate_datarequest.assert_called_once_with(self.context, test_data.create_request_data) actions.db.DataRequest.assert_called_once() self.context['session'].add.assert_called_once_with(datarequest) self.context['session'].commit.assert_called_once() send_mail_mock.assert_called_once_with(set(['user_1', 'user_2']), 'new_datarequest', result) # Check the object stored in the database self.assertEquals(self.context['auth_user_obj'].id, datarequest.user_id) self.assertEquals(test_data.create_request_data['title'], datarequest.title) self.assertEquals(test_data.create_request_data['description'], datarequest.description) self.assertEquals(test_data.create_request_data['organization_id'], datarequest.organization_id) self.assertEquals(current_time, datarequest.open_time) # Check the returned object self._check_basic_response(datarequest, result, default_user, default_org, default_pkg)
def test_comment_update(self): # Configure the mock comment = test_data._generate_basic_comment(id=test_data.comment_update_request_data['id']) actions.db.Comment.get.return_value = [comment] # Mock actions default_user = {'user': '******'} test_data._initialize_basic_actions(actions, default_user, None, None) # Store previous user (needed to check that it has not been modified) previous_user_id = comment.user_id # Call the action result = actions.datarequest_comment_update(self.context, test_data.comment_update_request_data) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_COMMENT_UPDATE, self.context, test_data.comment_update_request_data) actions.db.Comment.get.assert_called_once_with(id=test_data.comment_update_request_data['id']) actions.validator.validate_comment.assert_called_once_with(self.context, test_data.comment_update_request_data) self.context['session'].add.assert_called_once_with(comment) self.context['session'].commit.assert_called_once() # Check the object stored in the database self.assertEquals(previous_user_id, comment.user_id) self.assertEquals(test_data.comment_update_request_data['datarequest_id'], comment.datarequest_id) self.assertEquals(test_data.comment_update_request_data['comment'], comment.comment) # Check the result self._check_comment(comment, result, default_user)
def _test_datarequest_show_found(self, datarequest, org_checked, pkg_checked): # Configure mock actions.db.DataRequest.get.return_value = [datarequest] # Mock actions default_pkg = {'pkg': 1} default_org = {'org': 2} default_user = {'user': 3} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Call the function result = actions.datarequest_show(self.context, test_data.show_request_data) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with( constants.DATAREQUEST_SHOW, self.context, test_data.show_request_data) actions.db.DataRequest.get.assert_called_once_with( id=test_data.show_request_data['id']) org = default_org if org_checked else None pkg = default_pkg if pkg_checked else None self._check_basic_response(datarequest, result, default_user, org, pkg)
def test_datarequest_delete(self, organization_id, accepted_dataset_id): # Configure the mock datarequest = test_data._generate_basic_datarequest() datarequest.organization_id = organization_id datarequest.accepted_dataset_id = accepted_dataset_id actions.db.DataRequest.get.return_value = [datarequest] default_pkg = {'pkg': 1} default_org = {'org': 2} default_user = {'user': 3} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Call the function expected_data_dict = test_data.delete_request_data.copy() result = actions.datarequest_delete(self.context, test_data.delete_request_data) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_DELETE, self.context, expected_data_dict) self.context['session'].delete.assert_called_once_with(datarequest) self.context['session'].commit.assert_called_once_with() org = default_org if organization_id else None pkg = default_pkg if accepted_dataset_id else None self._check_basic_response(datarequest, result, default_user, org, pkg)
def test_comment_update(self): # Configure the mock comment = test_data._generate_basic_comment(id=test_data.comment_update_request_data["id"]) actions.db.Comment.get.return_value = [comment] # Mock actions default_user = {"user": "******"} test_data._initialize_basic_actions(actions, default_user, None, None) # Store previous user (needed to check that it has not been modified) previous_user_id = comment.user_id # Call the action result = actions.datarequest_comment_update(self.context, test_data.comment_update_request_data) # Assertions actions.db.init_db.assert_called_once_with(self.context["model"]) actions.tk.check_access.assert_called_once_with( constants.DATAREQUEST_COMMENT_UPDATE, self.context, test_data.comment_update_request_data ) actions.db.Comment.get.assert_called_once_with(id=test_data.comment_update_request_data["id"]) actions.validator.validate_comment.assert_called_once_with(self.context, test_data.comment_update_request_data) self.context["session"].add.assert_called_once_with(comment) self.context["session"].commit.assert_called_once() # Check the object stored in the database self.assertEquals(previous_user_id, comment.user_id) self.assertEquals(test_data.comment_update_request_data["datarequest_id"], comment.datarequest_id) self.assertEquals(test_data.comment_update_request_data["comment"], comment.comment) # Check the result self._check_comment(comment, result, default_user)
def test_datarequest_update(self, title_checked, organization_id=None, accepted_dataset_id=None): # Configure the mock datarequest = test_data._generate_basic_datarequest() datarequest.organization_id = organization_id datarequest.accepted_dataset_id = accepted_dataset_id # Title should not be checked when it does not change datarequest.title = test_data.create_request_data[ 'title'] + 'a' if title_checked else test_data.create_request_data[ 'title'] actions.db.DataRequest.get.return_value = [datarequest] # Mock actions default_pkg = {'pkg': 1} default_org = {'org': 2} default_user = {'user': 3} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Store previous user (needed to check that it has not been modified) previous_user_id = datarequest.user_id # Call the action result = actions.datarequest_update(self.context, test_data.update_request_data) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with( constants.DATAREQUEST_UPDATE, self.context, test_data.update_request_data) actions.db.DataRequest.get.assert_called_once_with( id=test_data.update_request_data['id']) expected_context = self.context.copy() expected_context['avoid_existing_title_check'] = not title_checked actions.validator.validate_datarequest.assert_called_once_with( expected_context, test_data.update_request_data) self.context['session'].add.assert_called_once_with(datarequest) self.context['session'].commit.assert_called_once() # Check the object stored in the database self.assertEquals(previous_user_id, datarequest.user_id) self.assertEquals(test_data.update_request_data['title'], datarequest.title) self.assertEquals(test_data.update_request_data['description'], datarequest.description) self.assertEquals(test_data.update_request_data['organization_id'], datarequest.organization_id) # Check the result org = default_org if organization_id else None pkg = default_pkg if accepted_dataset_id else None self._check_basic_response(datarequest, result, default_user, org, pkg)
def test_comment_show(self): # Configure mock comment = test_data._generate_basic_comment() actions.db.Comment.get.return_value = [comment] # User default_user = {'user': '******'} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function result = actions.datarequest_comment_show(self.context, test_data.comment_show_request_data) # Check that the response is OK self._check_comment(comment, result, default_user)
def test_comment_show(self): # Configure mock comment = test_data._generate_basic_comment() actions.db.Comment.get.return_value = [comment] # User default_user = {"user": "******"} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function result = actions.datarequest_comment_show(self.context, test_data.comment_show_request_data) # Check that the response is OK self._check_comment(comment, result, default_user)
def test_close_datarequest(self, data, expected_accepted_ds, organization_id): # Configure the mock current_time = self._datetime.datetime.now() actions.datetime.datetime.now = MagicMock(return_value=current_time) datarequest = test_data._generate_basic_datarequest() datarequest.organization_id = organization_id datarequest.accepted_dataset_id = None actions.db.DataRequest.get.return_value = [datarequest] send_mail_patch = patch('ckanext.datarequests.actions._send_mail') send_mail_mock = send_mail_patch.start() self.addCleanup(send_mail_patch.stop) get_datarequest_involved_users_patch = patch('ckanext.datarequests.actions._get_datarequest_involved_users') get_datarequest_involved_users_mock = get_datarequest_involved_users_patch.start() self.addCleanup(get_datarequest_involved_users_patch.stop) # Mock actions default_pkg = {'pkg': 1} default_org = {'org': 2} default_user = {'user': 3} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Call the function expected_data_dict = data.copy() result = actions.close_datarequest(self.context, data) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.CLOSE_DATAREQUEST, self.context, expected_data_dict) self.context['session'].add.assert_called_once_with(datarequest) self.context['session'].commit.assert_called_once_with() # The data object returned by the database has been modified appropriately self.assertTrue(datarequest.closed) self.assertEquals(datarequest.close_time, current_time) if expected_accepted_ds: self.assertEquals(datarequest.accepted_dataset_id, data['accepted_dataset_id']) else: self.assertIsNone(datarequest.accepted_dataset_id) org = default_org if organization_id else None pkg = default_pkg if expected_accepted_ds else None self._check_basic_response(datarequest, result, default_user, org, pkg) send_mail_mock.assert_called_once_with(get_datarequest_involved_users_mock.return_value, 'close_datarequest', result) get_datarequest_involved_users_mock.assert_called_once_with(self.context, result)
def test_update_datarequest(self, title_checked, organization_id=None, accepted_dataset_id=None): # Configure the mock datarequest = test_data._generate_basic_datarequest() datarequest.organization_id = organization_id datarequest.accepted_dataset_id = accepted_dataset_id # Title should not be checked when it does not change datarequest.title = (test_data.create_request_data['title'] + 'a' if title_checked else test_data.create_request_data['title']) actions.db.DataRequest.get.return_value = [datarequest] # Mock actions default_pkg = {'pkg': 1} default_org = {'org': 2} default_user = {'user': 3} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Store previous user (needed to check that it has not been modified) previous_user_id = datarequest.user_id # Call the action result = actions.update_datarequest(self.context, test_data.update_request_data) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.UPDATE_DATAREQUEST, self.context, test_data.update_request_data) actions.db.DataRequest.get.assert_called_once_with(id=test_data.update_request_data['id']) expected_context = self.context.copy() expected_context['avoid_existing_title_check'] = not title_checked actions.validator.validate_datarequest.assert_called_once_with(expected_context, test_data.update_request_data) self.context['session'].add.assert_called_once_with(datarequest) self.context['session'].commit.assert_called_once() # Check the object stored in the database self.assertEquals(previous_user_id, datarequest.user_id) self.assertEquals(test_data.update_request_data['title'], datarequest.title) self.assertEquals(test_data.update_request_data['description'], datarequest.description) self.assertEquals(test_data.update_request_data['organization_id'], datarequest.organization_id) # Check the result org = default_org if organization_id else None pkg = default_pkg if accepted_dataset_id else None self._check_basic_response(datarequest, result, default_user, org, pkg)
def test_comment_list(self): # Configure mock comments = [] for i in range(0, 5): comments.append(test_data._generate_basic_comment()) actions.db.Comment.get_ordered_by_date.return_value = comments # User default_user = {"user": "******"} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function results = actions.datarequest_comment_list(self.context, test_data.comment_show_request_data) # Check that the response is OK for i in range(0, len(results)): self._check_comment(comments[i], results[i], default_user)
def test_comment_list(self): # Configure mock comments = [] for i in range(0, 5): comments.append(test_data._generate_basic_comment()) actions.db.Comment.get_ordered_by_date.return_value = comments # User default_user = {'user': '******'} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function results = actions.datarequest_comment_list(self.context, test_data.comment_show_request_data) # Check that the response is OK for i in range(0, len(results)): self._check_comment(comments[i], results[i], default_user)
def test_comment_delete(self): # Configure the mock comment = test_data._generate_basic_comment(id=test_data.comment_update_request_data['id']) actions.db.Comment.get.return_value = [comment] default_user = {'user': '******'} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function expected_data_dict = test_data.comment_delete_request_data.copy() result = actions.delete_datarequest_comment(self.context, test_data.comment_delete_request_data) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.DELETE_DATAREQUEST_COMMENT, self.context, expected_data_dict) self.context['session'].delete.assert_called_once_with(comment) self.context['session'].commit.assert_called_once_with() self._check_comment(comment, result, default_user)
def test_comment_delete(self): # Configure the mock comment = test_data._generate_basic_comment(id=test_data.comment_update_request_data['id']) actions.db.Comment.get.return_value = [comment] default_user = {'user': '******'} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function expected_data_dict = test_data.comment_delete_request_data.copy() result = actions.datarequest_comment_delete(self.context, test_data.comment_delete_request_data) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_COMMENT_DELETE, self.context, expected_data_dict) self.context['session'].delete.assert_called_once_with(comment) self.context['session'].commit.assert_called_once_with() self._check_comment(comment, result, default_user)
def test_datarequest_close(self, data, expected_accepted_ds, organization_id): # Configure the mock current_time = self._datetime.datetime.now() actions.datetime.datetime.now = MagicMock(return_value=current_time) datarequest = test_data._generate_basic_datarequest() datarequest.organization_id = organization_id datarequest.accepted_dataset_id = None actions.db.DataRequest.get.return_value = [datarequest] # Mock actions default_pkg = {'pkg': 1} default_org = {'org': 2} default_user = {'user': 3} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Call the function expected_data_dict = data.copy() result = actions.datarequest_close(self.context, data) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with( constants.DATAREQUEST_CLOSE, self.context, expected_data_dict) self.context['session'].add.assert_called_once_with(datarequest) self.context['session'].commit.assert_called_once_with() # The data object returned by the database has been modified appropriately self.assertTrue(datarequest.closed) self.assertEquals(datarequest.close_time, current_time) if expected_accepted_ds: self.assertEquals(datarequest.accepted_dataset_id, data['accepted_dataset_id']) else: self.assertIsNone(datarequest.accepted_dataset_id) org = default_org if organization_id else None pkg = default_pkg if expected_accepted_ds else None self._check_basic_response(datarequest, result, default_user, org, pkg)
def test_comment(self, get_datarequest_involved_users_mock, send_mail_mock): # Configure the mocks current_time = self._datetime.datetime.now() datarequest_dict = MagicMock() actions.datetime.datetime.now = MagicMock(return_value=current_time) actions.validator.validate_comment.return_value = datarequest_dict # User default_user = {'user': '******'} test_data._initialize_basic_actions(actions, default_user, None, None) # Call the function result = actions.comment_datarequest(self.context, test_data.comment_request_data) # Assertions comment = actions.db.Comment.return_value actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access(constants.COMMENT_DATAREQUEST, self.context, test_data.comment_request_data) actions.validator.validate_comment.assert_called_once_with(self.context, test_data.comment_request_data) actions.db.Comment.assert_called_once() self.context['session'].add.assert_called_once_with(comment) self.context['session'].commit.assert_called_once() # Check the object stored in the database self.assertEquals(self.context['auth_user_obj'].id, comment.user_id) self.assertEquals(test_data.comment_request_data['comment'], comment.comment) self.assertEquals(test_data.comment_request_data['datarequest_id'], comment.datarequest_id) self.assertEquals(current_time, comment.time) # Check that the response is OK self._check_comment(comment, result, default_user) send_mail_mock.assert_called_once_with(get_datarequest_involved_users_mock.return_value, 'new_comment', datarequest_dict) get_datarequest_involved_users_mock.assert_called_once_with(self.context, datarequest_dict)
def test_datarequest_close(self, data, expected_accepted_ds, organization_id): # Configure the mock current_time = self._datetime.datetime.now() actions.datetime.datetime.now = MagicMock(return_value=current_time) datarequest = test_data._generate_basic_datarequest() datarequest.organization_id = organization_id datarequest.accepted_dataset_id = None actions.db.DataRequest.get.return_value = [datarequest] # Mock actions default_pkg = {"pkg": 1} default_org = {"org": 2} default_user = {"user": 3} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Call the function expected_data_dict = data.copy() result = actions.datarequest_close(self.context, data) # Assertions actions.db.init_db.assert_called_once_with(self.context["model"]) actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_CLOSE, self.context, expected_data_dict) self.context["session"].add.assert_called_once_with(datarequest) self.context["session"].commit.assert_called_once_with() # The data object returned by the database has been modified appropriately self.assertTrue(datarequest.closed) self.assertEquals(datarequest.close_time, current_time) if expected_accepted_ds: self.assertEquals(datarequest.accepted_dataset_id, data["accepted_dataset_id"]) else: self.assertIsNone(datarequest.accepted_dataset_id) org = default_org if organization_id else None pkg = default_pkg if expected_accepted_ds else None self._check_basic_response(datarequest, result, default_user, org, pkg)
def test_datarequest_create_valid(self): # Configure the mocks current_time = self._datetime.datetime.now() actions.datetime.datetime.now = MagicMock(return_value=current_time) # Mock actions default_user = {"user": 1} default_org = {"org": 2} default_pkg = None # Accepted dataset cannot be different from None at this time test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Call the function result = actions.datarequest_create(self.context, test_data.create_request_data) # Assertions datarequest = actions.db.DataRequest.return_value actions.db.init_db.assert_called_once_with(self.context["model"]) actions.tk.check_access.assert_called_once_with( constants.DATAREQUEST_CREATE, self.context, test_data.create_request_data ) actions.validator.validate_datarequest.assert_called_once_with(self.context, test_data.create_request_data) actions.db.DataRequest.assert_called_once() self.context["session"].add.assert_called_once_with(datarequest) self.context["session"].commit.assert_called_once() # Check the object stored in the database self.assertEquals(self.context["auth_user_obj"].id, datarequest.user_id) self.assertEquals(test_data.create_request_data["title"], datarequest.title) self.assertEquals(test_data.create_request_data["description"], datarequest.description) self.assertEquals(test_data.create_request_data["organization_id"], datarequest.organization_id) self.assertEquals(current_time, datarequest.open_time) # Check the returned object self._check_basic_response(datarequest, result, default_user, default_org, default_pkg)
def test_list_datarequests(self, test_case): content = test_case['content'] expected_ddbb_params = test_case['expected_ddbb_params'] ddbb_response = test_case['ddbb_response'] expected_response = test_case['expected_response'] _organization_show = test_case['organization_show_func'] _user_show = test_case.get('user_show_func', None) # Set the mocks actions.db.DataRequest.get_ordered_by_date.return_value = ddbb_response actions.db.DataRequestFollower.get_datarequest_followers_number.return_value = test_data.DEFAULT_FOLLOWERS default_pkg = {'pkg': 1} default_org = {'org': 2} default_user = {'user': 3, 'id': test_data.user_default_id} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) actions.tk._ = lambda x: x # Modify the default behaviour of 'organization_show' organization_show = actions.tk.get_action('organization_show') organization_show.side_effect = _organization_show # Call the function response = actions.list_datarequests(self.context, content) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.LIST_DATAREQUESTS, self.context, content) actions.db.DataRequest.get_ordered_by_date.assert_called_once_with(**expected_ddbb_params) # Expected organizations_show calls expected_organization_show_calls = 0 # The initial one to get the real ID and not the name if 'organization_id' in content: organization_show.assert_any_call({'ignore_auth': True}, {'id': content['organization_id']}) expected_organization_show_calls += 1 # The reamining ones to include the display name into the facets if 'organization' in expected_response['facets']: expected_organization_show_calls += len(expected_response['facets']['organization']['items']) for organization_facet in expected_response['facets']['organization']['items']: organization_show.assert_any_call({'ignore_auth': True}, {'id': organization_facet['name']}) # We have to substract the number of times that the function is called to parse # the datarequest that will be returned count = 0 datarequests = expected_response['result'] for datarequest in datarequests: if datarequest['organization_id']: count += 1 datarequest['user'] = default_user # Assert that organization_show has been called the appropriate number of times self.assertEquals(organization_show.call_count - count, expected_organization_show_calls) # user, organization and accepted_dataset are None by default. The value of these fields # must be set based on the value returned by the defined actions for datarequest in datarequests: datarequest['user'] = default_user datarequest['accepted_dataset'] = None organization_id = datarequest['organization_id'] datarequest['organization'] = _organization_show(None, {'id': organization_id}) if organization_id else None # Check that the result is correct # We cannot execute self.assertEquals (for facets) because items # can have different orders self.assertEquals(expected_response['count'], response['count']) self.assertEquals(expected_response['result'], response['result']) for facet in expected_response['facets']: items = expected_response['facets'][facet]['items'] # The response has the facet self.assertIn(facet, response['facets']) # The number of items is the same self.assertEquals(len(items), len(response['facets'][facet]['items'])) # The items are the same ones for item in items: self.assertIn(item, response['facets'][facet]['items'])
def test_datarequest_index(self, test_case): content = test_case["content"] expected_ddbb_params = test_case["expected_ddbb_params"] ddbb_response = test_case["ddbb_response"] expected_response = test_case["expected_response"] _organization_show = test_case["organization_show_func"] _user_show = test_case.get("user_show_func", None) # Set the mocks actions.db.DataRequest.get_ordered_by_date.return_value = ddbb_response default_pkg = {"pkg": 1} default_org = {"org": 2} default_user = {"user": 3, "id": test_data.user_default_id} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Modify the default behaviour of 'organization_show' organization_show = actions.tk.get_action("organization_show") organization_show.side_effect = _organization_show # Call the function response = actions.datarequest_index(self.context, content) # Assertions actions.db.init_db.assert_called_once_with(self.context["model"]) actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_INDEX, self.context, content) actions.db.DataRequest.get_ordered_by_date.assert_called_once_with(**expected_ddbb_params) # Expected organizations_show calls expected_organization_show_calls = 0 # The initial one to get the real ID and not the name if "organization_id" in content: organization_show.assert_any_call({"ignore_auth": True}, {"id": content["organization_id"]}) expected_organization_show_calls += 1 # The reamining ones to include the display name into the facets if "organization" in expected_response["facets"]: expected_organization_show_calls += len(expected_response["facets"]["organization"]["items"]) for organization_facet in expected_response["facets"]["organization"]["items"]: organization_show.assert_any_call({"ignore_auth": True}, {"id": organization_facet["name"]}) # We have to substract the number of times that the function is called to parse # the datarequest that will be returned count = 0 datarequests = expected_response["result"] for datarequest in datarequests: if datarequest["organization_id"]: count += 1 datarequest["user"] = default_user # Assert that organization_show has been called the appropriate number of times self.assertEquals(organization_show.call_count - count, expected_organization_show_calls) # user, organization and accepted_dataset are None by default. The value of these fields # must be set based on the value returned by the defined actions for datarequest in datarequests: datarequest["user"] = default_user datarequest["accepted_dataset"] = None organization_id = datarequest["organization_id"] datarequest["organization"] = _organization_show(None, {"id": organization_id}) if organization_id else None # Check that the result is correct # We cannot execute self.assertEquals (for facets) because items # can have different orders self.assertEquals(expected_response["count"], response["count"]) self.assertEquals(expected_response["result"], response["result"]) for facet in expected_response["facets"]: items = expected_response["facets"][facet]["items"] # The response has the facet self.assertIn(facet, response["facets"]) # The number of items is the same self.assertEquals(len(items), len(response["facets"][facet]["items"])) # The items are the same ones for item in items: self.assertIn(item, response["facets"][facet]["items"])
def test_datarequest_index(self, test_case): content = test_case['content'] expected_ddbb_params = test_case['expected_ddbb_params'] ddbb_response = test_case['ddbb_response'] expected_response = test_case['expected_response'] _organization_show = test_case['organization_show_func'] _user_show = test_case.get('user_show_func', None) # Set the mocks actions.db.DataRequest.get_ordered_by_date.return_value = ddbb_response default_pkg = {'pkg': 1} default_org = {'org': 2} default_user = {'user': 3, 'id': test_data.user_default_id} test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg) # Modify the default behaviour of 'organization_show' organization_show = actions.tk.get_action('organization_show') organization_show.side_effect = _organization_show # Call the function response = actions.datarequest_index(self.context, content) # Assertions actions.db.init_db.assert_called_once_with(self.context['model']) actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_INDEX, self.context, content) actions.db.DataRequest.get_ordered_by_date.assert_called_once_with(**expected_ddbb_params) # Expected organizations_show calls expected_organization_show_calls = 0 # The initial one to get the real ID and not the name if 'organization_id' in content: organization_show.assert_any_call({'ignore_auth': True}, {'id': content['organization_id']}) expected_organization_show_calls += 1 # The reamining ones to include the display name into the facets if 'organization' in expected_response['facets']: expected_organization_show_calls += len(expected_response['facets']['organization']['items']) for organization_facet in expected_response['facets']['organization']['items']: organization_show.assert_any_call({'ignore_auth': True}, {'id': organization_facet['name']}) # We have to substract the number of times that the function is called to parse # the datarequest that will be returned count = 0 datarequests = expected_response['result'] for datarequest in datarequests: if datarequest['organization_id']: count += 1 datarequest['user'] = default_user # Assert that organization_show has been called the appropriate number of times self.assertEquals(organization_show.call_count - count, expected_organization_show_calls) # user, organization and accepted_dataset are None by default. The value of these fields # must be set based on the value returned by the defined actions for datarequest in datarequests: datarequest['user'] = default_user datarequest['accepted_dataset'] = None organization_id = datarequest['organization_id'] datarequest['organization'] = _organization_show(None, {'id': organization_id}) if organization_id else None # Check that the result is correct # We cannot execute self.assertEquals (for facets) because items # can have different orders self.assertEquals(expected_response['count'], response['count']) self.assertEquals(expected_response['result'], response['result']) for facet in expected_response['facets']: items = expected_response['facets'][facet]['items'] # The response has the facet self.assertIn(facet, response['facets']) # The number of items is the same self.assertEquals(len(items), len(response['facets'][facet]['items'])) # The items are the same ones for item in items: self.assertIn(item, response['facets'][facet]['items'])