-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_scrape_ratings.py
169 lines (120 loc) · 5.18 KB
/
test_scrape_ratings.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""Unit test for scrape_ratings"""
# TODO experiment with mocking to test remaining methods
import scrape_ratings
from nose.tools import assert_equal
from nose.tools import assert_raises
# Well formatted test file
TEST_PLAYERS = 'test_players.csv'
# Test file with invalid Voobly url
TEST_PLAYERS_BAD = 'test_players_bad.csv'
# Test file with players who have multiple accounts
TEST_PLAYERS_MULTIPLE = 'test_players_multiple.csv'
# Tests that a file without a header is processed correctly
TEST_PLAYERS_MULTIPLE_NOHDR = 'test_players_multiple.csv'
# Test a player file with no text
TEST_PLAYERS_EMPTY = 'test_players_empty.csv'
# Test a player file with only a header
TEST_PLAYERS_EMPTY_HDR = 'test_players_empty_hdr.csv'
# Test a player file with a single player
TEST_PLAYERS_SINGLE = 'test_players_single.csv'
# Test a player file with a single player and a header
TEST_PLAYERS_SINGLE_HDR = 'test_players_single_hdr.csv'
def test_load_players_success():
"""Tests loading the example players file."""
players = scrape_ratings.load_players(TEST_PLAYERS)
expected_players = {
'TWest': ['123684015'],
'robo_boro': ['123905987'],
'smarthy_': ['124230162'],
'Pete26196': ['123685133'],
'AkeNo': ['123723545']
}
assert_equal(expected_players, players)
def test_load_players_multiple():
"""Tests loading a player file with a player who has multiple accounts."""
players = scrape_ratings.load_players(TEST_PLAYERS_MULTIPLE)
expected_players = {
'TWest': ['123684015'],
'robo_boro': ['123905987'],
'smarthy_': ['124230162'],
'Pete26196': ['123685133', '124976639'],
'AkeNo': ['123723545']
}
assert_equal(expected_players, players)
def test_load_players_nohdr():
"""
Tests loading a player file with a player who has multiple accounts,
using a file that does not include the csv header.
"""
players = scrape_ratings.load_players(TEST_PLAYERS_MULTIPLE_NOHDR)
expected_players = {
'TWest': ['123684015'],
'robo_boro': ['123905987'],
'smarthy_': ['124230162'],
'Pete26196': ['123685133', '124976639'],
'AkeNo': ['123723545']
}
assert_equal(expected_players, players)
def test_load_players_empty():
"""Tests loading an empty players file."""
players = scrape_ratings.load_players(TEST_PLAYERS_EMPTY)
expected_players = {}
assert_equal(expected_players, players)
def test_load_players_empty_hdr():
"""Tests loading an empty players file with a header."""
players = scrape_ratings.load_players(TEST_PLAYERS_EMPTY_HDR)
expected_players = {}
assert_equal(expected_players, players)
def test_load_players_single():
"""Tests loading a players file with a single player."""
players = scrape_ratings.load_players(TEST_PLAYERS_SINGLE)
expected_players = {'TWest': ['123684015']}
assert_equal(expected_players, players)
def test_load_players_single_hdr():
"""Tests loading a players file with a single player and a header."""
players = scrape_ratings.load_players(TEST_PLAYERS_SINGLE_HDR)
expected_players = {'TWest': ['123684015']}
assert_equal(expected_players, players)
def test_load_players_not_found():
"""Tests that a FileNotFoundError is thrown if the file does not exist."""
with assert_raises(FileNotFoundError):
scrape_ratings.load_players('i do not exist')
def test_load_players_bad_url():
"""
Tests that a ValueError is raised when loading a list of players that
contains a bad url.
"""
with assert_raises(ValueError):
scrape_ratings.load_players(TEST_PLAYERS_BAD)
def test_parse_id_basic():
"""Tests parsing a voobly url with a simple format."""
url = 'https://www.voobly.com/profile/view/123684015'
assert_equal('123684015', scrape_ratings.parse_id(url))
def test_parse_id_slash():
"""Tests parsing a voobly url that ends with a slash."""
url = 'https://www.voobly.com/profile/view/123684015/'
assert_equal('123684015', scrape_ratings.parse_id(url))
def test_parse_id_no_prefix():
"""Tests parsing a voobly url that starts with www."""
url = 'www.voobly.com/profile/view/123684015'
assert_equal('123684015', scrape_ratings.parse_id(url))
def test_parse_id_suffix():
"""Tests parsing a url that has extra links at the end."""
url = 'https://www.voobly.com/profile/view/123684015/Ratings/games/profile/123684015/131' # pylint: disable=line-too-long
assert_equal('123684015', scrape_ratings.parse_id(url))
def test_parse_id_no_view():
"""Tests that a ValueError is raised when the url does not contain view."""
url = 'https://www.voobly.com/profile/123684015'
with assert_raises(ValueError): scrape_ratings.parse_id(url)
def test_parse_id_view_last():
"""
Tests that a ValueError is raised when view is the final part of the url.
"""
url = 'https://www.voobly.com/profile/view'
with assert_raises(ValueError): scrape_ratings.parse_id(url)
def test_parse_id_not_int():
"""
Tests that a ValueError is raised when the entry after view is not an int.
"""
url = 'https://www.voobly.com/profile/view/notanint'
with assert_raises(ValueError): scrape_ratings.parse_id(url)