Skip to content

Commit

Permalink
Generic vcr function (#26)
Browse files Browse the repository at this point in the history
* add generic VCR function

* version, changelog
  • Loading branch information
jonny-onna authored Jan 17, 2020
1 parent ee7e81c commit 1f09e55
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

2.1.0
-----

- add generic VCR function to help with test implementation

2.0.3
-----

Expand Down
2 changes: 0 additions & 2 deletions cassettedeck/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@


class CassetteDeck:
"""This is the object to use as a fixture in tests.
"""

def __init__(self, cassette_library_dir=None, ignore_localhost=False,
ignore_hosts=(), mode='once', mocked_services=None):
Expand Down
20 changes: 20 additions & 0 deletions cassettedeck/tests/test_vcr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os

import aiohttp
from aiounittest import AsyncTestCase

from cassettedeck.vcr import vcr


class TestVCR(AsyncTestCase):

async def test_should_store_cassette_at_expected_location(self):
expected_path = os.path.join(os.path.dirname(__file__), "cassettes", f"{self.id()}.yaml")
self.assertFalse(os.path.exists(expected_path))

with vcr(self):
async with aiohttp.request("GET", "https://example.com") as response:
response.raise_for_status()

self.assertTrue(os.path.isfile(expected_path))
os.remove(expected_path)
18 changes: 18 additions & 0 deletions cassettedeck/vcr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import inspect
import os
from contextlib import contextmanager
from unittest import TestCase

from cassettedeck import CassetteDeck


@contextmanager
def vcr(test_case: TestCase):
"""
Test utility which allows cassettes to be stored with a unique name near their point of use.
"""
test_file = inspect.getfile(type(test_case))
library = os.path.join(os.path.dirname(test_file), "cassettes")
my_vcr = CassetteDeck(cassette_library_dir=library, ignore_localhost=True)
with my_vcr.use_cassette(f"{test_case.id()}.yaml", mode="once") as cassette:
yield cassette
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='cassettedeck',
version='2.0.3',
version='2.1.0',
description='A library store and replay aiohttp requests',
long_description='To simplify and speed up tests that make HTTP requests',
author='Developer team at Onna Technologies',
Expand All @@ -25,6 +25,7 @@
packages=find_packages(),
install_requires=[
'aiohttp',
"aiounittest",
'vcrpy==1.12.0',
'pyyaml==4.2b4',
]
Expand Down

0 comments on commit 1f09e55

Please sign in to comment.