def tasks(): return [ Task(name='name1', content=[], status=mock.Mock(), creator=mock.Mock()), Task(name='name2', content=[], status=mock.Mock(), creator=mock.Mock()), ]
def test_user_serializer(): task_content = 'test content' task_name = 'test name' task_dict = dict(name=task_name, content=task_content, status=dict(id=1, name='new'), creator=dict(id=1, name='creator', roles=[dict(id=1, name='creator')]), doer=None, id=1) status = TaskStatus(**(task_dict['status'])) creator = User(id=task_dict['creator']['id'], name=task_dict['creator']['name'], roles=[ UserRole(name=role['name'], id=role['id']) for role in task_dict['creator']['roles'] ]) task = Task( id=task_dict['id'], name=task_dict['name'], content=task_dict['content'], status=status, creator=creator, doer=None, ) expected_json = json.dumps(task_dict) assert json.loads(json.dumps(task, cls=TaskEncoder)) ==\ json.loads(expected_json)
def test_add_task_action(): task_name = 'task_name' task_content = 'task_content' creator_id = 1 task_id = 1 users_repo = mock.Mock() tasks_repo = mock.Mock() statuses_repo = mock.Mock() tasks_repo.save.return_value = Task(name=task_name, content=task_content, status=mock.Mock(), creator=mock.Mock(), doer=None, id=task_id) request = AddTaskRequest(name=task_name, creator_id=creator_id, content=task_content) action = AddTaskAction(tasks_repo, users_repo, statuses_repo) response = action.execute(request) assert bool(response) is True assert tasks_repo.save.called assert response.value == tasks_repo.save.return_value
def test_add_task_action_with_hooks(): task_name = 'task_name' task_content = 'task_content' creator_id = 1 task_id = 1 users_repo = mock.Mock() tasks_repo = mock.Mock() statuses_repo = mock.Mock() tasks_repo.save.return_value = Task(name=task_name, content=task_content, status=mock.Mock(), creator=mock.Mock(), doer=None, id=task_id) request = AddTaskRequest(name=task_name, creator_id=creator_id, content=task_content) action = AddTaskAction(tasks_repo, users_repo, statuses_repo) before = mock.MagicMock() after = mock.MagicMock() action.add_before_execution_hook(before) action.add_after_execution_hook(after) response = action.execute(request) assert before.called assert after.called assert bool(response) is True assert tasks_repo.save.called assert response.value == tasks_repo.save.return_value
def _to_domain_model(self, data): status = TaskStatus(id=data.status.id, name=data.status.name) creator = User( id=data.creator.id, name=data.creator.name, roles=[UserRole(id=role.id, name=role.name) for role in data.creator.roles] ) doer = None if data.doer: doer = User( id=data.doer.id, name=data.doer.name, roles=[UserRole(id=role.id, name=role.name) for role in data.doer.roles] ) return Task( name=data.name, content=data.content, status=status, creator=creator, doer=doer, id=data.id )
def test_tasks_repository_update_not_existing_task(): task = Task(name='task_name', content='task_content', status=TaskStatus(id=1, name='new'), creator=User(id=1, name='creator', roles=[UserRole(id=1, name='creator')]), doer=None, id=9) with pytest.raises(NoResultFound): repository.update(task)
def test_get_task_details_action(): creator = mock.Mock() status = mock.Mock() task = Task(name='task', content=[], status=status, creator=creator, id=1) tasks_repo = mock.Mock() tasks_repo.one.return_value = task request = GetTaskDetailsRequest(task.id) action = GetTaskDetailsAction(tasks_repo) response = action.execute(request) assert bool(response) is True tasks_repo.one.assert_called_once_with(task.id) assert response.value == task
def process_request(self, request): creator = self.users_repo.one(request.creator_id) status = self.statuses_repo.one(Statuses.NEW) task = Task(name=request.name, content=request.content, status=status, creator=creator) self._call_before_execution_hooks(dict(request=request, task=task)) response = self.tasks_repo.save(task) self._call_after_execution_hooks(dict(request=request, task=response)) return ResponseSuccess(response)
def test_tasks_repository_save_with_not_existing_creator(): task_content = 'test content' task_name = 'test name' task = Task( name=task_name, content=task_content, status=TaskStatus(id=1, name='new'), creator=User(id=9, name='creator', roles=[UserRole(id=1, name='creator')]), doer=None, ) with pytest.raises(NoResultFound): repository.save(task)
def test_get_task_details_action_handles_bad_request(): creator = mock.Mock() status = mock.Mock() task = Task(name='task', content=[], status=status, creator=creator, id=1) tasks_repo = mock.Mock() tasks_repo.one.return_value = task request = GetTaskDetailsRequest(task_id=None) action = GetTaskDetailsAction(tasks_repo) response = action.execute(request) assert bool(response) is False assert not tasks_repo.one.called assert response.value == { 'type': ResponseFailure.PARAMETER_ERROR, 'message': 'task_id: is required' }
def test_tasks_repository_save(): task_content = 'test content' task_name = 'test name' task = Task( name=task_name, content=task_content, status=TaskStatus(id=1, name='new'), creator=User(id=1, name='creator', roles=[UserRole(id=1, name='creator')]), doer=None, ) result = repository.save(task) assert result.name == task_name assert result.content == task_content assert result.status.id == 1 assert result.creator.id == 1 assert result.doer is None assert isinstance(result, DomainModel) assert len(repository.list()) == 3
def test_task(): role_creator = UserRole(name='creator') role_doer = UserRole(name='doer') task_name = 'example name' task_content = 'lorem ipsum' task_status = TaskStatus(name='new', id=1) task_creator = User(name='creator', roles=[role_creator]) task_doer = User(name='doer', roles=[role_doer]) task_id = 1 task = Task(name=task_name, content=task_content, status=task_status, creator=task_creator, doer=task_doer, id=task_id) assert task.name == task_name assert task.content == task_content assert task.status == task_status assert task.creator == task_creator assert task.doer == task_doer assert task.id == task_id assert isinstance(task, DomainModel)
def test_get_task_details_action_with_hooks(): creator = mock.Mock() status = mock.Mock() task = Task(name='task', content=[], status=status, creator=creator, id=1) tasks_repo = mock.Mock() tasks_repo.one.return_value = task request = GetTaskDetailsRequest(task.id) action = GetTaskDetailsAction(tasks_repo) before = mock.MagicMock() after = mock.MagicMock() action.add_before_execution_hook(before) action.add_after_execution_hook(after) response = action.execute(request) assert before.called assert after.called assert bool(response) is True tasks_repo.one.assert_called_once_with(task.id) assert response.value == task