import nose import os import pandas as pd import vcr import geopandas as gpd from data_cleaner import DataCleaner from data_cleaner.data_cleaner import DuplicatedField from .rules.integration import rules import csv import sys sys.path.insert(0, '') BASE_DIR = os.path.dirname(__file__) VCR = vcr.VCR(path_transformer=vcr.VCR.ensure_suffix('.yaml'), cassette_library_dir=os.path.join("tests", "cassetes", "data_cleaner"), record_mode='once') def get_input(case_name): """Retorna el path al csv que se debe limpiar.""" file_name = "to_clean_" + case_name + ".csv" return os.path.join(os.path.join(BASE_DIR, "input"), file_name) def get_output(case_name): """Retorna el path al csv limpio que se espera.""" file_name = "clean_" + case_name + ".csv" return os.path.join(os.path.join(BASE_DIR, "output"), file_name)
def filter_out_whoami(response): body = response['body']['string'] try: body.decode('utf-8') json_blob = json.loads(body) json_blob.pop('api_key', None) response['body']['string'] = json.dumps(json_blob).encode('utf-8') except UnicodeDecodeError: pass # It's not a json string return response vcr = vcrpy.VCR( serializer='yaml', cassette_library_dir=str(CASSETTES), record_mode='once', filter_headers=[('X-RFToken', 'XXXXXX')], before_record_response=filter_out_whoami, ) class RFTest(unittest.TestCase): def setUp(self) -> None: base_url = "https://api.recordedfuture.com/v2/" verify_ssl = True self.token = os.environ.get("RF_TOKEN") headers = { "X-RFToken": self.token, "X-RF-User-Agent": "Cortex_XSOAR/2.0 Cortex_XSOAR_unittest_0.1", }
import os import vcr import pytest import unittest import logging import datetime logger = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG, format="%(asctime)s [%(levelname)-5.5s] %(message)s") curdir = os.path.dirname(os.path.abspath(__file__)) cassette_dir = os.path.join(curdir, 'cassettes', 'profiler') testvcr = vcr.VCR(cassette_library_dir=cassette_dir, record_mode='new_episodes') @testvcr.use_cassette() @pytest.fixture(scope="class") def create_profiler(request): """ Create default NetProfiler lab instance. """ auth = UserAuth('admin', 'admin') profiler = NetProfiler('10.38.131.150', auth=auth) request.cls.profiler = profiler @pytest.mark.usefixtures("create_profiler") class ProfilerTests(unittest.TestCase): @testvcr.use_cassette() def setUp(self):
import os import unittest import mock import vcr import util import swa, exceptions v = vcr.VCR(cassette_library_dir=os.path.join(os.path.dirname(__file__), 'fixtures'), decode_compressed_response=True) @mock.patch('swa.requests') class TestRequest(unittest.TestCase): def test_make_request_get(self, mock_requests): expected_headers = { "User-Agent": "SouthwestAndroid/7.2.1 android/10", "Accept": "application/json", "X-API-Key": swa.API_KEY, "X-Channel-ID": "MWEB" } expected_url = "https://mobile.southwest.com/api/foo/123456/bar" fake_data = '' _ = swa._make_request( # NOQA "get", "foo/123456/bar", fake_data) mock_requests.get.assert_called_with(expected_url,
from kuberwatcher import * import pystache import pytest import vcr my_vcr = vcr.VCR( cassette_library_dir='tests/cassettes', record_mode='new_episodes', filter_headers=['authorization'], match_on=['path', 'query'] ) def mustache_render(template, event): return pystache.render(template, {'ctx':{'payload':{'aggregations': {'result': {'hits': {'hits': {'0': {'_source': event }}}}}}}}) def test_template_defaults_with_no_outputs(): watch = { 'name': 'namespace.podgroup', 'regex': 'pod-.*', 'pod_type': 'replicaset', 'namespace': 'namespace' } template = render_template(**watch) assert template['metadata']['window'] == '300s' assert template['input']['search']['request']['body']['query']['bool']['must'][0]['regexp']['kubernetes.pod.name'] == 'pod-.*' assert template['input']['search']['request']['body']['query']['bool']['must'][1]['match']['kubernetes.namespace'] == 'namespace' def test_template_with_job_pod_type(): watch = { 'name': 'namespace.podgroup', 'regex': 'pod-.*',
from functools import wraps from importlib import import_module import pytest import vcr from lexicon.config import ConfigResolver, ConfigSource, DictConfigSource # Configure VCR. Parameter record_mode depends on the LEXICON_LIVE_TESTS environment variable value. RECORD_MODE = "none" if os.environ.get("LEXICON_LIVE_TESTS", "false") == "true": RECORD_MODE = "once" PROVIDER_VCR = vcr.VCR( cassette_library_dir=os.environ.get( "LEXICON_VCRPY_CASSETTES_PATH", "tests/fixtures/cassettes" ), record_mode=RECORD_MODE, decode_compressed_response=True, ) # Prepare custom decorator: it will start a casette in relevant folder for current provider, # and using the name of the test method as the cassette's name. def _vcr_integration_test(decorated): @wraps(decorated) def wrapper(self): with PROVIDER_VCR.use_cassette( self._cassette_path(f"IntegrationTests/{decorated.__name__}.yaml"), filter_headers=self._filter_headers(), filter_query_parameters=self._filter_query_parameters(), filter_post_data_parameters=self._filter_post_data_parameters(),
import contextlib from builtins import object import lexicon.client from lexicon.common.options_handler import SafeOptions, env_auth_options import pytest import vcr import os # Configure VCR provider_vcr = vcr.VCR(cassette_library_dir='tests/fixtures/cassettes', record_mode='new_episodes', decode_compressed_response=True) """ https://stackoverflow.com/questions/26266481/pytest-reusable-tests-for-different-implementations-of-the-same-interface Single, reusable definition of tests for the interface. Authors of new implementations of the interface merely have to provide the test data, as class attributes of a class which inherits unittest.TestCase AND this class. Required test data: self.Provider must be set self.provider_name must be set self.domain must be set self._filter_headers can be defined to provide a list of sensitive headers self._filter_query_parameters can be defined to provide a list of sensitive parameter Extended test suites can be skipped by adding the following snippet to the test_{PROVIDER_NAME}.py file
from django.test import TestCase, Client import vcr from openhumans.models import OpenHumansMember from django.conf import settings FILTERSET = [('access_token', 'ACCESSTOKEN')] my_vcr = vcr.VCR( path_transformer=vcr.VCR.ensure_suffix('.yaml'), cassette_library_dir='main/tests/cassettes', # filter_headers=[('Authorization', 'XXXXXXXX')], filter_query_parameters=FILTERSET, filter_post_data_parameters=FILTERSET) class LoginTestCase(TestCase): """ Test the login logic of the OH API """ def setUp(self): settings.DEBUG = True settings.OPENHUMANS_APP_BASE_URL = "http://127.0.0.1" # self.invalid_token = 'INVALID_TOKEN' # self.master_token = 'ACCESSTOKEN' # self.project_info_url = 'https://www.openhumans.org/api/direct-sharing/project/?access_token={}' # @my_vcr.use_cassette() # def test_complete(self): # c = Client() # self.assertEqual(0, # OpenHumansMember.objects.all().count())
import json import memoize import freezegun import vcr from django.test import TestCase, override_settings, modify_settings from api.tests.factories import UserFactory from test_utils.cassette_utils import assert_cassette_playback_length, scrub_host_name my_vcr = vcr.VCR(before_record=scrub_host_name, path_transformer=vcr.VCR.ensure_suffix('.yaml'), cassette_library_dir='jetstream/fixtures', filter_headers=[('Authorization', 'Basic XXXXX')], inject_cassette=True) @modify_settings(INSTALLED_APPS={ 'append': 'jetstream', }) @override_settings(TACC_API_URL='https://localhost/api-test') class TestJetstream(TestCase): """Tests for Jetstream allocation source API""" def setUp(self): # Because we use memoize to cache the tacc api, calling tacc_api_get # doesn't necessarily trigger an http request. This means that a # cassette will not necessarily be played. In order to test cassette # playback, we just need to clear the memoize cache from jetstream.tas_api import tacc_api_get memoize.delete_memoized(tacc_api_get) @my_vcr.use_cassette()
import vcr cryptocompy_vcr = vcr.VCR(cassette_library_dir='tests/cassettes', record_mode='none')
from pathlib import Path import pytest import vcr from .._utils import SessionClosedError from .._client import (WaybackSession, WaybackClient, original_url_for_memento) from ..exceptions import MementoPlaybackError # This stashes HTTP responses in JSON files (one per test) so that an actual # server does not have to be running. cassette_library_dir = str(Path(__file__).parent / Path('cassettes/')) ia_vcr = vcr.VCR( serializer='yaml', cassette_library_dir=cassette_library_dir, record_mode='once', match_on=['uri', 'method'], ) @ia_vcr.use_cassette() def test_search(): with WaybackClient() as client: versions = client.search('nasa.gov', from_date=datetime(1996, 10, 1), to_date=datetime(1997, 2, 1)) version = next(versions) assert version.timestamp == datetime(1996, 12, 31, 23, 58, 47) # Exhaust the generator and make sure no entries trigger errors. list(versions)
import vcr import pytest import hubblepy tape = vcr.VCR( cassette_library_dir='tests/cassettes', serializer='json', record_mode='once' ) @vcr.use_cassette('tests/cassettes/test_news.yml') def test_news(): p1 = hubblepy.news(1) p2 = hubblepy.news('1') p3 = hubblepy.news([1, 2, 3]) p4 = hubblepy.news(1, 'text') p5 = hubblepy.news(1, 'content') assert isinstance(p1, list) assert isinstance(p1[0], dict) assert isinstance(p2[0], dict) assert isinstance(p3, list) assert isinstance(p4, str) assert isinstance(p5, bytes) @vcr.use_cassette('tests/cassettes/test_news_releases.yml') def test_news_release():
from aylienapiclient.errors import HttpError from aylienapiclient.errors import MissingParameterError from aylienapiclient.errors import MissingCredentialsError try: from urllib.request import urlopen except ImportError: from urllib2 import urlopen APP_ID = os.environ['TEXTAPI_APP_ID'] APP_KEY = os.environ['TEXTAPI_APP_KEY'] aylien_vcr = vcr.VCR( cassette_library_dir='tests/fixtures/cassettes', path_transformer=vcr.VCR.ensure_suffix('.yaml'), filter_headers=[ 'x-aylien-textapi-application-id', 'x-aylien-textapi-application-key' ], ) endpoints = [ 'Extract', 'Classify', 'Concepts', 'Entities', 'Hashtags', 'Language', 'Related', 'Sentiment', 'Summarize', 'Microformats', 'ImageTags', 'UnsupervisedClassify', 'Combined' ] generic_counter = 0 def build_path_for_generic_generator(function): return function.__name__ + "_" + endpoints[generic_counter].lower()
parsed_output = json.loads(result.output) if parsed_output == {} and cmd not in COMMANDS_WITH_NO_PARAMS: commands_with_bad_json.append(cmd) except Exception: failed_to_parse_commands.append(cmd) assert len(failed_to_parse_commands ) == 0, 'The following commands failed to parse: {}'.format( failed_to_parse_commands) assert len( commands_with_bad_json ) == 0, 'The following commands had invalid JSON skeletons: {}'.format( commands_with_bad_json) none_mode_vcr = vcr.VCR(record_mode='none') # Test which invokes all the command using the request formed by invoking --generate-full-command-json-input option. # The main aim of this test is to verify if all the generated CLI code is function and CLI <-> PythonSDK interaction. # In this test, we use VCR with record mode none as to stop any out-going http request. @none_mode_vcr.use_cassette('invalid-file-path') def test_run_all_commands(runner, config_file, config_profile, tmpdir, ignored_extended_commands): failed_commands = [] for cmd in commands_list: if cmd not in ignored_extended_commands: full_command = list(cmd) try: result = util.invoke_command( full_command + ['--generate-full-command-json-input'])
def gen_vcr(): return vcr.VCR( cassette_library_dir='tests/fixtures_vcr', record_mode='none', match_on=['method', 'scheme', 'host', 'port', 'path', 'query'], )
import pytest import vcr from django.contrib.auth.models import User from django.test import Client @pytest.fixture() def users(): user = User.objects.create_user(username='******', email='*****@*****.**', password='******') user.save() return {'user': user} @pytest.mark.usefixtures('users') @pytest.fixture() def clients(users): c_in = Client() c_in.login(username='******', password='******') c_out = Client() return {'c_in': c_in, 'c_out': c_out} base_vcr = vcr.VCR(record_mode='none', ignore_localhost=True) elvanto_vcr = base_vcr.use_cassette( 'elvanto_sync/tests/fixtures/elvanto.yaml', filter_headers=['authorization'], match_on=['method', 'scheme', 'host', 'port', 'path', 'query', 'body'])
import pytest import requests import requests_mock import vcr from apiclient import APIClient from apiclient.request_formatters import BaseRequestFormatter from apiclient.response_handlers import BaseResponseHandler BASE_DIR = os.path.abspath(os.path.realpath(os.path.dirname(__file__))) VCR_CASSETTE_DIR = os.path.join(BASE_DIR, "vcr_cassettes") api_client_vcr = vcr.VCR( serializer="yaml", cassette_library_dir=VCR_CASSETTE_DIR, record_mode="once", match_on=["uri", "method", "query"], ) error_cassette_vcr = vcr.VCR(serializer="yaml", cassette_library_dir=VCR_CASSETTE_DIR, record_mode="once", match_on=["uri"]) @pytest.fixture def cassette(): with api_client_vcr.use_cassette("cassette.yaml") as cassette: yield cassette
import os import vcr from simple_settings import settings cassettes_dir = os.path.join(settings.BASE_PATH, 'tests', 'fixtures/cassettes') vcr_client = vcr.VCR( serializer='json', cassette_library_dir=cassettes_dir, record_mode='once', match_on=['uri', 'method'], ignore_localhost=True, )
import vcr my_vcr = vcr.VCR(cassette_library_dir='tests/fixtures/cassettes')
# tests/test_tmdbwrapper.py from tmdbwrapper import TV from pytest import fixture import vcr tmdb_vcr = vcr.VCR(filter_query_parameters=['api_key']) @fixture def tv_keys(): # Will return the test data return [ "id", "origin_country", "poster_path", "name", "overview", "popularity", "backdrop_path", "first_air_date", "vote_count", "vote_average" ] # The following code will pull the most popular tv shows from tmbd # and test to make sure the first show meets the tv_keys requirements @vcr.use_cassette('tests/vcr_cassettes/tv-popular.yml') def test_tv_popular(): """Tests an API call to get a popular tv shows""" response = TV.popular() assert isinstance(response, dict) assert isinstance(response['results'], list) assert isinstance(response['results'][0], dict) # Currently issue with testing the following line. Probably a syntax issue
import vcr gvcr = vcr.VCR(cassette_library_dir='fixtures/cassettes', record_mode='once', match_on=['path', 'method', 'query', 'body'])
import pytest import vcr my_vcr = vcr.VCR( path_transformer=vcr.VCR.ensure_suffix('.yaml'), cassette_library_dir='fixtures/cassettes', record_mode='once', match_on=['uri', 'method'], ) @pytest.fixture def raw_data(): return """ { "persons": [ { "favoriteColor": "Yellow", "name": "Alice" }, { "favoriteColor": "Green", "name": "Bob" }, { "favoriteColor": "Red", "name": "Charlie" }, { "favoriteColor": "Red", "name": "Dean"
# -*- coding: utf-8 -*- import re import os import shutil import signal import tempfile from contextlib import contextmanager from django.template.defaultfilters import slugify from django.conf import settings from django.core.management import call_command from aloe import before, after, around, step, world import aloe_webdriver.django # noqa from selenium.webdriver import Chrome, ChromeOptions import vcr selenium_vcr = vcr.VCR() # We need to ignore localhost as selenium communicates over local http # to interact with the 'browser' selenium_vcr.ignore_localhost = True temp_dir = tempfile.mkdtemp() @before.all def before_all(): # build static assets into a temporary location settings.STATIC_ROOT = temp_dir call_command("collectstatic", interactive=False, verbosity=0) @after.all
import unittest from datetime import datetime import vcr from britishair import BA my_vcr = vcr.VCR( serializer='yaml', cassette_library_dir='tests/fixtures/cassettes', record_mode='once', ) class TestBASearch(unittest.TestCase): def setUp(self): self.ba = BA() #@unittest.skip("temp disabled") def test_SimpleEconomySearch(self): outboundDateTime = datetime(month=4, day=3, year=2015) inboundDateTime = datetime(month=4, day=10, year=2015) locationOutbound = "LHR" locationInbound = "LAX" fareClass = "Economy" with my_vcr.use_cassette('test_SimpleEconomySearch.json'): fares = self.ba.search(outboundDateTime, locationOutbound, inboundDateTime, locationInbound, fareClass) amount = fares[0][u'AirItineraryPricingInfo']['ItinTotalFare'][ 'TotalFare']['@Amount'] self.assertEqual(amount, u'965.66')