/
test.py
executable file
·105 lines (88 loc) · 3.54 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import unittest
from solution import Team, MatchResult, Rankings, Table
class TestTeam(unittest.TestCase):
def test___init__(self):
"""Test that we correctly parse result input lines"""
team1 = Team("team A with spaces")
self.assertEqual(team1.name, 'team A with spaces', 'Snakes')
class TestMatchResult(unittest.TestCase):
def _make_teams(self, name1, name2):
team1 = Team(name1)
team2 = Team(name2)
return team1, team2
def test_create_match_result(self):
team1, team2 = (Team('team A with spaces'), Team('Snakes'))
m = MatchResult([(team1, team2), (0, 0)])
self.assertIsInstance(m, MatchResult)
self.assertIn(team1, m.teams)
self.assertIn(team2, m.teams)
def test_tie_match(self):
team1, team2 = self._make_teams('a', 'b')
m = MatchResult([(team1, team2), (0, 0)])
m.record_result()
self.assertDictEqual({'a': 1}, {team1.name: team1.points})
self.assertDictEqual({'b': 1}, {team2.name: team2.points})
def test_win_match(self):
team1, team2 = self._make_teams('a', 'b')
# [(teams), (scores)]
m = MatchResult([(team1, team2), (2, 1)])
m.record_result()
# {team name: points from fixture}
self.assertDictEqual({'a': 3}, {team1.name: team1.points})
self.assertDictEqual({'b': 0}, {team2.name: team2.points})
self.assertEqual(m.winner.name, team1.name)
class TestRankings(unittest.TestCase):
def test__rank_comparison(self):
"""Test that get_sorted_rows correctly sorts by points and alphabet."""
# start in unsorted order
table_rows = {"a": 2, "cxx": 5, "bstring": 5}
r = Rankings(rows=table_rows)
sorted_table_rows = r.get_sorted_rows()
self.assertListEqual([name[0] for name in sorted_table_rows], ['bstring', 'cxx', 'a'])
class TestTable(unittest.TestCase):
def test_generate_rankings(self):
"""Test that our ranking output lines are generated and formatted correctly"""
input = """
Lions 3, Snakes 3
Tarantulas 1, FC Awesome 0
Lions 1, FC Awesome 1
Tarantulas 3, Snakes 1
Lions 4, Grouches 0
"""
expected_output = """
1. Tarantulas, 6 pts
2. Lions, 5 pts
3. FC Awesome, 1 pt
3. Snakes, 1 pt
5. Grouches, 0 pts
"""
file_input = ''
yield file_input.join(input.splitlines(True))
t = Table(file_input=file_input)
self.assertMultiLineEqual(t.display_teams(), expected_output)
def test_encoded_input(self):
"""Test that our ranking output lines are generated and formatted correctly"""
input = """
リバプール 3, Манчестер Юнайтед 3
Tarantulas 2 1, FC Awesome 0
Lions 1, FC Awesome 1
Tarantulas 2 3, Манчестер Юнайтед 1
Lions 4, Grouches 0
"""
expected_output = """
1. Tarantulas 2, 6 pts
2. Lions, 4 pts
3. FC Awesome, 1 pt
4. Манчестер Юнайтед, 1 pt
5. リバプール, 1 pt
6. Grouches, 0 pts
"""
file_input = ''
yield file_input.join(input.splitlines(True))
t = Table(file_input=file_input)
self.assertMultiLineEqual(t.display_teams(), expected_output)
if __name__ == '__main__':
unittest.main()