Repository navigation
Report invalid source schemes as OperationalError #296
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9f642c8
fix: report missing and unknown source schemes as OperationalError
ryanduguid 3c760fe
ci: preserve established renderer and command signatures under Ruff 0.16
ryanduguid 7f60479
ci: scope Ruff positional-argument exceptions to existing functions
ryanduguid 36f2dff
docs: explain dotted source import errors
ryanduguid 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| import beanquery | ||
|
|
||
|
|
||
| class TestConnection(unittest.TestCase): | ||
| def test_connect_without_source(self): | ||
| for dsn in [None, '']: | ||
| with self.subTest(dsn=dsn): | ||
| connection = beanquery.connect(dsn) | ||
| self.assertEqual(connection.execute('SELECT 42').fetchall(), [(42,)]) | ||
|
|
||
| def test_missing_scheme(self): | ||
| connection = beanquery.connect(None) | ||
| for dsn in ['', 'ledger.beancount', './ledger.beancount']: | ||
| with self.subTest(dsn=dsn), self.assertRaisesRegex(beanquery.OperationalError, '^missing source scheme$'): | ||
| connection.attach(dsn) | ||
|
|
||
| def test_connect_missing_scheme(self): | ||
| with self.assertRaisesRegex(beanquery.OperationalError, '^missing source scheme$'): | ||
| beanquery.connect('ledger.beancount') | ||
|
|
||
| def test_unknown_scheme(self): | ||
| for scheme in ['beancounnt', 'unknown', 'foo.bar', 'foo.bar.baz', 'foo+bar', 'foo-bar']: | ||
| with self.subTest(scheme=scheme): | ||
| with self.assertRaises(beanquery.OperationalError) as caught: | ||
| beanquery.connect(f'{scheme}:ledger') | ||
| self.assertEqual(str(caught.exception), f'unknown source scheme: {scheme!r}') | ||
|
|
||
| def test_message_only_contains_scheme(self): | ||
| with self.assertRaises(beanquery.OperationalError) as caught: | ||
| beanquery.connect('unknown://example.invalid/private-ledger?fixture=synthetic') | ||
| self.assertEqual(str(caught.exception), "unknown source scheme: 'unknown'") | ||
|
|
||
| def test_import_dependency_failure(self): | ||
| names = ['some_dependency', 'beanquery.sources', 'beanquery.sources.other', 'beanquery.sources.foo.optional', None] | ||
| for name in names: | ||
| error = ModuleNotFoundError('missing dependency', name=name) | ||
| with self.subTest(name=name), mock.patch('beanquery.importlib.import_module', side_effect=error): | ||
| with self.assertRaises(ModuleNotFoundError) as caught: | ||
| beanquery.connect('foo.bar:ledger') | ||
| self.assertIs(caught.exception, error) | ||
|
|
||
| def test_import_error(self): | ||
| error = ImportError('source import failed') | ||
| with mock.patch('beanquery.importlib.import_module', side_effect=error): | ||
| with self.assertRaises(ImportError) as caught: | ||
| beanquery.connect('test:') | ||
| self.assertIs(caught.exception, error) | ||
|
|
||
| def test_source_attach_failure(self): | ||
| error = ModuleNotFoundError('source attach failed', name='beanquery.sources.test') | ||
| source = mock.Mock() | ||
| source.attach.side_effect = error | ||
| with mock.patch('beanquery.importlib.import_module', return_value=source): | ||
| with self.assertRaises(ModuleNotFoundError) as caught: | ||
| beanquery.connect('test:') | ||
| self.assertIs(caught.exception, error) | ||
|
|
||
| def test_source_attach_arguments(self): | ||
| source = mock.Mock() | ||
| dsn = 'test:fixture?name=example' | ||
| entries = [] | ||
| options = {'example': 'synthetic'} | ||
| with mock.patch('beanquery.importlib.import_module', return_value=source): | ||
| connection = beanquery.connect(dsn, entries=entries, options=options) | ||
| source.attach.assert_called_once_with(connection, dsn, entries=entries, options=options) | ||
|
|
||
| def test_valid_source(self): | ||
| connection = beanquery.connect('test:magic?name=example&start=1&stop=3') | ||
| self.assertEqual(connection.execute('SELECT x FROM #example').fetchall(), [(1,), (2,)]) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain what this code is doing? I cannot understand why it is needed in the form it is written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python can name a missing parent package instead of the requested module. For
foo.bar:ledger, the import requestsbeanquery.sources.foo.bar, butexc.namecan bebeanquery.sources.foo.The predicate converts a missing source module or one of its parents below
beanquery.sourcestoOperationalError. It re-raises unrelated dependency errors, and excludesbeanquery.sourcesitself.source.attach()runs outside the handler so its failures also propagate.I've named
exc.namelocally and added a comment explaining the dotted-parent case. The real dotted-scheme and dependency tests pass, along with all 303 Linux tests, Ruff, ten Windows connection tests and both package builds.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems still very much LLM crap. I warned you. Goodbye