1
0
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:
shu zOMG chen
2013-08-21 14:27:29 -10:00
committed by Kevin McCarthy
parent 7264780960
commit b55834e929
3 changed files with 57 additions and 7 deletions

View File

@@ -51,11 +51,12 @@ def test_patched_content(tmpdir):
assert cass.play_count == 0
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
cass._save(force=True)
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 response == response2
@@ -68,11 +69,12 @@ def test_patched_content_json(tmpdir):
assert cass.play_count == 0
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
cass._save(force=True)
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 response == response2

View 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

View File

@@ -25,6 +25,7 @@ class Cassette(object):
self._serializer = serializer
self.data = OrderedDict()
self.play_counts = Counter()
self.dirty = False
@property
def play_count(self):
@@ -47,22 +48,26 @@ class Cassette(object):
def append(self, request, response):
'''Add a request, response pair to this cassette'''
self.data[request] = response
self.dirty = True
def response_of(self, request):
'''Find the response corresponding to a request'''
return self.data[request]
def _save(self):
save_cassette(self._path, self._as_dict(), serializer=self._serializer)
def _as_dict(self):
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):
try:
requests, responses = load_cassette(self._path, serializer=self._serializer)
for request, response in zip(requests, responses):
self.append(request, response)
self.dirty = False
except IOError:
pass