Skip to content

Commit 364d42a

Browse files
committed
use fake fs for unit tests
1 parent aaa4d78 commit 364d42a

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

tests/unit/utils/test_cache.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def test_connection_info_post_init():
518518
assert connection_info2.engines["engine1"] is engine_obj
519519

520520

521-
@mark.nofakefs
521+
@mark.usefixtures("fs")
522522
def test_file_based_cache_read_data_json_file_not_exists(
523523
file_based_cache, encrypter_with_key
524524
):
@@ -530,6 +530,7 @@ def test_file_based_cache_read_data_json_file_not_exists(
530530
assert result == {}
531531

532532

533+
@mark.usefixtures("fs")
533534
def test_file_based_cache_read_data_json_valid_data(
534535
file_based_cache, encrypter_with_key
535536
):
@@ -553,6 +554,7 @@ def test_file_based_cache_read_data_json_valid_data(
553554
assert result["token"] == "test_token"
554555

555556

557+
@mark.usefixtures("fs")
556558
def test_file_based_cache_read_data_json_decryption_failure(file_based_cache):
557559
"""Test _read_data_json returns empty dict when decryption fails."""
558560
# Create encrypters with different keys
@@ -574,6 +576,7 @@ def test_file_based_cache_read_data_json_decryption_failure(file_based_cache):
574576
assert result == {}
575577

576578

579+
@mark.usefixtures("fs")
577580
def test_file_based_cache_read_data_json_invalid_json(
578581
file_based_cache, encrypter_with_key
579582
):
@@ -593,17 +596,22 @@ def test_file_based_cache_read_data_json_invalid_json(
593596
assert result == {}
594597

595598

596-
@mark.nofakefs
599+
@mark.usefixtures("fs")
597600
def test_file_based_cache_read_data_json_io_error(file_based_cache, encrypter_with_key):
598601
"""Test _read_data_json returns empty dict when IOError occurs."""
602+
# Create a file in the fake filesystem and then mock open to raise IOError
603+
test_file_path = "/test_cache/io_error_test.txt"
604+
os.makedirs(os.path.dirname(test_file_path), exist_ok=True)
605+
599606
# Mock open to raise IOError
600607
with patch("builtins.open", mock_open()) as mock_file:
601608
mock_file.side_effect = IOError("File read error")
602609

603-
result = file_based_cache._read_data_json("test_file.txt", encrypter_with_key)
610+
result = file_based_cache._read_data_json(test_file_path, encrypter_with_key)
604611
assert result == {}
605612

606613

614+
@mark.usefixtures("fs")
607615
def test_file_based_cache_read_data_json_empty_encrypted_data(
608616
file_based_cache, encrypter_with_key
609617
):
@@ -622,6 +630,7 @@ def test_file_based_cache_read_data_json_empty_encrypted_data(
622630
assert result == {}
623631

624632

633+
@mark.usefixtures("fs")
625634
def test_file_based_cache_read_data_json_invalid_encrypted_format(
626635
file_based_cache, encrypter_with_key
627636
):
@@ -639,6 +648,7 @@ def test_file_based_cache_read_data_json_invalid_encrypted_format(
639648
assert result == {}
640649

641650

651+
@mark.usefixtures("fs")
642652
def test_file_based_cache_delete_method(file_based_cache, encrypter_with_key):
643653
"""Test FileBasedCache delete method removes data from both memory and file."""
644654
# Create test data
@@ -672,21 +682,23 @@ def test_file_based_cache_delete_method(file_based_cache, encrypter_with_key):
672682
assert cache_result is None
673683

674684

675-
@mark.nofakefs
685+
@mark.usefixtures("fs")
676686
def test_file_based_cache_delete_method_file_removal_failure(
677687
file_based_cache, encrypter_with_key
678688
):
679689
"""Test FileBasedCache delete method handles file removal failures gracefully."""
680690
sample_key = SecureCacheKey(["delete", "failure"], "test_secret")
681691
sample_data = ConnectionInfo(id="test_connection", token="test_token")
682692

683-
# Set data in memory cache only (no file operations due to @mark.nofakefs)
684-
file_based_cache.memory_cache.set(sample_key, sample_data)
693+
# Set data in cache (both memory and file in fake filesystem)
694+
file_based_cache.set(sample_key, sample_data)
685695

686-
# Mock path.exists to return True and os.remove to raise OSError
687-
with patch("firebolt.utils.cache.path.exists", return_value=True), patch(
688-
"firebolt.utils.cache.os.remove"
689-
) as mock_remove:
696+
# Verify file exists in fake filesystem
697+
file_path = file_based_cache._get_file_path(sample_key)
698+
assert os.path.exists(file_path)
699+
700+
# Mock os.remove to raise OSError while keeping path.exists returning True
701+
with patch("firebolt.utils.cache.os.remove") as mock_remove:
690702
mock_remove.side_effect = OSError("Permission denied")
691703

692704
# Delete should not raise an exception despite file removal failure
@@ -696,7 +708,11 @@ def test_file_based_cache_delete_method_file_removal_failure(
696708
memory_result = file_based_cache.memory_cache.get(sample_key)
697709
assert memory_result is None
698710

711+
# Verify the file still exists (removal failed due to mocked OSError)
712+
assert os.path.exists(file_path)
699713

714+
715+
@mark.usefixtures("fs")
700716
def test_file_based_cache_get_from_file_when_not_in_memory(
701717
file_based_cache, encrypter_with_key
702718
):
@@ -740,6 +756,7 @@ def test_file_based_cache_get_from_file_when_not_in_memory(
740756
assert memory_result_after_load.id == "test_file_connection"
741757

742758

759+
@mark.usefixtures("fs")
743760
def test_file_based_cache_get_from_corrupted_file(file_based_cache, encrypter_with_key):
744761
"""Test FileBasedCache get method handles corrupted file gracefully."""
745762
sample_key = SecureCacheKey(["corrupted", "file"], "test_secret")
@@ -763,6 +780,7 @@ def test_file_based_cache_get_from_corrupted_file(file_based_cache, encrypter_wi
763780
assert memory_result is None
764781

765782

783+
@mark.usefixtures("fs")
766784
def test_file_based_cache_disabled_behavior(file_based_cache, encrypter_with_key):
767785
"""Test FileBasedCache methods when cache is disabled."""
768786
sample_key = SecureCacheKey(["disabled", "test"], "test_secret")
@@ -795,6 +813,7 @@ def test_file_based_cache_disabled_behavior(file_based_cache, encrypter_with_key
795813
file_based_cache.delete(sample_key) # Should not raise exception
796814

797815

816+
@mark.usefixtures("fs")
798817
def test_file_based_cache_preserves_expiry_from_file(
799818
file_based_cache, encrypter_with_key, fixed_time
800819
):
@@ -831,6 +850,7 @@ def test_file_based_cache_preserves_expiry_from_file(
831850
assert memory_result_after_load.expiry_time == expected_expiry
832851

833852

853+
@mark.usefixtures("fs")
834854
def test_file_based_cache_deletes_expired_file_on_get(
835855
file_based_cache, encrypter_with_key, fixed_time
836856
):
@@ -873,6 +893,7 @@ def test_file_based_cache_deletes_expired_file_on_get(
873893
assert memory_result is None
874894

875895

896+
@mark.usefixtures("fs")
876897
def test_file_based_cache_expiry_edge_case_exactly_expired(
877898
file_based_cache, encrypter_with_key, fixed_time
878899
):
@@ -907,6 +928,7 @@ def test_file_based_cache_expiry_edge_case_exactly_expired(
907928
assert not os.path.exists(file_path)
908929

909930

931+
@mark.usefixtures("fs")
910932
def test_file_based_cache_non_expired_file_loads_correctly(
911933
file_based_cache, encrypter_with_key, fixed_time
912934
):
@@ -1176,7 +1198,7 @@ def test_cache_delete_consistency_with_connections(
11761198
assert disk_result is None, "Disk cache should be deleted"
11771199

11781200

1179-
@mark.nofakefs # These tests need to test real disk behavior
1201+
@mark.usefixtures("fs") # Use pyfakefs for filesystem mocking
11801202
def test_memory_first_disk_fallback_with_connections(
11811203
clean_cache,
11821204
auth_client_1,
@@ -1205,6 +1227,12 @@ def test_memory_first_disk_fallback_with_connections(
12051227
memory_data = _firebolt_cache.memory_cache.get(cache_key)
12061228
assert memory_data is not None, "Data should be in memory cache"
12071229

1230+
# Verify cache file exists in fake filesystem
1231+
cache_dir = os.path.expanduser("~/.firebolt")
1232+
if os.path.exists(cache_dir):
1233+
cache_files = [f for f in os.listdir(cache_dir) if f.endswith(".json")]
1234+
assert len(cache_files) > 0, "Cache files should be created"
1235+
12081236
# Clear memory cache but keep disk cache
12091237
_firebolt_cache.memory_cache.clear()
12101238
assert (

0 commit comments

Comments
 (0)