def test_survey_disabled(self): """If the survey is disabled, kick up an error""" survey = SurveyFactory.create(enabled=False) data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(), 'question_text': 'how was lunch?', 'variation_id': '1' } resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 400 errors = json.loads(resp.content)['errors'] assert ( errors['survey_id'] == ['survey "%s" is not enabled' % survey.name] )
def test_country_unknown(self): """This is a stopgap fix for handling 'unknown' value for country""" survey = SurveyFactory.create() data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(), 'question_text': 'ou812?', 'variation_id': '1', 'country': 'unknown' } resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 201 ans = Answer.objects.latest('id') assert ans.country == 'UNK'
def create_answers(self, number, score): survey = SurveyFactory.create( name='heartbeat-by-user-first-impression') AnswerFactory.create_batch(number, score=score, locale='en-us', survey_id=survey)
def test_update_survey(self): survey = SurveyFactory() data = { 'id': survey.id, 'name': 'rehanrocks', 'description': survey.description, 'enabled': survey.enabled } resp = self.client.post( reverse('hb_surveys_update', args=(survey.id,)), data ) assert resp.status_code == 403 jane = AnalyzerProfileFactory().user self.client_login_user(jane) resp = self.client.post( reverse('hb_surveys_update', args=(survey.id,)), data ) assert resp.status_code == 302 # Make sure it's in the survey list resp = self.client.get(reverse('hb_surveys')) assert resp.status_code == 200 assert data['name'] in resp.content
def test_missing_data(self): """Missing required data kicks up an error""" survey = SurveyFactory.create() orig_data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(), 'question_text': 'how was lunch?', 'variation_id': '1' } for key in orig_data.keys(): data = dict(orig_data) del data[key] resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 400 resp_data = json.loads(resp.content) assert key in resp_data['errors']
def test_complete_answer(self): """Complete answer using sane values for everything""" survey = SurveyFactory.create() data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(), 'question_text': 'ou812?', 'variation_id': '1', 'score': 5.0, 'max_score': 1.0, 'flow_began_ts': self.timestamp(offset=-40), 'flow_offered_ts': self.timestamp(offset=-30), 'flow_voted_ts': self.timestamp(offset=-20), 'flow_engaged_ts': self.timestamp(offset=-10), 'platform': 'Windows', 'channel': 'stable', 'version': '33.1', 'locale': 'en-US', 'country': 'US', 'build_id': 'dd2f144cdf44', 'partner_id': 'Phil', 'profile_age': 365, 'profile_usage': { 'abc': 'def' }, 'addons': { 'foo': 'bar' }, 'extra': { 'baz': 'bat' } } resp = self.client.post(reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 201 ans = Answer.objects.latest('id') for field in data.keys(): # survey_id is a special case since it's a foreign key. if field == 'survey_id': # This looks goofy because it's not the normal way to # do things, but the "survey_id" attribute is a # Survey rather than the pk for a Survey. assert ans.survey_id.name == data[field] continue assert getattr(ans, field) == data[field] assert ans.is_test is False
def create_answers(self, number, score): survey = SurveyFactory.create(name='heartbeat-by-user-first-impression') AnswerFactory.create_batch( number, score=score, locale='en-us', survey_id=survey )
def test_complete_answer(self): """Complete answer using sane values for everything""" survey = SurveyFactory.create() data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(), 'question_text': 'ou812?', 'variation_id': '1', 'score': 5.0, 'max_score': 1.0, 'flow_began_ts': self.timestamp(offset=-40), 'flow_offered_ts': self.timestamp(offset=-30), 'flow_voted_ts': self.timestamp(offset=-20), 'flow_engaged_ts': self.timestamp(offset=-10), 'platform': 'Windows', 'channel': 'stable', 'version': '33.1', 'locale': 'en-US', 'country': 'US', 'build_id': 'dd2f144cdf44', 'partner_id': 'Phil', 'profile_age': 365, 'profile_usage': {'abc': 'def'}, 'addons': {'foo': 'bar'}, 'extra': {'baz': 'bat'} } resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 201 ans = Answer.objects.latest('id') for field in data.keys(): # survey_id is a special case since it's a foreign key. if field == 'survey_id': # This looks goofy because it's not the normal way to # do things, but the "survey_id" attribute is a # Survey rather than the pk for a Survey. assert ans.survey_id.name == data[field] continue assert getattr(ans, field) == data[field] assert ans.is_test is False
def test_initial_answer(self): """Initial answer using "non values" for many fields""" survey = SurveyFactory.create() data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(), 'question_text': 'ou812?', 'variation_id': '1', 'score': None, 'max_score': None, 'flow_began_ts': 0, 'flow_offered_ts': 0, 'flow_voted_ts': 0, 'flow_engaged_ts': 0, 'platform': '', 'channel': '', 'version': '', 'locale': '', 'country': '', 'build_id': '', 'partner_id': '', 'profile_age': None, 'profile_usage': {}, 'addons': {}, 'extra': {}, 'is_test': False } resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 201 ans = Answer.objects.latest('id') for field in data.keys(): # survey_id is a special case since it's a foreign key. if field == 'survey_id': # This looks goofy because it's not the normal way to # do things, but the "survey_id" attribute is a # Survey rather than the pk for a Survey. assert ans.survey_id.name == data[field] continue assert getattr(ans, field) == data[field]
def test_updated_ts_lessthan_equal(self): """If the updated_ts <= existing, then update the Answer""" survey = SurveyFactory.create() data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(), 'question_text': 'how was lunch?', 'variation_id': '1', } resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 201 # Test the less-than case. data['updated_ts'] = self.timestamp(offset=-10) resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 400 resp_data = json.loads(resp.content) assert ( resp_data['errors']['updated_ts'] == 'updated timestamp is same or older than existing data' ) # Test the equal case. data['updated_ts'] = self.timestamp() resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 400 resp_data = json.loads(resp.content) assert ( resp_data['errors']['updated_ts'] == 'updated timestamp is same or older than existing data' )
def test_minimal_data(self): """Minimum amount of data required""" survey = SurveyFactory.create() data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(), 'question_text': 'how was lunch?', 'variation_id': '1' } resp = self.client.post(reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 201
def generate_sampledata(options): survey = SurveyFactory(name=u'Sample survey ' + str(uuid4()), description=u'This is a sample survey.', enabled=True) # Add some "real data" for i in range(10): AnswerFactory(person_id=str(uuid4()), survey_id=survey, score=random.randint(1, 5), max_score=5) # Add a test data item AnswerFactory(person_id=str(uuid4()), survey_id=survey, score=3, max_score=5, is_test=True) print 'Done!'
def test_minimal_data(self): """Minimum amount of data required""" survey = SurveyFactory.create() data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(), 'question_text': 'how was lunch?', 'variation_id': '1' } resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 201
def test_updated_ts_greater(self): """If the updated_ts > existing, then update the Answer""" survey = SurveyFactory.create() data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(offset=-10), 'question_text': 'how was lunch?', 'variation_id': '1' } resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 201 # Make sure the answer made it to the db and that the data # matches. ans = Answer.objects.latest('id') assert ans.person_id == data['person_id'] assert ans.survey_id.name == data['survey_id'] assert ans.flow_id == data['flow_id'] assert ans.question_id == data['question_id'] assert ans.updated_ts == data['updated_ts'] assert ans.question_text == data['question_text'] assert ans.variation_id == data['variation_id'] assert ans.score is None assert ans.max_score is None ans_id = ans.id data = { 'experiment_version': '1', 'response_version': 1, 'person_id': 'joemamma', 'survey_id': survey.name, 'flow_id': '20141113', 'question_id': '1', 'updated_ts': self.timestamp(), 'question_text': 'how was lunch?', 'variation_id': '1', 'score': 5.0, 'max_score': 10.0 } resp = self.client.post( reverse('heartbeat-api'), content_type='application/json', data=json.dumps(data)) assert resp.status_code == 201 ans = Answer.objects.get(id=ans_id) ans = Answer.objects.latest('id') assert ans.person_id == data['person_id'] assert ans.survey_id.name == data['survey_id'] assert ans.flow_id == data['flow_id'] assert ans.question_id == data['question_id'] assert ans.updated_ts == data['updated_ts'] assert ans.question_text == data['question_text'] assert ans.variation_id == data['variation_id'] assert ans.score == 5.0 assert ans.max_score == 10.0