From 65cd211108b21c7f564bc9df5ed3bd33721605de Mon Sep 17 00:00:00 2001 From: ageorgou <1186102+ageorgou@users.noreply.github.com> Date: Mon, 2 Sep 2024 00:16:29 +0100 Subject: [PATCH] Replace deprecated method in csv2rdf (#2901) * Replace deprecated method ConfigParser.readfp has been deprecated since Python 3.2 and is removed in 3.12. * Test that the config file is opened --- rdflib/tools/csv2rdf.py | 2 +- test/test_tools/test_csv2rdf.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/rdflib/tools/csv2rdf.py b/rdflib/tools/csv2rdf.py index 9c3560f42..d518e809f 100644 --- a/rdflib/tools/csv2rdf.py +++ b/rdflib/tools/csv2rdf.py @@ -482,7 +482,7 @@ def main(): if "-f" in opts: config = configparser.ConfigParser() - config.readfp(open(opts["-f"])) + config.read_file(open(opts["-f"])) for k, v in config.items("csv2rdf"): if k == "out": csv2rdf.OUT = codecs.open(v, "w", "utf-8") diff --git a/test/test_tools/test_csv2rdf.py b/test/test_tools/test_csv2rdf.py index 450aa31bf..007fd0bd1 100644 --- a/test/test_tools/test_csv2rdf.py +++ b/test/test_tools/test_csv2rdf.py @@ -3,7 +3,9 @@ import subprocess import sys from tempfile import mkstemp +from unittest.mock import mock_open, patch, sentinel +from rdflib.tools import csv2rdf from test.data import TEST_DATA_DIR REALESTATE_FILE_PATH = os.path.join(TEST_DATA_DIR, "csv", "realestate.csv") @@ -44,3 +46,20 @@ def test_csv2rdf_cli_fileout(self): assert len(f.readlines()) == 228 os.close(fh) os.remove(fname) + + @patch.object(csv2rdf.CSV2RDF, "convert", return_value=None) + def test_csv2rdf_config_file_opened(self, config_mock): + """Test that the config file is read when specified.""" + # Pretend there is a file with the section we're looking for. + # We don't care about the actual path, since it won't really be opened + # but when we try to open it, we will get back the section header + # so that the reader doesn't complain. + config_file = sentinel.file_path + open_mock = mock_open(read_data="[csv2rdf]") + # Also pretend that we're passing the arguments from the command line + cli_args = ["csv2rdf.py", "-f", config_file, str(REALESTATE_FILE_PATH)] + with patch.object(csv2rdf.sys, "argv", cli_args): + with patch("builtins.open", open_mock): + csv2rdf.main() + # Check that we've "opened" the right file + open_mock.assert_called_once_with(config_file)