]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
replace cached() with mark_played / play_count
authorKevin McCarthy <me@kevinmccarthy.org>
Tue, 6 Aug 2013 08:25:45 +0000 (22:25 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Mon, 12 Aug 2013 01:05:40 +0000 (15:05 -1000)
tests/integration/test_basic.py
tests/integration/test_requests.py
tests/integration/test_urllib2.py
tests/unit/test_cassettes.py
vcr/cassette.py
vcr/stubs/__init__.py

index dc3382c64e3ca6ed771fba91d0663895cc9b5767..3c178a871a0c6e88c8cfd497702203af87646aed 100644 (file)
@@ -31,4 +31,4 @@ class TestCassette(TestVCR):
         # Make the same requests, and assert that we haven't served any more
         # requests out of cache
         urllib2.urlopen('http://httpbin.org/').read()
-        self.assertEqual(len(cass.cached()), 0)
+        self.assertEqual(cass.play_count, 0)
index 4c32bc1846333875e2e7c133e6617540c03c4b79..b3c0233372410550de1c7cb2be3b15a70e4a22dc 100644 (file)
@@ -26,13 +26,13 @@ class TestHTTPRequests(TestRequestsBase):
         with vcr.use_cassette(self.fixture('atts.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 requests.get(url).status_code,
                 requests.get(url).status_code)
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
     def test_headers(self):
         '''Ensure that we can read the headers back'''
@@ -40,13 +40,13 @@ class TestHTTPRequests(TestRequestsBase):
         with vcr.use_cassette(self.fixture('headers.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 requests.get(url).headers,
                 requests.get(url).headers)
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
     def test_body(self):
         '''Ensure the responses are all identical enough'''
@@ -54,13 +54,13 @@ class TestHTTPRequests(TestRequestsBase):
         with vcr.use_cassette(self.fixture('body.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 requests.get(url).content,
                 requests.get(url).content)
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
     def test_auth(self):
         '''Ensure that we can handle basic auth'''
@@ -69,14 +69,14 @@ class TestHTTPRequests(TestRequestsBase):
         with vcr.use_cassette(self.fixture('auth.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             one = requests.get(url, auth=auth)
             two = requests.get(url, auth=auth)
             self.assertEqual(one.content, two.content)
             self.assertEqual(one.status_code, two.status_code)
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
     def test_auth_failed(self):
         '''Ensure that we can save failed auth statuses'''
@@ -85,7 +85,7 @@ class TestHTTPRequests(TestRequestsBase):
         with vcr.use_cassette(self.fixture('auth-failed.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             one = requests.get(url, auth=auth)
             two = requests.get(url, auth=auth)
             self.assertEqual(one.content, two.content)
@@ -93,7 +93,7 @@ class TestHTTPRequests(TestRequestsBase):
             self.assertNotEqual(one.status_code, 200)
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
     def test_post(self):
         '''Ensure that we can post and cache the results'''
@@ -102,13 +102,13 @@ class TestHTTPRequests(TestRequestsBase):
         with vcr.use_cassette(self.fixture('redirect.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 requests.post(url, data).content,
                 requests.post(url, data).content)
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
     def test_redirects(self):
         '''Ensure that we can handle redirects'''
@@ -116,14 +116,14 @@ class TestHTTPRequests(TestRequestsBase):
         with vcr.use_cassette(self.fixture('redirect.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 requests.get(url).content,
                 requests.get(url).content)
             # Ensure that we've now cached /two/ responses. One for the redirect
             # and one for the final fetch
             self.assertEqual(len(cass), 2)
-            self.assertEqual(len(cass.cached()), 2)
+            self.assertEqual(cass.play_count, 2)
 
 
 class TestHTTPSRequests(TestHTTPRequests):
@@ -139,7 +139,7 @@ class TestHTTPSRequests(TestHTTPRequests):
             requests.get('https://httpbin.org/')
             requests.get('http://httpbin.org/')
             self.assertEqual(len(cass), 2)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
 
 
 class TestWild(TestRequestsBase):
@@ -160,4 +160,4 @@ class TestWild(TestRequestsBase):
             requests.get(url, headers={'User-Agent': 'vcrpy-test'})
             # Ensure that we've now served two responses. One for the original
             # redirect, and a second for the actual fetch
-            self.assertEqual(len(cass.cached()), 2)
+            self.assertEqual(cass.play_count, 2)
index f930333133c983a0a53e92202158ef5d52ef8455..ac8a6c07ad78cd215f01103fa683c83df5b22c46 100644 (file)
@@ -26,13 +26,13 @@ class TestUrllib2Http(TestUrllib2):
         with vcr.use_cassette(self.fixture('atts.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 urllib2.urlopen(url).getcode(),
                 urllib2.urlopen(url).getcode())
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
     def test_random_body(self):
         '''Ensure we can read the content, and that it's served from cache'''
@@ -40,13 +40,13 @@ class TestUrllib2Http(TestUrllib2):
         with vcr.use_cassette(self.fixture('body.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 urllib2.urlopen(url).read(),
                 urllib2.urlopen(url).read())
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
     def test_response_headers(self):
         '''Ensure we can get information from the response'''
@@ -54,7 +54,7 @@ class TestUrllib2Http(TestUrllib2):
         with vcr.use_cassette(self.fixture('headers.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 urllib2.urlopen(url).info().items(),
                 urllib2.urlopen(url).info().items())
@@ -72,12 +72,12 @@ class TestUrllib2Http(TestUrllib2):
             for index in range(len(urls)):
                 url = urls[index]
                 self.assertEqual(len(cass), index)
-                self.assertEqual(len(cass.cached()), index)
+                self.assertEqual(cass.play_count, index)
                 self.assertEqual(
                     urllib2.urlopen(url).read(),
                     urllib2.urlopen(url).read())
                 self.assertEqual(len(cass), index + 1)
-                self.assertEqual(len(cass.cached()), index + 1)
+                self.assertEqual(cass.play_count, index + 1)
 
     def test_get_data(self):
         '''Ensure that it works with query data'''
@@ -86,13 +86,13 @@ class TestUrllib2Http(TestUrllib2):
         with vcr.use_cassette(self.fixture('get_data.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 urllib2.urlopen(url).read(),
                 urllib2.urlopen(url).read())
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
     def test_post_data(self):
         '''Ensure that it works when posting data'''
@@ -101,13 +101,13 @@ class TestUrllib2Http(TestUrllib2):
         with vcr.use_cassette(self.fixture('post_data.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 urllib2.urlopen(url, data).read(),
                 urllib2.urlopen(url, data).read())
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
     def test_post_unicode_data(self):
         '''Ensure that it works when posting unicode data'''
@@ -116,13 +116,13 @@ class TestUrllib2Http(TestUrllib2):
         with vcr.use_cassette(self.fixture('post_data.yaml')) as cass:
             # Ensure that this is empty to begin with
             self.assertEqual(len(cass), 0)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
             self.assertEqual(
                 urllib2.urlopen(url, data).read(),
                 urllib2.urlopen(url, data).read())
             # Ensure that we've now cached a single response
             self.assertEqual(len(cass), 1)
-            self.assertEqual(len(cass.cached()), 1)
+            self.assertEqual(cass.play_count, 1)
 
 
 class TestUrllib2Https(TestUrllib2Http):
@@ -138,4 +138,4 @@ class TestUrllib2Https(TestUrllib2Http):
             urllib2.urlopen('https://httpbin.org/')
             urllib2.urlopen('http://httpbin.org/')
             self.assertEqual(len(cass), 2)
-            self.assertEqual(len(cass.cached()), 0)
+            self.assertEqual(cass.play_count, 0)
index 54bf222e9286ee79b2359ba21301e03f155f8c09..18104b687302605229e9bed9726554fcfa88a306 100644 (file)
@@ -23,14 +23,14 @@ def test_cassette_deserialize():
     assert a._requests == ['foo']
     assert a._responses == ['bar']
 
-def test_cassette_not_cached():
+def test_cassette_not_played():
     a = Cassette('test')
-    assert not a.cached()
+    assert not a.play_count
 
-def test_cassette_cached():
+def test_cassette_played():
     a = Cassette('test')
-    a.cached('yup')
-    assert a.cached()
+    a.mark_played()
+    assert a.play_count == 1
 
 def test_cassette_append():
     a = Cassette('test')
index 3c25e066dfb1712b72b38325b3c88afc6ef27de4..67634c1a2e6bcfa428e078e872482b5387c07956 100644 (file)
@@ -23,9 +23,9 @@ class Cassette(object):
 
     def __init__(self, path, data=None):
         self._path = path
-        self._cached = []
         self._requests = []
         self._responses = []
+        self.play_count = 0
         if data:
             self.deserialize(data)
 
@@ -45,18 +45,11 @@ class Cassette(object):
         self._requests, self._responses = (
             [r['request'] for r in source], [r['response'] for r in source])
 
-    def cached(self, request=None):
+    def mark_played(self, request=None):
         '''
-        Alert the cassette of a request that's been cached, or get the
-        requests that we've cached.  This is used mainly for 
-        debugging purposes.
+        Alert the cassette of a request that's been played
         '''
-        # TODO: maybe dependency injection for this method since
-        # it's only used in tests?
-        if request:
-            self._cached.append(request)
-        else:
-            return self._cached
+        self.play_count += 1
 
     def append(self, request, response):
         '''Add a pair of request, response pairs to this cassette'''
index 955c66b486c467d604a359cfde0580205b77ed80..3c93677ec0e182222213e6b387df5c66940f41ed 100644 (file)
@@ -67,9 +67,9 @@ class VCRConnectionMixin:
         # then return it
         response = self.cassette.response(self._request)
         if response:
-            # Alert the cassette to the fact that we've served another cached
+            # Alert the cassette to the fact that we've served another
             # response for the provided requests
-            self.cassette.cached(self._request)
+            self.cassette.mark_played()
             return VCRHTTPResponse(response)
 
         # Otherwise, we made an actual request, and should return the response