Connects to Impala and creates a new database, then returns a connection to it. This is intended to be used in a "with" block. Upon exit, the database will be dropped. A database name can be provided by 'db_name', a database by that name must not exist prior to calling this method.
(pytest_config, db_name=None, timeout=DEFAULT_CONN_TIMEOUT)
| 490 | |
| 491 | @contextlib.contextmanager |
| 492 | def __unique_conn(pytest_config, db_name=None, timeout=DEFAULT_CONN_TIMEOUT): |
| 493 | """Connects to Impala and creates a new database, then returns a connection to it. |
| 494 | This is intended to be used in a "with" block. Upon exit, the database will be |
| 495 | dropped. A database name can be provided by 'db_name', a database by that name |
| 496 | must not exist prior to calling this method. |
| 497 | |
| 498 | with __unique_conn() as conn: |
| 499 | # Use conn |
| 500 | # The database no longer exists and the conn is closed. |
| 501 | |
| 502 | The returned connection will have a 'db_name' property. |
| 503 | |
| 504 | DEPRECATED: |
| 505 | See the 'unique_database' fixture above to use Impala's custom python |
| 506 | API instead of DB-API. |
| 507 | """ |
| 508 | if not db_name: |
| 509 | db_name = choice(ascii_lowercase) + "".join(sample(ascii_lowercase + digits, 5)) |
| 510 | with __auto_closed_conn(pytest_config, timeout=timeout) as conn: |
| 511 | with __auto_closed_cursor(conn) as cur: |
| 512 | cur.execute("CREATE DATABASE %s" % db_name) |
| 513 | with __auto_closed_conn(pytest_config, db_name=db_name, timeout=timeout) as conn: |
| 514 | try: |
| 515 | yield conn |
| 516 | finally: |
| 517 | try: |
| 518 | with __auto_closed_cursor(conn) as cur: |
| 519 | try: |
| 520 | cur.execute("USE DEFAULT") |
| 521 | cur.execute("DROP DATABASE IF EXISTS %s CASCADE" % db_name) |
| 522 | except Exception as e: |
| 523 | LOG.warn("Error dropping database: %s", e) |
| 524 | except Exception as e: |
| 525 | LOG.warn("Error creating a cursor: %s", e) |
| 526 | |
| 527 | |
| 528 | @contextlib.contextmanager |
no test coverage detected