File: test_config.py

package info (click to toggle)
vcr.py 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,060 kB
  • sloc: python: 6,264; makefile: 188; sh: 1
file content (87 lines) | stat: -rw-r--r-- 2,510 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import json
import os
from urllib.request import urlopen

import pytest

import vcr


@pytest.mark.online
def test_set_serializer_default_config(tmpdir, httpbin):
    my_vcr = vcr.VCR(serializer="json")

    with my_vcr.use_cassette(str(tmpdir.join("test.json"))):
        assert my_vcr.serializer == "json"
        urlopen(httpbin.url)

    with open(str(tmpdir.join("test.json"))) as f:
        file_content = f.read()
        assert file_content.endswith("\n")
        assert json.loads(file_content)


@pytest.mark.online
def test_default_set_cassette_library_dir(tmpdir, httpbin):
    my_vcr = vcr.VCR(cassette_library_dir=str(tmpdir.join("subdir")))

    with my_vcr.use_cassette("test.json"):
        urlopen(httpbin.url)

    assert os.path.exists(str(tmpdir.join("subdir").join("test.json")))


@pytest.mark.online
def test_override_set_cassette_library_dir(tmpdir, httpbin):
    my_vcr = vcr.VCR(cassette_library_dir=str(tmpdir.join("subdir")))

    cld = str(tmpdir.join("subdir2"))

    with my_vcr.use_cassette("test.json", cassette_library_dir=cld):
        urlopen(httpbin.url)

    assert os.path.exists(str(tmpdir.join("subdir2").join("test.json")))
    assert not os.path.exists(str(tmpdir.join("subdir").join("test.json")))


@pytest.mark.online
def test_override_match_on(tmpdir, httpbin):
    my_vcr = vcr.VCR(match_on=["method"])

    with my_vcr.use_cassette(str(tmpdir.join("test.json"))):
        urlopen(httpbin.url)

    with my_vcr.use_cassette(str(tmpdir.join("test.json"))) as cass:
        urlopen(httpbin.url)

    assert len(cass) == 1
    assert cass.play_count == 1


def test_missing_matcher():
    my_vcr = vcr.VCR()
    my_vcr.register_matcher("awesome", object)
    with pytest.raises(KeyError):
        with my_vcr.use_cassette("test.yaml", match_on=["notawesome"]):
            pass


@pytest.mark.online
def test_dont_record_on_exception(tmpdir, httpbin):
    my_vcr = vcr.VCR(record_on_exception=False)

    @my_vcr.use_cassette(str(tmpdir.join("dontsave.yml")))
    def some_test():
        assert b"Not in content" in urlopen(httpbin.url)

    with pytest.raises(AssertionError):
        some_test()

    assert not os.path.exists(str(tmpdir.join("dontsave.yml")))

    # Make sure context decorator has the same behavior
    with pytest.raises(AssertionError):
        with my_vcr.use_cassette(str(tmpdir.join("dontsave2.yml"))):
            assert b"Not in content" in urlopen(httpbin.url).read()

    assert not os.path.exists(str(tmpdir.join("dontsave2.yml")))