0

I would like an exception to be thrown so I can complete coverage for a few lines.

def __query_items_from_db(self, my_id: str) -> list:
        result = None

        try:
            result = self.table.query(
                KeyConditionExpression='#id = :id',
                ExpressionAttributeValues={
                    ':id': my_id
                },
                ExpressionAttributeNames={
                    '#id': 'MY_ID'
                }
            )
        except ClientError as e:
            print('__query_items_from_db', e)

        return result

This code works and won't throw an error as I have other code that sets up the table and and seeds data. Here's what I tried to get the error to throw:

@mock_dynamodb2
def test_should_handle_an_error():
    db_resource = create_mock_table()
    module = CoverageReport(db_resource)
    with pytest.raises(ClientError) as e:
        raise ClientError() <-- i don't think this is right
        actual_result = module._CoverageReport__query_items_from_db(
            1) <-- this should return None because the ClientError is fired

    assert actual_result == None

Any ideas?

1 Answers1

0

Turns out I was thinking about this the wrong way. I forced an error by not creating the table before the test executes so I can't "query" a non-existent table. Now I can check that my result is None.

def test_should_handle_an_error():
    db_resource = boto3.resource('dynamodb')
    module = CoverageReport(db_resource)
    actual_result = module._CoverageReport__query_items_from_db('testtesttest')

    assert actual_result == None