def close_review(review: Review, session) -> Review: review.status = "closed" review.finish_timestamp = helper.get_date_time_now() user_handler.give_experience_point(review.user_id, session) pair = review_pair_handler.get_review_pair_from_review(review, session) partner_review = get_partner_review(review, session) if partner_review != None and partner_review.status == 'closed': difference = review_pair_handler.compute_difference(pair) pair.variance = difference pair.is_good = True if difference <= 1 else False review.item.in_progress_reviews_level_1 -= 1 review.item.in_progress_reviews_level_2 -= 1 if pair.is_good: review.item.open_reviews -= 1 review.item.open_reviews_level_1 -= 1 review.item.open_reviews_level_2 -= 1 pairs = review_pair_handler.get_review_pairs_by_item( pair.item_id, session) if(len(list(filter(lambda p: p.is_good, pairs))) >= 4): review.item.status = "closed" review.item.close_timestamp = review.finish_timestamp review.item.result_score = item_handler.compute_item_result_score( review.item_id, session) update_object(review, session) update_object(pair, session) update_object(review.item, session) return review
def test_simple_pair(junior_answer_1: int, junior_answer_2: int, senior_answer_1: int, senior_answer_2: int, expected: float): """ Tests simple data sets to ensure difference between review answers is correctly computed """ pair = ReviewPair() pair.junior_review = Review() pair.senior_review = Review() pair.junior_review.review_answers = [ review_answer_creator.generate_answer(junior_answer_1), review_answer_creator.generate_answer(junior_answer_2) ] pair.senior_review.review_answers = [ review_answer_creator.generate_answer(senior_answer_1), review_answer_creator.generate_answer(senior_answer_2) ] result = review_pair_handler.compute_difference(pair) assert result == expected
def test_tags_to_dict(): with Session() as session: user = User(id=str(uuid4()), name='user1') level = Level(id=1, description='beginner') item = Item(id=str(uuid4()), content='item') tag1 = Tag(id=str(uuid4()), tag='tag1') tag2 = Tag(id=str(uuid4()), tag='tag2') tag3 = Tag(id=str(uuid4()), tag='tag3') review = Review() review.id = str(uuid4()) review.is_peer_review = False review.user_id = user.id review.item_id = item.id session.add_all([user, item, tag1, tag2, tag3, review, level]) session.commit() review_dict = review.to_dict(with_tags=True, with_user=True) assert 'tags' in review_dict assert len(review_dict['tags']) == 0 item_tag1 = ItemTag(id=str(uuid4()), item_id=item.id, tag_id=tag1.id, review_id=review.id) session.add(item_tag1) session.commit() session.refresh(review) review_dict = review.to_dict(with_tags=True, with_user=True) assert 'tags' in review_dict assert len(review_dict['tags']) == 1 assert review_dict['tags'][0] == 'tag1' item_tag2 = ItemTag(id=str(uuid4()), item_id=item.id, tag_id=tag2.id, review_id=review.id) session.add(item_tag2) session.commit() session.refresh(review) review_dict = review.to_dict(with_tags=True, with_user=True) assert 'tags' in review_dict assert len(review_dict['tags']) == 2 assert 'tag2' in review_dict['tags'] session.delete(item_tag1) session.commit() session.refresh(review) review_dict = review.to_dict(with_tags=True, with_user=True) assert 'tags' in review_dict assert len(review_dict['tags']) == 1 assert review_dict['tags'][0] == 'tag2'
def new_senior_review(item): rip2 = Review() rip2.id = str(uuid4()) rip2.start_timestamp = datetime.now() rip2.is_peer_review = True rip2.status = "in_progress" rip2.item_id = item.id return rip2
def old_junior_review(item): rip1 = Review() rip1.id = str(uuid4()) rip1.start_timestamp = datetime.now() + timedelta(hours=-2) rip1.is_peer_review = False rip1.status = "in_progress" rip1.item_id = item.id return rip1
def test_get_user(): with Session() as session: session = setup_scenarios.create_levels_junior_and_senior_detectives( session) junior_detective1 = user_handler.get_user_by_id("1", session) event = event_creator.get_create_review_event(junior_detective1.id, "abc") resp = get_user(event, None) body = json.loads(resp["body"]) assert body["id"] == junior_detective1.id assert body["level"] == 1 assert body["level_description"] == "Junior" assert body["progress"] == 0 assert body["total_rank"] == session.query(User).count() assert body["level_rank"] == session.query(User).filter( User.level_id == junior_detective1.level_id).count() assert body["solved_cases_total"] == 0 assert body["solved_cases_today"] == 0 assert body["exp_needed"] == 5 sign_up_date = datetime.strptime(body["sign_up_timestamp"], '%Y-%m-%d %H:%M:%S').date() assert sign_up_date != datetime.today() item1 = Item(id='item1', status='closed') review1 = Review(id='review1', user_id=junior_detective1.id, item_id=item1.id) item2 = Item(id='item2', status='open') review2 = Review(id='review2', user_id=junior_detective1.id, item_id=item2.id) session.add_all([item1, item2, review1, review2]) session.commit() resp = get_user(event, None) body = json.loads(resp["body"]) assert len(body['closed_items']) == 1 assert body['closed_items'][0]['id'] == 'item1'
def test_create_review(item_id, junior_user_id, senior_user_id): with Session() as session: session = setup_scenarios.create_questions(session) session = setup_scenarios.create_levels_junior_and_senior_detectives(session) item = Item(id=item_id, item_type_id="Type1") session.add(item) session.commit() # Create junior review event = event_creator.get_create_review_event(junior_user_id, item_id) response = create_review.create_review(event, None) assert response['statusCode'] == 201 reviews = session.query(Review).all() assert len(reviews) == 1 review = Review() review = reviews[0] assert review.user_id == junior_user_id assert review.item_id == item_id question_ids = [] for answer in review.review_answers: question_ids.append(answer.review_question_id) assert "other_type" not in question_ids parent_question_counter = 0 for answer in review.review_answers: if answer.review_question.max_children > 0: child_questions = session.query(ReviewQuestion).filter( ReviewQuestion.parent_question_id == answer.review_question_id).all() for child_question in child_questions: assert child_question.id in question_ids if answer.review_question.parent_question_id is None: parent_question_counter += 1 assert parent_question_counter == 6 event = event_creator.get_create_review_event(senior_user_id, item_id) response = create_review.create_review(event, None) assert response['statusCode'] == 201 reviews = session.query(Review).all() assert len(reviews) == 2 reviews = session.query(Review).filter( Review.user_id == senior_user_id).all() assert len(reviews) == 1 review = reviews[0] assert review.user_id == senior_user_id assert review.item_id == item_id for answer in review.review_answers: assert answer.review_question_id in question_ids
def session(item_id, review_id, review_question_id, user_id): session = get_db_session(True, None) item = Item() item.id = item_id user = User() user.id = user_id level = Level(id=1) review = Review() review.id = review_id review.item_id = item.id review.user_id = user.id review_question = ReviewQuestion() review_question.id = review_question_id review_question.content = "Question content" review_question.info = "Question info" review_question.hint = "Question hint" o1 = AnswerOption(id="1", text="Option 1", value=0) o2 = AnswerOption(id="2", text="Option 2", value=1, tooltip="Tooltip 2") o3 = AnswerOption(id="3", text="Option 3", value=2) o4 = AnswerOption(id="4", text="Option 4", value=3) o1.questions = [review_question] o2.questions = [review_question] o4.questions = [review_question] o3.questions = [review_question] # all answers use the same review questions in order to keep the test data small reviewanswer1 = generate_review_answer(1, review_id, review_question_id) reviewanswer2 = generate_review_answer(0, review_id, review_question_id) reviewanswer3 = generate_review_answer(1, review_id, review_question_id) reviewanswer4 = generate_review_answer(3, review_id, review_question_id) reviewanswer5 = generate_review_answer(2, review_id, review_question_id) reviewanswer6 = generate_review_answer(1, review_id, review_question_id) reviewanswer7 = generate_review_answer(2, review_id, review_question_id) review.review_answers = [reviewanswer1, reviewanswer2, reviewanswer3, reviewanswer4, reviewanswer5, reviewanswer6, reviewanswer7] session.add(item) session.add(user) session.add(level) session.add(review_question) # refernenced ReviewAnswers are stored as well session.add(review) session.commit() return session
def test_create_review(session, item_id, junior_user_id, senior_user_id): # Create junior review event = event_creator.get_create_review_event(junior_user_id, item_id) response = create_review.create_review(event, None, True, session) assert response['statusCode'] == 201 reviews = session.query(Review).all() assert len(reviews) == 1 review = Review() review = reviews[0] assert review.user_id == junior_user_id assert review.item_id == item_id question_ids = [] for answer in review.review_answers: question_ids.append(answer.review_question_id) assert "other_type" not in question_ids parent_question_counter = 0 for answer in review.review_answers: if answer.review_question.max_children > 0: child_questions = session.query(ReviewQuestion).filter( ReviewQuestion.parent_question_id == answer.review_question_id).all() for child_question in child_questions: assert child_question.id in question_ids if answer.review_question.parent_question_id is None: parent_question_counter += 1 assert parent_question_counter == 6 event = event_creator.get_create_review_event(senior_user_id, item_id) response = create_review.create_review(event, None, True, session) assert response['statusCode'] == 201 reviews = session.query(Review).all() assert len(reviews) == 2 reviews = session.query(Review).filter( Review.user_id == senior_user_id).all() assert len(reviews) == 1 review = reviews[0] assert review.user_id == senior_user_id assert review.item_id == item_id for answer in review.review_answers: assert answer.review_question_id in question_ids
def test_update_review(session, item_id, junior_user_id, senior_user_id): # Create junior review event = event_creator.get_create_review_event(junior_user_id, item_id) response = create_review.create_review(event, None, True, session) assert response['statusCode'] == 201 reviews = session.query(Review).all() assert len(reviews) == 1 review = Review() review = reviews[0] # Test 403 event = event_creator.get_review_event(review, item_id, "in progress", senior_user_id, 1, session) response = update_review.update_review(event, None, True, session) assert response['statusCode'] == 403 event = event_creator.get_review_event(review, item_id, "in progress", junior_user_id, 1, session) response = update_review.update_review(event, None, True, session) assert response['statusCode'] == 200
def test_update_review(item_id, junior_user_id, senior_user_id, monkeypatch): monkeypatch.setenv("CORS_ALLOW_ORIGIN", "http://localhost:4200") with Session() as session: session = setup_scenarios.create_questions(session) session = setup_scenarios.create_levels_junior_and_senior_detectives( session) item = Item(id=item_id, item_type_id="Type1") session.add(item) session.commit() # Create junior review event = event_creator.get_create_review_event(junior_user_id, item_id) response = create_review.create_review(event, None) assert response['statusCode'] == 201 reviews = session.query(Review).all() assert len(reviews) == 1 review = Review() review = reviews[0] # Test 403 event = event_creator.get_review_event( review, item_id, "in progress", senior_user_id, 1) response = update_review.update_review(event, None) assert response['statusCode'] == 403 event = event_creator.get_review_event( review, item_id, "in progress", junior_user_id, 1, tags=['test', 'tag']) response = update_review.update_review(event, None) assert response['statusCode'] == 200 # Test comments comments = session.query(Comment).all()[0] assert comments.comment == "Test comment" assert comments.review_id == review.id assert comments.user_id == junior_user_id assert comments.status == "published" assert comments.is_review_comment == True # Test tags item_tags = session.query(ItemTag).all() assert len(item_tags) == 2 for item_tag in item_tags: assert item_tag.item_id == item.id assert item_tag.review_id == review.id assert item_tag.tag.tag in ['test', 'tag'] event = event_creator.get_review_event( review, item_id, "in progress", junior_user_id, 1, tags=['test']) response = update_review.update_review(event, None) assert response['statusCode'] == 200 item_tags = session.query(ItemTag).all() assert len(item_tags) == 1 assert item_tags[0].item_id == item.id assert item_tags[0].review_id == review.id assert item_tags[0].tag.tag == 'test' response_body = json.loads(response['body']) assert 'tags' in response_body assert len(response_body['tags']) == 1 assert response_body['tags'][0] == 'test' event = event_creator.get_review_event( review, item_id, "in progress", junior_user_id, 1, tags=[]) response = update_review.update_review(event, None) assert response['statusCode'] == 200 response_body = json.loads(response['body']) assert 'tags' in response_body assert len(response_body['tags']) == 0 event = event_creator.get_review_event( review, item_id, "in progress", junior_user_id, 1, tags=None) response = update_review.update_review(event, None) assert response['statusCode'] == 200 response_body = json.loads(response['body']) assert 'tags' in response_body assert len(response_body['tags']) == 0 event = event_creator.get_review_event( review, item_id, "closed", junior_user_id, 1, tags=None) response = update_review.update_review(event, None) assert response['statusCode'] == 200 # Test not existing review fake_review = review fake_review.id = "fake" event = event_creator.get_review_event( fake_review, item_id, "in progress", junior_user_id, 1) response = update_review.update_review(event, None) assert response['statusCode'] == 404
def create_review(user, item, is_test, session) -> Review: """Accepts an item for review Parameters ---------- user: User The user that reviews the item item: Item The item to be reviewed by the user Returns ------ item: Item The case to be assigned to the user """ # If a ReviewInProgress exists for the user, return session = get_db_session(is_test, session) try: review = session.query(Review).filter(Review.user_id == user.id, Review.status == "in_progress", Review.item_id == item.id).one() return review except: pass # If the amount of reviews in progress equals the amount of reviews needed, raise an error if item.in_progress_reviews_level_1 >= item.open_reviews_level_1: if user.level_id > 1: if item.in_progress_reviews_level_2 >= item.open_reviews_level_2: raise Exception( 'Item cannot be accepted since enough other detecitves are already working on the case' ) else: raise Exception( 'Item cannot be accepted since enough other detecitves are already working on the case' ) # Create a new ReviewInProgress rip = Review() rip.id = str(uuid4()) rip.item_id = item.id rip.user_id = user.id rip.start_timestamp = helper.get_date_time_now(is_test) rip.status = "in_progress" session.add(rip) session.commit() # If a user is a senior, the review will by default be a senior review, # except if no senior reviews are needed if user.level_id > 1 and item.open_reviews_level_2 > item.in_progress_reviews_level_2: rip.is_peer_review = True item.in_progress_reviews_level_2 = item.in_progress_reviews_level_2 + 1 # Check if a pair with open senior review exists pair_found = False for pair in item.review_pairs: if pair.senior_review_id == None: pair.senior_review_id = rip.id pair_found = True session.merge(pair) break # Create new pair, if review cannot be attached to existing pair if pair_found == False: pair = ReviewPair() pair.id = str(uuid4()) pair.senior_review_id = rip.id item.review_pairs.append(pair) session.merge(pair) # If review is junior review else: rip.is_peer_review = False item.in_progress_reviews_level_1 = item.in_progress_reviews_level_1 + 1 # Check if a pair with open junior review exists pair_found = False for pair in item.review_pairs: if pair.junior_review_id == None: pair.junior_review_id = rip.id pair_found = True session.merge(pair) break # Create new pair, if review cannot be attached to existing pair if pair_found == False: pair = ReviewPair() pair.id = str(uuid4()) pair.junior_review_id = rip.id item.review_pairs.append(pair) session.merge(pair) session.merge(rip) session.merge(item) session.commit() rip = create_answers_for_review(rip, is_test, session) return rip
def test_get_review(item_id, review_id, review_question_id, user_id, tag_id, item_tag_id, monkeypatch): """ Gets a simple Review """ monkeypatch.setenv("CORS_ALLOW_ORIGIN", "http://localhost:4200") with Session() as session: item = Item() item.id = item_id user = User() user.id = user_id level = Level(id=1) review = Review() review.id = review_id review.item_id = item.id review.user_id = user.id review.status = 'in_progress' tag = Tag(tag='test', id=tag_id) item_tag = ItemTag(id=item_tag_id, item_id=item_id, tag_id=tag_id, review_id=review_id) comment = Comment(id='comment_id', comment='testcomment', review_id=review_id, is_review_comment=True) review_question = ReviewQuestion() review_question.id = review_question_id review_question.content = "Question content" review_question.info = "Question info" review_question.hint = "Question hint" o1 = AnswerOption(id="1", text="Option 1", value=0) o2 = AnswerOption(id="2", text="Option 2", value=1, tooltip="Tooltip 2") o3 = AnswerOption(id="3", text="Option 3", value=2) o4 = AnswerOption(id="4", text="Option 4", value=3) o1.questions = [review_question] o2.questions = [review_question] o4.questions = [review_question] o3.questions = [review_question] # all answers use the same review questions in order to keep the test data small reviewanswer1 = generate_review_answer(1, review_id, review_question_id) reviewanswer2 = generate_review_answer(0, review_id, review_question_id) reviewanswer3 = generate_review_answer(1, review_id, review_question_id) reviewanswer4 = generate_review_answer(3, review_id, review_question_id) reviewanswer5 = generate_review_answer(2, review_id, review_question_id) reviewanswer6 = generate_review_answer(1, review_id, review_question_id) reviewanswer7 = generate_review_answer(2, review_id, review_question_id) review.review_answers = [ reviewanswer1, reviewanswer2, reviewanswer3, reviewanswer4, reviewanswer5, reviewanswer6, reviewanswer7 ] session.add(item) session.add(user) session.add(level) session.add(review_question) session.add_all([tag, item_tag, comment]) # referenced ReviewAnswers are stored as well session.add(review) session.commit() event = event_creator.get_get_review_event(user_id, review_id) resp = get_review(event, None) status = resp["statusCode"] assert status == 200 body = json.loads(resp["body"]) assert body["id"] == review_id assert len(body["questions"]) == 7 assert body["questions"][0]["answer_id"] != None assert body["questions"][0]["question_id"] != None assert body["questions"][0]["parent_question_id"] == None assert body["questions"][0]["max_children"] == None assert body["questions"][0]["content"] != None assert body["questions"][0]["info"] != None assert body["questions"][0]["hint"] != None assert body["questions"][0]["answer_value"] == 1 assert len(body["questions"][0]["options"]) == 4 assert body["questions"][0]["options"][0]["text"] != None assert body["questions"][0]["options"][0]["value"] == 0 assert body["questions"][0]["options"][1]["tooltip"] != None assert 'tags' in body assert len(body['tags']) == 1 assert body['tags'][0] == 'test' assert 'comment' in body assert body['comment'] == 'testcomment'