mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
hold counts in a Counter
This commit is contained in:
@@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
# Internal imports
|
# Internal imports
|
||||||
from .patch import install, reset
|
from .patch import install, reset
|
||||||
from .files import load_cassette, save_cassette
|
from .files import load_cassette, save_cassette
|
||||||
from .request import Request
|
from .request import Request
|
||||||
|
|
||||||
|
|
||||||
class Cassette(object):
|
class Cassette(object):
|
||||||
'''A container for recorded requests and responses'''
|
'''A container for recorded requests and responses'''
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -22,7 +22,7 @@ class Cassette(object):
|
|||||||
def __init__(self, path, data=None):
|
def __init__(self, path, data=None):
|
||||||
self._path = path
|
self._path = path
|
||||||
self.requests = {}
|
self.requests = {}
|
||||||
self.play_count = 0
|
self.play_counts = Counter()
|
||||||
if data:
|
if data:
|
||||||
self.deserialize(data)
|
self.deserialize(data)
|
||||||
|
|
||||||
@@ -42,16 +42,24 @@ class Cassette(object):
|
|||||||
for r in source:
|
for r in source:
|
||||||
self.requests[r['request']] = r['response']
|
self.requests[r['request']] = r['response']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def play_count(self):
|
||||||
|
return sum(self.play_counts.values())
|
||||||
|
|
||||||
def mark_played(self, request=None):
|
def mark_played(self, request=None):
|
||||||
'''
|
'''
|
||||||
Alert the cassette of a request that's been played
|
Alert the cassette of a request that's been played
|
||||||
'''
|
'''
|
||||||
self.play_count += 1
|
self.play_counts[request] += 1
|
||||||
|
|
||||||
def append(self, request, response):
|
def append(self, request, response):
|
||||||
'''Add a pair of request, response to this cassette'''
|
'''Add a pair of request, response to this cassette'''
|
||||||
self.requests[request] = response
|
self.requests[request] = response
|
||||||
|
|
||||||
|
def response(self, request):
|
||||||
|
'''Find the response corresponding to a request'''
|
||||||
|
return self.requests[request]
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
'''Return the number of request / response pairs stored in here'''
|
'''Return the number of request / response pairs stored in here'''
|
||||||
return len(self.requests)
|
return len(self.requests)
|
||||||
@@ -60,10 +68,6 @@ class Cassette(object):
|
|||||||
'''Return whether or not a request has been stored'''
|
'''Return whether or not a request has been stored'''
|
||||||
return request in self.requests
|
return request in self.requests
|
||||||
|
|
||||||
def response(self, request):
|
|
||||||
'''Find the response corresponding to a request'''
|
|
||||||
return self.requests[request]
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
'''Patch the fetching libraries we know about'''
|
'''Patch the fetching libraries we know about'''
|
||||||
install(self)
|
install(self)
|
||||||
|
|||||||
@@ -16,3 +16,6 @@ class Request(object):
|
|||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return hash(self) == hash(other)
|
return hash(self) == hash(other)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "<Request ({0}) {1}>".format(self.method, self.body)
|
||||||
|
|||||||
Reference in New Issue
Block a user