| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
This utility provides a consistent way to check MongoDB sample database availability and conditionally skip tests when required sample data is missing. It's designed specifically for the MongoDB PyMongo Driver code example test suite.
The utility is included as part of the PyMongo code example test suite. Make sure you have the required dependencies:
pip install pymongo python-dotenvimport unittest
from utils.sample_data import requires_sample_data
class TestMovieQueries(unittest.TestCase):
@requires_sample_data("sample_mflix")
def test_find_movies(self):
# This test will be skipped if sample_mflix database is not available
# Your test implementation here
pass
@requires_sample_data("sample_mflix", collections=["movies", "theaters"])
def test_specific_collections(self):
# This test requires specific collections to be present
# Your test implementation here
pass
@requires_sample_data(["sample_mflix", "sample_restaurants"])
def test_multiple_databases(self):
# This test requires multiple sample databases
# Your test implementation here
passimport unittest
from utils.sample_data import ensure_sample_data_or_skip, check_sample_data_available
class TestRestaurantQueries(unittest.TestCase):
def test_restaurant_search(self):
# Check availability programmatically
ensure_sample_data_or_skip("sample_restaurants")
# Your test implementation here
pass
def test_conditional_logic(self):
# Check without skipping for conditional test logic
if check_sample_data_available("sample_mflix"):
# Run movie-related tests
pass
else:
# Run alternative tests or mock data
passimport unittest
from utils.sample_data import RequiresSampleDataMixin
class TestAnalytics(unittest.TestCase, RequiresSampleDataMixin):
def test_customer_analysis(self):
# Use mixin methods
self.ensure_sample_data("sample_analytics", ["customers", "accounts"])
# Your test implementation here
pass
def test_check_available_data(self):
available_dbs = self.get_available_sample_databases()
self.assertGreater(len(available_dbs), 0, "No sample databases available")The utility uses the same connection string configuration as the existing test suite:
CONNECTION_STRING=mongodb://your-connection-string
Example .env file:
# Local MongoDB
CONNECTION_STRING=mongodb://localhost:27017
# MongoDB Atlas
CONNECTION_STRING=mongodb+srv://username:password@cluster.mongodb.net/
# MongoDB with authentication
CONNECTION_STRING=mongodb://username:password@localhost:27017/adminThe utility recognizes these standard MongoDB sample databases:
| Database | Collections |
|---|---|
| sample_mflix | movies, theaters, users, comments, sessions |
| sample_restaurants | restaurants, neighborhoods |
| sample_training | posts, companies, inspections, routes, trips, grades, zips |
| sample_analytics | customers, accounts, transactions |
| sample_airbnb | listingsAndReviews |
| sample_geospatial | shipwrecks |
| sample_guides | planets, comets |
| sample_stores | sales |
| sample_supplies | sales |
| sample_weatherdata | data |
Decorator to mark tests that require specific sample databases and collections.
Parameters:
Examples:
@requires_sample_data("sample_mflix")
@requires_sample_data("sample_mflix", collections=["movies", "theaters"])
@requires_sample_data(["sample_mflix", "sample_restaurants"])
@requires_sample_data(
["sample_mflix", "sample_restaurants"],
collections_per_database={
"sample_mflix": ["movies", "theaters"],
"sample_restaurants": ["restaurants"]
}
)Check if a sample database and its collections are available.
Parameters:
Returns: bool - True if database and all required collections are available
Ensure sample data is available or skip the current test.
Parameters:
Raises: unittest.SkipTest if sample data is not available
Get list of available sample databases.
Returns: List[str] - List of available sample database names
Clear the sample data availability cache. Useful for testing or when database state changes.
Check multiple sample databases for availability.
Parameters:
Returns: Dict containing:
Ensure multiple sample databases are available or skip the current test.
Parameters:
Raises: unittest.SkipTest if any required sample data is not available
Mixin class that provides sample data checking methods to test classes.
Methods:
The utility automatically displays a summary of sample data availability once per test run:
When no sample data is available:
📊 Sample Data Status: No MongoDB sample databases found Some tests may be skipped. To load sample data: • Atlas: https://www.mongodb.com/docs/atlas/sample-data/ • Local: Use mongorestore with sample data archive
When sample data is available:
📊 Sample Data Status: 3 database(s) available Found: sample_mflix, sample_restaurants, sample_training
The utility is designed to be robust and never break test execution:
The utility includes comprehensive tests:
# Run all utility tests
python -m unittest discover -s utils/sample_data/tests -v
# Run specific test modules
python -m unittest utils.sample_data.tests.test_registry -v
python -m unittest utils.sample_data.tests.test_cache -v
python -m unittest utils.sample_data.tests.test_checker -v
python -m unittest utils.sample_data.tests.test_decorators -v
# Run integration tests (requires MongoDB connection)
python -m unittest utils.sample_data.tests.test_integration -v@requires_sample_data("sample_mflix", collections=["movies", "theaters"])
def test_movie_theater_relationships(self):
# Test that requires specific collections beyond the defaults
pass@requires_sample_data(
["sample_mflix", "sample_restaurants"],
collections_per_database={
"sample_mflix": ["movies"],
"sample_restaurants": ["restaurants", "neighborhoods"]
}
)
def test_cross_database_functionality(self):
# Test that uses data from multiple databases
passdef test_adaptive_behavior(self):
available_dbs = get_available_sample_databases()
if "sample_mflix" in available_dbs:
# Use real movie data
self._test_with_real_data()
else:
# Use mock data
self._test_with_mock_data()def test_with_fresh_cache(self):
# Clear cache to ensure fresh database checks
clear_sample_data_cache()
# Your test implementation
passIf tests are being skipped unexpectedly:
If tests are running slowly:
If you encounter import errors:
# Make sure to import from the correct path
from utils.sample_data import requires_sample_data, ensure_sample_data_or_skip
# For mixin usage
from utils.sample_data import RequiresSampleDataMixinWhen adding new sample databases to the registry:
This utility is part of the MongoDB documentation project and follows the same licensing terms.
| Back | FazBrowse Home | New Git URL |