def test_no_change(self): group_matches = Match.ready_group_matches() data = create_empty_match_post_data(group_matches) formset = UpcomingMatchesFromset(data) self.assertTrue(formset.is_valid()) formset.save() self.assertEqual(len(group_matches), len(Match.ready_group_matches()))
def test_assigning_table(self): group_matches = list(Match.ready_group_matches()) data = create_empty_match_post_data(group_matches) data['form-0-table'] = u'5' formset = UpcomingMatchesFromset(data) self.assertTrue(formset.is_valid()) formset.save() self.assertEqual(len(group_matches) - 1, len(Match.ready_group_matches()))
def test_assigning_multiple_tables(self): data = create_empty_match_post_data(Match.ready_group_matches()) data['form-0-table'] = u'5' data['form-1-table'] = u'6' formset = UpcomingMatchesFromset(data) self.assertTrue(formset.is_valid()) formset.save() self.assertEqual(0, len(Match.ready_group_matches())) pass
def test_ready_doubles_matches_only_returns_matches_with_free_players(self): self.assertEqual(Match.ready_doubles_matches().count(), 1) match1, match2 = Match.objects.filter(status__lt=Match.COMPLETE, player1_bracket_slot__bracket__category__name="DP").order_by('-status') match1.set_score(3, 0) match1.save() self.assertEqual(Match.ready_doubles_matches().count(), 2) match2.table = Table.objects.all()[0] match2.save() match2.set_score(3, 0) match2.save() self.assertEqual(Match.ready_doubles_matches().count(), 3)
def test_assigning_same_table_to_different_matches(self): data = create_empty_match_post_data(Match.ready_group_matches()) data['form-0-table'] = u'4' data['form-1-table'] = u'4' formset = UpcomingMatchesFromset(data) self.assertEqual(formset.is_valid(), False)
def create_groups_ng(request, category_id): category = get_object_or_404(Category, id=category_id) if request.method == 'POST': json_groups = json.loads(request.body) groups = [] members = [] for i, json_group in enumerate(json_groups): group = Group.objects.create(name=string.ascii_uppercase[i], category=category) groups.append(group) for player in json_group: members.append( GroupMember(player_id=player['id'], group=group, leader=player['leader'])) GroupMember.objects.bulk_create(members) group_members = defaultdict(list) for member in GroupMember.objects.filter( group__category=category).select_related('group'): group_members[member.group].append(member) matches = [] for group in groups: for p1, p2 in berger_tables(len(group_members[group])): matches.append( Match(player1=group_members[group][p1].player, player2=group_members[group][p2].player, group=group, status=Match.PENDING)) # Create a dummy match that will be used to assign table to the group. matches.append(Match(group=group, status=Match.READY)) Match.objects.bulk_create(matches) helpers.create_brackets(category) print_groups(category) return HttpResponse(json.dumps('ok'), content_type="application/json") return render(request, 'pingpong/category/create_groups_ng.html', dict(category=category))
def upcoming_matches(request): response_data = [] response_data.extend([ dict(id=match.id, type='Group', group=unicode(match.group)) for match in Match.ready_group_matches() ]) response_data.extend([ dict(id=match.id, type='Bracket', player1=unicode(match.player1), player2=unicode(match.player2)) for match in Match.ready_bracket_matches() ]) response_data.extend([ dict(id=match.id, type='Double', player1=unicode(match.player1), player2=unicode(match.player2)) for match in Match.ready_doubles_matches() ]) return HttpResponse(json.dumps(response_data), content_type="application/json")
def upcoming_matches(context): if 'formset' in context: formset = context['formset'] else: formset = UpcomingMatchesFromset( queryset=Match.ready_group_matches() | Match.ready_bracket_matches() | Match.ready_doubles_matches()) context.update({ 'formset': formset, 'matches': chain(Match.ready_group_matches(), Match.ready_bracket_matches(), Match.ready_doubles_matches()) }) return context
def test_create_groups_creates_group_matches(self): category = Category.objects.get(description="Category without clubs") category.create_groups(number_of_groups=4) self.assertEqual(len(Match.ready_group_matches()), 4)
def test_ready_doubles_matches_is_efficient(self): with self.assertNumQueries(1): doubles_matches = Match.ready_doubles_matches() for match in doubles_matches: str(match)
def test_ready_bracket_matches_is_efficient(self): with self.assertNumQueries(1): bracket_matches = Match.ready_bracket_matches() for match in bracket_matches: match.description()