单元测试模拟 GCS

Pie*_*e56 5 python testing unit-testing python-mock google-cloud-storage

我很难找到一种方法来对此类中存在的read和方法进行单元测试。write我正在尝试使用模拟补丁库创建一个模拟,以避免调用 Google Storage,但我很难弄清楚如何做到这一点。

from google.cloud import storage


class GCSObject(str):
    

    def __init__(self, uri=""):
        self.base, self.bucket, self.path = self.parse_uri(uri)


    def parse_uri(self, uri):
        uri = uri.lstrip("gs://").replace("//", "/").split("/", 1)
        if len(uri) > 1:
            return ("gs://", uri[0], uri[1])
        else:
            return ("gs://", uri[0], "")

    def read(self) -> bytes:
        storage_client = storage.Client()
        bucket = storage_client.bucket(self.bucket)
        return bucket.blob(self.path).download_as_string()

    def write(self, content: bytes):
        storage_client = storage.Client()
        bucket = storage_client.get_bucket(self.bucket)
        blob = bucket.blob(self.path)
        blob.upload_from_string(content)
Run Code Online (Sandbox Code Playgroud)

我目前正在尝试做的事情

@mock.patch("packages.pyGCP.pyGCP.gcs.storage.Client")
def test_upload(client):
    gcs_path = GCSObject("fake_path")
    reader = gcs_path.read()  
    #  confused what I should assert 
Run Code Online (Sandbox Code Playgroud)

sli*_*wp2 8

我正在使用google-cloud-storage==1.32.0python 3.7.5。这是单元测试解决方案:

gcs.py:

from google.cloud import storage


class GCSObject(str):

    def __init__(self, uri=""):
        self.base, self.bucket, self.path = self.parse_uri(uri)

    def parse_uri(self, uri):
        uri = uri.lstrip("gs://").replace("//", "/").split("/", 1)
        if len(uri) > 1:
            return ("gs://", uri[0], uri[1])
        else:
            return ("gs://", uri[0], "")

    def read(self) -> bytes:
        storage_client = storage.Client()
        bucket = storage_client.bucket(self.bucket)
        return bucket.blob(self.path).download_as_string()

    def write(self, content: bytes):
        storage_client = storage.Client()
        bucket = storage_client.get_bucket(self.bucket)
        blob = bucket.blob(self.path)
        blob.upload_from_string(content)
Run Code Online (Sandbox Code Playgroud)

test_gcs.py:

from google.cloud import storage


class GCSObject(str):

    def __init__(self, uri=""):
        self.base, self.bucket, self.path = self.parse_uri(uri)

    def parse_uri(self, uri):
        uri = uri.lstrip("gs://").replace("//", "/").split("/", 1)
        if len(uri) > 1:
            return ("gs://", uri[0], uri[1])
        else:
            return ("gs://", uri[0], "")

    def read(self) -> bytes:
        storage_client = storage.Client()
        bucket = storage_client.bucket(self.bucket)
        return bucket.blob(self.path).download_as_string()

    def write(self, content: bytes):
        storage_client = storage.Client()
        bucket = storage_client.get_bucket(self.bucket)
        blob = bucket.blob(self.path)
        blob.upload_from_string(content)
Run Code Online (Sandbox Code Playgroud)

单元测试结果:

..
----------------------------------------------------------------------
Ran 2 tests in 0.004s

OK
Name                                     Stmts   Miss  Cover   Missing
----------------------------------------------------------------------
src/stackoverflow/64672497/gcs.py           18      1    94%   12
src/stackoverflow/64672497/test_gcs.py      29      0   100%
----------------------------------------------------------------------
TOTAL                                       47      1    98%
Run Code Online (Sandbox Code Playgroud)