def setUpClass(cls): """ Set up the test class. Prevent tests running if the host is down. """ super(CheckHostnameMixin, cls).setUpClass() if not hasattr(cls, 'sites'): return for key, data in cls.sites.items(): if 'hostname' not in data: raise Exception('%s: hostname not defined for %s' % (cls.__name__, key)) hostname = data['hostname'] if hostname in cls._checked_hostnames: if isinstance(cls._checked_hostnames[hostname], Exception): raise unittest.SkipTest( '%s: hostname %s failed (cached): %s' % (cls.__name__, hostname, cls._checked_hostnames[hostname])) elif cls._checked_hostnames[hostname] is False: raise unittest.SkipTest('%s: hostname %s failed (cached)' % (cls.__name__, hostname)) else: continue e = None try: if '://' not in hostname: hostname = 'http://' + hostname r = http.fetch(uri=hostname, default_error_handling=False) if r.exception: e = r.exception else: if r.status not in [200, 301, 302, 303, 307, 308]: raise ServerError('HTTP status: %d' % r.status) r.content # default decode may raise exception except Exception as e2: pywikibot.error('%s: accessing %s caused exception:' % (cls.__name__, hostname)) pywikibot.exception(e2, tb=True) e = e2 pass if e: cls._checked_hostnames[hostname] = e raise unittest.SkipTest('%s: hostname %s failed: %s' % (cls.__name__, hostname, e)) cls._checked_hostnames[hostname] = True
def wrapper(*args, **kwargs): try: func(*args, **kwargs) except AssertionError: tb = traceback.extract_tb(sys.exc_info()[2]) for depth, line in enumerate(tb): if re.match('^assert[A-Z]', line[2]): break tb = traceback.format_list(tb[:depth]) pywikibot.error('\n' + ''.join(tb)[:-1]) # remove \n at the end raise unittest.SkipTest('Test is allowed to fail.') except Exception: pywikibot.exception(tb=True) raise unittest.SkipTest('Test is allowed to fail.')
def assertPagesInNamespacesAll(self, gen, namespaces, skip=False): """ Try to confirm that generator returns Pages for all namespaces. @param gen: generator to iterate @type gen: generator @param namespaces: expected namespaces @type namespaces: int or set of int @param count: maximum results to process @type count: int @param skip: skip test if not all namespaces found @param skip: bool """ if isinstance(namespaces, int): namespaces = set([namespaces]) else: assert (isinstance(namespaces, set)) page_namespaces = [page.namespace() for page in gen] if skip and set(page_namespaces) != namespaces: raise unittest.SkipTest('Pages in namespaces %r not found.' % list(namespaces - set(page_namespaces))) else: self.assertEqual(set(page_namespaces), namespaces)
def test_create_pipeline(self): # In order to create a pipeline, we need to create 2 s3 buckets # and 1 iam role. input_bucket = self.create_bucket() output_bucket = self.create_bucket() role = self.create_iam_role() pipeline_name = 'botocore-test-create-%s' % (random.randint( 1, 1000000)) operation = self.service.get_operation('CreatePipeline') http, parsed = operation.call(self.endpoint, input_bucket=input_bucket, output_bucket=output_bucket, role=role, name=pipeline_name, notifications={ 'Progressing': '', 'Completed': '', 'Warning': '', 'Error': '' }) if http.status_code == 429: # It's possible that we have too many existing pipelines. # We don't want to fail the test, but we need to indicate # that it didn't pass. A SkipTest is a reasonable compromise. raise unittest.SkipTest( "HTTP status 429, too many existing pipelines.") self.assertEqual(http.status_code, 201) self.assertIn('Pipeline', parsed)
def setUpClass(cls): """ Set up the test class. Reject write test classes configured with non-test wikis, or caching. Prevent test classes from writing to the site by default. If class attribute 'write' is -1, the test class is skipped unless environment variable PYWIKIBOT2_TEST_WRITE_FAIL is set to 1. Otherwise the test class is skipped unless environment variable PYWIKIBOT2_TEST_WRITE is set to 1. """ super(SiteWriteMixin, cls).setUpClass() site = cls.get_site() assert ('test' in (site.family.name, site.code)) if cls.write == -1: env_var = 'PYWIKIBOT2_TEST_WRITE_FAIL' else: env_var = 'PYWIKIBOT2_TEST_WRITE' if os.environ.get(env_var, '0') != '1': raise unittest.SkipTest('%r write tests disabled. ' 'Set %s=1 to enable.' % (cls.__name__, env_var)) if issubclass(cls, ForceCacheMixin): raise Exception('%s can not be a subclass of both ' 'SiteEditTestCase and ForceCacheMixin' % cls.__name__)
def get_lab_app( env_client_id="LAB_APP_CLIENT_ID", env_client_secret="LAB_APP_CLIENT_SECRET", ): """Returns the lab app as an MSAL confidential client. Get it from environment variables if defined, otherwise fall back to use MSI. """ if os.getenv(env_client_id) and os.getenv(env_client_secret): # A shortcut mainly for running tests on developer's local development machine # or it could be setup on Travis CI # https://docs.travis-ci.com/user/environment-variables/#defining-variables-in-repository-settings # Data came from here # https://microsoft.sharepoint-df.com/teams/MSIDLABSExtended/SitePages/Rese.aspx#programmatic-access-info-for-lab-request-api logger.info("Using lab app defined by ENV variables %s and %s", env_client_id, env_client_secret) client_id = os.getenv(env_client_id) client_secret = os.getenv(env_client_secret) else: logger.info( "ENV variables %s and/or %s are not defined. Fall back to MSI.", env_client_id, env_client_secret) # See also https://microsoft.sharepoint-df.com/teams/MSIDLABSExtended/SitePages/Programmatically-accessing-LAB-API's.aspx raise unittest.SkipTest( "MSI-based mechanism has not been implemented yet") return msal.ConfidentialClientApplication( client_id, client_secret, authority="https://login.microsoftonline.com/" "72f988bf-86f1-41af-91ab-2d7cd011db47", # Microsoft tenant ID )
def setUpClass(cls): """ Set up the test class. Checks that all sites are configured with a Wikibase repository, with Site.has_data_repository() returning True, and all sites use the same data repository. """ super(WikibaseTestCase, cls).setUpClass() for data in cls.sites.values(): if 'site' not in data: continue site = data['site'] if not site.has_data_repository: raise unittest.SkipTest( u'%s: %r does not have data repository' % (cls.__name__, site)) if (hasattr(cls, 'repo') and cls.repo != site.data_repository()): raise Exception( '%s: sites do not all have the same data repository' % cls.__name__) cls.repo = site.data_repository()
def create_rest_api_or_skip(self): try: api_id = self.client.create_rest_api(name=self.api_name)['id'] except exceptions.ClientError as e: if e.response['Error']['Code'] == 'TooManyRequestsException': raise unittest.SkipTest( "Hit API gateway throttle limit, skipping test.") return api_id
def setUpClass(cls): if os.environ.get('PYWIKIBOT2_TEST_NO_RC', '0') == '1': raise unittest.SkipTest('RecentChanges tests disabled.') super(RecentChangesTestCase, cls).setUpClass() if cls.get_site().code == 'test': cls.override_default_site(pywikibot.Site('en', 'wikipedia'))
def get_missing_article(self, site=None): """Get a Page which refers to a missing page on the site.""" if not site: site = self.get_site() page = pywikibot.Page( pywikibot.page.Link("There is no page with this title", site)) if page.exists(): raise unittest.SkipTest("Did not find a page that does not exist.") return page
def setUpClass(cls): """ Set up the test class. Require the data repository is wikidata.org. """ super(WikibaseClientTestCase, cls).setUpClass() if str(cls.get_repo()) != 'wikidata:wikidata': raise unittest.SkipTest(u'%s: %s is not connected to Wikidata.' % (cls.__name__, cls.get_site()))
def setUpClass(cls): """ Set up the test class. Prefetch the Site object for each of the sites the test class has declared are needed. """ super(TestCase, cls).setUpClass() if not hasattr(cls, 'sites'): return # This stores the site under the site name. if not cls.sites: cls.sites = {} # If the test is not cached, create new Site objects for this class if not hasattr(cls, 'cached') or not cls.cached: orig_sites = pywikibot._sites pywikibot._sites = {} interface = None # defaults to 'APISite' dry = hasattr(cls, 'dry') and cls.dry if dry: interface = DrySite for data in cls.sites.values(): if ('code' in data and data['code'] in ('test', 'mediawiki') and 'PYWIKIBOT2_TEST_PROD_ONLY' in os.environ and not dry): raise unittest.SkipTest( 'Site code "%s" and PYWIKIBOT2_TEST_PROD_ONLY is set.' % data['code']) if 'site' not in data and 'code' in data and 'family' in data: data['site'] = Site(data['code'], data['family'], interface=interface) if 'hostname' not in data and 'site' in data: try: data['hostname'] = data['site'].hostname() except KeyError: # The family has defined this as obsolete # without a mapping to a hostname. pass if not hasattr(cls, 'cached') or not cls.cached: pywikibot._sites = orig_sites if len(cls.sites) == 1: key = next(iter(cls.sites.keys())) if 'site' in cls.sites[key]: cls.site = cls.sites[key]['site']
def setUpClass(cls): """ Set up the test class. Checks that all sites are configured as a Wikibase client, with Site.has_transcluded_data() returning True. """ super(WikibaseClientTestCase, cls).setUpClass() for site in cls.sites.values(): if not site['site'].has_transcluded_data: raise unittest.SkipTest( u'%s: %r does not have transcluded data' % (cls.__name__, site['site']))
def assertLoosely(self, response, assertion=None, skippable_errors=("invalid_grant", "interaction_required")): if response.get("error") in skippable_errors: logger.debug("Response = %s", response) # Some of these errors are configuration issues, not library issues raise unittest.SkipTest(response.get("error_description")) else: if assertion is None: assertion = lambda: self.assertIn( "access_token", response, "{error}: {error_description}".format( # Do explicit response.get(...) rather than **response error=response.get("error"), error_description=response.get("error_description"))) assertion()
def setUpClass(cls): """ Set up the test class. Skip the test class if the user config does not have a valid login to the site. """ super(RequireUserMixin, cls).setUpClass() cls.require_site_user() cls.site.login() if not cls.site.user(): raise unittest.SkipTest( '%s: Unable able to login to %s' % cls.__name__, cls.site)
def test_query_string_params_in_urls(self): if not hasattr(self.AuthClass, 'canonical_query_string'): raise unittest.SkipTest('%s does not expose interim steps' % self.AuthClass.__name__) request = AWSRequest() request.url = ('https://s3.amazonaws.com/bucket?' 'marker=%C3%A4%C3%B6%C3%BC-01.txt&prefix') request.data = {'Action': 'MyOperation'} request.method = 'GET' # Check that the canonical query string is correct formatting # by ensuring that query string paramters that are added to the # canonical query string are correctly formatted. cqs = self.auth.canonical_query_string(request) self.assertEqual('marker=%C3%A4%C3%B6%C3%BC-01.txt&prefix=', cqs)
def setUpClass(cls): """ Set up the test class. Skip the test class if the user config does not have a valid login to the site. """ super(RequireUserMixin, cls).setUpClass() sysop = hasattr(cls, 'sysop') and cls.sysop for site in cls.sites.values(): cls.require_site_user(site['family'], site['code'], sysop) site['site'].login(sysop) if not site['site'].user(): raise unittest.SkipTest( '%s: Unable able to login to %s as %s' % (cls.__name__, 'sysop' if sysop else 'bot', site['site']))
def setUpClass(cls): """ Set up the test class. Prevent test classes to write to the site and also cache results. Skip the test class if environment variable PYWIKIBOT2_TEST_WRITE does not equal 1. """ if os.environ.get('PYWIKIBOT2_TEST_WRITE', '0') != '1': raise unittest.SkipTest('%r write tests disabled. ' 'Set PYWIKIBOT2_TEST_WRITE=1 to enable.' % cls.__name__) if issubclass(cls, ForceCacheMixin): raise Exception('%s can not be a subclass of both ' 'SiteEditTestCase and ForceCacheMixin' % cls.__name__) super(SiteWriteMixin, cls).setUpClass()
def require_site_user(cls): """Check the user config has a valid login to the site.""" if not cls.has_site_user(cls.family, cls.code): raise unittest.SkipTest('%s: No username for %s:%s' % (cls.__name__, cls.family, cls.code))
import os import sys import datetime from tests import unittest, capture if not os.environ.get('PEEWEE'): raise unittest.SkipTest('Not a Peewee build') from tests.models.peewee import * class BasePeeweePartitionTestCase(object): @classmethod def setUpClass(cls): sys.argv = [ 'architect', 'partition', '--module', 'tests.models.peewee' ] with capture() as (out, _): search = 'successfully (re)configured the database for the following models' assert search in out, '{0} not in {1}'.format(search, out) def test_raises_partition_column_error(self): from architect.exceptions import PartitionColumnError RangeDateDay.PartitionableMeta.partition_column = 'foo' with self.assertRaises(PartitionColumnError): RangeDateDay.create(name='foo', created=datetime.datetime( 2014, 4, 15, 18, 44, 23))
def require_site_user(cls, family, code, sysop=False): """Check the user config has a valid login to the site.""" if not cls.has_site_user(family, code, sysop=sysop): raise unittest.SkipTest( '%s: No %susername for %s:%s' % (cls.__name__, "sysop " if sysop else "", family, code))
import os import sys import datetime from tests import unittest, capture if not os.environ.get('PONY'): raise unittest.SkipTest('Not a Pony build') from tests.models.pony import * class BasePonyPartitionTestCase(object): @classmethod def setUpClass(cls): sys.argv = ['architect', 'partition', '--module', 'tests.models.pony'] with capture() as (out, _): search = 'successfully (re)configured the database for the following models' assert search in out, '{0} not in {1}'.format(search, out) def test_raises_partition_column_error(self): RangeDateDay.PartitionableMeta.partition_column = 'foo' with self.assertRaises(CommitException): with db_session: RangeDateDay(name='foo', created=datetime.datetime(2014, 4, 15, 18, 44, 23)) RangeDateDay.PartitionableMeta.partition_column = 'created'
import os import sys import datetime from tests import unittest, capture if not os.environ.get('DJANGO'): raise unittest.SkipTest('Not a Django build') from tests.models.django import * class BaseDjangoPartitionTestCase(object): @classmethod def setUpClass(cls): sys.argv = ['architect', 'partition', '--module', 'tests.models.django'] with capture() as (out, _): search = 'successfully (re)configured the database for the following models' assert search in out, '{0} not in {1}'.format(search, out) def test_raises_partition_column_error(self): from architect.exceptions import PartitionColumnError RangeDateDay.PartitionableMeta.partition_column = 'foo' with self.assertRaises(PartitionColumnError): RangeDateDay.objects.create(name='foo', created=datetime.datetime(2014, 4, 15, 18, 44, 23)) RangeDateDay.PartitionableMeta.partition_column = 'created' @unittest.skipUnless(os.environ.get('DB') == 'postgresql', 'Not a PostgreSQL build')
import os import sys import datetime from tests import unittest, capture if not os.environ.get('SQLALCHEMY'): raise unittest.SkipTest('Not a SQLAlchemy build') from tests.models.sqlalchemy import * from sqlalchemy.orm import sessionmaker class BaseSqlAlchemyPartitionTestCase(object): @classmethod def setUpClass(cls): sys.argv = [ 'architect', 'partition', '--module', 'tests.models.sqlalchemy', '--connection', dsn ] with capture() as (out, _): search = 'successfully (re)configured the database for the following models' assert search in out, '{0} not in {1}'.format(search, out) cls.session = sessionmaker(bind=engine)() def test_dsn_not_provided_error(self): from architect.exceptions import DsnNotProvidedError sys.argv = [ 'architect', 'partition', '--module', 'tests.models.sqlalchemy' ] with capture() as (_, err):
def setUpClass(cls): if not __debug__: raise unittest.SkipTest( '%s is disabled when __debug__ is disabled.' % cls.__name__) super(DebugOnlyTestCase, cls).setUpClass()
def setUpModule(): # noqa: N802 """Always skip.""" raise unittest.SkipTest('flow_tests.py tests are disabled ' 'due to upstream issue T249705.')
def wrapper(*args, **kwargs): try: func(*args, **kwargs) except Exception: pywikibot.exception(tb=True) raise unittest.SkipTest()
def setUpClass(cls): super(OpMsgReadPrefBase, cls).setUpClass() if version_tuple < (3, 7): raise unittest.SkipTest("requires PyMongo 3.7")