Files
impala/tests/common/patterns.py
Michael Brown 58219eac2c IMPALA-2537: EE tests: create and use unique database fixture
To speed up tests and reduce flakiness, introduce a pytest fixture
whereby a test maintainer may request a database unique to his test.
Such databases are suitable for tests that need to create tables within
Python test code. Because the database name is unique to the test, the
test can create any tables within that database it wants without fear
that the same tables will be picked up by another test. Unique databases
effectively guarantee a unique namespace for tables.

To generate the database name, we use the CRC32 checksum of the test's
so-called pytest test ID. This ID is a long string containing the test's
module path, class (if applicable), function name, and parameter set
(e.g., vector). We then concatenate the CRC32 checksum with the test
function name, so that it's easier to identify the test to which the
database belongs. The test author may also override the prefix by
parametrizing the fixture.

We then use a pytest fixture to create the database, hand the name to
the test using the fixture, and clean up the database automatically
after the test completes.

The command `impala-py.test --fixtures` executed from the tests/
directory explains the full usage.

Finally, we modify a few tests to show how test maintainers can use this
fixture.

Not supported here are databases used by .test files, creation of hive
databases, databases with special CREATE parameters such as LOCATION and
COMMENT, or asking the fixture to create multiple databases. Also not
supported would be attempted parallel runs of the same test with the
same test parameters.

Testing:

1. Manual testing of the fixture usage, both in vanilla and
   parametrized context.

2. Manual runs of the tests modified.

3. An exhaustive exploration strategy test run.

Change-Id: I74d200da8a59379388e1edfbb849828f92a1b3b7
Reviewed-on: http://gerrit.cloudera.org:8080/1821
Reviewed-by: Michael Brown <mikeb@cloudera.com>
Tested-by: Internal Jenkins
2016-03-16 18:29:57 +00:00

27 lines
1.1 KiB
Python

# Copyright (c) 2016 Cloudera, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Common patterns that ought to be the same throughout the framework should be placed
# here.
import re
# http://www.cloudera.com/content/www/en-us/documentation/enterprise/latest/topics/impala_identifiers.html
VALID_IMPALA_IDENTIFIER_REGEX = re.compile(r'^[a-zA-Z][a-zA-Z0-9_]{,127}$')
def is_valid_impala_identifier(identifier):
"""Return True if identifier is a valid Impala identifier, False otherwise."""
return VALID_IMPALA_IDENTIFIER_REGEX.match(identifier) is not None