]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Don't save cassette when it hasn't been modified
authorshu zOMG chen <shu.chen@freelancedreams.com>
Thu, 22 Aug 2013 00:27:29 +0000 (14:27 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Sat, 24 Aug 2013 06:35:42 +0000 (20:35 -1000)
tests/integration/test_basic.py
tests/integration/test_disksaver.py [new file with mode: 0644]
vcr/cassette.py

index 250eea68d3aba62d307aef15148776eb635f96e9..d7bdb09715a81b6fbfa9bb40da33dbfd671e2d0c 100644 (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
diff --git a/tests/integration/test_disksaver.py b/tests/integration/test_disksaver.py
new file mode 100644 (file)
index 0000000..bc19fc1
--- /dev/null
@@ -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
+
index 87c9ed3c2c53aaabeb1d6fdb0388957ab8a55a4b..84acc806f836931172aa55a7ec95ae163911e927 100644 (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