mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 17:45:35 +00:00
refactored, 1 failing test
This commit is contained in:
@@ -10,41 +10,31 @@ import vcr
|
||||
from vcr.persisters.filesystem import FilesystemPersister
|
||||
|
||||
|
||||
def test_overriding_save_cassette_with_callback(tmpdir, httpbin):
|
||||
def test_save_cassette_with_custom_persister(tmpdir, httpbin):
|
||||
'''Ensure you can save a cassette using save_callback'''
|
||||
|
||||
def save_callback(cassette_path, data):
|
||||
FilesystemPersister.write(cassette_path, data)
|
||||
my_vcr = vcr.VCR()
|
||||
my_vcr.register_persister(FilesystemPersister)
|
||||
|
||||
# Check to make sure directory doesnt exist
|
||||
assert not os.path.exists(str(tmpdir.join('nonexistent')))
|
||||
|
||||
# Run VCR to create dir and cassette file using new save_cassette callback
|
||||
with vcr.use_cassette(
|
||||
str(tmpdir.join('nonexistent', 'cassette.yml')),
|
||||
save_callback=save_callback
|
||||
):
|
||||
with my_vcr.use_cassette(str(tmpdir.join('nonexistent', 'cassette.yml'))):
|
||||
urlopen(httpbin.url).read()
|
||||
|
||||
# Callback should have made the file and the directory
|
||||
assert os.path.exists(str(tmpdir.join('nonexistent', 'cassette.yml')))
|
||||
|
||||
|
||||
def test_overriding_load_cassette_with_callback(tmpdir, httpbin):
|
||||
def test_load_cassette_with_custom_persister(tmpdir, httpbin):
|
||||
'''
|
||||
Ensure you can load a cassette using load_callback
|
||||
'''
|
||||
my_vcr = vcr.VCR()
|
||||
my_vcr.register_persister(FilesystemPersister)
|
||||
|
||||
test_fixture = str(tmpdir.join('synopsis.json'))
|
||||
|
||||
def load_callback(cassette_path):
|
||||
with open(cassette_path) as f:
|
||||
cassette_content = f.read()
|
||||
return cassette_content
|
||||
|
||||
with vcr.use_cassette(
|
||||
test_fixture,
|
||||
serializer='json',
|
||||
load_callback=load_callback
|
||||
):
|
||||
with my_vcr.use_cassette(test_fixture, serializer='json'):
|
||||
response = urlopen(httpbin.url).read()
|
||||
assert b'difficult sometimes' in response
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import copy
|
||||
import inspect
|
||||
import os
|
||||
import sys
|
||||
|
||||
from six.moves import http_client as httplib
|
||||
import pytest
|
||||
@@ -8,6 +9,7 @@ import yaml
|
||||
|
||||
from vcr.compat import mock, contextlib
|
||||
from vcr.cassette import Cassette
|
||||
from vcr.persisters.filesystem import FilesystemPersister
|
||||
from vcr.errors import UnhandledHTTPRequestError
|
||||
from vcr.patch import force_reset
|
||||
from vcr.stubs import VCRHTTPSConnection
|
||||
@@ -83,7 +85,8 @@ def make_get_request():
|
||||
|
||||
|
||||
@mock.patch('vcr.cassette.requests_match', return_value=True)
|
||||
@mock.patch('vcr.cassette.load_cassette', lambda *args, **kwargs: (('foo',), (mock.MagicMock(),)))
|
||||
@mock.patch('vcr.cassette.FilesystemPersister.load_cassette',
|
||||
lambda *args, **kwargs: (('foo',), (mock.MagicMock(),)))
|
||||
@mock.patch('vcr.cassette.Cassette.can_play_response_for', return_value=True)
|
||||
@mock.patch('vcr.stubs.VCRHTTPResponse')
|
||||
def test_function_decorated_with_use_cassette_can_be_invoked_multiple_times(*args):
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
|
||||
import vcr.persist
|
||||
from vcr.persisters.filesystem import FilesystemPersister
|
||||
from vcr.serializers import jsonserializer, yamlserializer
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ from vcr.serializers import jsonserializer, yamlserializer
|
||||
])
|
||||
def test_load_cassette_with_old_cassettes(cassette_path, serializer):
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
vcr.persist.load_cassette(cassette_path, serializer)
|
||||
FilesystemPersister.load_cassette(cassette_path, serializer)
|
||||
assert "run the migration script" in excinfo.exconly()
|
||||
|
||||
|
||||
@@ -20,5 +20,5 @@ def test_load_cassette_with_old_cassettes(cassette_path, serializer):
|
||||
])
|
||||
def test_load_cassette_with_invalid_cassettes(cassette_path, serializer):
|
||||
with pytest.raises(Exception) as excinfo:
|
||||
vcr.persist.load_cassette(cassette_path, serializer)
|
||||
FilesystemPersister.load_cassette(cassette_path, serializer)
|
||||
assert "run the migration script" not in excinfo.exconly()
|
||||
|
||||
@@ -94,7 +94,7 @@ def test_vcr_before_record_response_iterable():
|
||||
response = object() # just can't be None
|
||||
|
||||
# Prevent actually saving the cassette
|
||||
with mock.patch('vcr.cassette.save_cassette'):
|
||||
with mock.patch('vcr.cassette.FilesystemPersister.save_cassette'):
|
||||
|
||||
# Baseline: non-iterable before_record_response should work
|
||||
mock_filter = mock.Mock()
|
||||
@@ -118,7 +118,7 @@ def test_before_record_response_as_filter():
|
||||
response = object() # just can't be None
|
||||
|
||||
# Prevent actually saving the cassette
|
||||
with mock.patch('vcr.cassette.save_cassette'):
|
||||
with mock.patch('vcr.cassette.FilesystemPersister.save_cassette'):
|
||||
|
||||
filter_all = mock.Mock(return_value=None)
|
||||
vcr = VCR(before_record_response=filter_all)
|
||||
@@ -132,7 +132,7 @@ def test_vcr_path_transformer():
|
||||
# Regression test for #199
|
||||
|
||||
# Prevent actually saving the cassette
|
||||
with mock.patch('vcr.cassette.save_cassette'):
|
||||
with mock.patch('vcr.cassette.FilesystemPersister.save_cassette'):
|
||||
|
||||
# Baseline: path should be unchanged
|
||||
vcr = VCR()
|
||||
|
||||
Reference in New Issue
Block a user