Search code examples
amazon-web-servicesamazon-dynamodbboto3serverless-frameworkmoto

Mocking DynamoDB using moto + serverless


I am trying to write tests for a serverless application using the AWS serverless framework. I am facing a weird issue. Whenever I try to mock S3 or DynamoDB using moto, it does not work. Instead of mocking, the boto3 call actually goes to my AWS account and tries to do things there.

This is not desirable behaviour. Could you please help?

Sample Code:

import datetime
import boto3
import uuid
import os
from moto import mock_dynamodb2
from unittest import mock, TestCase
from JobEngine.job_engine import check_duplicate


class TestJobEngine(TestCase):
    @mock.patch.dict(os.environ, {'IN_QUEUE_URL': 'mytemp'})
    @mock.patch('JobEngine.job_engine.logger')
    @mock_dynamodb2
    def test_check_duplicate(self, mock_logger):
        id = 'ABCD123'  

        db = boto3.resource('dynamodb', 'us-east-1')

        table = db.create_table(
            TableName='my_table',
            KeySchema=[
                {
                    'AttributeName': 'id',
                    'KeyType': 'HASH'
                }
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': 'id',
                    'AttributeType': 'S'
                }
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 1,
                'WriteCapacityUnits': 1
            }
        )


    table.meta.client.get_waiter('table_exists').wait(TableName='my_table')

        table.put_item(
        Item={
            'id': {'S': id},
           ... other data ...
        }
    )

    res = check_duplicate(id)
    self.assertTrue(mock_logger.info.called)
    self.assertEqual(res, True, 'True')

Please see the above code, I am trying to insert a record into the table and then call a function that would verify if the specified id is already present in the table. Here, I get an error table already exists when I run this code.

If I disable the network, I get an error:

botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "https://dynamodb.us-east-1.amazonaws.com/"

I fail to understand why there is an attempt to connect to AWS if we are trying to mock.


Solution

  • I did some digging and have finally managed to solve this.

    See https://github.com/spulec/moto/issues/1793

    This issue was due to some incompatibilities between boto and moto. Turns around that everything works fine when we downgrade botocore to 1.10.84