예제 #1
0
def test_nesting_cassette_context_managers(*args):
    first_response = {
        "body": {
            "string": b"first_response"
        },
        "headers": {},
        "status": {
            "message": "m",
            "code": 200
        },
    }

    second_response = copy.deepcopy(first_response)
    second_response["body"]["string"] = b"second_response"

    with contextlib.ExitStack() as exit_stack:
        first_cassette = exit_stack.enter_context(Cassette.use(path="test"))
        exit_stack.enter_context(
            mock.patch.object(first_cassette,
                              "play_response",
                              return_value=first_response))
        assert_get_response_body_is("first_response")

        # Make sure a second cassette can supercede the first
        with Cassette.use(path="test") as second_cassette:
            with mock.patch.object(second_cassette,
                                   "play_response",
                                   return_value=second_response):
                assert_get_response_body_is("second_response")

        # Now the first cassette should be back in effect
        assert_get_response_body_is("first_response")
예제 #2
0
def test_nesting_cassette_context_managers(*args):
    first_response = {
        'body': {
            'string': b'first_response'
        },
        'headers': {},
        'status': {
            'message': 'm',
            'code': 200
        }
    }

    second_response = copy.deepcopy(first_response)
    second_response['body']['string'] = b'second_response'

    with contextlib2.ExitStack() as exit_stack:
        first_cassette = exit_stack.enter_context(Cassette.use('test'))
        exit_stack.enter_context(
            mock.patch.object(first_cassette,
                              'play_response',
                              return_value=first_response))
        assert_get_response_body_is('first_response')

        # Make sure a second cassette can supercede the first
        with Cassette.use('test') as second_cassette:
            with mock.patch.object(second_cassette,
                                   'play_response',
                                   return_value=second_response):
                assert_get_response_body_is('second_response')

        # Now the first cassette should be back in effect
        assert_get_response_body_is('first_response')
예제 #3
0
def test_nesting_context_managers_by_checking_references_of_http_connection():
    original = httplib.HTTPConnection
    with Cassette.use(path="test"):
        first_cassette_HTTPConnection = httplib.HTTPConnection
        with Cassette.use(path="test"):
            second_cassette_HTTPConnection = httplib.HTTPConnection
            assert second_cassette_HTTPConnection is not first_cassette_HTTPConnection
            with Cassette.use(path="test"):
                assert httplib.HTTPConnection is not second_cassette_HTTPConnection
                with force_reset():
                    assert httplib.HTTPConnection is original
            assert httplib.HTTPConnection is second_cassette_HTTPConnection
        assert httplib.HTTPConnection is first_cassette_HTTPConnection
예제 #4
0
def test_nesting_context_managers_by_checking_references_of_http_connection():
    original = httplib.HTTPConnection
    with Cassette.use(path='test'):
        first_cassette_HTTPConnection = httplib.HTTPConnection
        with Cassette.use(path='test'):
            second_cassette_HTTPConnection = httplib.HTTPConnection
            assert second_cassette_HTTPConnection is not first_cassette_HTTPConnection
            with Cassette.use(path='test'):
                assert httplib.HTTPConnection is not second_cassette_HTTPConnection
                with force_reset():
                    assert httplib.HTTPConnection is original
            assert httplib.HTTPConnection is second_cassette_HTTPConnection
        assert httplib.HTTPConnection is first_cassette_HTTPConnection
예제 #5
0
def test_custom_patchers():
    class Test(object):
        attribute = None

    with Cassette.use(path="custom_patches", custom_patches=((Test, "attribute", VCRHTTPSConnection),)):
        assert issubclass(Test.attribute, VCRHTTPSConnection)
        assert VCRHTTPSConnection is not Test.attribute
        old_attribute = Test.attribute

        with Cassette.use(path="custom_patches", custom_patches=((Test, "attribute", VCRHTTPSConnection),)):
            assert issubclass(Test.attribute, VCRHTTPSConnection)
            assert VCRHTTPSConnection is not Test.attribute
            assert Test.attribute is not old_attribute

        assert issubclass(Test.attribute, VCRHTTPSConnection)
        assert VCRHTTPSConnection is not Test.attribute
        assert Test.attribute is old_attribute
예제 #6
0
def test_custom_patchers():
    class Test(object):
        attribute = None
    with Cassette.use(path='custom_patches',
                      custom_patches=((Test, 'attribute', VCRHTTPSConnection),)):
        assert issubclass(Test.attribute, VCRHTTPSConnection)
        assert VCRHTTPSConnection is not Test.attribute
        old_attribute = Test.attribute

        with Cassette.use(path='custom_patches',
                          custom_patches=((Test, 'attribute', VCRHTTPSConnection),)):
            assert issubclass(Test.attribute, VCRHTTPSConnection)
            assert VCRHTTPSConnection is not Test.attribute
            assert Test.attribute is not old_attribute

        assert issubclass(Test.attribute, VCRHTTPSConnection)
        assert VCRHTTPSConnection is not Test.attribute
        assert Test.attribute is old_attribute
예제 #7
0
def test_nesting_cassette_context_managers(*args):
    first_response = {"body": {"string": b"first_response"}, "headers": {}, "status": {"message": "m", "code": 200}}

    second_response = copy.deepcopy(first_response)
    second_response["body"]["string"] = b"second_response"

    with contextlib.ExitStack() as exit_stack:
        first_cassette = exit_stack.enter_context(Cassette.use(path="test"))
        exit_stack.enter_context(mock.patch.object(first_cassette, "play_response", return_value=first_response))
        assert_get_response_body_is("first_response")

        # Make sure a second cassette can supercede the first
        with Cassette.use(path="test") as second_cassette:
            with mock.patch.object(second_cassette, "play_response", return_value=second_response):
                assert_get_response_body_is("second_response")

        # Now the first cassette should be back in effect
        assert_get_response_body_is("first_response")
예제 #8
0
def test_nesting_cassette_context_managers(*args):
    first_response = {'body': {'string': b'first_response'}, 'headers': {},
                      'status': {'message': 'm', 'code': 200}}

    second_response = copy.deepcopy(first_response)
    second_response['body']['string'] = b'second_response'

    with contextlib.ExitStack() as exit_stack:
        first_cassette = exit_stack.enter_context(Cassette.use(path='test'))
        exit_stack.enter_context(mock.patch.object(first_cassette, 'play_response',
                                                    return_value=first_response))
        assert_get_response_body_is('first_response')

        # Make sure a second cassette can supercede the first
        with Cassette.use(path='test') as second_cassette:
            with mock.patch.object(second_cassette, 'play_response', return_value=second_response):
                assert_get_response_body_is('second_response')

        # Now the first cassette should be back in effect
        assert_get_response_body_is('first_response')
예제 #9
0
def test_function_decorated_with_use_cassette_can_be_invoked_multiple_times(
        *args):
    decorated_function = Cassette.use(path="test")(make_get_request)
    for i in range(4):
        decorated_function()
예제 #10
0
def test_path_transformer_None():
    with Cassette.use(path="a", path_transformer=None) as cassette:
        assert cassette._path == "a"
예제 #11
0
def test_path_transformer_with_context_manager():
    with Cassette.use(path="b",
                      path_transformer=lambda *args: "a") as cassette:
        assert cassette._path == "a"
예제 #12
0
def test_function_decorated_with_use_cassette_can_be_invoked_multiple_times(*args):
    decorated_function = Cassette.use(path='test')(make_get_request)
    for i in range(4):
         decorated_function()
예제 #13
0
def test_path_transformer_with_context_manager():
    with Cassette.use(
        path='b', path_transformer=lambda *args: 'a'
    ) as cassette:
        assert cassette._path == 'a'
예제 #14
0
def test_path_transformer_with_context_manager():
    with Cassette.use(path='b',
                      path_transformer=lambda *args: 'a') as cassette:
        assert cassette._path == 'a'
예제 #15
0
def test_path_transformer_None():
    with Cassette.use(
        path='a', path_transformer=None,
    ) as cassette:
        assert cassette._path == 'a'
예제 #16
0
def test_path_transformer_with_context_manager():
    with Cassette.use(path="b", path_transformer=lambda *args: "a") as cassette:
        assert cassette._path == "a"