def setUp(self): self.DATA = [{"value": "some"}, {"value": "row"}, {"value": "data"}] self.COLUMN_NAMES = ["some", "column", "names"] self.row_formatter = mock.Mock(side_effect=lambda x: x) self.csv_dict_writer = mock.Mock(writeheader=mock.Mock(), writerows=mock.Mock()) self.writer = CSVWriter(value_formatter=self.row_formatter)
def regular_season_player_box_scores(player_identifier, season_end_year, output_type=None, output_file_path=None, output_write_option=None, json_options=None): try: http_service = HTTPService(parser=ParserService()) values = http_service.regular_season_player_box_scores( player_identifier=player_identifier, season_end_year=season_end_year, ) except requests.exceptions.HTTPError as http_error: if http_error.response.status_code == requests.codes.internal_server_error \ or http_error.response.status_code == requests.codes.not_found: raise InvalidPlayerAndSeason(player_identifier=player_identifier, season_end_year=season_end_year) else: raise http_error options = OutputOptions.of( file_options=FileOptions.of(path=output_file_path, mode=output_write_option), output_type=output_type, json_options=json_options, csv_options={"column_names": PLAYER_SEASON_BOX_SCORE_COLUMN_NAMES}) output_service = OutputService( json_writer=JSONWriter(value_formatter=BasketballReferenceJSONEncoder), csv_writer=CSVWriter(value_formatter=format_value)) return output_service.output(data=values, options=options)
def player_box_scores(day, month, year, output_type=None, output_file_path=None, output_write_option=None, json_options=None): try: http_service = HTTPService(parser=ParserService()) values = http_service.player_box_scores(day=day, month=month, year=year) except requests.exceptions.HTTPError as http_error: if http_error.response.status_code == requests.codes.not_found: raise InvalidDate(day=day, month=month, year=year) else: raise http_error options = OutputOptions.of( file_options=FileOptions.of(path=output_file_path, mode=output_write_option), output_type=output_type, json_options=json_options, csv_options={"column_names": BOX_SCORE_COLUMN_NAMES}) output_service = OutputService( json_writer=JSONWriter(value_formatter=BasketballReferenceJSONEncoder), csv_writer=CSVWriter(value_formatter=format_value)) return output_service.output(data=values, options=options)
def players_advanced_season_totals(season_end_year, include_combined_values=False, output_type=None, output_file_path=None, output_write_option=None, json_options=None): try: http_service = HTTPService(parser=ParserService()) values = http_service.players_advanced_season_totals( season_end_year, include_combined_values=include_combined_values) except requests.exceptions.HTTPError as http_error: if http_error.response.status_code == requests.codes.not_found: raise InvalidSeason(season_end_year=season_end_year) else: raise http_error options = OutputOptions.of(file_options=FileOptions.of( path=output_file_path, mode=output_write_option), output_type=output_type, json_options=json_options, csv_options={ "column_names": PLAYER_ADVANCED_SEASON_TOTALS_COLUMN_NAMES }) output_service = OutputService( json_writer=JSONWriter(value_formatter=BasketballReferenceJSONEncoder), csv_writer=CSVWriter(value_formatter=format_value)) return output_service.output(data=values, options=options)
def standings(season_end_year, output_type=None, output_file_path=None, output_write_option=None, json_options=None): try: http_service = HTTPService(parser=ParserService()) values = http_service.standings(season_end_year=season_end_year) except requests.exceptions.HTTPError as http_error: if http_error.response.status_code == requests.codes.not_found: raise InvalidSeason(season_end_year=season_end_year) else: raise http_error options = OutputOptions.of( file_options=FileOptions.of(path=output_file_path, mode=output_write_option), output_type=output_type, json_options=json_options, csv_options={"column_names": STANDINGS_COLUMNS_NAMES}) output_service = OutputService( json_writer=JSONWriter(value_formatter=BasketballReferenceJSONEncoder), csv_writer=CSVWriter(value_formatter=format_value)) return output_service.output(data=values, options=options)
class TestCSVWriter(TestCase): def setUp(self): self.DATA = [{"value": "some"}, {"value": "row"}, {"value": "data"}] self.COLUMN_NAMES = ["some", "column", "names"] self.row_formatter = mock.Mock(side_effect=lambda x: x) self.csv_dict_writer = mock.Mock(writeheader=mock.Mock(), writerows=mock.Mock()) self.writer = CSVWriter(value_formatter=self.row_formatter) @mock.patch("csv.DictWriter") def test_opens_correct_file(self, mock_csv_dict_writer): with mock.patch("builtins.open", mock.mock_open()) as mock_file: mock_csv_dict_writer.return_value = self.csv_dict_writer self.writer.write( data=self.DATA, options=OutputOptions( file_options=FileOptions( path="some file path", mode=OutputWriteOption.WRITE, ), formatting_options={"column_names": self.COLUMN_NAMES}, output_type=OutputType.CSV, ), ) mock_file.assert_called_with("some file path", OutputWriteOption.WRITE.value, newline="", encoding="utf8") @mock.patch("csv.DictWriter") def test_file_and_columns_are_used_by_writer(self, mock_csv_dict_writer): with mock.patch("builtins.open", mock.mock_open()) as mock_file: mock_csv_dict_writer.return_value = self.csv_dict_writer self.writer.write( data=self.DATA, options=OutputOptions( file_options=FileOptions( path="some file path", mode=OutputWriteOption.WRITE, ), formatting_options={"column_names": self.COLUMN_NAMES}, output_type=OutputType.CSV, ), ) mock_csv_dict_writer.assert_called_with( mock_file(), fieldnames=self.COLUMN_NAMES) @mock.patch("csv.DictWriter") def test_header_is_written(self, mock_csv_dict_writer): with mock.patch("builtins.open", mock.mock_open()): mock_csv_dict_writer.return_value = self.csv_dict_writer self.writer.write( data=self.DATA, options=OutputOptions( file_options=FileOptions( path="some file path", mode=OutputWriteOption.WRITE, ), formatting_options={"column_names": self.COLUMN_NAMES}, output_type=OutputType.CSV, ), ) self.csv_dict_writer.writeheader.assert_called_once_with() @mock.patch("csv.DictWriter") def test_rows_are_written(self, mock_csv_dict_writer): with mock.patch("builtins.open", mock.mock_open()): mock_csv_dict_writer.return_value = self.csv_dict_writer self.writer.write( data=self.DATA, options=OutputOptions( file_options=FileOptions( path="some file path", mode=OutputWriteOption.WRITE, ), formatting_options={"column_names": self.COLUMN_NAMES}, output_type=OutputType.CSV, ), ) self.csv_dict_writer.writerows.assert_called_once_with([{ "value": "some" }, { "value": "row" }, { "value": "data" }])