Exemplo n.º 1
0
 def parametrize(name_prefix='test_db'):
   name_prefix = str(name_prefix)
   if not is_valid_impala_identifier(name_prefix):
     raise ValueError('name_prefix "{0}" is not a valid Impala identifier; check '
                      'value for long length or invalid '
                      'characters.'.format(name_prefix))
   return pytest.mark.parametrize('unique_database', [name_prefix], indirect=True)
Exemplo n.º 2
0
 def parametrize(name_prefix=None, sync_ddl=False, num_dbs=1):
   named_params = {}
   if name_prefix is not None:
     name_prefix = str(name_prefix)
     if not is_valid_impala_identifier(name_prefix):
       raise ValueError('name_prefix "{0}" is not a valid Impala identifier; check '
                        'value for long length or invalid '
                        'characters.'.format(name_prefix))
     named_params["name_prefix"] = name_prefix
   if not isinstance(sync_ddl, bool):
     raise ValueError('value {0} of sync_ddl is be a boolean'.format(sync_ddl))
   named_params["sync_ddl"] = sync_ddl
   if not isinstance(num_dbs, int) or num_dbs <= 0:
     raise ValueError("num_dbs must be an integer >= 1 but '{0}' given".format(num_dbs))
   named_params["num_dbs"] = num_dbs
   return pytest.mark.parametrize('unique_database', [named_params], indirect=True)
Exemplo n.º 3
0
def unique_database(request, testid_checksum):
  """
  Return a database name unique to any test using the fixture. The fixture creates the
  database during setup, allows the test to know the database name, and drops the
  database after the test has completed.

  By default, the database name is a concatenation of the test function name and the
  testid_checksum. The database name prefix can be changed via parameter (see below).

  A good candidate for a test to use this fixture is a test that needs to have a
  test-local database or test-local tables that are created and destroyed at test run
  time. Because the fixture generates a unique name, tests using this fixture can be run
  in parallel as long as they don't need exclusion on other test-local resources.

  Sample usage:

    def test_something(self, vector, unique_database):
      # fixture creates database test_something_48A80F
      self.client.execute('DROP TABLE IF EXISTS `{0}`.`mytable`'.format(unique_database))
      # test does other stuff with the unique_database name as needed

  We also allow for parametrization:

    from tests.common.parametrize import UniqueDatabase

    @UniqueDatabase.parametrize(name_prefix='mydb')
    def test_something(self, vector, unique_database):
      # fixture creates database mydb_48A80F
      self.client.execute('DROP TABLE IF EXISTS `{0}`.`mytable`'.format(unique_database))
      # test does other stuff with the unique_database name as needed

  The supported parameter:

    name_prefix: string (defaults to test function __name__) - prefix to be used for the
    database name

  For a similar DB-API 2 compliant connection/cursor that use HS2 see the 'conn' and
  'unique_cursor' fixtures below.
  """

  # Test cases are at the function level, so no one should "accidentally" re-scope this.
  assert 'function' == request.scope, ('This fixture must have scope "function" since '
                                       'the fixture must guarantee unique per-test '
                                       'databases.')

  db_name_prefix = getattr(request, 'param', request.function.__name__)

  db_name = '{0}_{1}'.format(db_name_prefix, testid_checksum)
  if not is_valid_impala_identifier(db_name):
    raise ValueError('Unique database name "{0}" is not a valid Impala identifer; check '
                     'test function name or any prefixes for long length or invalid '
                     'characters.'.format(db_name))

  def cleanup():
    # Make sure we don't try to drop the current session database
    request.instance.execute_query_expect_success(request.instance.client, "use default")
    request.instance.execute_query_expect_success(
        request.instance.client, 'DROP DATABASE `{0}` CASCADE'.format(db_name))
    LOG.info('Dropped database "{0}" for test ID "{1}"'.format(db_name,
                                                               str(request.node.nodeid)))

  request.addfinalizer(cleanup)

  request.instance.execute_query_expect_success(
      request.instance.client, 'DROP DATABASE IF EXISTS `{0}` CASCADE'.format(db_name))
  request.instance.execute_query_expect_success(
      request.instance.client, 'CREATE DATABASE `{0}`'.format(db_name))
  LOG.info('Created database "{0}" for test ID "{1}"'.format(db_name,
                                                             str(request.node.nodeid)))
  return db_name
Exemplo n.º 4
0
def unique_database(request, testid_checksum):
    """
  Return a database name unique to any test using the fixture. The fixture creates the
  database during setup, allows the test to know the database name, and drops the
  database after the test has completed.

  By default, the database name is a concatenation of the test function name and the
  testid_checksum. The database name prefix can be changed via parameter (see below).

  A good candidate for a test to use this fixture is a test that needs to have a
  test-local database or test-local tables that are created and destroyed at test run
  time. Because the fixture generates a unique name, tests using this fixture can be run
  in parallel as long as they don't need exclusion on other test-local resources.

  Sample usage:

    def test_something(self, vector, unique_database):
      # fixture creates database test_something_48A80F
      self.client.execute('DROP TABLE IF EXISTS `{0}`.`mytable`'.format(unique_database))
      # test does other stuff with the unique_database name as needed

  We also allow for parametrization:

    from tests.common.parametrize import UniqueDatabase

    @UniqueDatabase.parametrize(name_prefix='mydb', num_dbs=3, sync_ddl=True)
    def test_something(self, vector, unique_database):
      # fixture creates databases mydb_48A80F, mydb_48A80F2, mydb_48A80F3 with sync_ddl
      self.client.execute('DROP TABLE IF EXISTS `{0}`.`mytable`'.format(unique_database))
      # test does other stuff with the unique_database name as needed

  The supported parameters:

    name_prefix: string (defaults to test function __name__)
      - prefix to be used for the database name

    num_dbs: integer (defaults to 1)
      - number of unique databases to create
      - the name of the 2nd, 3rd, etc. databases are generated by appending "2", "3",
        etc., to the first database name (which does not have a "1" suffix)

    sync_ddl: boolean (defaults to False)
      - indicates whether the unique database should be created with sync_ddl

  For a similar DB-API 2 compliant connection/cursor that uses HS2 see the 'conn' and
  'unique_cursor' fixtures below.
  """

    # Test cases are at the function level, so no one should "accidentally" re-scope this.
    assert 'function' == request.scope, (
        'This fixture must have scope "function" since '
        'the fixture must guarantee unique per-test '
        'databases.')

    db_name_prefix = request.function.__name__
    sync_ddl = False
    num_dbs = 1
    fixture_params = getattr(request, 'param', None)
    if fixture_params is not None:
        if "name_prefix" in fixture_params:
            db_name_prefix = fixture_params["name_prefix"]
        if "sync_ddl" in fixture_params:
            sync_ddl = fixture_params["sync_ddl"]
        if "num_dbs" in fixture_params:
            num_dbs = fixture_params["num_dbs"]

    first_db_name = '{0}_{1}'.format(db_name_prefix, testid_checksum)
    db_names = [first_db_name]
    for i in range(2, num_dbs + 1):
        db_names.append(first_db_name + str(i))
    for db_name in db_names:
        if not is_valid_impala_identifier(db_name):
            raise ValueError(
                'Unique database name "{0}" is not a valid Impala identifer; check'
                ' test function name or any prefixes for long length or invalid '
                'characters.'.format(db_name))

    def cleanup():
        # Make sure we don't try to drop the current session database
        request.instance.execute_query_expect_success(request.instance.client,
                                                      "use default")
        for db_name in db_names:
            request.instance.execute_query_expect_success(
                request.instance.client,
                'DROP DATABASE `{0}` CASCADE'.format(db_name),
                {'sync_ddl': sync_ddl})
            LOG.info('Dropped database "{0}" for test ID "{1}"'.format(
                db_name, str(request.node.nodeid)))

    request.addfinalizer(cleanup)

    for db_name in db_names:
        request.instance.execute_query_expect_success(
            request.instance.client,
            'DROP DATABASE IF EXISTS `{0}` CASCADE'.format(db_name),
            {'sync_ddl': sync_ddl})
        request.instance.execute_query_expect_success(
            request.instance.client, 'CREATE DATABASE `{0}`'.format(db_name),
            {'sync_ddl': sync_ddl})
        LOG.info('Created database "{0}" for test ID "{1}"'.format(
            db_name, str(request.node.nodeid)))
    return first_db_name
Exemplo n.º 5
0
def unique_database(request, testid_checksum):
  """
  Return a database name unique to any test using the fixture. The fixture creates the
  database during setup, allows the test to know the database name, and drops the
  database after the test has completed.

  By default, the database name is a concatenation of the test function name and the
  testid_checksum. The database name prefix can be changed via parameter (see below).

  A good candidate for a test to use this fixture is a test that needs to have a
  test-local database or test-local tables that are created and destroyed at test run
  time. Because the fixture generates a unique name, tests using this fixture can be run
  in parallel as long as they don't need exclusion on other test-local resources.

  Sample usage:

    def test_something(self, vector, unique_database):
      # fixture creates database test_something_48A80F
      self.client.execute('DROP TABLE IF EXISTS `{0}`.`mytable`'.format(unique_database))
      # test does other stuff with the unique_database name as needed

  We also allow for parametrization:

    from tests.common.parametrize import UniqueDatabase

    @UniqueDatabase.parametrize(name_prefix='mydb', num_dbs=3, sync_ddl=True)
    def test_something(self, vector, unique_database):
      # fixture creates databases mydb_48A80F, mydb_48A80F2, mydb_48A80F3 with sync_ddl
      self.client.execute('DROP TABLE IF EXISTS `{0}`.`mytable`'.format(unique_database))
      # test does other stuff with the unique_database name as needed

  The supported parameters:

    name_prefix: string (defaults to test function __name__)
      - prefix to be used for the database name

    num_dbs: integer (defaults to 1)
      - number of unique databases to create
      - the name of the 2nd, 3rd, etc. databases are generated by appending "2", "3",
        etc., to the first database name (which does not have a "1" suffix)

    sync_ddl: boolean (defaults to False)
      - indicates whether the unique database should be created with sync_ddl

  For a similar DB-API 2 compliant connection/cursor that uses HS2 see the 'conn' and
  'unique_cursor' fixtures below.
  """

  # Test cases are at the function level, so no one should "accidentally" re-scope this.
  assert 'function' == request.scope, ('This fixture must have scope "function" since '
                                       'the fixture must guarantee unique per-test '
                                       'databases.')

  db_name_prefix = request.function.__name__
  sync_ddl = False
  num_dbs = 1
  fixture_params = getattr(request, 'param', None)
  if fixture_params is not None:
    if "name_prefix" in fixture_params:
      db_name_prefix = fixture_params["name_prefix"]
    if "sync_ddl" in fixture_params:
      sync_ddl = fixture_params["sync_ddl"]
    if "num_dbs" in fixture_params:
      num_dbs = fixture_params["num_dbs"]

  first_db_name = '{0}_{1}'.format(db_name_prefix, testid_checksum)
  db_names = [first_db_name]
  for i in range(2, num_dbs + 1):
    db_names.append(first_db_name + str(i))
  for db_name in db_names:
    if not is_valid_impala_identifier(db_name):
      raise ValueError('Unique database name "{0}" is not a valid Impala identifer; check'
                       ' test function name or any prefixes for long length or invalid '
                       'characters.'.format(db_name))

  def cleanup():
    # Make sure we don't try to drop the current session database
    request.instance.execute_query_expect_success(request.instance.client, "use default")
    for db_name in db_names:
      request.instance.execute_query_expect_success(
          request.instance.client, 'DROP DATABASE `{0}` CASCADE'.format(db_name),
          {'sync_ddl': sync_ddl})
      LOG.info('Dropped database "{0}" for test ID "{1}"'.format(
          db_name, str(request.node.nodeid)))

  request.addfinalizer(cleanup)

  for db_name in db_names:
    request.instance.execute_query_expect_success(
        request.instance.client, 'DROP DATABASE IF EXISTS `{0}` CASCADE'.format(db_name),
        {'sync_ddl': sync_ddl})
    request.instance.execute_query_expect_success(
        request.instance.client, 'CREATE DATABASE `{0}`'.format(db_name),
        {'sync_ddl': sync_ddl})
    LOG.info('Created database "{0}" for test ID "{1}"'.format(db_name,
                                                               str(request.node.nodeid)))
  return first_db_name