diff --git a/tests/test_transformer.py b/tests/test_transformer.py index b1b5acd..3a0ddb9 100644 --- a/tests/test_transformer.py +++ b/tests/test_transformer.py @@ -57,19 +57,19 @@ def test_xmltransformer_iterates_successfully_if_get_optional_fields_returns_non assert len(output_records.deleted_records) == 1 -def test_xmltransformer__write_output_files_writes_timdex_records_and_deleted_files( +def test_xmltransformer_transform_and_write_output_files_writes_output_files( tmp_path, oai_pmh_records ): output_file = str(tmp_path / "output_file.json") transformer = XmlTransformer("cool-repo", oai_pmh_records) - transformer._write_output_files(output_file) + transformer.transform_and_write_output_files(output_file) output_files = list(tmp_path.iterdir()) assert len(output_files) == 2 assert output_files[0].name == "output_file.json" assert output_files[1].name == "output_file.txt" -def test_xmltransformer__write_output_files_no_deleted_records_file_if_not_needed( +def test_xmltransformer_transform_and_write_output_files_no_txt_file_if_not_needed( tmp_path, ): output_file = str(tmp_path / "output_file.json") @@ -77,7 +77,7 @@ def test_xmltransformer__write_output_files_no_deleted_records_file_if_not_neede "tests/fixtures/datacite/datacite_records.xml" ) transformer = XmlTransformer("cool-repo", datacite_records) - transformer._write_output_files(output_file) + transformer.transform_and_write_output_files(output_file) assert len(list(tmp_path.iterdir())) == 1 assert next(tmp_path.iterdir()).name == "output_file.json" diff --git a/transmogrifier/cli.py b/transmogrifier/cli.py index 13cd200..373f62d 100644 --- a/transmogrifier/cli.py +++ b/transmogrifier/cli.py @@ -41,7 +41,7 @@ def main(source, input_file, output_file, verbose): logger.info("Running transform for source %s", source) transformer = Transformer.load(source, input_file) - transformer._write_output_files(output_file) + transformer.transform_and_write_output_files(output_file) logger.info( ( "Completed transform, total records processed: %d, " diff --git a/transmogrifier/sources/transformer.py b/transmogrifier/sources/transformer.py index 2044ef1..90eae10 100644 --- a/transmogrifier/sources/transformer.py +++ b/transmogrifier/sources/transformer.py @@ -70,8 +70,13 @@ def __next__(self) -> TimdexRecord: continue @final - def _write_output_files(self, output_file: str) -> None: - self.write_timdex_records_to_json_file(output_file) + def transform_and_write_output_files(self, output_file: str) -> None: + """Iterates through source records to transform and write to output files. + + Args: + output_file: The name of the output files. + """ + self._write_timdex_records_to_json_file(output_file) if self.processed_record_count == 0: raise ValueError( "No records processed from input file, needs investigation" @@ -80,16 +85,17 @@ def _write_output_files(self, output_file: str) -> None: deleted_output_file = output_file.replace("index", "delete").replace( "json", "txt" ) - self.write_deleted_records_to_txt_file(deleted_records, deleted_output_file) + self._write_deleted_records_to_txt_file( + deleted_records, deleted_output_file + ) @final - def write_timdex_records_to_json_file(self, output_file: str) -> int: + def _write_timdex_records_to_json_file(self, output_file: str) -> int: """ Write TIMDEX records to JSON file. Args: output_file: The JSON file used for writing TIMDEX records. - """ count = 0 try: @@ -121,10 +127,16 @@ def write_timdex_records_to_json_file(self, output_file: str) -> int: @final @staticmethod - def write_deleted_records_to_txt_file( - deleted_records: list[str], output_file_path: str + def _write_deleted_records_to_txt_file( + deleted_records: list[str], output_file: str ): - with open(output_file_path, "w") as file: + """Write deleted records to the specified text file. + + Args: + deleted_records: The deleted records to write to file. + output_file: The text file used for writing deleted records. + """ + with open(output_file, "w") as file: for record_id in deleted_records: file.write(f"{record_id}\n") @@ -232,6 +244,7 @@ def get_required_fields(self, source_record: JSON | Tag) -> dict: pass @classmethod + @abstractmethod def get_main_titles(cls, source_record: JSON | Tag) -> list[str]: """ Retrieve main title(s) from an source record. @@ -241,7 +254,7 @@ def get_main_titles(cls, source_record: JSON | Tag) -> list[str]: Args: source_record: A single source record. """ - return [] + pass @classmethod @abstractmethod