Esempio n. 1
0
def output(values,
           output_type,
           output_file_path,
           csv_writer,
           output_write_option=None,
           json_options=None):
    if output_type is None:
        return values

    write_option = OutputWriteOption.WRITE if output_write_option is None else output_write_option

    if output_type == OutputType.JSON:
        options = WriteOptions(file_path=output_file_path,
                               mode=write_option,
                               custom_options=json_options)
        writer = JSONWriter(encoder=BasketballReferenceJSONEncoder)
        return writer.write(data=values, options=options)

    if output_type == OutputType.CSV:
        options = WriteOptions(file_path=output_file_path, mode=write_option)
        if options.should_write_to_file():
            return csv_writer.write(data=values, options=options)
        else:
            raise ValueError("CSV output must contain a file path")

    raise ValueError(
        "Unknown output type: {output_type}".format(output_type=output_type))
Esempio n. 2
0
    def test_output_json_when_output_write_option_is_none_and_custom_options(
        self,
        mock_json_writer,
    ):
        mock_json_writer_instance = mock.Mock(write=mock.Mock())
        mock_json_writer.return_value = mock_json_writer_instance

        output(
            values=self.values,
            output_type=OutputType.JSON,
            output_file_path=self.output_file_path,
            csv_writer=None,
            json_options={
                "jae": "baebae",
                "bae": "jadley",
            },
        )

        mock_json_writer.assert_called_once_with(
            encoder=BasketballReferenceJSONEncoder)
        mock_json_writer_instance.write.assert_called_once_with(
            data=self.values,
            options=WriteOptions(
                file_path=self.output_file_path,
                mode=OutputWriteOption.WRITE,
                custom_options={
                    "jae": "baebae",
                    "bae": "jadley",
                },
            ))
 def test_header_is_written(self, mock_csv_dict_writer):
     csv_dict_writer = mock.Mock(wrteheader=mock.Mock(),
                                 writerows=mock.Mock())
     mock_csv_dict_writer.return_value = csv_dict_writer
     row_formatter = mock.Mock(format=mock.Mock())
     csv_dict_writer = mock.Mock(wrteheader=mock.Mock(),
                                 writerows=mock.Mock())
     mock_csv_dict_writer.return_value = csv_dict_writer
     writer = CSVWriter(column_names=self.COLUMN_NAMES,
                        row_formatter=row_formatter)
     writer.write(data=self.DATA,
                  options=WriteOptions(file_path="some file path",
                                       mode=OutputWriteOption.WRITE))
     csv_dict_writer.writeheader.assert_called_once_with()
 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:
         csv_dict_writer = mock.Mock(wrteheader=mock.Mock(),
                                     writerows=mock.Mock())
         mock_csv_dict_writer.return_value = csv_dict_writer
         row_formatter = mock.Mock(format=mock.Mock())
         csv_dict_writer = mock.Mock(wrteheader=mock.Mock(),
                                     writerows=mock.Mock())
         mock_csv_dict_writer.return_value = csv_dict_writer
         writer = CSVWriter(column_names=self.COLUMN_NAMES,
                            row_formatter=row_formatter)
         writer.write(data=self.DATA,
                      options=WriteOptions(file_path="some file path",
                                           mode=OutputWriteOption.WRITE))
         mock_csv_dict_writer.assert_called_with(
             mock_file(), fieldnames=self.COLUMN_NAMES)
Esempio n. 5
0
    def test_output_csv_when_output_write_option_is_none_and_no_custom_options(
            self):
        csv_writer = mock.Mock(name="csv_writer", write=mock.Mock())
        output(
            values=self.values,
            output_type=OutputType.CSV,
            output_file_path=self.output_file_path,
            csv_writer=csv_writer,
        )

        csv_writer.write.assert_called_once_with(
            data=self.values,
            options=WriteOptions(
                file_path=self.output_file_path,
                mode=OutputWriteOption.WRITE,
            ),
        )
 def test_opens_correct_file(self, mock_csv_dict_writer):
     with mock.patch("builtins.open", mock.mock_open()) as mock_file:
         csv_dict_writer = mock.Mock(wrteheader=mock.Mock(),
                                     writerows=mock.Mock())
         mock_csv_dict_writer.return_value = csv_dict_writer
         row_formatter = mock.Mock(format=mock.Mock())
         csv_dict_writer = mock.Mock(wrteheader=mock.Mock(),
                                     writerows=mock.Mock())
         mock_csv_dict_writer.return_value = csv_dict_writer
         writer = CSVWriter(column_names=self.COLUMN_NAMES,
                            row_formatter=row_formatter)
         writer.write(data=self.DATA,
                      options=WriteOptions(file_path="some file path",
                                           mode=OutputWriteOption.WRITE))
         mock_file.assert_called_with("some file path",
                                      OutputWriteOption.WRITE.value,
                                      newline="",
                                      encoding="utf8")
 def test_rows_are_written(self, mock_csv_dict_writer):
     csv_dict_writer = mock.Mock(wrteheader=mock.Mock(),
                                 writerows=mock.Mock())
     mock_csv_dict_writer.return_value = csv_dict_writer
     row_formatter = mock.Mock(format=mock.Mock())
     csv_dict_writer = mock.Mock(wrteheader=mock.Mock(),
                                 writerows=mock.Mock())
     mock_csv_dict_writer.return_value = csv_dict_writer
     writer = CSVWriter(column_names=self.COLUMN_NAMES,
                        row_formatter=row_formatter)
     writer.write(data=self.DATA,
                  options=WriteOptions(file_path="some file path",
                                       mode=OutputWriteOption.WRITE))
     csv_dict_writer.writerows.assert_called_once_with(mock.ANY)
     self.assertEqual(3, row_formatter.format.call_count)
     row_formatter.format.assert_has_calls(calls=[
         mock.call("some"),
         mock.call("row"),
         mock.call("data"),
     ],
                                           any_order=False)
Esempio n. 8
0
    def test_output_json_when_output_write_option_is_append_and_no_custom_options(
        self,
        mock_json_writer,
    ):
        mock_json_writer_instance = mock.Mock(write=mock.Mock())
        mock_json_writer.return_value = mock_json_writer_instance

        output(
            values=self.values,
            output_type=OutputType.JSON,
            output_file_path=self.output_file_path,
            csv_writer=None,
            output_write_option=OutputWriteOption.APPEND,
        )

        mock_json_writer.assert_called_once_with(
            encoder=BasketballReferenceJSONEncoder)
        mock_json_writer_instance.write.assert_called_once_with(
            data=self.values,
            options=WriteOptions(
                file_path=self.output_file_path,
                mode=OutputWriteOption.APPEND,
                custom_options=None,
            ))
Esempio n. 9
0
 def test_should_write_to_file_is_false_when_file_path_is_not_none_but_mode_is_none(self):
     self.assertFalse(WriteOptions(file_path="some file path", mode=None).should_write_to_file())
Esempio n. 10
0
 def test_should_write_to_file_is_false_when_file_path_is_none(self):
     self.assertFalse(WriteOptions(file_path=None).should_write_to_file())
Esempio n. 11
0
 def test_two_options_with_same_properties_except_custom_option_are_not_equivalent(self):
     self.assertNotEqual(WriteOptions(custom_options="some optoins"), WriteOptions())
Esempio n. 12
0
 def test_two_options_with_same_properties_except_mode_are_not_equivalent(self):
     self.assertNotEqual(WriteOptions(mode="some mode"), WriteOptions())
Esempio n. 13
0
 def test_two_options_with_same_properties_except_file_path_are_not_equivalent(self):
     self.assertNotEqual(WriteOptions(file_path="some file path"), WriteOptions())
Esempio n. 14
0
 def test_two_options_with_same_properties_are_equivalent(self):
     self.assertEqual(WriteOptions(), WriteOptions())
Esempio n. 15
0
 def test_should_write_to_file_is_true_when_file_path_is_not_none_and_mode_is_not_none(self):
     self.assertTrue(WriteOptions(file_path="some file path", mode="some mode").should_write_to_file())