Skip to content

Commit

Permalink
update custom scheme tests to utilize pytest fixture
Browse files Browse the repository at this point in the history
This isolates the implementation in response to PR feedback:
#467 (comment)
  • Loading branch information
kujenga committed Sep 4, 2024
1 parent 2cec1a6 commit 147db5b
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import string
from pathlib import Path

import pytest

from cloudpathlib import CloudPath
from cloudpathlib.client import register_client_class
from cloudpathlib.cloudpath import register_path_class
from cloudpathlib.cloudpath import implementation_registry, register_path_class
from cloudpathlib.s3.s3client import S3Client
from cloudpathlib.s3.s3path import S3Path

Expand Down Expand Up @@ -121,32 +123,47 @@ def my_content_type(path):
_test_write_content_type(suffix, content_type, rig)


@register_path_class("mys3")
class MyS3Path(S3Path):
cloud_prefix: str = "mys3://"
@pytest.fixture
def custom_s3_path():
# A fixture isolates these classes as they modify the global registry of
# implementations.
@register_path_class("mys3")
class MyS3Path(S3Path):
cloud_prefix: str = "mys3://"

@register_client_class("mys3")
class MyS3Client(S3Client):
pass

yield (MyS3Path, MyS3Client)

# cleanup after use
implementation_registry.pop("mys3")

@register_client_class("mys3")
class MyS3Client(S3Client):
pass

def test_custom_mys3path_instantiation(custom_s3_path):
CustomPath, _ = custom_s3_path

def test_custom_mys3path_instantiation():
path = MyS3Path("mys3://bucket/dir/file.txt")
assert isinstance(path, MyS3Path)
path = CustomPath("mys3://bucket/dir/file.txt")
assert isinstance(path, CustomPath)
assert path.cloud_prefix == "mys3://"
assert path.bucket == "bucket"
assert path.key == "dir/file.txt"


def test_custom_mys3client_instantiation():
client = MyS3Client()
assert isinstance(client, MyS3Client)
def test_custom_mys3client_instantiation(custom_s3_path):
_, CustomClient = custom_s3_path

client = CustomClient()
assert isinstance(client, CustomClient)
assert client.CloudPath("mys3://bucket/dir/file.txt").cloud_prefix == "mys3://"


def test_custom_mys3client_default_client():
MyS3Client().set_as_default_client()
def test_custom_mys3client_default_client(custom_s3_path):
_, CustomClient = custom_s3_path

CustomClient().set_as_default_client()

path = CloudPath("mys3://bucket/dir/file.txt")
assert isinstance(path.client, MyS3Client)
assert isinstance(path.client, CustomClient)
assert path.cloud_prefix == "mys3://"

0 comments on commit 147db5b

Please sign in to comment.