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

mock_s3 Class Decorator Not Working? #363

Closed
apapanico opened this issue Jun 23, 2015 · 3 comments
Closed

mock_s3 Class Decorator Not Working? #363

apapanico opened this issue Jun 23, 2015 · 3 comments

Comments

@apapanico
Copy link

Hi,
I am trying to use the mock_s3 class decorator but I am running into a S3ResponseError. Here's the four test cases I wrote:

  • MyTest1: Uses the decorator, connects to S3 and stores that as an attribute in MyTest1.setUp. This is the test that fails because of the S3ResponseError.
  • MyTest2: Uses the decorator, but connects to S3 within MyTest2.test_mymodel instead of MyTest2.setUP. This test passes.
  • MyTest3 & MyTest4: Instead of using the decorator, going "raw usage" with mock_s3.start() and mock_s3.stop(). Whether or not I store the connection as an attribute, these tests pass.

So what is going on with MyTest1 that it would fail because of the S3ResponseError?

import boto
from moto import mock_s3
from mymodel import MyModel

import unittest

@mock_s3
class MyTest1(unittest.TestCase):
    def setUp(self):
        self.conn = boto.connect_s3()
        self.conn.create_bucket('mybucket')

    def test_mymodel(self):
        model_instance = MyModel('steve', 'is awesome')
        model_instance.save()
        assert self.conn.get_bucket('mybucket').get_key('steve').get_contents_as_string() == 'is awesome'

    def tearDown(self):
        pass

@mock_s3
class MyTest2(unittest.TestCase):
    def setUp(self):
        pass

    def test_mymodel(self):
        conn = boto.connect_s3()
        conn.create_bucket('mybucket')

        model_instance = MyModel('steve', 'is awesome')
        model_instance.save()
        assert conn.get_bucket('mybucket').get_key('steve').get_contents_as_string() == 'is awesome'

    def tearDown(self):
        pass

class MyTest3(unittest.TestCase):
    def setUp(self):
        self.mock_s3 = mock_s3()
        self.mock_s3.start()
        self.conn = boto.connect_s3()
        self.conn.create_bucket('mybucket')

    def test_mymodel(self):
        model_instance = MyModel('steve', 'is awesome')
        model_instance.save()
        assert self.conn.get_bucket('mybucket').get_key('steve').get_contents_as_string() == 'is awesome'


    def tearDown(self):
        self.mock_s3.stop()

class MyTest4(unittest.TestCase):
    def setUp(self):
        self.mock_s3 = mock_s3()
        self.mock_s3.start()

    def test_mymodel(self):
        conn = boto.connect_s3()
        conn.create_bucket('mybucket')

        model_instance = MyModel('steve', 'is awesome')
        model_instance.save()
        assert conn.get_bucket('mybucket').get_key('steve').get_contents_as_string() == 'is awesome'


    def tearDown(self):
        self.mock_s3.stop()

And here is the mymodel.py code (the usual demo):

import boto
from boto.s3.key import Key

class MyModel(object):
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def save(self):
        conn = boto.connect_s3()
        bucket = conn.get_bucket('mybucket')
        k = Key(bucket)
        k.key = self.name
        k.set_contents_from_string(self.value)
@spulec spulec closed this as completed in 3ed9428 Jun 27, 2015
@spulec
Copy link
Collaborator

spulec commented Jun 27, 2015

Thanks for opening the issue!

I pushed a fix with 3ed9428 that I think should work. Can you give it a test?

@apapanico
Copy link
Author

Sorry for the slow response. It works! Thanks for making the fix.

@MartinThoma
Copy link

People who come here might be interested in #1793

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

3 participants