How can i mock pynamodb in pytest

Viewed 31

I am using pynamodb for dynamodb crud.

I need to mock dynamodb for testing, how do I do that?

pynamodb did not mock when using moto.

Please advise.

ㅡㅡㅡㅡㅡㅡaddtionㅡㅡㅡㅡㅡㅡ

Mocked data is retrieved well when pynamodb is not used. However, when SomeModel.get_something() is called, the local DynamoDB value is brought instead of the mocked value.

from pynamodb.models import Model as PynamoModel

class Model(PynamoModel):
    class Meta:
        region = ...
        host = ...
        aws_access_key_id = ...
        aws_secret_access_key = ...

class SomeModel(Model):
    id = attributes.UnicodeAttribute(hash_key=True)
    something = attributes.UnicodeAttribute(attr_name="something")

    @classmethod
    def get_something(cls): ...

@mock_dynamodb
def test_something():
    # create table
    boto3.resource('dynamodb').create_table(
        TableName="somgthing",
        KeySchema=[
            {
                "AttributeName": "id",
                "KeyType": "HASH"
            },
        ],
        AttributeDefinitions=[
            {
                "AttributeName": "id",
                "AttributeType": "S"
            },
            {
                "AttributeName": "something",
                "AttributeType": "S"
            },
        ]
        ...
    )

    # local dynamodb value instead of mock value
    SomeModel.get_something()
0 Answers
Related