def matches(): comp = g.comp_man.get_comp() matches = [] for slots in comp.schedule.matches: matches.extend(match_json_info(comp, match) for match in slots.values()) def parse_date(string): if ' ' in string: raise errors.BadRequest('Date string should not contain spaces. ' "Did you pass in a '+'?") else: return dateutil.parser.parse(string) filters = [ ('type', MatchType, lambda x: x['type']), ('arena', str, lambda x: x['arena']), ('num', int, lambda x: x['num']), ('game_start_time', parse_date, lambda x: x['times']['game']['start']), ('game_end_time', parse_date, lambda x: x['times']['game']['end']), ('slot_start_time', parse_date, lambda x: x['times']['slot']['start']), ('slot_end_time', parse_date, lambda x: x['times']['slot']['end']) ] # check for unknown filters filter_names = [name for name, _, _ in filters] + ['limit'] for arg in request.args: if arg not in filter_names: raise errors.UnknownMatchFilter(arg) # actually run the filters for filter_key, filter_type, filter_value in filters: if filter_key in request.args: value = request.args[filter_key] try: predicate = parse_difference_string(value, filter_type) matches = [match for match in matches if predicate(filter_type(filter_value(match)))] except ValueError: raise errors.BadRequest("Bad value '{0}' for '{1}'.".format(value, filter_key)) # limit the results try: limit = int(request.args['limit']) except KeyError: pass except ValueError: raise errors.BadRequest( 'Limit must be a positive or negative integer.') else: if limit == 0: matches = [] elif limit > 0: matches = matches[:limit] elif limit < 0: matches = matches[limit:] else: raise AssertionError("Limit isn't a number?") return jsonify(matches=matches, last_scored=comp.scores.last_scored_match)
def test_bounds_upper(): assert parse_difference_string('4..6')(6)
def test_bounds_lower(): assert parse_difference_string('4..6')(4)
def test_lower_lt(): assert not parse_difference_string('4..')(2)
def test_lower_equal(): assert parse_difference_string('4..')(4)
def test_upper_gt(): assert not parse_difference_string('..4')(6)
def test_parse_failure(): parse_difference_string('cheese', int)
def test_double_open(): parse_difference_string('..', str)
def test_exact_equal(): assert parse_difference_string('4')(4)
def test_lower_gt(): assert parse_difference_string('4..')(6)
def test_upper_lt(): assert parse_difference_string('..4')(2)
def test_other_types(): assert parse_difference_string('cheese..', str)('cheese')
def test_invalid_ds(): parse_difference_string('1..2..3')
def test_bounds_mid(): assert parse_difference_string('4..6')(5)
def test_upper_equal(): assert parse_difference_string('..4')(4)
def test_bounds_gt(): assert not parse_difference_string('4..6')(8)
def test_other_types_negative(): assert not parse_difference_string('cheese..', str)('')
def test_inverted_bounds(): parse_difference_string('3..2')
def test_exact_gt(): assert not parse_difference_string('4')(6)
def matches(): comp = g.comp_man.get_comp() matches = [] for slots in comp.schedule.matches: matches.extend( match_json_info(comp, match) for match in slots.values()) def parse_date(string): if ' ' in string: raise errors.BadRequest('Date string should not contain spaces. ' "Did you pass in a '+'?") else: return dateutil.parser.parse(string) filters = [ ('type', MatchType, lambda x: x['type']), ('arena', str, lambda x: x['arena']), ('num', int, lambda x: x['num']), ('game_start_time', parse_date, lambda x: x['times']['game']['start']), ('game_end_time', parse_date, lambda x: x['times']['game']['end']), ('slot_start_time', parse_date, lambda x: x['times']['slot']['start']), ('slot_end_time', parse_date, lambda x: x['times']['slot']['end']) ] # check for unknown filters filter_names = [name for name, _, _ in filters] + ['limit'] for arg in request.args: if arg not in filter_names: raise errors.UnknownMatchFilter(arg) # actually run the filters for filter_key, filter_type, filter_value in filters: if filter_key in request.args: value = request.args[filter_key] try: predicate = parse_difference_string(value, filter_type) matches = [ match for match in matches if predicate(filter_type(filter_value(match))) ] except ValueError: raise errors.BadRequest("Bad value '{0}' for '{1}'.".format( value, filter_key)) # limit the results try: limit = int(request.args['limit']) except KeyError: pass except ValueError: raise errors.BadRequest( 'Limit must be a positive or negative integer.') else: if limit == 0: matches = [] elif limit > 0: matches = matches[:limit] elif limit < 0: matches = matches[limit:] else: raise AssertionError("Limit isn't a number?") return jsonify(matches=matches, last_scored=comp.scores.last_scored_match)