diff --git a/vcr/cassette.py b/vcr/cassette.py index 0daaa50..e5c237f 100644 --- a/vcr/cassette.py +++ b/vcr/cassette.py @@ -2,13 +2,13 @@ import os import tempfile +from collections import Counter # Internal imports from .patch import install, reset from .files import load_cassette, save_cassette from .request import Request - class Cassette(object): '''A container for recorded requests and responses''' @classmethod @@ -22,7 +22,7 @@ class Cassette(object): def __init__(self, path, data=None): self._path = path self.requests = {} - self.play_count = 0 + self.play_counts = Counter() if data: self.deserialize(data) @@ -42,16 +42,24 @@ class Cassette(object): for r in source: self.requests[r['request']] = r['response'] + @property + def play_count(self): + return sum(self.play_counts.values()) + def mark_played(self, request=None): ''' Alert the cassette of a request that's been played ''' - self.play_count += 1 + self.play_counts[request] += 1 def append(self, request, response): '''Add a pair of request, response to this cassette''' self.requests[request] = response + def response(self, request): + '''Find the response corresponding to a request''' + return self.requests[request] + def __len__(self): '''Return the number of request / response pairs stored in here''' return len(self.requests) @@ -60,10 +68,6 @@ class Cassette(object): '''Return whether or not a request has been stored''' return request in self.requests - def response(self, request): - '''Find the response corresponding to a request''' - return self.requests[request] - def __enter__(self): '''Patch the fetching libraries we know about''' install(self) diff --git a/vcr/request.py b/vcr/request.py index 632c8b2..dc48692 100644 --- a/vcr/request.py +++ b/vcr/request.py @@ -16,3 +16,6 @@ class Request(object): def __eq__(self, other): return hash(self) == hash(other) + + def __str__(self): + return "".format(self.method, self.body)