Example #1
0
    def on_post(self):
        command = AddItemCommand({
            **flask.request.json, 'seller_id': g.user_id
        },
                                 strict=False)
        command_name = type(command).__name__

        import pdb
        pdb.set_trace()
        if not command.is_valid():
            flask.abort(
                Response(json_response({
                    'title':
                    'Invalid Command',
                    'description':
                    f'{command_name} validation failed due to {command.validation_errors()}'
                }),
                         status=HTTPStatus.BAD_REQUEST))

        try:
            result = self._command_bus.execute(command)
            return json_response(result), HTTPStatus.OK
        except Exception as e:
            flask.abort(
                Response(json_response({
                    'title': f'Failed to execute {command_name}',
                    'description': str(e)
                }),
                         status=HTTPStatus.BAD_REQUEST))
Example #2
0
def test_add_item_command_will_not_return_errors_when_command_is_valid():
    command = AddItemCommand({
        'seller_id': 'some_id_here',
        'title': 'Fluffy dragon',
        'description': 'Fluffy dragon'
    })
    actual: DataError = command.validation_errors()
    assert actual is None
def test_command_bus_will_dispatch_command():
    # Arrange
    bus = OverriddenCommandBusContainer.command_bus_factory()
    command = AddItemCommand()

    # Act
    result = bus.execute(command)

    # Assert
    assert result.status == ResultStatus.OK
Example #4
0
    def on_post(self, req, res):
        command = AddItemCommand(
            {
                **req.media, 'seller_id': req.context['user_id']
            }, strict=False)
        command_name = type(command).__name__

        if not command.is_valid():
            raise falcon.HTTPError(
                status=falcon.HTTP_400,
                title='Invalid command',
                description="{} validation failed due to {}".format(
                    command_name, command.validation_errors()))

        try:
            result = self._command_bus.execute(command)
            res.status = falcon.HTTP_200
            res.body = json_response(result)
        except Exception as e:
            raise falcon.HTTPError(
                status=falcon.HTTP_400,
                title='Failed to execute {}'.format(command_name),
                description=str(e))
Example #5
0
 def on_get(self, req, res):
     command = AddItemCommand(req.params, strict=False)
     command.validate()
     result = command_bus.execute(command)
     res.body = json.dumps(result, ensure_ascii=False)
     res.status = falcon.HTTP_200
Example #6
0
def test_valid_add_item_command():
    command = AddItemCommand({
        'seller_id': 'some_id_here',
        'title': 'Fluffy dragon'
    })
    assert command.is_valid() is True
Example #7
0
def test_add_item_command_will_return_errors():
    command = AddItemCommand({'description': 'Fluffy dragon'})
    actual: DataError = command.validation_errors()
    assert actual.errors['seller_id'] is not None
    assert actual.errors['title'] is not None
Example #8
0
def test_add_item_command_title_is_required():
    command = AddItemCommand({
        'seller_id': 'some_id_here',
        'description': 'Fluffy dragon'
    })
    assert command.is_valid() is False