From a1833c69446a8226fe4939e11637c0562be2c69a Mon Sep 17 00:00:00 2001 From: Moritz-Alexander-Kern <92092328+Moritz-Alexander-Kern@users.noreply.github.com> Date: Fri, 14 Jun 2024 16:21:47 +0200 Subject: [PATCH] add tests for download_datasets function --- elephant/test/test_datasets.py | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 elephant/test/test_datasets.py diff --git a/elephant/test/test_datasets.py b/elephant/test/test_datasets.py new file mode 100644 index 000000000..24334e2f6 --- /dev/null +++ b/elephant/test/test_datasets.py @@ -0,0 +1,35 @@ +import unittest +import os +from unittest.mock import patch +from pathlib import Path +import urllib + +from elephant.datasets import download_datasets + + +class TestDownloadDatasets(unittest.TestCase): + @patch.dict(os.environ, {'ELEPHANT_DATA_LOCATION': '/valid/path'}, clear=True) + @patch('os.path.exists', return_value=True) + def test_valid_path(self, mock_exists): + repo_path = 'some/repo/path' + expected = Path('/valid/path/some/repo/path') + result = download_datasets(repo_path) + self.assertEqual(result, expected) + + @patch.dict(os.environ, {'ELEPHANT_DATA_LOCATION': 'http://valid.url'}, clear=True) + @patch('os.path.exists', return_value=False) + def test_valid_url(self, mock_exists): + repo_path = 'some/repo/path' + self.assertRaises(urllib.error.URLError, download_datasets, repo_path) + + @patch.dict(os.environ, {'ELEPHANT_DATA_LOCATION': 'invalid_path_or_url'}, clear=True) + @patch('os.path.exists', return_value=False) + def test_invalid_value(self, mock_exists): + repo_path = 'some/repo/path' + with self.assertRaises(ValueError) as cm: + download_datasets(repo_path) + self.assertIn("invalid_path_or_url", str(cm.exception)) + + +if __name__ == '__main__': + unittest.main()