def test_remote_missing(get_bucket, load_to): config = Mock() get_bucket().download_fileobj.side_effect = Boto3Error('test') with pytest.raises(Boto3Error) as e: load_from_s3(config, filename='s3://somebucket/mycfg.py') assert str(e.value) == 'test' load_to.assert_not_called()
def test_cache(get_bucket, load_to, tmpdir): config = Mock() out_filename = str(tmpdir.join('out.py')) load_from_s3(config, filename='s3://somebucket/mycfg.py', cache_filename=out_filename) # It must download mycfg.py to cache file. assert get_bucket().method_calls == [ call.download_file('mycfg.py', out_filename) ] # Then load config from the cached file. assert load_to.mock_calls == [call(config, 'pyfile', 'dict', out_filename, silent=False)]
def test_cache_exist(get_bucket, load_to, tmpdir): config = Mock() cache = tmpdir.join('out.py') cache.write('1') out_filename = str(cache) load_from_s3(config, filename='s3://somebucket/mycfg.py', cache_filename=out_filename) # If cache file exists and 'update_cache' is not set then it won't # download remote file. get_bucket.assert_not_called() # Load config from the cached file must happen anyway. assert load_to.mock_calls == [call(config, 'pyfile', 'dict', out_filename, silent=False)]
def test_nocache(get_bucket, BytesIO): sio = Mock() sio.read.return_value = 'A=1' BytesIO.return_value = sio config = {} res = load_from_s3(config, filename='s3://somebucket/mycfg.py') assert res is True assert config == {'A': 1}
def test_nocache_fake(get_bucket, load_to, BytesIO): sio = Mock() BytesIO.return_value = sio m = Mock() m.attach_mock(get_bucket, 'get_bucket') m.attach_mock(load_to, 'load_to') m.attach_mock(BytesIO, 'BytesIO') config = Mock() load_from_s3(config, 's3://somebucket/mycfg.py') assert m.mock_calls == [ call.BytesIO(), call.get_bucket('somebucket', None, None, None), call.get_bucket().download_fileobj('mycfg.py', sio), call.BytesIO().flush(), call.BytesIO().seek(0), call.load_to(config, 'pyfile', 'dict', sio, silent=False) ]
def test_remote_missing_silent(get_bucket, load_to): config = Mock() get_bucket().download_fileobj.side_effect = Boto3Error('test') res = load_from_s3(config, filename='s3://somebucket/mycfg.py', silent=True) assert res is False load_to.assert_not_called()
def test_cache_exist_force(get_bucket, load_to, tmpdir): config = Mock() cache = tmpdir.join('out.py') cache.write('1') out_filename = str(cache) load_from_s3(config, filename='s3://somebucket/mycfg.py', cache_filename=out_filename, update_cache=True) # It must download mycfg.py and overwirte cache file # since update_cache=True. assert get_bucket().method_calls == [ call.download_file('mycfg.py', out_filename) ] # Then load config from the cached file. assert load_to.mock_calls == [call(config, 'pyfile', 'dict', out_filename, silent=False)]
def test_empty_filename(get_bucket, load_to): config = Mock() with pytest.raises(ValueError) as e: load_from_s3(config, 's3://somebucket') assert str(e.value) == 'Empty filename'
def test_invalid_path(get_bucket, load_to): config = Mock() with pytest.raises(ValueError) as e: load_from_s3(config, '/somebucket/mycfg.py') assert str(e.value) == 'Invalid S3 path'