diff --git a/tests/test_common.py b/tests/test_common.py index 51d379c..fc0a237 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -1,9 +1,7 @@ import unittest -from unittest.mock import patch, mock_open, MagicMock -from pathlib import Path -import logging +from unittest.mock import patch +from pyfakefs.fake_filesystem_unittest import TestCase -# Assuming the function path2bytes is in a module named common from gnssanalysis.gn_io.common import path2bytes @@ -34,19 +32,25 @@ def test_bytes_input(self): result = path2bytes(b"test data") self.assertEqual(result, b"test data") - @patch("gnssanalysis.gn_io.common._logging.error") - def test_file_not_found(self, mock_logging_error): - with patch("gnssanalysis.gn_io.common._txt2bytes", side_effect=FileNotFoundError): - print("testing path") - result = path2bytes("nonexistent.txt") - self.assertIsNone(result) - mock_logging_error.assert_called_once_with("File nonexistent.txt not found. Returning empty bytes.") - - @patch("gnssanalysis.gn_io.common._logging.error") - def test_generic_exception(self, mock_logging_error): - with patch("gnssanalysis.gn_io.common._txt2bytes", side_effect=Exception("Generic error")): - result = path2bytes("test.txt") - self.assertIsNone(result) - mock_logging_error.assert_called_once_with( - "Error reading file test.txt with error Generic error. Returning empty bytes." - ) + +class TestPath2BytesWithFakeFs(TestCase): + def setUp(self): + self.setUpPyfakefs() + + def test_file_not_found_and_file_read(self): + # Create a file, but not the one we're looking for + self.fs.create_file("testfile.txt", contents=b"hello") + with self.assertRaises(FileNotFoundError): + path2bytes("nonexistent.txt") + + # Now open the file that does exist and check the contents + self.assertEqual(path2bytes("testfile.txt"), b"hello") + + def test_invalid_archive_expand_exception(self): + # Test that trying to unpack an archive file which isn't valid archive data, raises an exception + self.fs.create_file("invalidarchive.gz", contents=b"hello") + self.fs.create_file("invalidarchive.Z", contents=b"hello") + with self.assertRaises(Exception): + path2bytes("invalidarchive.gz") + with self.assertRaises(Exception): + path2bytes("invalidarchive.Z")