File: create_free_text_question_regex.doctest

package info (click to toggle)
python-boto 2.49.0-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,888 kB
  • sloc: python: 86,396; makefile: 112
file content (100 lines) | stat: -rw-r--r-- 3,365 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
>>> import uuid
>>> import datetime
>>> from _init_environment import MTurkConnection, mturk_host
>>> from boto.mturk.question import Question, QuestionContent, AnswerSpecification, FreeTextAnswer, RegExConstraint

>>> conn = MTurkConnection(host=mturk_host)

# 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 a free text answer that is not quite so free!
>>> constraints = [
...     RegExConstraint(
...         "^[12][0-9]{3}-[01]?\d-[0-3]?\d$",
...         error_text="You must enter a date with the format yyyy-mm-dd.",
...         flags='i',
...     )]
>>> ft_answer = FreeTextAnswer(constraints=constraints,
...                            default="This is not a valid format")

# create the question specification
>>> qn = Question(identifier=str(uuid.uuid4()),
...               content=qn_content,
...               answer_spec=AnswerSpecification(ft_answer))

# now, create the actual HIT for the question without using a HIT type
# NOTE - the response_groups are specified to get back additional information for testing
>>> keywords=['boto', 'test', 'doctest']
>>> create_hit_rs = conn.create_hit(question=qn,
...                                 lifetime=60*65,
...                                 max_assignments=2,
...                                 title='Boto create_hit title',
...                                 description='Boto create_hit description',
...                                 keywords=keywords,
...                                 reward=0.23,
...                                 duration=60*6,
...                                 approval_delay=60*60,
...                                 annotation='An annotation from boto create_hit test',
...                                 response_groups=['Minimal',
...                                                  'HITDetail',
...                                                  'HITQuestion',
...                                                  'HITAssignmentSummary',])

# this is a valid request
>>> create_hit_rs.status
True

# for the requested hit type id
# the HIT Type Id is a unicode string
>>> len(create_hit_rs)
1
>>> hit = create_hit_rs[0]
>>> hit_type_id = hit.HITTypeId
>>> hit_type_id # doctest: +ELLIPSIS
u'...'

>>> hit.MaxAssignments
u'2'

>>> hit.AutoApprovalDelayInSeconds
u'3600'

# expiration should be very close to now + the lifetime in seconds
>>> expected_datetime = datetime.datetime.utcnow() + datetime.timedelta(seconds=3900)
>>> expiration_datetime = datetime.datetime.strptime(hit.Expiration, '%Y-%m-%dT%H:%M:%SZ')
>>> delta = expected_datetime - expiration_datetime
>>> abs(delta).seconds < 5
True

# duration is as specified for the HIT type
>>> hit.AssignmentDurationInSeconds
u'360'

# the reward has been set correctly (allow for float error here)
>>> int(float(hit.Amount) * 100)
23

>>> hit.FormattedPrice
u'$0.23'

# only US currency supported at present
>>> hit.CurrencyCode
u'USD'

# title is the HIT type title
>>> hit.Title
u'Boto create_hit title'

# title is the HIT type description
>>> hit.Description
u'Boto create_hit description'

# annotation is correct
>>> hit.RequesterAnnotation
u'An annotation from boto create_hit test'

>>> hit.HITReviewStatus
u'NotReviewed'