-
Notifications
You must be signed in to change notification settings - Fork 283
feat: allow optional database configuration #608
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
badmonster0
merged 3 commits into
cocoindex-io:main
from
vumichien:feature/database_not_provide_cocoindex_operation
Jun 11, 2025
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| """ | ||
| Test suite for optional database functionality in CocoIndex. | ||
|
|
||
| This module tests that: | ||
| 1. cocoindex.init() works without database settings | ||
| 2. Transform flows work without database | ||
| 3. Database functionality still works when database settings are provided | ||
| 4. Operations requiring database properly complain when no database is configured | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
| from unittest.mock import patch | ||
| import pytest | ||
|
|
||
| import cocoindex | ||
| from cocoindex import op | ||
| from cocoindex.setting import Settings | ||
|
|
||
|
|
||
| class TestOptionalDatabase: | ||
| """Test suite for optional database functionality.""" | ||
|
|
||
| def setup_method(self): | ||
| """Setup method called before each test.""" | ||
| # Stop any existing cocoindex instance | ||
| try: | ||
| cocoindex.stop() | ||
| except: | ||
| pass | ||
|
|
||
| def teardown_method(self): | ||
| """Teardown method called after each test.""" | ||
| # Stop cocoindex instance after each test | ||
| try: | ||
| cocoindex.stop() | ||
| except: | ||
| pass | ||
|
|
||
| def test_init_without_database(self): | ||
| """Test that cocoindex.init() works without database settings.""" | ||
| # Remove database environment variables | ||
| with patch.dict(os.environ, {}, clear=False): | ||
| # Remove database env vars if they exist | ||
| for env_var in [ | ||
| "COCOINDEX_DATABASE_URL", | ||
| "COCOINDEX_DATABASE_USER", | ||
| "COCOINDEX_DATABASE_PASSWORD", | ||
| ]: | ||
| os.environ.pop(env_var, None) | ||
|
|
||
| # Test initialization without database | ||
| cocoindex.init() | ||
|
|
||
| # If we get here without exception, the test passes | ||
| assert True | ||
|
|
||
| def test_transform_flow_without_database(self): | ||
| """Test that transform flows work without database.""" | ||
| # Remove database environment variables | ||
| with patch.dict(os.environ, {}, clear=False): | ||
| # Remove database env vars if they exist | ||
| for env_var in [ | ||
| "COCOINDEX_DATABASE_URL", | ||
| "COCOINDEX_DATABASE_USER", | ||
| "COCOINDEX_DATABASE_PASSWORD", | ||
| ]: | ||
| os.environ.pop(env_var, None) | ||
|
|
||
| # Initialize without database | ||
| cocoindex.init() | ||
|
|
||
| # Create a simple custom function for testing | ||
| @op.function() | ||
| def add_prefix(text: str) -> str: | ||
| """Add a prefix to text.""" | ||
| return f"processed: {text}" | ||
|
|
||
| @cocoindex.transform_flow() | ||
| def simple_transform( | ||
| text: cocoindex.DataSlice[str], | ||
| ) -> cocoindex.DataSlice[str]: | ||
| """A simple transform that adds a prefix.""" | ||
| return text.transform(add_prefix) | ||
|
|
||
| # Test the transform flow | ||
| result = simple_transform.eval("hello world") | ||
| expected = "processed: hello world" | ||
|
|
||
| assert result == expected | ||
|
|
||
| @pytest.mark.skipif( | ||
| not os.getenv("COCOINDEX_DATABASE_URL"), | ||
| reason="Database URL not configured in environment", | ||
| ) | ||
| def test_init_with_database(self): | ||
| """Test that cocoindex.init() works with database settings when available.""" | ||
| # This test only runs if database URL is configured | ||
| settings = Settings.from_env() | ||
| assert settings.database is not None | ||
| assert settings.database.url is not None | ||
|
|
||
| try: | ||
| cocoindex.init(settings) | ||
| assert True | ||
| except Exception as e: | ||
| assert ( | ||
| "Failed to connect to database" in str(e) | ||
| or "connection" in str(e).lower() | ||
| ) | ||
|
|
||
| def test_settings_from_env_without_database(self): | ||
| """Test that Settings.from_env() correctly handles missing database settings.""" | ||
| with patch.dict(os.environ, {}, clear=False): | ||
| # Remove database env vars if they exist | ||
| for env_var in [ | ||
| "COCOINDEX_DATABASE_URL", | ||
| "COCOINDEX_DATABASE_USER", | ||
| "COCOINDEX_DATABASE_PASSWORD", | ||
| ]: | ||
| os.environ.pop(env_var, None) | ||
|
|
||
| settings = Settings.from_env() | ||
| assert settings.database is None | ||
| assert settings.app_namespace == "" | ||
|
|
||
| def test_settings_from_env_with_database(self): | ||
| """Test that Settings.from_env() correctly handles database settings when provided.""" | ||
| test_url = "postgresql://test:test@localhost:5432/test" | ||
| test_user = "testuser" | ||
| test_password = "testpass" | ||
|
|
||
| with patch.dict( | ||
| os.environ, | ||
| { | ||
| "COCOINDEX_DATABASE_URL": test_url, | ||
| "COCOINDEX_DATABASE_USER": test_user, | ||
| "COCOINDEX_DATABASE_PASSWORD": test_password, | ||
| }, | ||
| ): | ||
| settings = Settings.from_env() | ||
| assert settings.database is not None | ||
| assert settings.database.url == test_url | ||
| assert settings.database.user == test_user | ||
| assert settings.database.password == test_password | ||
|
|
||
| def test_settings_from_env_with_partial_database_config(self): | ||
| """Test Settings.from_env() with only database URL (no user/password).""" | ||
| test_url = "postgresql://localhost:5432/test" | ||
|
|
||
| with patch.dict( | ||
| os.environ, | ||
| { | ||
| "COCOINDEX_DATABASE_URL": test_url, | ||
| }, | ||
| clear=False, | ||
| ): | ||
| # Remove user/password env vars if they exist | ||
| os.environ.pop("COCOINDEX_DATABASE_USER", None) | ||
| os.environ.pop("COCOINDEX_DATABASE_PASSWORD", None) | ||
|
|
||
| settings = Settings.from_env() | ||
| assert settings.database is not None | ||
| assert settings.database.url == test_url | ||
| assert settings.database.user is None | ||
| assert settings.database.password is None | ||
|
|
||
| def test_multiple_init_calls(self): | ||
| """Test that multiple init calls work correctly.""" | ||
| with patch.dict(os.environ, {}, clear=False): | ||
| # Remove database env vars if they exist | ||
| for env_var in [ | ||
| "COCOINDEX_DATABASE_URL", | ||
| "COCOINDEX_DATABASE_USER", | ||
| "COCOINDEX_DATABASE_PASSWORD", | ||
| ]: | ||
| os.environ.pop(env_var, None) | ||
|
|
||
| # First init | ||
| cocoindex.init() | ||
|
|
||
| # Stop and init again | ||
| cocoindex.stop() | ||
| cocoindex.init() | ||
|
|
||
| # Should work without issues | ||
| assert True | ||
|
|
||
| def test_app_namespace_setting(self): | ||
| """Test that app_namespace setting works correctly.""" | ||
| test_namespace = "test_app" | ||
|
|
||
| with patch.dict( | ||
| os.environ, | ||
| { | ||
| "COCOINDEX_APP_NAMESPACE": test_namespace, | ||
| }, | ||
| clear=False, | ||
| ): | ||
| # Remove database env vars if they exist | ||
| for env_var in [ | ||
| "COCOINDEX_DATABASE_URL", | ||
| "COCOINDEX_DATABASE_USER", | ||
| "COCOINDEX_DATABASE_PASSWORD", | ||
| ]: | ||
| os.environ.pop(env_var, None) | ||
|
|
||
| settings = Settings.from_env() | ||
| assert settings.app_namespace == test_namespace | ||
| assert settings.database is None | ||
|
|
||
| # Init should work with app namespace but no database | ||
| cocoindex.init(settings) | ||
| assert True | ||
|
|
||
|
|
||
| class TestDatabaseRequiredOperations: | ||
| """Test suite for operations that require database.""" | ||
|
|
||
| def setup_method(self): | ||
| """Setup method called before each test.""" | ||
| # Stop any existing cocoindex instance | ||
| try: | ||
| cocoindex.stop() | ||
| except: | ||
| pass | ||
|
|
||
| def teardown_method(self): | ||
| """Teardown method called after each test.""" | ||
| # Stop cocoindex instance after each test | ||
| try: | ||
| cocoindex.stop() | ||
| except: | ||
| pass | ||
|
|
||
| def test_database_required_error_message(self): | ||
| """Test that operations requiring database show proper error messages.""" | ||
| with patch.dict(os.environ, {}, clear=False): | ||
| # Remove database env vars if they exist | ||
| for env_var in [ | ||
| "COCOINDEX_DATABASE_URL", | ||
| "COCOINDEX_DATABASE_USER", | ||
| "COCOINDEX_DATABASE_PASSWORD", | ||
| ]: | ||
| os.environ.pop(env_var, None) | ||
|
|
||
| # Initialize without database | ||
| cocoindex.init() | ||
|
|
||
| assert True |
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
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.