Fix test connectivity by implementing lazy connection initialization #61
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.
Problem
The test suite was failing with 44 errors when run in environments without network access to
api.tibber.com. Even though the project includes a backup data fallback mechanism (tests/backup_demo_data.json) inconftest.py, it wasn't working becauseQueryExecutorwas attempting to connect to the API during initialization, regardless of theimmediate_updateparameter.Solution
Implemented lazy connection initialization to defer API connection until it's actually needed:
Added
connect_on_initparameter toQueryExecutor.__init__()Truefor backward compatibilityChanged
fetch_schema_from_transport=FalseAdded
_ensure_connected()methodexecute_async_single()Updated
Account.__init__()connect_on_init=immediate_updateto parent classimmediate_update=FalseImpact
Before: 0/46 tests passing (44 errors, 2 failures)
After: 40/46 tests passing (87% success rate)
All cached/offline tests now pass successfully:
The 6 remaining failures are expected:
Benefits
This change enables:
Backward Compatibility
All changes are fully backward compatible. Existing code continues to work exactly as before:
Testing
Documentation
Added comprehensive test status documentation:
TEST_STATUS_REPORT.md- Detailed analysis and recommendationsTEST_RESULTS_SUMMARY.txt- Visual test results summaryCloses #[issue_number]
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
api.tibber.compython -m pytest tests -v(dns block)import tibber
import json
try:
account = tibber.Account(tibber.DEMO_TOKEN)
print('Success with live API')
except Exception as e:
print(f'Failed to connect: {e}')
print('Using backup data...')
account = tibber.Account(tibber.DEMO_TOKEN, immediate_update=False)
with open('./tests/backup_demo_data.json', 'r') as f:
data = json.load(f)
account.update_cache(data)
print(f'Account name: {account.name}')` (dns block)
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.