mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
Don't save cassette when it hasn't been modified
This commit is contained in:
committed by
Kevin McCarthy
parent
7264780960
commit
b55834e929
@@ -51,11 +51,12 @@ def test_patched_content(tmpdir):
|
|||||||
assert cass.play_count == 0
|
assert cass.play_count == 0
|
||||||
|
|
||||||
with vcr.use_cassette(str(tmpdir.join('synopsis.yaml'))) as cass:
|
with vcr.use_cassette(str(tmpdir.join('synopsis.yaml'))) as cass:
|
||||||
response2= urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
response2 = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
||||||
assert cass.play_count == 1
|
assert cass.play_count == 1
|
||||||
|
cass._save(force=True)
|
||||||
|
|
||||||
with vcr.use_cassette(str(tmpdir.join('synopsis.yaml'))) as cass:
|
with vcr.use_cassette(str(tmpdir.join('synopsis.yaml'))) as cass:
|
||||||
response3= urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
response3 = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
||||||
assert cass.play_count == 1
|
assert cass.play_count == 1
|
||||||
|
|
||||||
assert response == response2
|
assert response == response2
|
||||||
@@ -68,11 +69,12 @@ def test_patched_content_json(tmpdir):
|
|||||||
assert cass.play_count == 0
|
assert cass.play_count == 0
|
||||||
|
|
||||||
with vcr.use_cassette(str(tmpdir.join('synopsis.json')), serializer='json') as cass:
|
with vcr.use_cassette(str(tmpdir.join('synopsis.json')), serializer='json') as cass:
|
||||||
response2= urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
response2 = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
||||||
assert cass.play_count == 1
|
assert cass.play_count == 1
|
||||||
|
cass._save(force=True)
|
||||||
|
|
||||||
with vcr.use_cassette(str(tmpdir.join('synopsis.json')), serializer='json') as cass:
|
with vcr.use_cassette(str(tmpdir.join('synopsis.json')), serializer='json') as cass:
|
||||||
response3= urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
response3 = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
||||||
assert cass.play_count == 1
|
assert cass.play_count == 1
|
||||||
|
|
||||||
assert response == response2
|
assert response == response2
|
||||||
|
|||||||
43
tests/integration/test_disksaver.py
Normal file
43
tests/integration/test_disksaver.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
'''Basic tests about save behavior'''
|
||||||
|
# coding=utf-8
|
||||||
|
|
||||||
|
# External imports
|
||||||
|
import os
|
||||||
|
import urllib2
|
||||||
|
|
||||||
|
# Internal imports
|
||||||
|
import vcr
|
||||||
|
|
||||||
|
def test_disk_saver_nowrite(tmpdir):
|
||||||
|
'''Ensure that when you close a cassette without changing it it doesn't rewrite the file'''
|
||||||
|
fname = str(tmpdir.join('synopsis.yaml'))
|
||||||
|
with vcr.use_cassette(fname) as cass:
|
||||||
|
urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
||||||
|
assert cass.play_count == 0
|
||||||
|
last_mod = os.path.getmtime(fname)
|
||||||
|
|
||||||
|
with vcr.use_cassette(fname) as cass:
|
||||||
|
urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
||||||
|
assert cass.play_count == 1
|
||||||
|
assert cass.dirty == False
|
||||||
|
last_mod2 = os.path.getmtime(fname)
|
||||||
|
|
||||||
|
assert last_mod == last_mod2
|
||||||
|
|
||||||
|
def test_disk_saver_write(tmpdir):
|
||||||
|
'''Ensure that when you close a cassette with changing it it does rewrite the file'''
|
||||||
|
fname = str(tmpdir.join('synopsis.yaml'))
|
||||||
|
with vcr.use_cassette(fname) as cass:
|
||||||
|
urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
||||||
|
assert cass.play_count == 0
|
||||||
|
last_mod = os.path.getmtime(fname)
|
||||||
|
|
||||||
|
with vcr.use_cassette(fname) as cass:
|
||||||
|
urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
||||||
|
urllib2.urlopen('http://httpbin.org/').read()
|
||||||
|
assert cass.play_count == 1
|
||||||
|
assert cass.dirty
|
||||||
|
last_mod2 = os.path.getmtime(fname)
|
||||||
|
|
||||||
|
assert last_mod != last_mod2
|
||||||
|
|
||||||
@@ -25,6 +25,7 @@ class Cassette(object):
|
|||||||
self._serializer = serializer
|
self._serializer = serializer
|
||||||
self.data = OrderedDict()
|
self.data = OrderedDict()
|
||||||
self.play_counts = Counter()
|
self.play_counts = Counter()
|
||||||
|
self.dirty = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def play_count(self):
|
def play_count(self):
|
||||||
@@ -47,22 +48,26 @@ class Cassette(object):
|
|||||||
def append(self, request, response):
|
def append(self, request, response):
|
||||||
'''Add a request, response pair to this cassette'''
|
'''Add a request, response pair to this cassette'''
|
||||||
self.data[request] = response
|
self.data[request] = response
|
||||||
|
self.dirty = True
|
||||||
|
|
||||||
def response_of(self, request):
|
def response_of(self, request):
|
||||||
'''Find the response corresponding to a request'''
|
'''Find the response corresponding to a request'''
|
||||||
return self.data[request]
|
return self.data[request]
|
||||||
|
|
||||||
def _save(self):
|
|
||||||
save_cassette(self._path, self._as_dict(), serializer=self._serializer)
|
|
||||||
|
|
||||||
def _as_dict(self):
|
def _as_dict(self):
|
||||||
return {"requests": self.requests, "responses": self.responses}
|
return {"requests": self.requests, "responses": self.responses}
|
||||||
|
|
||||||
|
def _save(self, force=False):
|
||||||
|
if force or self.dirty:
|
||||||
|
save_cassette(self._path, self._as_dict(), serializer=self._serializer)
|
||||||
|
self.dirty = False
|
||||||
|
|
||||||
def _load(self):
|
def _load(self):
|
||||||
try:
|
try:
|
||||||
requests, responses = load_cassette(self._path, serializer=self._serializer)
|
requests, responses = load_cassette(self._path, serializer=self._serializer)
|
||||||
for request, response in zip(requests, responses):
|
for request, response in zip(requests, responses):
|
||||||
self.append(request, response)
|
self.append(request, response)
|
||||||
|
self.dirty = False
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user