Skip to content

Commit

Permalink
NPI-3501 updated unit tests to more closely fit updated empty or inva…
Browse files Browse the repository at this point in the history
…lid file exception handling
  • Loading branch information
treefern committed Sep 11, 2024
1 parent eb89d6b commit 20daa6d
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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")

0 comments on commit 20daa6d

Please sign in to comment.