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

TDL-19801: Tap fetch data for sub-questions #62

7 changes: 7 additions & 0 deletions tap_typeform/schemas/questions.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
"ref": {
"type": ["null", "string"],
"selected": true
},
"sub_questions": {
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {}
}
}
},
"key_properties": ["form_id","question_id"]
Expand Down
22 changes: 21 additions & 1 deletion tap_typeform/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ def get_landings(atx, form_id, bookmark):

yield from items

def fetch_sub_questions(row):
'''This function fetches records for each sub_question in a question group and returns a list of fetched sub_questions'''
sub_questions = [] #Creating blank list to accommodate each sub-question's records

#Appending each sub-question to the list
for question in row['properties'].get('fields',[]):
dbshah1212 marked this conversation as resolved.
Show resolved Hide resolved
sub_questions.append({
"question_id": question['id'],
"title": question['title'],
"ref": question['ref']
})

return sub_questions

def sync_form_definition(atx, form_id):
with singer.metrics.job_timer('form definition '+form_id):
Expand All @@ -156,12 +169,19 @@ def sync_form_definition(atx, form_id):
# we only care about a few fields in the form definition
# just those that give an analyst a reference to the submissions
for row in data:
sub_questions ={} #Creating a blank dictionary to store records of sub_questions,if any

#If type of question is group, i.e. it has sub_questions, then fetch those sub_questions
if row['type'] == 'group':
sub_questions['sub_questions'] = fetch_sub_questions(row)

#If sub_questions are fetched then add those in this field and display the same, else don't display this field
definition_data_rows.append({
"form_id": form_id,
"question_id": row['id'],
"title": row['title'],
"ref": row['ref']
})
}|sub_questions)

write_records(atx, 'questions', definition_data_rows)

Expand Down
41 changes: 41 additions & 0 deletions tests/unittests/test_sub_questions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from tap_typeform.streams import fetch_sub_questions

def test_fetched_sub_question():
"""To verify that we are getting expected response or not for question group"""

test_cases = [
{
'case1a': 'value1a', 'case1b': 'value1b', 'case1c': 'value1c',
'properties': {
'description': 'Group question',
'case1': 'value1','case2':'value2',
'fields': [
{
'id': 'id1', 'title': 'title1', 'ref': 'ref1'
},
{
'id': 'id2','title': 'title2', 'ref': 'title2'
}
]
},
'type': 'group'},
{
'case2a': 'value2a', 'case2b': 'value2b', 'case2c': 'value2c',
'properties': {
'description': 'Group question', 'case3':'value3', 'case4':'value4'
},
'type': 'group'
}]
expected_case = [
{
'question_id': 'id1', 'title': 'title1', 'ref': 'ref1'
},
{
'question_id': 'id2', 'title': 'title2', 'ref': 'title2'
}]

for test_case in test_cases :
if test_case ['properties'].get('fields'):
assert expected_case == fetch_sub_questions(test_case)
else:
assert [] == fetch_sub_questions(test_case)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If first assert fails, test will exit without executing next test. Please use parameterised unittests.