File: test_basic.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (90 lines) | stat: -rw-r--r-- 2,775 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
88
89
90
# -*- coding: utf-8 -*-
"""Basic tests for cassettes"""

# External imports
import os
from six.moves.urllib.request import urlopen

# Internal imports
import vcr


def test_nonexistent_directory(tmpdir, httpbin):
    """If we load a cassette in a nonexistent directory, it can save ok"""
    # Check to make sure directory doesnt exist
    assert not os.path.exists(str(tmpdir.join("nonexistent")))

    # Run VCR to create dir and cassette file
    with vcr.use_cassette(str(tmpdir.join("nonexistent", "cassette.yml"))):
        urlopen(httpbin.url).read()

    # This should have made the file and the directory
    assert os.path.exists(str(tmpdir.join("nonexistent", "cassette.yml")))


def test_unpatch(tmpdir, httpbin):
    """Ensure that our cassette gets unpatched when we're done"""
    with vcr.use_cassette(str(tmpdir.join("unpatch.yaml"))) as cass:
        urlopen(httpbin.url).read()

    # Make the same request, and assert that we haven't served any more
    # requests out of cache
    urlopen(httpbin.url).read()
    assert cass.play_count == 0


def test_basic_json_use(tmpdir, httpbin):
    """
    Ensure you can load a json serialized cassette
    """
    test_fixture = str(tmpdir.join("synopsis.json"))
    with vcr.use_cassette(test_fixture, serializer="json"):
        response = urlopen(httpbin.url).read()
        assert b"difficult sometimes" in response


def test_patched_content(tmpdir, httpbin):
    """
    Ensure that what you pull from a cassette is what came from the
    request
    """
    with vcr.use_cassette(str(tmpdir.join("synopsis.yaml"))) as cass:
        response = urlopen(httpbin.url).read()
        assert cass.play_count == 0

    with vcr.use_cassette(str(tmpdir.join("synopsis.yaml"))) as cass:
        response2 = urlopen(httpbin.url).read()
        assert cass.play_count == 1
        cass._save(force=True)

    with vcr.use_cassette(str(tmpdir.join("synopsis.yaml"))) as cass:
        response3 = urlopen(httpbin.url).read()
        assert cass.play_count == 1

    assert response == response2
    assert response2 == response3


def test_patched_content_json(tmpdir, httpbin):
    """
    Ensure that what you pull from a json cassette is what came from the
    request
    """

    testfile = str(tmpdir.join("synopsis.json"))

    with vcr.use_cassette(testfile) as cass:
        response = urlopen(httpbin.url).read()
        assert cass.play_count == 0

    with vcr.use_cassette(testfile) as cass:
        response2 = urlopen(httpbin.url).read()
        assert cass.play_count == 1
        cass._save(force=True)

    with vcr.use_cassette(testfile) as cass:
        response3 = urlopen(httpbin.url).read()
        assert cass.play_count == 1

    assert response == response2
    assert response2 == response3