Skip to content

Commit efc4efc

Browse files
committed
add tests
1 parent 94b5784 commit efc4efc

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

gcsfs/tests/test_credentials.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,43 @@ def test_connect_google_default_uses_request():
4545
assert isinstance(kwargs["request"], Request)
4646

4747

48+
def test_connect_cloud_success():
49+
with patch("google.auth.compute_engine.Credentials") as mock_creds_class:
50+
mock_creds = Mock()
51+
mock_creds_class.return_value = mock_creds
52+
53+
cred = GoogleCredentials(
54+
project="my-project", access="read_only", token="cloud", on_google=True
55+
)
56+
57+
assert cred.credentials == mock_creds
58+
assert mock_creds.refresh.called
59+
assert cred.method == "cloud"
60+
61+
62+
def test_connect_cloud_failure():
63+
import google.auth.exceptions
64+
65+
with patch("google.auth.compute_engine.Credentials") as mock_creds_class:
66+
mock_creds = Mock()
67+
mock_creds_class.return_value = mock_creds
68+
mock_creds.refresh.side_effect = google.auth.exceptions.RefreshError(
69+
"mock error"
70+
)
71+
72+
with pytest.raises(ValueError, match="Invalid gcloud credentials"):
73+
GoogleCredentials(
74+
project="my-project", access="read_only", token="cloud", on_google=True
75+
)
76+
77+
78+
def test_connect_cloud_not_on_google():
79+
with pytest.raises(ValueError):
80+
GoogleCredentials(
81+
project="my-project", access="read_only", token="cloud", on_google=False
82+
)
83+
84+
4885
@pytest.mark.parametrize("token", ["", "incorrect.token", "x" * 100])
4986
def test_credentials_from_raw_token(token):
5087
with patch.dict(os.environ, {"FETCH_RAW_TOKEN_EXPIRY": "false"}):

gcsfs/tests/test_extended_gcsfs.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,3 +1615,55 @@ async def test_cat_file_non_zonal_fallback(extended_gcsfs):
16151615
mock_super_cat.assert_awaited_once_with(
16161616
"standard_bucket/obj", start=10, end=20, concurrency=2, custom_arg="val"
16171617
)
1618+
1619+
1620+
@pytest.mark.asyncio
1621+
async def test_lookup_bucket_type_not_cached_unknown(extended_gcsfs):
1622+
"""Test that BucketType.UNKNOWN is not cached when _cache_unknown_buckets is False."""
1623+
fs = extended_gcsfs
1624+
fs._cache_unknown_buckets = False
1625+
1626+
# Clear cache just in case
1627+
fs._storage_layout_cache.clear()
1628+
1629+
# Mock _get_bucket_type to return UNKNOWN
1630+
with mock.patch.object(
1631+
fs, "_get_bucket_type", new_callable=mock.AsyncMock
1632+
) as mock_get_type:
1633+
mock_get_type.return_value = BucketType.UNKNOWN
1634+
1635+
# First lookup
1636+
type1 = await fs._lookup_bucket_type("my-bucket")
1637+
assert type1 == BucketType.UNKNOWN
1638+
assert mock_get_type.call_count == 1
1639+
1640+
# Second lookup should call _get_bucket_type again because it's not cached
1641+
type2 = await fs._lookup_bucket_type("my-bucket")
1642+
assert type2 == BucketType.UNKNOWN
1643+
assert mock_get_type.call_count == 2
1644+
1645+
1646+
@pytest.mark.asyncio
1647+
async def test_lookup_bucket_type_cached_unknown(extended_gcsfs):
1648+
"""Test that BucketType.UNKNOWN is cached when _cache_unknown_buckets is True."""
1649+
fs = extended_gcsfs
1650+
fs._cache_unknown_buckets = True
1651+
1652+
# Clear cache just in case
1653+
fs._storage_layout_cache.clear()
1654+
1655+
# Mock _get_bucket_type to return UNKNOWN
1656+
with mock.patch.object(
1657+
fs, "_get_bucket_type", new_callable=mock.AsyncMock
1658+
) as mock_get_type:
1659+
mock_get_type.return_value = BucketType.UNKNOWN
1660+
1661+
# First lookup
1662+
type1 = await fs._lookup_bucket_type("my-bucket")
1663+
assert type1 == BucketType.UNKNOWN
1664+
assert mock_get_type.call_count == 1
1665+
1666+
# Second lookup should NOT call _get_bucket_type again because it's cached
1667+
type2 = await fs._lookup_bucket_type("my-bucket")
1668+
assert type2 == BucketType.UNKNOWN
1669+
assert mock_get_type.call_count == 1

0 commit comments

Comments
 (0)