def test_produces_one_row_given_no_jhu_degree(self): response = SurveyResponse() formatted_responses = LongformFormatter().format(response) self.assertEqual(1, len(formatted_responses)) self.assertIsNone(formatted_responses[0]['jhu_degree']) self.assertIsNone(formatted_responses[0]['jhu_major']) self.assertIsNone(formatted_responses[0]['jhu_college'])
def test_look_up_response_by_username(self): test_dataset = ResponseDataset() test_response = SurveyResponse() test_response.student.username = '******' test_dataset.add_response(test_response) self.assertEqual(test_response, test_dataset.get_response_by_username('AJG3T9'))
def __init__(self, raw_data: dict, custom_questions_parser: CustomParser = NullCustomParser()): self._raw_data = raw_data self._response = SurveyResponse( custom_questions_parser.get_questions_class()) self._custom_parser = custom_questions_parser
def test_flattens_single_degree(self): response = SurveyResponse() response.student.jhu_degrees = [ JHUDegree(degree='B.S.', major='Comp Sci', college='WSE') ] formatted_responses = ShortformFormatter().format(response) self.assertEqual('B.S.: Comp Sci', formatted_responses[0]['jhu_majors']) self.assertEqual('WSE', formatted_responses[0]['jhu_colleges'])
def test_produces_one_row_given_one_jhu_degree(self): response = SurveyResponse() response.student.jhu_degrees = [ JHUDegree(degree='B.S.', major='Comp Sci', college='WSE') ] formatted_responses = LongformFormatter().format(response) self.assertEqual(1, len(formatted_responses)) self.assertEqual('B.S.', formatted_responses[0]['jhu_degree']) self.assertEqual('Comp Sci', formatted_responses[0]['jhu_major']) self.assertEqual('WSE', formatted_responses[0]['jhu_college'])
def test_flattens_multiple_degrees_with_different_college_and_degree(self): response = SurveyResponse() response.student.jhu_degrees = [ JHUDegree(degree='B.A.', major='English', college='KSAS'), JHUDegree(degree='B.S.', major='Comp Sci', college='WSE') ] formatted_responses = ShortformFormatter().format(response) self.assertEqual('B.A.: English; B.S.: Comp Sci', formatted_responses[0]['jhu_majors']) self.assertEqual('KSAS; WSE', formatted_responses[0]['jhu_colleges'])
def test_flattens_location_data(self): response = SurveyResponse() response.metadata.location.city = 'Folsom' response.metadata.location.state = 'California' response.metadata.location.country = 'United States' formatted_responses = ShortformFormatter().format(response) self.assertEqual(1, len(formatted_responses)) self.assertEqual('Folsom', formatted_responses[0]['city']) self.assertEqual('California', formatted_responses[0]['state']) self.assertEqual('United States', formatted_responses[0]['country']) with self.assertRaises(KeyError): formatted_responses[0]['location']
def test_flattens_location_data(self): response = SurveyResponse() response.student.jhu_degrees = [ JHUDegree(degree='B.S.', major='Comp Sci', college='WSE') ] response.metadata.location.city = 'Folsom' response.metadata.location.state = 'California' response.metadata.location.country = 'United States' formatted_responses = LongformFormatter().format(response) self.assertEqual('Folsom', formatted_responses[0]['city']) self.assertEqual('California', formatted_responses[0]['state']) self.assertEqual('United States', formatted_responses[0]['country']) with self.assertRaises(KeyError): formatted_responses[0]['location']
def test_produces_multiple_rows_given_multiple_jhu_degrees(self): response = SurveyResponse() response.student.jhu_degrees = [ JHUDegree(degree='B.S.', major='Comp Sci', college='WSE'), JHUDegree(degree='B.A.', major='English', college='KSAS'), JHUDegree(degree='M.B.A.', major='Business', college='Carey') ] formatted_responses = LongformFormatter().format(response) self.assertEqual(3, len(formatted_responses)) self.assertEqual('B.S.', formatted_responses[0]['jhu_degree']) self.assertEqual('Comp Sci', formatted_responses[0]['jhu_major']) self.assertEqual('WSE', formatted_responses[0]['jhu_college']) self.assertEqual('B.A.', formatted_responses[1]['jhu_degree']) self.assertEqual('English', formatted_responses[1]['jhu_major']) self.assertEqual('KSAS', formatted_responses[1]['jhu_college']) self.assertEqual('M.B.A.', formatted_responses[2]['jhu_degree']) self.assertEqual('Business', formatted_responses[2]['jhu_major']) self.assertEqual('Carey', formatted_responses[2]['jhu_college'])
def test_jhu_degrees_field_is_not_present_after_formatting(self): response = SurveyResponse() formatted_responses = LongformFormatter().format(response) self.assertTrue('jhu_degrees' not in formatted_responses[0])
def format(self, response: SurveyResponse) -> List[dict]: self._base_row = response.to_dict() self._flatten_location_fields() self._custom_formatter.format(self._base_row) return self._generate_one_row_per_degree()
def format(self, response: SurveyResponse) -> List[dict]: self._result = response.to_dict() self._flatten_location_fields() self._flatten_jhu_degrees() self._custom_formatter.format(self._result) return [self._column_order.apply_to(self._result)]
def test_flattens_degree_fields_given_no_degrees(self): formatted_responses = ShortformFormatter().format(SurveyResponse()) self.assertIsNone(formatted_responses[0]['jhu_majors']) self.assertIsNone(formatted_responses[0]['jhu_colleges'])
def test_removes_degree_field(self): formatted_responses = ShortformFormatter().format(SurveyResponse()) with self.assertRaises(KeyError): formatted_responses[0]['jhu_degrees']