lundi 16 décembre 2019

How to mock out `s3fs.FileSystem().glob`?

I have a function which includes the following:

FS = s3fs.S3FileSystem()

def my_function(s3_path: str):
    files = FS.glob('s3://'+s3_path)

It lists all files on AWS S3 which match a certain string s3_path, and then (later) downloads them.

When testing this function, I don't want to connect to AWS S3 each time. But I do want to check what happens to the files I download.

So, I have locally made a folder called testinputs. In here, I have copied some of the files I have on AWS S3.

My idea then was to mock out the FS.glob(s3_path) call so that, instead, it returns the output of

import glob

glob.glob(os.path.join('testinputs', s3_path))

How can I do that? I have tried using mock from unittest to write:

from unittest import mock

def test_my_function():
    with mock.patch('s3fs.S3FileSystem') as mock_fs:
        mock_fs.glob = glob.glob
        my_function('<SOME FILE PATH>')

But this doesn't seem to be changing the behaviour of FS.glob at all.

Aucun commentaire:

Enregistrer un commentaire