def surveys(questions, criterions, weights): survey1 = survey.Survey(questions) crt = list(criterions) for i, question in enumerate(questions): survey1.set_criterion(crt[i], question) survey1.set_weight(weights[i], question) return survey1
def test_survey_class_survey_set_criterion() -> None: criteria = criterion.HomogeneousCriterion() question1 = survey.YesNoQuestion(1, 'Hello?') survey1 = survey.Survey([question1]) survey1.set_criterion(criteria, question1) # noinspection PyProtectedMember assert question1.id in survey1._criteria
def test_window(self): students = [course.Student(i, "Shit" + str(i)) for i in range(5)] questions = [survey.NumericQuestion(i, "FUCKNQ" + str(i), 2, 8) for i in range(5)] s = survey.Survey(questions) answers = [1, 0, 0, 3, 4, 1, 0, 1, 1, 1, 2, 1, 2, 2, 2, 3, 2, 1, 0, 1, 3, 0, 0, 3, 2] i = 0 for q in questions: for stu in students: stu.set_answer(q, survey.Answer(2 + answers[i])) i += 1 c = course.Course("Asshole101") c.enroll_students(students) grouping1 = grouper.WindowGrouper(1).make_grouping(c, s) assert len(grouping1) == 5 assert grouping_to_list_of_list(grouping1) == [[0], [1], [2], [3], [4]] grouping2 = grouper.WindowGrouper(2).make_grouping(c, s) assert len(grouping2) == 3 assert grouping_to_list_of_list(grouping2) == [[0], [1, 2], [3, 4]] grouping3 = grouper.WindowGrouper(3).make_grouping(c, s) assert len(grouping3) == 2 assert grouping_to_list_of_list(grouping3) == [[0, 1, 2], [3, 4]] grouping5 = grouper.WindowGrouper(5).make_grouping(c, s) assert len(grouping5) == 1 assert grouping_to_list_of_list(grouping5) == [[0, 1, 2, 3, 4]]
def test_survey_class_survey_set_weight() -> None: question1 = survey.YesNoQuestion(1, 'Hello?') survey1 = survey.Survey([question1]) weight1 = 1 survey1.set_weight(weight1, question1) # noinspection PyProtectedMember assert weight1 in survey1._weights
def test_set_criterion(self, questions): s = survey.Survey(questions) for q in questions: s.set_criterion(criterion.LonelyMemberCriterion(), q) for f in questions: assert isinstance(s._get_criterion(f), criterion.LonelyMemberCriterion)
def test_make_grouping_greedy(self): new_course = course.Course('CSC148') q1 = survey.CheckboxQuestion(1, 'Hobbies?', ['Movie', 'Sing', 'Dance', 'Game']) new_survey = survey.Survey([q1]) students = [course.Student(1, 'Zoro'), course.Student(2, 'Aaron'), course.Student(3, 'Gertrude'), course.Student(4, 'Yvette'), course.Student(6, 'Steph'), course.Student(7, 'Draymond'), course.Student(8, 'Andrew')] new_course.enroll_students(students) new_grouper = grouper.GreedyGrouper(2) new_grouping = new_grouper.make_grouping(new_course, new_survey) groups = new_grouping.get_groups() temp_list = [] for i in range(0, len(groups)): people = groups[i].get_members() temp_list.extend(people) if i == (len(groups) - 1): assert len(groups[i]) <= 2 else: assert len(groups[i]) == 2 course_students = new_course.get_students() for student in course_students: assert student in temp_list assert len(temp_list) == 7
def test_complicated(self): students = [course.Student(i, "Shit" + str(i)) for i in range(3)] questions1 = [survey.NumericQuestion(i, "FUCKNQ" + str(i), 2, 8) for i in range(5)] s = survey.Survey(questions1) assert len(s) == 5 assert questions1[0] in s assert survey.YesNoQuestion(99, "Yue?") not in s assert str(s).count("FUCKNQ") >= 5 assert len(s.get_questions()) == 5 assert fuck_compare_lists([x.id for x in s.get_questions()], [0, 1, 2, 3, 4]) answers = [0, 1, 0, 1, 2] * 3 i = 0 for q in questions1: for stu in students: stu.set_answer(q, survey.Answer(2 + answers[i])) i += 1 assert fuck_within(s.score_students(students), 0.8222222) answers = [1, 0, 0, 3, 4] * 3 i = 0 for q in questions1: for stu in students: stu.set_answer(q, survey.Answer(2 + answers[i])) i += 1 assert fuck_within(s.score_students(students), 0.6666666666666667) s.set_weight(2, questions1[0]) assert fuck_within(s.score_students(students), 0.8444444444444444) s.set_criterion(criterion.HeterogeneousCriterion(), questions1[1]) assert fuck_within(s.score_students(students), 0.7777777777777779) # this should result in InvalidAnswerError answers = [9, 9, 9, 9, 9] * 3 i = 0 for q in questions1: for stu in students: stu.set_answer(q, survey.Answer(2 + answers[i])) i += 1 assert fuck_within(s.score_students(students), 0) assert s.score_grouping(grouper.Grouping()) == 0, '''empty grouping result always 1''' g = grouper.Grouping() g.add_group(grouper.Group([students[0]])) g.add_group(grouper.Group([students[1]])) g.add_group(grouper.Group([students[2]])) assert s.score_students([students[0]]) == 0 assert s.score_students([students[1]]) == 0 assert s.score_students([students[2]]) == 0 assert s.score_grouping(g) == 0 answers = [1, 0, 0, 3, 4] * 3 i = 0 for q in questions1: for stu in students: stu.set_answer(q, survey.Answer(2 + answers[i])) i += 1 assert s.score_grouping(g) == 1.0
def test_get_questions(self) -> None: s = survey.Survey(self.questions) descr = s.get_questions() assert self.q1 in descr assert self.q2 not in descr assert self.q3 in descr assert self.q4 in descr assert self.q5 in descr
def survey_(questions, criteria, weights) -> survey.Survey: s = survey.Survey(questions) for i, question in enumerate(questions): if i: s.set_weight(weights[i - 1], question) if len(questions) - 1 != i: s.set_criterion(criteria[i], question) return s
def post(self): logging.debug('SubmitNewSurvey.post() request.body=' + self.request.body) # Collect inputs requestLogId = os.environ.get(conf.REQUEST_LOG_ID) inputData = json.loads(self.request.body) logging.debug('SubmitNewSurvey.post() inputData=' + str(inputData)) introduction = text.formTextToStored(inputData.get('introduction', '')) browserCrumb = inputData.get('crumb', '') loginCrumb = inputData.get('crumbForLogin', '') loginRequired = inputData.get('loginRequired', False) logging.debug('SubmitNewSurvey.post() introduction=' + str(introduction) + ' browserCrumb=' + str(browserCrumb) + ' loginCrumb=' + str(loginCrumb) + ' loginRequired=' + str(loginRequired)) responseData = {'success': False, 'requestLogId': requestLogId} cookieData = httpServer.validate(self.request, inputData, responseData, self.response, loginRequired=loginRequired) if not cookieData.valid(): return userId = cookieData.id() # Check survey introduction length. if not httpServer.isLengthOk(introduction, '', conf.minLengthSurveyIntro): return httpServer.outputJson(cookieData, responseData, self.response, errorMessage=conf.TOO_SHORT) # Construct and store new survey record. surveyRecord = survey.Survey(creator=userId, introduction=introduction, allowEdit=True) surveyRecordKey = surveyRecord.put() logging.debug('surveyRecordKey.id={}'.format(surveyRecordKey.id())) # Construct and store link key. surveyId = str(surveyRecordKey.id()) linkKeyRecord = httpServer.createAndStoreLinkKey( conf.SURVEY_CLASS_NAME, surveyId, loginRequired, cookieData) # Display survey. surveyDisplay = httpServerAutocomplete.surveyToDisplay( surveyRecord, userId) linkKeyDisplay = httpServer.linkKeyToDisplay(linkKeyRecord) responseData.update({ 'success': True, 'linkKey': linkKeyDisplay, 'survey': surveyDisplay }) httpServer.outputJson(cookieData, responseData, self.response)
def test_course_class_course_all_answered() -> None: s1 = course.Student(1, 'Misha') courses = course.Course('CSC148') courses.enroll_students([s1]) question = survey.YesNoQuestion(1, 'Do you like food?') answer = survey.Answer(True) s1.set_answer(question, answer) surveys = survey.Survey([question]) assert courses.all_answered(surveys)
def test_score_grouping(self, questions, students_with_answers): new_course = course.Course('CSC148') q1 = survey.CheckboxQuestion(1, 'Hobbies?', ['Movie', 'Sing', 'Dance', 'Game']) new_survey = survey.Survey([q1]) students = [course.Student(1, 'Zoro'), course.Student(2, 'Aaron'), course.Student(3, 'Gertrude'), course.Student(4, 'Yvette'), course.Student(6, 'Steph'), course.Student(7, 'Draymond'), course.Student(8, 'Andrew')] new_course.enroll_students(students_with_answers) new_grouper = grouper.AlphaGrouper(2) new_grouping = new_grouper.make_grouping(new_course, new_survey) s = survey.Survey(questions) score = s.score_grouping(new_grouping) assert score == 2.0
def test_weight_crit(self): stu = course.Student(0, "Shit0") q = survey.YesNoQuestion(0, "Yue me?") stu.set_answer(q, survey.Answer(True)) s = survey.Survey([q]) assert s.score_students([stu]) == 1.0 s.set_weight(5, q) assert s.score_students([stu]) == 5.0 s.set_criterion(criterion.HeterogeneousCriterion(), q) assert s.score_students([stu]) == 0.0
def test_empty_survey(self): students = [course.Student(i, "Shit" + str(i)) for i in range(10)] q1 = survey.YesNoQuestion(0, "Yue me?") s = survey.Survey([]) assert len(s) == 0 assert q1 not in s assert str(s) is not None assert len(s.get_questions()) == 0 assert s.get_questions() == [] assert isinstance(s._get_criterion(q1), criterion.HomogeneousCriterion) assert isinstance(s._get_weight(q1), int) assert s.score_students(students) == 0
def test_all_answered(self, answers, questions): new_course = course.Course('CSC148') students = [course.Student(1, 'Zoro'), course.Student(2, 'Aaron'), course.Student(3, 'Gertrude'), course.Student(4, 'Yvette'), course.Student(6, 'Steph'), course.Student(7, 'Draymond'), course.Student(8, 'Andrew')] s = survey.Survey(questions) new_course.enroll_students(students) assert new_course.all_answered(s) == False
def test_survey_class_survey_score_students_raise_invalidanswererror() -> None: s1 = course.Student(1, 'Misha') students = [s1] question1 = survey.NumericQuestion(1, 'Do you like food?', 0, 10) answer1 = survey.Answer(True) survey1 = survey.Survey([question1]) criteria = criterion.HomogeneousCriterion() weight1 = 1 s1.set_answer(question1, answer1) survey1.set_weight(weight1, question1) survey1.set_criterion(criteria, question1) assert survey1.score_students(students) == 0.0
def test_grouper_class_grouping_get_groups_() -> None: c1 = course.Course('CSC148') s1 = course.Student(1, 'Ali') s2 = course.Student(2, 'Kat') s3 = course.Student(3, 'Dorsa') s4 = course.Student(4, 'Sophia') s5 = course.Student(5, 'Momo') s6 = course.Student(6, 'Joseph') c1.enroll_students([s1, s2, s3, s4, s5, s6]) a1 = grouper.AlphaGrouper(2) q1 = survey.MultipleChoiceQuestion(1, 'What is your choice?', ['A', 'B', 'C']) sur1 = survey.Survey([q1]) a1.make_grouping(c1, sur1) assert len(a1.make_grouping(c1, sur1).get_groups()) == 3
def test_survey_class_survey_score_students_two_students() -> None: s1 = course.Student(1, 'Misha') s2 = course.Student(2, 'Jennifer') students = [s1, s2] question1 = survey.NumericQuestion(1, 'Do you like food?', 0, 10) answer1 = survey.Answer(5) answer2 = survey.Answer(10) survey1 = survey.Survey([question1]) criteria = criterion.HeterogeneousCriterion() weight1 = 3 s1.set_answer(question1, answer1) s2.set_answer(question1, answer2) survey1.set_weight(weight1, question1) survey1.set_criterion(criteria, question1) assert survey1.score_students(students) == 1.5
def test_super_complicated(self): students = [course.Student(i, "Shit" + str(i)) for i in range(8)] q1 = survey.YesNoQuestion(0, "To be, or not to be") q2 = survey.NumericQuestion(1, "How many times you f**k up", 0, 10000) q3 = survey.MultipleChoiceQuestion(2, "What's your favorite thing to do", ['A', 'B', 'C', 'D']) q4 = survey.CheckboxQuestion(3, "How many things you have done", ['A', 'B', 'C', 'D', 'E']) questions = [q1, q2, q3, q4] s = survey.Survey(questions) answers = [True, 1111, 'A', ['A', 'C'], False, 44, 'C', ['A', 'B', 'C'], True, 3, 'B', ['A', 'C', 'D'], True, 56, 'C', ['D', 'C'], False, 12, 'C', ['E'], True, 0, 'A', ['A'], False, 888, 'C', ['A', 'B', 'C', 'D'], True, 12, 'B', ['A', 'B', 'C', 'D', 'E']] i = 0 for stu in students: for q in questions: stu.set_answer(q, survey.Answer(answers[i])) i += 1 g = grouper.Grouping() g.add_group(grouper.Group([students[0], students[1], students[2]])) g.add_group(grouper.Group([students[3], students[4], students[5]])) g.add_group(grouper.Group([students[6], students[7]])) assert fuck_within(s.score_students(students), 0.5175303571428571) assert fuck_within(s.score_grouping(g), 0.43715925925925925) s.set_weight(2, q2) s.set_weight(5, q1) s.set_weight(6, q3) s.set_weight(3, q4) assert fuck_within(s.score_students(students), 1.7600607142857143) assert fuck_within(s.score_grouping(g), 1.2696888888888889) s.set_criterion(criterion.HeterogeneousCriterion(), q1) s.set_criterion(criterion.HeterogeneousCriterion(), q2) s.set_criterion(criterion.HeterogeneousCriterion(), q3) s.set_criterion(criterion.HeterogeneousCriterion(), q4) assert fuck_within(s.score_students(students), 2.2399392857142857) assert fuck_within(s.score_grouping(g), 2.7303111111111114)
def test_windowgrouping() -> None: ted = course.Student(1, "Ted") fred = course.Student(2, "Fred") jack = course.Student(3, "Jack") bob = course.Student(4, "Bob") course1 = course.Course("CSC148") course1.enroll_students([ted, fred, jack, bob]) question1 = survey.YesNoQuestion(1, "Really?") survey1 = survey.Survey([question1]) ted.set_answer(question1, survey.Answer(True)) fred.set_answer(question1, survey.Answer(False)) jack.set_answer(question1, survey.Answer(False)) bob.set_answer(question1, survey.Answer(True)) grouper1 = grouper.WindowGrouper(2) grouping1 = grouper1.make_grouping(course1, survey1) assert fred in grouping1.get_groups()[0] assert jack in grouping1.get_groups()[0] assert ted in grouping1.get_groups()[1] assert bob in grouping1.get_groups()[1]
def test_survey_class_survey_score_grouping() -> None: q1 = survey.NumericQuestion(1, "How many?", 0, 5) q2 = survey.YesNoQuestion(2, 'Are you okay?') survey1 = survey.Survey([q1, q2]) ted = course.Student(1, "Ted") fred = course.Student(2, "Fred") jack = course.Student(3, "Jack") bob = course.Student(4, "Bob") ted.set_answer(q1, survey.Answer(2)) fred.set_answer(q1, survey.Answer(3)) jack.set_answer(q1, survey.Answer(4)) bob.set_answer(q1, survey.Answer(1)) ted.set_answer(q2, survey.Answer(True)) fred.set_answer(q2, survey.Answer(True)) jack.set_answer(q2, survey.Answer(False)) bob.set_answer(q2, survey.Answer(False)) grouping1 = grouper.Grouping() grouping1.add_group(grouper.Group([ted, fred])) grouping1.add_group(grouper.Group([jack, bob])) assert survey1.score_grouping(grouping1) == 0.8
def load_survey(data: Dict[str, Any]) -> survey.Survey: """Return a survey created using the information in <data>""" questions = {} criteria = {} weights = {} for q_data in data['questions']: args = q_data['question'].get('args', []) question = getattr(survey, q_data['question']['class'])(*args) questions[question.id] = question weight = q_data.get('weight') crit_data = q_data.get('criterion') if crit_data is not None: criteria[question.id] = _load_criterion(crit_data) if weight is not None: weights[question.id] = weight survey_ = survey.Survey(list(questions.values())) for id_, criterion_ in criteria.items(): survey_.set_criterion(criterion_, questions[id_]) for id_, weight in weights.items(): survey_.set_weight(weight, questions[id_]) return survey_
def test_grouper_greedygrouper() -> None: c1 = course.Course('CSC148') s1 = course.Student(1, 'Ali') s2 = course.Student(2, 'Kat') s3 = course.Student(3, 'Dorsa') s4 = course.Student(4, 'Sophia') s5 = course.Student(5, 'Momo') s6 = course.Student(6, 'Joseph') c1.enroll_students([s1, s2, s3, s4, s5, s6]) a1 = grouper.GreedyGrouper(2) q1 = survey.MultipleChoiceQuestion(1, 'What is your choice?', ['A', 'B', 'C']) a = survey.Answer('A') b = survey.Answer('B') c = survey.Answer('C') s1.set_answer(q1, a) s2.set_answer(q1, b) s3.set_answer(q1, c) s4.set_answer(q1, a) s5.set_answer(q1, b) s6.set_answer(q1, c) sur1 = survey.Survey([q1]) assert a1.make_grouping(c1, sur1).get_groups()[0].get_members()[0] == s1 assert a1.make_grouping(c1, sur1).get_groups()[2].get_members()[1] == s6
def test_survey_properties(self) -> None: s = survey.Survey(self.qs) assert len(s) == 5 for q in self.qs: assert q in s
class TestGrouper: q1 = survey.MultipleChoiceQuestion(1, "species?", ["human", 'bug', 'owl']) q2 = survey.YesNoQuestion(2, "are you human?") q3 = survey.CheckboxQuestion(3, "type?", ["chaotic", "evil", "neutral"]) q4 = survey.NumericQuestion(4, "number of legs?", 1, 8) s = survey.Survey([q1, q2, q3, q4]) c = course.Course("Learning 101") def answer_questions(self) -> List[course.Student]: # students s1 = course.Student(2, "First") s1.set_answer(self.q1, survey.Answer("owl")) s1.set_answer(self.q2, survey.Answer(False)) s1.set_answer(self.q3, survey.Answer(["chaotic", "evil"])) s1.set_answer(self.q4, survey.Answer(1)) s2 = course.Student(25, "Second") s2.set_answer(self.q1, survey.Answer("bug")) s2.set_answer(self.q2, survey.Answer(False)) s2.set_answer(self.q3, survey.Answer(["chaotic", "evil", "neutral"])) s2.set_answer(self.q4, survey.Answer(8)) s3 = course.Student(34, "Third") s3.set_answer(self.q1, survey.Answer("human")) s3.set_answer(self.q2, survey.Answer(True)) s3.set_answer(self.q3, survey.Answer(["chaotic", "neutral"])) s3.set_answer(self.q4, survey.Answer(5)) s4 = course.Student(9, "Fourth") s4.set_answer(self.q1, survey.Answer("owl")) s4.set_answer(self.q2, survey.Answer(False)) s4.set_answer(self.q3, survey.Answer(["neutral"])) s4.set_answer(self.q4, survey.Answer(2)) s5 = course.Student(89, "Last") s5.set_answer(self.q1, survey.Answer("human")) s5.set_answer(self.q2, survey.Answer(True)) s5.set_answer(self.q3, survey.Answer(["evil"])) s5.set_answer(self.q4, survey.Answer(7)) s = [s1, s2, s3, s4, s5] return s def test_course_all_answered(self) -> None: self.c.enroll_students(self.answer_questions()) assert self.c.all_answered(self.s) def test_score_students(self) -> None: self.c.enroll_students(self.answer_questions()) assert self.s.score_students(self.answer_questions()) \ == 0.34761904761904766 assert self.s.score_students(self.answer_questions()[:3]) \ == 0.3055555555555555 assert self.s.score_students(self.answer_questions()[2:4]) \ == 0.26785714285714285 def test_alpha_grouper(self) -> None: self.c.enroll_students(self.answer_questions()) g = grouper.AlphaGrouper(3) gps = g.make_grouping(self.c, self.s).get_groups() assert len(gps) == 2 assert gps[1].get_members()[1].id == 34 assert len(gps[1].get_members()) == 2 g = grouper.AlphaGrouper(2) gps = g.make_grouping(self.c, self.s).get_groups() assert gps[1].get_members()[1].id == 25 # number incorrect assert len(gps) == 3 h = [] for group in gps: h.extend(group.get_members()) for student in self.c.students: assert student in h def test_score_grouping(self) -> None: self.c.enroll_students(self.answer_questions()) g = grouper.AlphaGrouper(3) gps = g.make_grouping(self.c, self.s) assert self.s.score_grouping(gps) == 0.3125 self.c.enroll_students(self.answer_questions()) g2 = grouper.GreedyGrouper(3) gps2 = g2.make_grouping(self.c, self.s) assert self.s.score_grouping(gps2) == 0.5892857142857143 def test_random_grouper(self) -> None: self.c.enroll_students(self.answer_questions()) g = grouper.RandomGrouper(3) gps = g.make_grouping(self.c, self.s).get_groups() assert len(gps) == 2 assert len(gps[1].get_members()) == 2 g = grouper.RandomGrouper(2) gps = g.make_grouping(self.c, self.s).get_groups() assert len(gps) == 3 h = [] for group in gps: h.extend(group.get_members()) for student in self.c.students: assert student in h def test_greedy_grouper(self) -> None: self.c.enroll_students(self.answer_questions()) g = grouper.GreedyGrouper(3) gps = g.make_grouping(self.c, self.s).get_groups() assert len(gps) == 2 assert gps[1].get_members()[1].id == 89 assert len(gps[1].get_members()) == 2 g = grouper.GreedyGrouper(2) gps = g.make_grouping(self.c, self.s).get_groups() assert len(gps) == 3 assert gps[1].get_members()[1].id == 34 h = [] for group in gps: h.extend(group.get_members()) for student in self.c.students: assert student in h def test_window_grouper(self) -> None: self.c.enroll_students(self.answer_questions()) g = grouper.WindowGrouper(3) gps = g.make_grouping(self.c, self.s).get_groups() assert len(gps) == 2 assert gps[0].get_members()[1] == 9 # number incorrect assert len(gps[1].get_members()) == 2 h = [] for group in gps: h.extend(group.get_members()) for student in self.c.students: assert student in h
def test_survey_class_survey_get_questions() -> None: question1 = survey.YesNoQuestion(1, 'Hello?') survey1 = survey.Survey([question1]) assert survey1.get_questions() == [question1]
def test_survey_class_survey__str__() -> None: question1 = survey.YesNoQuestion(1, 'Hello?') survey1 = survey.Survey([question1]) assert 'Hello' in str(survey1)
def test_survey_class_survey__contains__() -> None: q1 = survey.YesNoQuestion(1, 'Hello?') q2 = survey.YesNoQuestion(3, 'Hello who are you?') q3 = survey.NumericQuestion(4, 'How are you?', 0, 10) survey1 = survey.Survey([q1, q2, q3]) assert survey1.__contains__(q2)
def test_set_weight(self) -> None: s = survey.Survey(self.questions) assert s.set_weight(2, self.q1) assert s.set_weight(5, self.q5) assert s.set_weight(-1, self.q3) assert not s.set_weight(2, self.q2)
def test_set_criterion(self) -> None: s = survey.Survey(self.questions) assert s.set_criterion(criterion.LonelyMemberCriterion(), self.q1) assert s.set_criterion(criterion.HeterogeneousCriterion(), self.q5) assert not s.set_criterion(criterion.HomogeneousCriterion(), self.q2)