def setUp(self): self.pred = is_with( {'len': lambda data: len(data['columns'])}, is_dict_where(columns=is_list_of(is_str), rows=is_list_of( is_list_of(is_number) & is_transformed(len, Get('len')))), )
def test_is_list_of(self, value): pred = is_list_of(is_eq(True)) with self.subTest('explain=True == explain=False'): self.assertEqual(pred(value), pred.explain(value).valid) with self.subTest('pred correct'): self.assertEqual(pred(value), isinstance(value, list) and all(value))
def test_incorrect_key(self): value = { 'columns': ['A', 'B', 'C'], 'rows': [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ], } pred = is_with( {'len': lambda data: len(data['columns'])}, is_dict_where(columns=is_list_of(is_str), rows=is_list_of( is_list_of(is_number) & is_transformed(len, Get('length')))), ) with self.subTest('explain=True == explain=False'): self.assertEqual(pred(value), pred.explain(value).valid) with self.subTest('pred correct'): self.assertEqual(pred(value), False)
data['admin_token'] = tournament.admin_token return data def tournament_view(request, token=None): if token is None: return tournament_list(request) else: return tournament_detail(request, token) @allow_methods('POST') @validate_body(is_dict_where({ 'name': is_pre(is_str, is_not_blank), 'players': is_list_of({ 'name': is_pre(is_str, is_not_blank), }), 'game_size': is_int, 'game_cups': is_int, 'game_races': is_int, }, { 'shuffle': is_bool, })) def tournament_list(request, data): tournament = Tournament.objects.create( name=data['name'], game_size=data['game_size'], game_cups=data['game_cups'], game_races=data['game_races'], ) for n, player_data in enumerate(data['players'], 1):
def setUp(self): self.pred = is_dict_where(foo=is_list_of( is_dict_where(bar=is_bool, baz=is_str)), bar=is_int) self.json_pred = is_json_where(self.pred)
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, }), }))