Skip to content
This repository has been archived by the owner on Jun 27, 2018. It is now read-only.

Commit

Permalink
Cleanup mturk testes to have no import time side affects
Browse files Browse the repository at this point in the history
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
James Saryerwinnie committed Jun 15, 2012
1 parent 14bd3b4 commit 99905dd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 56 deletions.
34 changes: 19 additions & 15 deletions tests/mturk/_init_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions tests/mturk/cleanup_tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,6 +18,7 @@ def dispose_hit(hit):

def cleanup():
"""Remove any boto test related HIT's"""
config_environment()

global conn

Expand Down
65 changes: 33 additions & 32 deletions tests/mturk/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',],
)

20 changes: 12 additions & 8 deletions tests/mturk/create_hit_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion tests/mturk/test_disable_hit.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 99905dd

Please sign in to comment.