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

Mocking decorator on a base class doesn't work for subclasses #7063

Closed
Seluj78 opened this issue Nov 24, 2023 · 2 comments
Closed

Mocking decorator on a base class doesn't work for subclasses #7063

Seluj78 opened this issue Nov 24, 2023 · 2 comments

Comments

@Seluj78
Copy link

Seluj78 commented Nov 24, 2023

moto version: 4.2.9

With this example

from moto import mock_dynamodb
import unittest
import os

from botocore.session import Session
from pynamodb.attributes import NumberAttribute
from pynamodb.attributes import UnicodeAttribute
from pynamodb.constants import PAY_PER_REQUEST_BILLING_MODE
from pynamodb.models import Model
import requests


class Logs(Model):
    class Meta:
        table_name = "Logs"
        region = Session().get_config_variable("region")
        billing_mode = PAY_PER_REQUEST_BILLING_MODE

    id = NumberAttribute(hash_key=True)
    content = UnicodeAttribute(null=True)


@mock_dynamodb
class TestCase(unittest.TestCase):
    def setUp(self):
        os.environ["AWS_ACCESS_KEY_ID"] = "testing"
        os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
        os.environ["AWS_SECURITY_TOKEN"] = "testing"
        os.environ["AWS_SESSION_TOKEN"] = "testing"
        os.environ["AWS_DEFAULT_REGION"] = "us-east-1"

        Logs.create_table()


class TestLogs(TestCase):
    def test_logs(self):
        Logs(id=1, content="test").save()
        requests.post("http://127.0.0.1:7000/test")

You can see that the error raised is

pynamodb.exceptions.PutError: Failed to put item: An error occurred (UnrecognizedClientException) on request (KSN7UV9EQ61HRSN4G52AHA5EBVVV4KQNSO5AEMVJF66Q9ASUAAJG) on table (Logs) when calling the PutItem operation: The security token included in the request is invalid.

If we change the code to

class TestCase(unittest.TestCase):
    def setUp(self):
        os.environ["AWS_ACCESS_KEY_ID"] = "testing"
        os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
        os.environ["AWS_SECURITY_TOKEN"] = "testing"
        os.environ["AWS_SESSION_TOKEN"] = "testing"
        os.environ["AWS_DEFAULT_REGION"] = "us-east-1"

        with mock_dynamodb():
            Logs.create_table()


@mock_dynamodb
class TestLogs(TestCase):
    def setUp(self):
        super().setUp()

    def test_logs(self):
        Logs(id=1, content="test").save()

then it is working

My problem is that I have a big test suite and I don't want to have @mock_dynamodb above each test suite but rather once in my base class, but it isn't working.

@Seluj78
Copy link
Author

Seluj78 commented Nov 24, 2023

Idealy I would love to be able to do

@mock_dynamodb
@mock_ec2
@mock_s3
...

Above my base class and not worry about it anywhere in my test suites

@bblommers
Copy link
Collaborator

Hi @Seluj78. This bug happens because the class-decorator only acts on the class as it is compiled. But in this scenario, we only know at runtime that we should be mocking the test_logs-method.

This is essentially the same problem as #4967, even if it's exposed differently. I'm going to close this as a duplicate - please follow that issue for updates.

@bblommers bblommers closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants