def test_it_should_get_stats(): input_rounds = create_round_queries([ (1, '2019-08-11 13:30:39+00', '2019-08-12 13:30:39+00'), (2, '2020-08-11 13:30:39+00', '2020-08-12 13:30:39+00') ]) input_votes = create_vote_queries([ (1, 1, 5, 'daniel'), (2, 1, 4, 'john'), (3, 1, 4, 'emily'), (4, 1, 6, 'arthur'), ]) input_data = input_rounds + input_votes expected = { 'payload': { "round_id": 1, "votes": [ [4, 2], [5, 1], [6, 1] ], "_links": { "self": {"href": "/rounds/1/stat"}, "round": {"href": "/rounds/1"} } } } with app_test_context(input_data) as client: current = client.get('/rounds/1/stat').get_json() assert current == expected
def test_it_should_start_a_new_round(): input_rounds = create_round_queries([ (None, '2019-08-11 13:30:39+00', '2019-08-12 13:30:39+00'), ]) input_votes = create_vote_queries([(1, 1, 5, 'daniel'), (2, 1, 4, 'john')]) input_data = input_rounds + input_votes expected = { 'payload': { 'id': 2, 'started_at': MatchString(), 'finished_at': None, 'participants': 0, 'winner_username': None, 'winner_vote': None, '_links': { 'self': { 'href': '/rounds/2' }, 'stat': { 'href': '/rounds/2/stat' } } } } with app_test_context(input_data) as client: client.post('/rounds', json={}) current = client.get('/rounds/2').get_json() assert current == expected
def test_it_should_get_one_round(): input_rounds = create_round_queries([ (1, '2019-08-11 13:30:39+00', '2019-08-12 13:30:39+00'), (2, '2020-08-11 13:30:39+00', '2020-08-12 13:30:39+00') ]) input_votes = create_vote_queries([ (1, 1, 5, 'daniel'), (2, 1, 4, 'john'), (3, 1, 4, 'emily'), (4, 1, 6, 'arthur'), ]) input_data = input_rounds + input_votes expected = { 'payload': { 'id': 1, 'started_at': '2019-08-11T13:30:39+0000', 'finished_at': '2019-08-12T13:30:39+0000', 'participants': 4, 'winner_username': '******', 'winner_vote': 5, '_links': { 'self': { 'href': '/rounds/1' }, 'stat': { 'href': '/rounds/1/stat' } } } } with app_test_context(input_data) as client: current = client.get('/rounds/1').get_json() assert current == expected
def test_it_should_fail_if_the_round_not_exists(): input_rounds = create_round_queries([ (1, '2019-08-11 13:30:39+00', None), ]) input_votes = create_vote_queries([(None, 1, 5, 'daniel'), (None, 1, 4, 'john')]) input_data = input_rounds + input_votes input_vote = {'username': '******', 'vote': 5} expected = {'error': {'code': 'not_found', 'message': MatchString()}} with app_test_context(input_data) as client: current = client.post('/rounds/2/vote', json=input_vote).get_json() assert current == expected
def test_it_should_get_error_if_the_round_id_not_exists(): input_rounds = create_round_queries([ (None, '2019-08-11 13:30:39+00', '2019-08-12 13:30:39+00'), ]) input_votes = create_vote_queries([(None, 1, 5, 'daniel'), (None, 1, 4, 'john')]) input_data = input_rounds + input_votes expected = {'error': {'code': 'not_found', 'message': MatchString()}} with app_test_context(input_data) as client: current = client.post('/rounds/6/finish', json={}).get_json() assert current == expected
def test_it_should_vote(): input_rounds = create_round_queries([ (1, '2019-08-11 13:30:39+00', None), ]) input_votes = create_vote_queries([(None, 1, 5, 'daniel'), (None, 1, 4, 'john')]) input_data = input_rounds + input_votes input_vote = {'username': '******', 'vote': 1} expected = {'payload': {}} with app_test_context(input_data) as client: current = client.post('/rounds/1/vote', json=input_vote).get_json() assert current == expected
def test_it_should_fail_if_the_vote_is_large(): input_rounds = create_round_queries([ (1, '2019-08-11 13:30:39+00', None), ]) input_votes = create_vote_queries([(None, 1, 5, 'daniel'), (None, 1, 4, 'john')]) input_data = input_rounds + input_votes input_vote = {'username': '******', 'vote': 1000000000000} expected = { 'error': { 'code': 'invalid_vote', 'message': MatchString('at most') } } with app_test_context(input_data) as client: current = client.post('/rounds/1/vote', json=input_vote).get_json() assert current == expected
def test_it_should_fail_if_the_round_not_exists(): input_rounds = create_round_queries([ (1, '2019-08-11 13:30:39+00', None), (2, '2020-08-11 13:30:39+00', '2020-08-12 13:30:39+00') ]) input_votes = create_vote_queries([ (1, 1, 5, 'daniel'), (2, 1, 4, 'john'), (3, 1, 4, 'emily'), (4, 1, 6, 'arthur'), ]) input_data = input_rounds + input_votes expected = { 'error': {'code': 'not_found', 'message': MatchString()} } with app_test_context(input_data) as client: current = client.get('/rounds/6/stat').get_json() assert current == expected
def test_it_should_get_all_rounds(): input_rounds = create_round_queries([(1, '2019-08-11 13:30:39+00', '2019-08-12 13:30:39+00'), (2, '2020-08-13 13:30:39+00', None)]) input_votes = create_vote_queries([ (1, 1, 5, 'daniel'), (2, 1, 4, 'john'), (3, 1, 4, 'emily'), (4, 1, 6, 'arthur'), (5, 2, 6, 'arthur'), (6, 2, 5, 'daniel'), (7, 2, 5, 'zeno'), ]) input_data = input_rounds + input_votes expected = { 'payload': [{ 'id': 2, 'started_at': '2020-08-13T13:30:39+0000', 'finished_at': None, 'participants': 3, '_links': { 'self': { 'href': '/rounds/2' }, } }, { 'id': 1, 'started_at': '2019-08-11T13:30:39+0000', 'finished_at': '2019-08-12T13:30:39+0000', 'participants': 4, '_links': { 'self': { 'href': '/rounds/1' }, } }] } with app_test_context(input_data) as client: current = client.get('/rounds').get_json() assert current == expected