def test_action_find_by_action_distinguish_actions(db, assignment_tree, user_johaannes): # Add a fetched action orm_action = Action( user_id=user_johaannes.id, assignment_id=assignment_tree.id, action=AssignmentActions.fetched, location="/some/random/path/to/a/file.tzg", ) db.add(orm_action) db.commit() found_by_pk = Action.find_by_pk(db, 1) # Without an action, we just get the last action found_recent = Action.find_most_recent_action(db, found_by_pk.assignment_id) assert found_recent.action == AssignmentActions.fetched # We can get different entries if we define the action found_recent_a = Action.find_most_recent_action(db, found_by_pk.assignment_id, AssignmentActions.fetched) assert found_recent_a.action == AssignmentActions.fetched found_recent_b = Action.find_most_recent_action(db, found_by_pk.assignment_id, AssignmentActions.released) assert found_recent_b.action == AssignmentActions.released assert found_recent_a.id != found_recent_b.id
def test_feedback_find_all_for_student_again(db, assignment_tree, user_johaannes, user_kaylee): notebook = Notebook.find_by_name(db, "Exam 2", assignment_tree.id) released = Action.find_most_recent_action(db, assignment_tree.id, AssignmentActions.fetched) orm_feedback = Feedback( notebook_id=notebook.id, instructor_id=user_kaylee.id, student_id=user_johaannes.id, location=released.location, checksum="234567890abcdef1", timestamp=released.timestamp, ) db.add(orm_feedback) db.commit() # Note this this swaps instructor & user, so should *not* be included orm_feedback_2 = Feedback( notebook_id=notebook.id, instructor_id=user_johaannes.id, student_id=user_kaylee.id, location=released.location, checksum="34567890abcdef12", timestamp=released.timestamp, ) db.add(orm_feedback_2) db.commit() feedback = Feedback.find_all_for_student(db, user_johaannes.id, assignment_tree.id) assert len(feedback) == 2
def test_feedback_base_mathods_and_find_by_pk(db, assignment_tree, user_kaylee, user_johaannes): # previous subscriptions & notebooks still in the db notebook = Notebook.find_by_name(db, "Exam 2", assignment_tree.id) released = Action.find_most_recent_action(db, assignment_tree.id, AssignmentActions.released) orm_feedback = Feedback( notebook_id=notebook.id, instructor_id=user_kaylee.id, student_id=user_johaannes.id, location=released.location, checksum="1234567890abcdef", timestamp=released.timestamp, ) db.add(orm_feedback) db.commit() with pytest.raises(TypeError): found_by_pk = Feedback.find_by_pk() with pytest.raises(TypeError): found_by_pk = Feedback.find_by_pk(db) with pytest.raises(ValueError): found_by_pk = Feedback.find_by_pk(db, None) with pytest.raises(TypeError): found_by_pk = Feedback.find_by_pk(db, "abc") found_by_pk = Feedback.find_by_pk(db, orm_feedback.id) assert found_by_pk.id == orm_feedback.id assert ( str(found_by_pk) == f"Feedback<Notebook-{found_by_pk.notebook_id}/Student-{found_by_pk.student_id}/{found_by_pk.checksum}>" ) assert found_by_pk.notebook_id == notebook.id assert found_by_pk.instructor_id == user_kaylee.id assert found_by_pk.student_id == user_johaannes.id # relationships assert found_by_pk.notebook.name == notebook.name assert found_by_pk.instructor.name == user_kaylee.name assert found_by_pk.student.name == user_johaannes.name found_by_pk = Feedback.find_by_pk(db, orm_feedback.id + 10) assert found_by_pk is None
def test_action_find_by_action(db): # subscription & action set up earlier with pytest.raises(TypeError): found_by_pk = Action.find_most_recent_action(db, None) with pytest.raises(TypeError): found_by_pk = Action.find_most_recent_action(db, "abc") with pytest.raises(TypeError): found_by_pk = Action.find_most_recent_action(db, 1, dict()) found_by_pk = Action.find_by_pk(db, 1) # released found_recent = Action.find_most_recent_action(db, found_by_pk.assignment_id, found_by_pk.action) assert found_recent.action == found_by_pk.action found_recent = Action.find_most_recent_action(db, found_by_pk.assignment_id, "released") assert found_recent.action == found_by_pk.action found_recent = Action.find_most_recent_action(db, found_by_pk.assignment_id, AssignmentActions.released) assert found_recent.action == found_by_pk.action found_recent = Action.find_most_recent_action(db, found_by_pk.assignment_id, AssignmentActions.feedback_fetched) assert found_recent is None
def test_all_the_unicode(db, assignment_a2ovi, user_rur, course_strange): # subscribe user to course # add assignment to course role = "instructor" release_file = "/some/random/path/to/a/file.tzg" orm_subscription = Subscription(user_id=user_rur.id, course_id=course_strange.id, role=role) db.add(orm_subscription) assignment_a2ovi.course_id = course_strange.id db.commit() found_by_pk = Subscription.find_by_pk(db, orm_subscription.id) assert found_by_pk.id == orm_subscription.id assert orm_subscription.course.course_title == "Damnation Alley" # release orm_action = Action( action=AssignmentActions.released, location=release_file, ) db.add(orm_action) db.commit() orm_action.user_id = user_rur.id orm_action.assignment_id = assignment_a2ovi.id db.commit() # fetch orm_action = Action( user_id=user_rur.id, assignment_id=assignment_a2ovi.id, action=AssignmentActions.fetched, location=release_file, ) db.add(orm_action) db.commit() found = Action.find_most_recent_action(db, assignment_a2ovi.id) assert found.user.name == user_rur.name
def get(self): # def get(self, course_code, assignment_code=None): [course_code, assignment_code] = self.get_params(["course_id", "assignment_id"]) if not (course_code and assignment_code): note = "Assigment call requires both a course code and an assignment code!!" self.log.info(note) self.finish({"success": False, "note": note}) return this_user = self.nbex_user if not course_code in this_user["courses"]: note = f"User not subscribed to course {course_code}" self.log.info(note) self.finish({"success": False, "note": note}) return # Find the course being referred to with scoped_session() as session: course = Course.find_by_code( db=session, code=course_code, org_id=this_user["org_id"], log=self.log ) if course is None: note = f"Course {course_code} does not exist" self.log.info(note) self.finish({"success": False, "note": note}) return # needs a proper 'fail' here note = "" self.log.debug(f"Course:{course_code} assignment:{assignment_code}") # The location for the data-object is actually held in the 'released' action for the given assignment # We want the last one... assignment = AssignmentModel.find_by_code( db=session, code=assignment_code, course_id=course.id, action=AssignmentActions.released.value, ) if assignment is None: note = f"Assignment {assignment_code} does not exist" self.log.info(note) self.finish({"success": False, "note": note}) return # needs a proper 'fail' here self._headers = httputil.HTTPHeaders( { "Content-Type": "application/gzip", "Date": httputil.format_timestamp(time.time()), } ) data = b"" release_file = None action = Action.find_most_recent_action( db=session, assignment_id=assignment.id, action=AssignmentActions.released, log=self.log, ) release_file = action.location if release_file: try: with open(release_file, "r+b") as handle: data = handle.read() except Exception as e: # TODO: exception handling self.log.warning(f"Error: {e}") # TODO: improve error message self.log.info(f"Unable to open file") # error 500?? raise Exception self.log.info( f"Adding action {AssignmentActions.fetched.value} for user {this_user['id']} against assignment {assignment.id}" ) action = Action( user_id=this_user["id"], assignment_id=assignment.id, action=AssignmentActions.fetched, location=release_file, ) session.add(action) self.log.info("record of fetch action committed") self.finish(data) else: self.log.info("no release file found") raise Exception