-
Notifications
You must be signed in to change notification settings - Fork 145
Make DB Connect work out of the box for unit tests with the default-python template #3254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
71c623e
Add basic configuration
lennartkats-db 4fbf017
Cleanup test
lennartkats-db 4734cbe
Add conftest
lennartkats-db 58f043e
Update references
lennartkats-db 103e008
Add pytest to instructions
lennartkats-db cb8e2f8
Fix main
lennartkats-db 13978dd
Avoid auth login for now
lennartkats-db 154bb09
Make sure Spark session is initialized eagerly
lennartkats-db 4103dad
Update acceptance tests
lennartkats-db bad1412
Fix formatting
lennartkats-db 7ae4e5a
Remove PyCharm mention
lennartkats-db 3090e8d
Minor tweaks
lennartkats-db ae63222
Update tests
lennartkats-db 439ea2c
Change UV to uv
lennartkats-db 5685373
Update Python version spec
lennartkats-db 0cce9cd
Merge remote-tracking branch 'origin/main' into uv-db-connect
lennartkats-db 957824b
Fix aceptance test
lennartkats-db d0f6537
Add to changelog
lennartkats-db 56cae98
Merge remote-tracking branch 'origin/main' into uv-db-connect
lennartkats-db b7ef6b1
Move conftest into tests/ directory for now
lennartkats-db dbee730
Update comment
lennartkats-db ae92d07
Update test files
lennartkats-db f822242
Revert apps acceptance test changes
lennartkats-db 459cf10
Merge remote-tracking branch 'origin/main' into uv-db-connect
lennartkats-db 0f82370
Fix output
lennartkats-db 0403440
Merge remote-tracking branch 'origin/main' into uv-db-connect
lennartkats-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 4 additions & 15 deletions
19
...e/templates/default-python/classic/output/my_default_python/src/my_default_python/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...ptance/bundle/templates/default-python/classic/output/my_default_python/tests/conftest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """This file configures pytest.""" | ||
|
|
||
| import os, sys, pathlib | ||
| from contextlib import contextmanager | ||
|
|
||
|
|
||
| try: | ||
| from databricks.connect import DatabricksSession | ||
| from databricks.sdk import WorkspaceClient | ||
| from pyspark.sql import SparkSession | ||
| import pytest | ||
| except ImportError: | ||
| raise ImportError("Test dependencies not found.\n\nRun tests using 'uv run pytest'. See http://docs.astral.sh/uv to learn more about uv.") | ||
|
|
||
|
|
||
| def enable_fallback_compute(): | ||
| """Enable serverless compute if no compute is specified.""" | ||
| conf = WorkspaceClient().config | ||
| if conf.serverless_compute_id or conf.cluster_id or os.environ.get("SPARK_REMOTE"): | ||
| return | ||
|
|
||
| url = "https://docs.databricks.com/dev-tools/databricks-connect/cluster-config" | ||
| print("☁️ no compute specified, falling back to serverless compute", file=sys.stderr) | ||
| print(f" see {url} for manual configuration", file=sys.stdout) | ||
|
|
||
| os.environ["DATABRICKS_SERVERLESS_COMPUTE_ID"] = "auto" | ||
|
|
||
|
|
||
| @contextmanager | ||
| def allow_stderr_output(config: pytest.Config): | ||
| """Temporarily disable pytest output capture.""" | ||
| capman = config.pluginmanager.get_plugin("capturemanager") | ||
| if capman: | ||
| with capman.global_and_fixture_disabled(): | ||
| yield | ||
| else: | ||
| yield | ||
|
|
||
|
|
||
| def pytest_configure(config: pytest.Config): | ||
| """Configure pytest session.""" | ||
| with allow_stderr_output(config): | ||
| enable_fallback_compute() | ||
|
|
||
| # Initialize Spark session eagerly, so it is available even when | ||
| # SparkSession.builder.getOrCreate() is used. For DB Connect 15+, | ||
| # we validate version compatibility with the remote cluster. | ||
| if hasattr(DatabricksSession.builder, "validateSession"): | ||
| DatabricksSession.builder.validateSession().getOrCreate() | ||
| else: | ||
| DatabricksSession.builder.getOrCreate() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def spark() -> SparkSession: | ||
| """Provide a SparkSession fixture for tests.""" | ||
| return DatabricksSession.builder.getOrCreate() | ||
6 changes: 3 additions & 3 deletions
6
...tance/bundle/templates/default-python/classic/output/my_default_python/tests/main_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| from my_default_python.main import get_taxis, get_spark | ||
| from my_default_python import main | ||
|
|
||
|
|
||
| def test_main(): | ||
| taxis = get_taxis(get_spark()) | ||
| def test_find_all_taxis(): | ||
| taxis = main.find_all_taxis() | ||
| assert taxis.count() > 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 4 additions & 15 deletions
19
...emplates/default-python/serverless/output/my_default_python/src/my_default_python/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.