Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moto mocking not working. #1815

Closed
asahasrabuddhe opened this issue Sep 7, 2018 · 3 comments
Closed

moto mocking not working. #1815

asahasrabuddhe opened this issue Sep 7, 2018 · 3 comments

Comments

@asahasrabuddhe
Copy link

Hello,

I am trying to write tests for a serverless application. I am using 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?

@asahasrabuddhe
Copy link
Author

asahasrabuddhe commented Sep 7, 2018

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')

@asahasrabuddhe
Copy link
Author

asahasrabuddhe commented Sep 7, 2018

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.

@asahasrabuddhe
Copy link
Author

Hi,

The issue was due to a newer issue of botocore. See #1793

This was fixed by downgrading botocore to 1.10.84

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant