Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use importlib_resources to find test fixtures #588

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,15 @@ def run_tests(self):
packages=find_packages(exclude=["tests*"]),
python_requires=">=3.5",
install_requires=install_requires,
extras_require={
"tests": [
"pytest",
"mock",
"pytest-httpbin",
"importlib-resources"
],
},
license="MIT",
tests_require=["pytest", "mock", "pytest-httpbin"],
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
Expand Down
Empty file added tests/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion tests/integration/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from urllib.error import HTTPError
import vcr
import json
from assertions import assert_cassette_has_one_response, assert_is_json
from ..assertions import assert_cassette_has_one_response, assert_is_json


def _request_with_auth(url, username, password):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_httplib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import vcr

from assertions import assert_cassette_has_one_response
from ..assertions import assert_cassette_has_one_response

httplib2 = pytest.importorskip("httplib2")

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
import sys
import vcr
from assertions import assert_cassette_empty, assert_is_json
from ..assertions import assert_cassette_empty, assert_is_json

requests = pytest.importorskip("requests")
from requests.exceptions import ConnectionError # noqa E402
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import http.client as httplib

from assertions import assert_is_json
from ..assertions import assert_is_json


def _headers_are_case_insensitive(host, port):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import vcr
from vcr.errors import CannotOverwriteExistingCassetteException

from assertions import assert_cassette_empty, assert_is_json
from ..assertions import assert_cassette_empty, assert_is_json

tornado = pytest.importorskip("tornado")
http = pytest.importorskip("tornado.httpclient")
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Internal imports
import vcr

from assertions import assert_cassette_has_one_response
from ..assertions import assert_cassette_has_one_response


def urlopen_with_cafile(*args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest_httpbin
import vcr
from vcr.patch import force_reset
from assertions import assert_cassette_empty, assert_is_json
from ..assertions import assert_cassette_empty, assert_is_json

urllib3 = pytest.importorskip("urllib3")

Expand Down
7 changes: 6 additions & 1 deletion tests/integration/test_wild.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
from xmlrpc.client import ServerProxy
from xmlrpc.server import SimpleXMLRPCServer

from ..fixtures import wild
import importlib_resources

requests = pytest.importorskip("requests")

import vcr # NOQA

wild_fixtures = importlib_resources.files(wild)


def test_domain_redirect():
"""Ensure that redirects across domains are considered unique"""
# In this example, seomoz.org redirects to moz.com, and if those
# requests are considered identical, then we'll be stuck in a redirect
# loop.
url = "http://seomoz.org/"
with vcr.use_cassette("tests/fixtures/wild/domain_redirect.yaml") as cass:
with vcr.use_cassette(wild_fixtures / "domain_redirect.yaml") as cass:
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
Expand Down
Empty file added tests/unit/__init__.py
Empty file.
18 changes: 11 additions & 7 deletions tests/unit/test_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@

import vcr.migration

from ..fixtures import migration
import importlib_resources

# Use the libYAML versions if possible
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader

migration_fixtures = importlib_resources.files(migration)

def test_try_migrate_with_json(tmpdir):
cassette = tmpdir.join("cassette.json").strpath
shutil.copy("tests/fixtures/migration/old_cassette.json", cassette)
shutil.copy(migration_fixtures / "old_cassette.json", cassette)
assert vcr.migration.try_migrate(cassette)
with open("tests/fixtures/migration/new_cassette.json", "r") as f:
with open(migration_fixtures / "new_cassette.json", "r") as f:
expected_json = json.load(f)
with open(cassette, "r") as f:
actual_json = json.load(f)
Expand All @@ -25,9 +29,9 @@ def test_try_migrate_with_json(tmpdir):

def test_try_migrate_with_yaml(tmpdir):
cassette = tmpdir.join("cassette.yaml").strpath
shutil.copy("tests/fixtures/migration/old_cassette.yaml", cassette)
shutil.copy(migration_fixtures / "old_cassette.yaml", cassette)
assert vcr.migration.try_migrate(cassette)
with open("tests/fixtures/migration/new_cassette.yaml", "r") as f:
with open(migration_fixtures / "new_cassette.yaml", "r") as f:
expected_yaml = yaml.load(f, Loader=Loader)
with open(cassette, "r") as f:
actual_yaml = yaml.load(f, Loader=Loader)
Expand All @@ -37,9 +41,9 @@ def test_try_migrate_with_yaml(tmpdir):
def test_try_migrate_with_invalid_or_new_cassettes(tmpdir):
cassette = tmpdir.join("cassette").strpath
files = [
"tests/fixtures/migration/not_cassette.txt",
"tests/fixtures/migration/new_cassette.yaml",
"tests/fixtures/migration/new_cassette.json",
migration_fixtures / "not_cassette.txt",
migration_fixtures / "new_cassette.yaml",
migration_fixtures / "new_cassette.json",
]
for file_path in files:
shutil.copy(file_path, cassette)
Expand Down
16 changes: 10 additions & 6 deletions tests/unit/test_persist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@
from vcr.persisters.filesystem import FilesystemPersister
from vcr.serializers import jsonserializer, yamlserializer

from ..fixtures import migration
import importlib_resources

migration_fixtures = importlib_resources.files(migration)

@pytest.mark.parametrize(
"cassette_path, serializer",
[
("tests/fixtures/migration/old_cassette.json", jsonserializer),
("tests/fixtures/migration/old_cassette.yaml", yamlserializer),
("old_cassette.json", jsonserializer),
("old_cassette.yaml", yamlserializer),
],
)
def test_load_cassette_with_old_cassettes(cassette_path, serializer):
with pytest.raises(ValueError) as excinfo:
FilesystemPersister.load_cassette(cassette_path, serializer)
FilesystemPersister.load_cassette(migration_fixtures / cassette_path, serializer)
assert "run the migration script" in excinfo.exconly()


@pytest.mark.parametrize(
"cassette_path, serializer",
[
("tests/fixtures/migration/not_cassette.txt", jsonserializer),
("tests/fixtures/migration/not_cassette.txt", yamlserializer),
("not_cassette.txt", jsonserializer),
("not_cassette.txt", yamlserializer),
],
)
def test_load_cassette_with_invalid_cassettes(cassette_path, serializer):
with pytest.raises(Exception) as excinfo:
FilesystemPersister.load_cassette(cassette_path, serializer)
FilesystemPersister.load_cassette(migration_fixtures / cassette_path, serializer)
assert "run the migration script" not in excinfo.exconly()
13 changes: 9 additions & 4 deletions tests/unit/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,31 @@
from vcr.serialize import deserialize, serialize
from vcr.serializers import yamlserializer, jsonserializer, compat

from ..fixtures import migration
import importlib_resources

migration_fixtures = importlib_resources.files(migration)


def test_deserialize_old_yaml_cassette():
with open("tests/fixtures/migration/old_cassette.yaml", "r") as f:
with open(migration_fixtures / "old_cassette.yaml", "r") as f:
with pytest.raises(ValueError):
deserialize(f.read(), yamlserializer)


def test_deserialize_old_json_cassette():
with open("tests/fixtures/migration/old_cassette.json", "r") as f:
with open(migration_fixtures / "old_cassette.json", "r") as f:
with pytest.raises(ValueError):
deserialize(f.read(), jsonserializer)


def test_deserialize_new_yaml_cassette():
with open("tests/fixtures/migration/new_cassette.yaml", "r") as f:
with open(migration_fixtures / "new_cassette.yaml", "r") as f:
deserialize(f.read(), yamlserializer)


def test_deserialize_new_json_cassette():
with open("tests/fixtures/migration/new_cassette.json", "r") as f:
with open(migration_fixtures / "new_cassette.json", "r") as f:
deserialize(f.read(), jsonserializer)


Expand Down