Permalink
Browse files

Cleanup mturk testes to have no import time side affects

This interferes with nose's test discovery because it
will try to introspect modules looking for tests, which requires
importing the modules.
  • Loading branch information...
1 parent 14bd3b4 commit 99905ddf308c53142bbebef62cb612e4de673a14 @jamesls jamesls committed Jun 15, 2012
@@ -5,20 +5,24 @@
mturk_host = 'mechanicalturk.sandbox.amazonaws.com'
external_url = 'http://www.example.com/'
-try:
- local = os.path.join(os.path.dirname(__file__), 'local.py')
- execfile(local)
-except:
- pass
-if live_connection:
- #TODO: you must set the auth credentials to something valid
- from boto.mturk.connection import MTurkConnection
-else:
- # Here the credentials must be set, but it doesn't matter what
- # they're set to.
- os.environ.setdefault('AWS_ACCESS_KEY_ID', 'foo')
- os.environ.setdefault('AWS_SECRET_ACCESS_KEY', 'bar')
- from mocks import MTurkConnection
+SetHostMTurkConnection = None
-SetHostMTurkConnection = functools.partial(MTurkConnection, host=mturk_host)
+def config_environment():
+ global SetHostMTurkConnection
+ try:
+ local = os.path.join(os.path.dirname(__file__), 'local.py')
+ execfile(local)
+ except:
+ pass
+
+ if live_connection:
+ #TODO: you must set the auth credentials to something valid
+ from boto.mturk.connection import MTurkConnection
+ else:
+ # Here the credentials must be set, but it doesn't matter what
+ # they're set to.
+ os.environ.setdefault('AWS_ACCESS_KEY_ID', 'foo')
+ os.environ.setdefault('AWS_SECRET_ACCESS_KEY', 'bar')
+ from mocks import MTurkConnection
+ SetHostMTurkConnection = functools.partial(MTurkConnection, host=mturk_host)
@@ -1,6 +1,7 @@
import itertools
from _init_environment import SetHostMTurkConnection
+from _init_environment import config_environment
def description_filter(substring):
return lambda hit: substring in hit.Title
@@ -17,6 +18,7 @@ def dispose_hit(hit):
def cleanup():
"""Remove any boto test related HIT's"""
+ config_environment()
global conn
View
@@ -3,42 +3,43 @@
import datetime
from boto.mturk.question import (
- Question, QuestionContent, AnswerSpecification, FreeTextAnswer,
+ Question, QuestionContent, AnswerSpecification, FreeTextAnswer,
)
-from _init_environment import SetHostMTurkConnection
+from _init_environment import SetHostMTurkConnection, config_environment
class MTurkCommon(unittest.TestCase):
- def setUp(self):
- self.conn = SetHostMTurkConnection()
+ def setUp(self):
+ config_environment()
+ self.conn = SetHostMTurkConnection()
- @staticmethod
- def get_question():
- # create content for a question
- qn_content = QuestionContent()
- qn_content.append_field('Title', 'Boto no hit type question content')
- qn_content.append_field('Text', 'What is a boto no hit type?')
+ @staticmethod
+ def get_question():
+ # create content for a question
+ qn_content = QuestionContent()
+ qn_content.append_field('Title', 'Boto no hit type question content')
+ qn_content.append_field('Text', 'What is a boto no hit type?')
- # create the question specification
- qn = Question(identifier=str(uuid.uuid4()),
- content=qn_content,
- answer_spec=AnswerSpecification(FreeTextAnswer()))
- return qn
+ # create the question specification
+ qn = Question(identifier=str(uuid.uuid4()),
+ content=qn_content,
+ answer_spec=AnswerSpecification(FreeTextAnswer()))
+ return qn
- @staticmethod
- def get_hit_params():
- return dict(
- lifetime=datetime.timedelta(minutes=65),
- max_assignments=2,
- title='Boto create_hit title',
- description='Boto create_hit description',
- keywords=['boto', 'test'],
- reward=0.23,
- duration=datetime.timedelta(minutes=6),
- approval_delay=60*60,
- annotation='An annotation from boto create_hit test',
- response_groups=['Minimal',
- 'HITDetail',
- 'HITQuestion',
- 'HITAssignmentSummary',],
- )
+ @staticmethod
+ def get_hit_params():
+ return dict(
+ lifetime=datetime.timedelta(minutes=65),
+ max_assignments=2,
+ title='Boto create_hit title',
+ description='Boto create_hit description',
+ keywords=['boto', 'test'],
+ reward=0.23,
+ duration=datetime.timedelta(minutes=6),
+ approval_delay=60*60,
+ annotation='An annotation from boto create_hit test',
+ response_groups=['Minimal',
+ 'HITDetail',
+ 'HITQuestion',
+ 'HITAssignmentSummary',],
+ )
@@ -3,15 +3,19 @@
import datetime
from boto.mturk.question import ExternalQuestion
-from _init_environment import SetHostMTurkConnection, external_url
+from _init_environment import SetHostMTurkConnection, external_url, \
+ config_environment
class Test(unittest.TestCase):
- def test_create_hit_external(self):
- q = ExternalQuestion(external_url=external_url, frame_height=800)
- conn = SetHostMTurkConnection()
- keywords=['boto', 'test', 'doctest']
- create_hit_rs = conn.create_hit(question=q, lifetime=60*65, max_assignments=2, title="Boto External Question Test", keywords=keywords, reward = 0.05, duration=60*6, approval_delay=60*60, annotation='An annotation from boto external question test', response_groups=['Minimal', 'HITDetail', 'HITQuestion', 'HITAssignmentSummary',])
- assert(create_hit_rs.status == True)
+ def setUp(self):
+ config_environment()
+
+ def test_create_hit_external(self):
+ q = ExternalQuestion(external_url=external_url, frame_height=800)
+ conn = SetHostMTurkConnection()
+ keywords=['boto', 'test', 'doctest']
+ create_hit_rs = conn.create_hit(question=q, lifetime=60*65, max_assignments=2, title="Boto External Question Test", keywords=keywords, reward = 0.05, duration=60*6, approval_delay=60*60, annotation='An annotation from boto external question test', response_groups=['Minimal', 'HITDetail', 'HITQuestion', 'HITAssignmentSummary',])
+ assert(create_hit_rs.status == True)
if __name__ == "__main__":
- unittest.main()
+ unittest.main()
@@ -1,4 +1,4 @@
-from boto.mturk.test.support import unittest
+from tests.mturk.support import unittest
from common import MTurkCommon
from boto.mturk.connection import MTurkRequestError

0 comments on commit 99905dd

Please sign in to comment.