def field_to_pred(field): if field['type'] == 'choice': return is_in(field['choices']) else: return ValueError(f'unknown field type {field["type"]!r}')
return JsonResponse(serialize_room(player)) @allow_methods('GET') @with_player @transaction.atomic def room_read_view(request, player): return JsonResponse(serialize_room(player)) @allow_methods('PUT') @with_player @validate_body( is_subdict_where({ 'game': is_in(settings.INSTALLED_GAMES), 'started': is_bool, })) @transaction.atomic def room_update_view(request, data, player): if not player.admin: return JsonResponse( code=403, data={ 'code': 'Forbidden', 'message': 'Only the admin can make changes.', }, ) if player.room.started: return JsonResponse(
def test_in_set_with_self(self, value): pred = is_in(set([value])) with self.subTest('explain=True == explain=False'): self.assertEqual(pred(value), pred.explain(value).valid) with self.subTest('pred correct'): self.assertTrue(pred(value))
def test_create_tournament(self): res = self.client.post( '/api/tournament/', { 'name': 'Tournament', 'players': [ {'name': f'Player {n}'} for n in range(1, 9) ], 'game_size': 4, 'game_cups': 2, 'game_races': 4, }, content_type='application/json', ) self.assertEqual(res.status_code, 200) assert_valid(res.content, is_decodable_json_where({ 'name': 'Tournament', 'token': is_str, 'admin_token': is_str, 'players': is_list_of({ 'id': is_int, 'name': is_str, }), 'games': is_list_of({ 'name': is_str, 'round': is_int, 'state': is_in({'waiting', 'active', 'done'}), 'players': is_list_of(is_dict_union( player={ 'player': is_int, }, slot={ 'game': is_str, 'position': is_in({1, 2, 3, 4}), 'player': None, }, )), 'cups': is_list_of( is_list_of({ 'player': is_int, 'races': is_list_of(is_nullable(is_int)), 'points': is_int, 'rank': is_in({1, 2, 3, 4}), 'fag_points': is_int, 'f*g': is_bool, }), ), 'total': is_list_of({ 'player': is_int, 'points': is_int, 'rank': is_in({1, 2, 3, 4}), }), }), 'next_race': is_nullable({ 'game': is_str, 'cup': is_int, 'race': is_int, }), 'ranks': is_list_of({ 'rank': is_in({1, 2, 3, 4, 5, 6, 7, 8}), 'game': is_str, 'position': is_in({1, 2, 3, 4}), 'player': None, }), }))
def test_in(self, value, collection): pred = is_in(collection) with self.subTest('explain=True == explain=False'): self.assertEqual(pred(value), pred.explain(value).valid) with self.subTest('pred correct'): self.assertEqual(pred(value), value in collection)