forked from mcbarlowe/nba_scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_unit.py
167 lines (128 loc) · 5.09 KB
/
test_unit.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
"""
unit tests for the nba_scraper module
"""
from datetime import datetime
import pytest
import json
import pandas as pd
import nba_scraper.scrape_functions as sf
import nba_scraper.nba_scraper as ns
def test_pbp_scrape():
"""
test of the main_scrape function using pre downloaded JSON
"""
with open("test_files/v2_dict.json", "r") as v2_file:
v2_dict = json.load(v2_file)
game_df = sf.scrape_pbp(v2_dict)
assert isinstance(game_df, pd.DataFrame)
# fix this test later
def test_lineup_scrape():
# this will test the get_lineups function to make sure it is returning
# a dataframe of the pbp with the the players on the floor at each event
with open("test_files/v2_dict.json", "r") as v2_file:
v2_dict = json.load(v2_file)
with open("test_files/lineups.json", "r") as lineup:
lineup_dict = json.load(lineup)
game_df = sf.scrape_pbp(v2_dict)
game_df = sf.get_lineup(
game_df[game_df["period"] == 1].copy(), lineup_dict, game_df
)
assert isinstance(game_df, pd.DataFrame)
def test_get_season():
"""
tests the get get_season function in scraper_functions to make sure it
is returning the correct date
"""
assert sf.get_season(datetime.strptime("2018-09-01", "%Y-%m-%d")) == 2018
assert sf.get_season(datetime.strptime("2018-04-01", "%Y-%m-%d")) == 2017
assert sf.get_season(datetime.strptime("2018-01-01", "%Y-%m-%d")) == 2017
def test_made_shot():
"""
test the made_shot funciton to make sure it is calculating correctly
"""
with open("test_files/v2_dict.json", "r") as v2_file:
v2_dict = json.load(v2_file)
game_df = sf.scrape_pbp(v2_dict)
assert sf.made_shot(game_df.iloc[20, :].copy()) == 1
assert sf.made_shot(game_df.iloc[13, :].copy()) == 1
assert sf.made_shot(game_df.iloc[23, :].copy()) == 0
assert sf.made_shot(game_df.iloc[123, :].copy()) == 0
'''
def test_parse_foul():
"""
test for the parse_foul function
"""
with open("test_files/v2_dict.json", "r") as v2_file:
v2_dict = json.load(v2_file)
with open("test_files/pbp_dict.json", "r") as pbp:
pbp_dict = json.load(pbp)
game_df = sf.scrape_pbp(v2_dict, pbp_dict)
assert sf.parse_foul(game_df.iloc[12, :].copy()) == "3 second"
assert sf.parse_foul(game_df.iloc[108, :].copy()) == "shooting"
assert sf.parse_foul(game_df.iloc[125, :].copy()) == "charge"
assert sf.parse_foul(game_df.iloc[131, :].copy()) == "loose_ball"
assert sf.parse_foul(game_df.iloc[155, :].copy()) == "personal"
assert sf.parse_foul(game_df.iloc[256, :].copy()) == "technical"
'''
'''
def test_shot_types():
"""
function to test the parse_shot_types function in scrape_functions
"""
with open("test_files/v2_dict.json", "r") as v2_file:
v2_dict = json.load(v2_file)
with open("test_files/pbp_dict.json", "r") as pbp:
pbp_dict = json.load(pbp)
game_df = sf.scrape_pbp(v2_dict, pbp_dict)
assert sf.parse_shot_types(game_df.iloc[3, :].copy()) == "layup"
assert sf.parse_shot_types(game_df.iloc[7, :].copy()) == "jump"
assert sf.parse_shot_types(game_df.iloc[18, :].copy()) == "dunk"
assert sf.parse_shot_types(game_df.iloc[119, :].copy()) == "hook"
assert sf.parse_shot_types(game_df.iloc[119, :].copy()) == "hook"
assert sf.parse_shot_types(game_df.iloc[121, :].copy()) == "free"
'''
def test_seconds_elapsed():
"""
test create_seconds_elapsed function in scrape_functions
"""
with open("test_files/v2_dict.json", "r") as v2_file:
v2_dict = json.load(v2_file)
game_df = sf.scrape_pbp(v2_dict)
assert sf.create_seconds_elapsed(game_df.iloc[8, :].copy()) == 61
assert sf.create_seconds_elapsed(game_df.iloc[368, :].copy()) == 2197
assert sf.create_seconds_elapsed(game_df.iloc[233, :].copy()) == 1450
assert sf.create_seconds_elapsed(game_df.iloc[117, :].copy()) == 735
def test_calc_points():
"""
test calc_points_made function
"""
with open("test_files/v2_dict.json", "r") as v2_file:
v2_dict = json.load(v2_file)
game_df = sf.scrape_pbp(v2_dict)
assert sf.calc_points_made(game_df.iloc[110, :].copy()) == 1
assert sf.calc_points_made(game_df.iloc[195, :].copy()) == 2
assert sf.calc_points_made(game_df.iloc[151, :].copy()) == 3
def test_check_format(capfd):
"""
test check_format function
"""
ns.check_format("pandas")
out, err = capfd.readouterr()
assert out == ""
ns.check_format("pand")
out, err = capfd.readouterr()
assert out == (
f"You passed pand to scrape_game function as a data format.\n"
"This is an unaccepted format. Please either pass 'pandas' or 'csv'.\n\n"
)
def test_check_valid_dates():
"""
test for check_valid_dates function in nba_scraper.py
"""
ns.check_valid_dates("2018-01-01", "2018-01-30")
with pytest.raises(ValueError):
ns.check_valid_dates("2018-01-30", "2018-01-01")
with pytest.raises(ValueError):
ns.check_valid_dates("01-01-2018", "01-30-2018")
with pytest.raises(ValueError):
ns.check_valid_dates("30-01-2018", "15-02-2018")