diff --git a/tests/mturk/_init_environment.py b/tests/mturk/_init_environment.py index e709785ac9..3ca5cf6d81 100644 --- a/tests/mturk/_init_environment.py +++ b/tests/mturk/_init_environment.py @@ -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) diff --git a/tests/mturk/cleanup_tests.py b/tests/mturk/cleanup_tests.py index 2381dd9b2e..bda5167514 100644 --- a/tests/mturk/cleanup_tests.py +++ b/tests/mturk/cleanup_tests.py @@ -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 diff --git a/tests/mturk/common.py b/tests/mturk/common.py index 40e2726ce9..151714ae9a 100644 --- a/tests/mturk/common.py +++ b/tests/mturk/common.py @@ -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',], + ) diff --git a/tests/mturk/create_hit_external.py b/tests/mturk/create_hit_external.py index f8fcedefbf..f2264c80f4 100644 --- a/tests/mturk/create_hit_external.py +++ b/tests/mturk/create_hit_external.py @@ -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() diff --git a/tests/mturk/test_disable_hit.py b/tests/mturk/test_disable_hit.py index 09134433e4..2d9bd9bfc0 100644 --- a/tests/mturk/test_disable_hit.py +++ b/tests/mturk/test_disable_hit.py @@ -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