def check_pool_exam(name, questions, force=None, seed=None, report=MAIN_REPORT): """ Args: name: questions: force: seed: report: """ # Choose a question if force is None: if seed is None: force = MAIN_REPORT['questions']['seed'] if isinstance(force, str): force = _name_hash(force + name) else: force = seed elif isinstance(force, str): force = _name_hash(force + name) question = questions[force % len(questions)] # Ask it show_question(question['instructions']) # Check if they're done if 'settings' not in question: question['settings'] = {} question['settings'][SETTING_SHOW_CASE_DETAILS] = False results = list(load_question(question)) if results: message, label = results[0] gently(message, label=label)
def test_gently_order(self): clear_report() gently('A great and exciting message!') gently('A boring message that we should not show.') final = simple.resolve() self.assertFalse(final.success) self.assertEqual(final.message, 'A great and exciting message!')
def test_gently_and_set_success(self): clear_report() gently("What have you done?") set_success() final = simple.resolve() self.assertFalse(final.success) self.assertEqual(final.message, 'What have you done?')
def test_explain(self): # Tifa < Explain with Execution('1+""') as e: explain("You cannot add those.") self.assertEqual(e.final.message, "You cannot add those.") # Tifa > Gently with Execution('1+""') as e: gently("You cannot add those.") self.assertEqual(e.final.title, "Incompatible types")
def ensure_cisc108_tests(test_count, **kwargs): """ Ensure that the student has not failed their own tests. This is for the specific ``cisc108`` library, not the general unittest library. """ student = get_student_data() if 'assert_equal' not in student: return gently( f"You have not imported assert_equal from the cisc108 module.", label=f"missing_cisc108_assert_equal", title="Missing assert_equal", **kwargs) assert_equal = student['assert_equal'] if not hasattr(assert_equal, 'student_tests'): return gently( "The assert_equal function has been modified. Do not let it be overwritten!", title="overwrote_assert_equal", label="Overwrote assert_equal", **kwargs) student_tests = assert_equal.student_tests if student_tests.tests == 0: return gently("You are not unit testing the result.", title="No Student Unit Tests", label="no_student_tests", **kwargs) elif student_tests.tests < test_count: return gently("You have not written enough unit tests.", label="not_enough_tests", title="Not Enough Student Unit Tests", **kwargs) elif student_tests.failures > 0: failures = student_tests.failures successes = student_tests.successes tests = student_tests.tests return gently( f"{failures}/{tests} of your unit tests are not passing.", label="failing_student_tests", title="Student Unit Tests Failing", fields={ 'failures': failures, 'successes': successes, 'tests': tests }, **kwargs) return False
def grade_unit_tests(self, question): """ Args: question: Returns: """ all_good = True if self.UNIT_TEST_TOTAL_POINTS is None: TYPE_POINT_ADD = self.UNIT_TEST_TYPE_POINTS VALUE_POINT_ADD = self.UNIT_TEST_VALUE_POINTS else: ratio = self.UNIT_TEST_TYPE_RATIO TYPE_POINT_ADD = (self.UNIT_TEST_TOTAL_POINTS / len(self.tests) * (ratio)) VALUE_POINT_ADD = (self.UNIT_TEST_TOTAL_POINTS / len(self.tests) * (1 - ratio)) for arguments, expected in self.tests: # import sys # print(repr(arguments), file=sys.stderr) result = self.student.call(self.function_name, *arguments, context=False) # print(repr(self.student.exception), file=sys.stderr) if self.student.exception: all_good = False continue if assertIsInstance(result, type(expected)): self.points += TYPE_POINT_ADD else: all_good = False continue if self.assertEqual(result, expected): self.points += VALUE_POINT_ADD else: all_good = False if all_good: self.points += self.UNIT_TEST_COMPLETION_POINTS else: gently("Failing instructor unit tests") return all_good
def test_gently_vs_runtime(self): # Runtime > Gently clear_report() contextualize_report('import json\njson.loads("0")+"1"') verify() tifa_analysis() commands.run() gently("I have a gentle opinion, but you don't want to hear it.") final = simple.resolve() print(final.label) self.assertEqual(Feedback.CATEGORIES.RUNTIME, final.category) # Runtime < Explain clear_report() contextualize_report('import json\njson.loads("0")+"1"') verify() tifa_analysis() commands.run() explain("LISTEN TO ME") final = simple.resolve() self.assertEqual(Feedback.CATEGORIES.INSTRUCTOR, final.category)
def grade_definition(self, question): """ Args: question: Returns: """ self.student = run() if not ensure_function(self.function_name, *self.signature): gently("Function not defined") return False if self.student.exception: return False if not assertHasFunction(self.student, self.function_name): gently("Function defined incorrectly") return False self.points += self.DEFINITION_POINTS return True
def test_partials(self): with Execution('0') as e: gently("You were incorrect.") give_partial(.1, message="You had a zero in your code.") give_partial(.1, message="You looped correctly.") self.assertFeedback(e, "Instructor Feedback\nYou were incorrect.") self.assertEqual(.2, e.final.score) self.assertFalse(e.final.success) with Execution('0') as e: give_partial(.1, message="You had a zero in your code.") give_partial(.1, message="You looped correctly.") gently("Okay but you still only wrote 0.") self.assertEqual(e.final.message, "Okay but you still only wrote 0.") self.assertEqual(e.final.score, .2) self.assertFalse(e.final.success) with Execution('0') as e: give_partial(.1, message="You had a zero in your code.") give_partial(.1, message="You looped correctly.") set_success() self.assertEqual(e.final.message, "Great work!") self.assertEqual(e.final.score, 1.2) self.assertTrue(e.final.success)
def show_parens(): """ Returns: """ message = "Make sure you add parenthesis to <code>plt.show</code>" code = "show_parens" tldr = "Incorrect Show" match = find_match("plt.show") match2 = find_match("plt.show()") if match and not match2: return gently(message, label=code, title=tldr) return False
def all_labels_present( ): # TODO: make sure it's before the show, maybe check for default values """ plt.title("Distribution of Number of Sentences in Long Books") plt.xlabel("Number of Sentences") plt.ylabel("Number of Long Books") plt.show() Returns: """ message = "Make sure you supply labels to all your axes and provide a title and then call show" code = "labels_present" tldr = "Missing Label(s)" match = find_match("plt.title(___)\nplt.show()") match02 = find_match("plt.xlabel(___)\nplt.show()") match03 = find_match("plt.ylabel(___)\nplt.show()") if (not match) or (not match02) or (not match03): return gently(message, label=code, title=tldr) return False
def test_gently(self): clear_report() gently('You should always create unit tests.') final = simple.resolve() self.assertFalse(final.success) self.assertEqual(final.message, 'You should always create unit tests.')