def test_overall_sort_and_paging(self): self._add_ratings() ratings = logic_rating.get_project_rating_by_type( self.wp10db, b'Project 0') self.assertEqual(100, len(ratings)) for rating in ratings[:25]: self.assertEqual(b'FA-Class', rating.r_quality) self.assertEqual(b'High-Class', rating.r_importance) for rating in ratings[25:50]: self.assertEqual(b'FA-Class', rating.r_quality) self.assertEqual(b'Low-Class', rating.r_importance) for rating in ratings[50:75]: self.assertEqual(b'A-Class', rating.r_quality) self.assertEqual(b'High-Class', rating.r_importance) for rating in ratings[75:]: self.assertEqual(b'A-Class', rating.r_quality) self.assertEqual(b'Low-Class', rating.r_importance) ratings = logic_rating.get_project_rating_by_type(self.wp10db, b'Project 0', page=2) self.assertEqual(50, len(ratings)) for rating in ratings[:25]: self.assertEqual(b'B-Class', rating.r_quality) self.assertEqual(b'High-Class', rating.r_importance) for rating in ratings[25:50]: self.assertEqual(b'B-Class', rating.r_quality) self.assertEqual(b'Low-Class', rating.r_importance)
def test_assessed_class(self): self._add_ratings(use_unassessed=True) ratings = logic_rating.get_project_rating_by_type( self.wp10db, b'Project 0', quality=b'Assessed-Class') self.assertEqual(100, len(ratings)) for rating in ratings: self.assertNotEqual(b'Unassessed-Class', rating.r_quality) ratings = logic_rating.get_project_rating_by_type( self.wp10db, b'Project 0', quality=b'Assessed-Class', page=2) self.assertEqual(50, len(ratings)) for rating in ratings: self.assertNotEqual(b'Unassessed-Class', rating.r_quality)
def test_article_pattern_no_results(self): self._add_ratings() ratings = logic_rating.get_project_rating_by_type(self.wp10db, b'Project 0', pattern='xyz', limit=500) self.assertEqual(0, len(ratings))
def test_no_results(self): self._add_ratings() ratings = logic_rating.get_project_rating_by_type( self.wp10db, b'Project 0', importance=b'Foo-Class') self.assertEqual(0, len(ratings)) ratings = logic_rating.get_project_rating_by_type(self.wp10db, b'Project 0', quality=b'Bar-Class') self.assertEqual(0, len(ratings)) ratings = logic_rating.get_project_rating_by_type( self.wp10db, b'Project 0', quality=b'Foo-Class', importance=b'Bar-Class') self.assertEqual(0, len(ratings))
def test_importance(self): self._add_ratings() ratings = logic_rating.get_project_rating_by_type( self.wp10db, b'Project 0', importance=b'High-Class') self.assertEqual(75, len(ratings)) for rating in ratings: self.assertEqual(b'High-Class', rating.r_importance)
def test_with_project_b_no_quality_importance(self): self._add_ratings() ratings = logic_rating.get_project_rating_by_type( self.wp10db, b'Project 0', project_b_name=b'Project 1', limit=10) self.assertEqual(10, len(ratings)) for r in ratings: self.assertEqual(r[0].r_article, r[1].r_article) self.assertEqual(b'Project 0', r[0].r_project) self.assertEqual(b'Project 1', r[1].r_project)
def test_quality(self): self._add_ratings() ratings = logic_rating.get_project_rating_by_type(self.wp10db, b'Project 0', quality=b'FA-Class') self.assertEqual(50, len(ratings)) for rating in ratings: self.assertEqual(b'FA-Class', rating.r_quality)
def test_no_quality_or_importance(self): self._add_ratings() ratings = logic_rating.get_project_rating_by_type( self.wp10db, b'Project 0') # Currently limited to 100 results self.assertEqual(100, len(ratings)) for rating in ratings: self.assertEqual(b'Project 0', rating.r_project)
def test_sorted_by_importance(self): self._add_ratings() ratings = logic_rating.get_project_rating_by_type(self.wp10db, b'Project 0', quality=b'FA-Class') self.assertEqual(50, len(ratings)) for rating in ratings[:25]: self.assertEqual(b'High-Class', rating.r_importance) for rating in ratings[25:50]: self.assertEqual(b'Low-Class', rating.r_importance)
def test_sorted_by_quality(self): self._add_ratings() ratings = logic_rating.get_project_rating_by_type( self.wp10db, b'Project 0', importance=b'High-Class') self.assertEqual(75, len(ratings)) for rating in ratings[:25]: self.assertEqual(b'FA-Class', rating.r_quality) for rating in ratings[25:50]: self.assertEqual(b'A-Class', rating.r_quality) for rating in ratings[50:]: self.assertEqual(b'B-Class', rating.r_quality)
def test_with_project_b_right_quality_only(self): self._add_ratings() ratings = logic_rating.get_project_rating_by_type( self.wp10db, b'Project 0', project_b_name=b'Project 1', quality_b='A-Class', limit=10) self.assertEqual(10, len(ratings)) for r in ratings: self.assertEqual(r[0].r_article, r[1].r_article) self.assertEqual(b'A-Class', r[1].r_quality) self.assertEqual(b'Project 0', r[0].r_project) self.assertEqual(b'Project 1', r[1].r_project)
def articles(project_name): wp10db = get_db('wp10db') project_name_bytes = project_name.encode('utf-8') project = logic_project.get_project_by_name(wp10db, project_name_bytes) if project is None: return flask.abort(404) project_b_name_bytes = None quality_b = None importance_b = None project_b_name = flask.request.args.get('projectB') if project_b_name is not None: project_b_name_bytes = project_b_name.encode('utf-8') project_b = logic_project.get_project_by_name(wp10db, project_b_name_bytes) if project_b is None: return flask.abort(404) quality_b = flask.request.args.get('qualityB') importance_b = flask.request.args.get('importanceB') quality = flask.request.args.get('quality') importance = flask.request.args.get('importance') page = flask.request.args.get('page') page_int = 1 limit = flask.request.args.get('numRows') limit_int = 100 if page is not None: try: page_int = int(page) except ValueError: return flask.abort(400) if page_int < 1: return flask.abort(400) if limit is not None: try: limit_int = int(limit) except ValueError: return flask.abort(400) if limit_int < 1: return flask.abort(400) if limit_int > 500: limit_int = 500 if quality: quality = quality.encode('utf-8') if importance: importance = importance.encode('utf-8') article_pattern = flask.request.args.get('articlePattern') total = logic_rating.get_project_rating_count_by_type( wp10db, project_name_bytes, quality=quality, importance=importance, project_b_name=project_b_name_bytes, quality_b=quality_b, importance_b=importance_b, pattern=article_pattern) total_pages = total // limit_int + (1 if total % limit_int != 0 else 0) start = limit_int * (page_int - 1) + 1 end = min(limit_int - 1 + start, total) display = {'start': start, 'end': end, 'num_rows': limit_int} articles = logic_rating.get_project_rating_by_type( wp10db, project_name_bytes, quality=quality, importance=importance, project_b_name=project_b_name_bytes, quality_b=quality_b, importance_b=importance_b, pattern=article_pattern, page=page, limit=limit_int) if project_b_name is None: output_articles = list( article.to_web_dict(wp10db) for article in articles) else: output_articles = list( (a[0].to_web_dict(wp10db), a[1].to_web_dict(wp10db)) for a in articles) output = { 'pagination': { 'page': page or 1, 'total_pages': total_pages, 'total': total, 'display': display, }, 'articles': output_articles, } return flask.jsonify(output)