<commit_before>'''Basic tests about cassettes'''
# coding=utf-8

# External imports
import os
import urllib2

# Internal imports
import vcr

def test_nonexistent_directory(tmpdir):
    '''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'))):
        urllib2.urlopen('http://httpbin.org/').read()

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

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

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

def test_basic_use(tmpdir):
    '''
    Copied from the docs 
    '''
    with vcr.use_cassette('fixtures/vcr_cassettes/synopsis.yaml'):
        response = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
        assert 'Example domains' in response

def test_basic_json_use(tmpdir):
    '''Ensure you can load a json serialized cassette'''
    with vcr.use_cassette('fixtures/vcr_cassettes/synopsis.json', serializer='json'):
        response = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
        assert 'Example domains' in response<commit_msg>Added test to confirm whether cStringIO works<commit_after>'''Basic tests about cassettes'''
# coding=utf-8

# External imports
import os
import urllib2

# Internal imports
import vcr

def test_nonexistent_directory(tmpdir):
    '''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'))):
        urllib2.urlopen('http://httpbin.org/').read()

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

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

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

def test_basic_use(tmpdir):
    '''
    Copied from the docs 
    '''
    with vcr.use_cassette('fixtures/vcr_cassettes/synopsis.yaml'):
        response = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
        assert 'Example domains' in response

def test_basic_json_use(tmpdir):
    '''Ensure you can load a json serialized cassette'''
    with vcr.use_cassette('fixtures/vcr_cassettes/synopsis.json', serializer='json'):
        response = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
        assert 'Example domains' in response

def test_patched_content(tmpdir):
    '''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 = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
        assert cass.play_count == 0

    with vcr.use_cassette(str(tmpdir.join('synopsis.yaml'))) as cass:
        response2= urllib2.urlopen('http://www.iana.org/domains/reserved').read()
        assert cass.play_count == 1

    with vcr.use_cassette(str(tmpdir.join('synopsis.yaml'))) as cass:
        response3= urllib2.urlopen('http://www.iana.org/domains/reserved').read()
        assert cass.play_count == 1

    assert response == response2
    assert response2 == response3

def test_patched_content_json(tmpdir):
    '''Ensure that what you pull from a json cassette is what came from the request'''
    with vcr.use_cassette(str(tmpdir.join('synopsis.json')), serializer='json') as cass:
        response = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
        assert cass.play_count == 0

    with vcr.use_cassette(str(tmpdir.join('synopsis.json')), serializer='json') as cass:
        response2= urllib2.urlopen('http://www.iana.org/domains/reserved').read()
        assert cass.play_count == 1

    with vcr.use_cassette(str(tmpdir.join('synopsis.json')), serializer='json') as cass:
        response3= urllib2.urlopen('http://www.iana.org/domains/reserved').read()
        assert cass.play_count == 1

    assert response == response2
    assert response2 == response3
{'fraction_non_alphanumeric': 0.13211009174311927, 'fraction_numerical': 0.007747196738022426, 'mean_word_length': 3.823992133726647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5877438', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1558, 'n_words': 373}