Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /TrueSelph_jvspatial /tests /db /test_mongodb.py
| """Test suite for MongoDB database implementation. | |
| Tests the simplified MongoDB database functionality. | |
| """ | |
| from unittest.mock import AsyncMock, MagicMock, patch | |
| import pytest | |
| from jvspatial.db.mongodb import MongoDB | |
| from jvspatial.exceptions import DatabaseError | |
| class TestMongoDBBasicOperations: | |
| """Test basic MongoDB operations.""" | |
| def mongodb(self): | |
| """Create MongoDB instance for testing.""" | |
| with patch("jvspatial.db.mongodb.AsyncIOMotorClient") as mock_client_class: | |
| mock_client = MagicMock() | |
| mock_db = MagicMock() | |
| mock_client_class.return_value = mock_client | |
| mock_client.__getitem__.return_value = mock_db | |
| db = MongoDB(uri="mongodb://localhost:27017/test", db_name="test_db") | |
| db._client = mock_client | |
| db._db = mock_db | |
| return db | |
| def mock_collection(self, mongodb): | |
| """Create mock collection for testing.""" | |
| mock_collection = AsyncMock() | |
| mongodb._db.__getitem__.return_value = mock_collection | |
| return mock_collection | |
| async def test_mongodb_initialization(self): | |
| """Test MongoDB initialization.""" | |
| with patch("jvspatial.db.mongodb.AsyncIOMotorClient"): | |
| db = MongoDB(uri="mongodb://test:27017", db_name="test_db") | |
| assert db.uri == "mongodb://test:27017" | |
| assert db.db_name == "test_db" | |
| assert db._client is None | |
| assert db._db is None | |
| async def test_save_record(self, mongodb, mock_collection): | |
| """Test saving a record.""" | |
| test_data = {"id": "test_id", "name": "test_record", "value": 42} | |
| mock_collection.replace_one.return_value = AsyncMock() | |
| result = await mongodb.save("test_collection", test_data) | |
| assert result == test_data | |
| assert result["_id"] == "test_id" # MongoDB uses _id | |
| mock_collection.replace_one.assert_called_once_with( | |
| {"_id": "test_id"}, test_data, upsert=True | |
| ) | |
| async def test_save_record_without_id(self, mongodb, mock_collection): | |
| """Test saving a record without ID (generates UUID).""" | |
| test_data = {"name": "test_record", "value": 42} | |
| mock_collection.replace_one.return_value = AsyncMock() | |
| with patch("uuid.uuid4", return_value=MagicMock(hex="test-uuid")): | |
| result = await mongodb.save("test_collection", test_data) | |
| assert result["_id"] is not None | |
| mock_collection.replace_one.assert_called_once() | |
| async def test_get_record(self, mongodb, mock_collection): | |
| """Test retrieving a record.""" | |
| test_data = {"_id": "test_id", "name": "test_record", "value": 42} | |
| mock_collection.find_one.return_value = test_data | |
| result = await mongodb.get("test_collection", "test_id") | |
| assert result == test_data | |
| mock_collection.find_one.assert_called_once_with({"_id": "test_id"}) | |
| async def test_get_nonexistent_record(self, mongodb, mock_collection): | |
| """Test retrieving a non-existent record.""" | |
| mock_collection.find_one.return_value = None | |
| result = await mongodb.get("test_collection", "nonexistent_id") | |
| assert result is None | |
| mock_collection.find_one.assert_called_once_with({"_id": "nonexistent_id"}) | |
| async def test_delete_record(self, mongodb, mock_collection): | |
| """Test deleting a record.""" | |
| mock_collection.delete_one.return_value = AsyncMock() | |
| await mongodb.delete("test_collection", "test_id") | |
| mock_collection.delete_one.assert_called_once_with({"_id": "test_id"}) | |
| async def test_find_records(self, mongodb, mock_collection): | |
| """Test finding records with query.""" | |
| test_data = [ | |
| {"_id": "id1", "name": "record1", "value": 10}, | |
| {"_id": "id2", "name": "record2", "value": 20}, | |
| ] | |
| mock_cursor = AsyncMock() | |
| mock_cursor.to_list.return_value = test_data | |
| mock_collection.find = MagicMock(return_value=mock_cursor) | |
| query = {"value": {"$gte": 10}} | |
| result = await mongodb.find("test_collection", query) | |
| assert result == test_data | |
| mock_collection.find.assert_called_once_with(query) | |
| mock_cursor.to_list.assert_called_once_with(length=None) | |
| async def test_find_all_records(self, mongodb, mock_collection): | |
| """Test finding all records (empty query).""" | |
| test_data = [ | |
| {"_id": "id1", "name": "record1"}, | |
| {"_id": "id2", "name": "record2"}, | |
| ] | |
| mock_cursor = AsyncMock() | |
| mock_cursor.to_list.return_value = test_data | |
| mock_collection.find = MagicMock(return_value=mock_cursor) | |
| result = await mongodb.find("test_collection", {}) | |
| assert result == test_data | |
| mock_collection.find.assert_called_once_with({}) | |
| async def test_close_connection(self, mongodb): | |
| """Test closing database connection.""" | |
| mock_client = MagicMock() | |
| mongodb._client = mock_client | |
| mongodb._db = MagicMock() | |
| await mongodb.close() | |
| mock_client.close.assert_called_once() | |
| assert mongodb._client is None | |
| assert mongodb._db is None | |
| class TestMongoDBErrorHandling: | |
| """Test MongoDB error handling.""" | |
| def mongodb(self): | |
| """Create MongoDB instance for testing.""" | |
| with patch("jvspatial.db.mongodb.AsyncIOMotorClient") as mock_client_class: | |
| mock_client = MagicMock() | |
| mock_db = MagicMock() | |
| mock_client_class.return_value = mock_client | |
| mock_client.__getitem__.return_value = mock_db | |
| db = MongoDB(uri="mongodb://localhost:27017/test", db_name="test_db") | |
| db._client = mock_client | |
| db._db = mock_db | |
| return db | |
| async def test_save_error_handling(self, mongodb): | |
| """Test error handling during save operation.""" | |
| from pymongo.errors import PyMongoError | |
| mock_collection = AsyncMock() | |
| mock_collection.replace_one.side_effect = PyMongoError("Connection failed") | |
| mongodb._db = MagicMock() | |
| mongodb._db.__getitem__.return_value = mock_collection | |
| test_data = {"id": "test_id", "name": "test"} | |
| with pytest.raises(DatabaseError) as exc_info: | |
| await mongodb.save("test_collection", test_data) | |
| assert "MongoDB save error" in str(exc_info.value) | |
| assert "Connection failed" in str(exc_info.value) | |
| async def test_get_error_handling(self, mongodb): | |
| """Test error handling during get operation.""" | |
| from pymongo.errors import PyMongoError | |
| mock_collection = AsyncMock() | |
| mock_collection.find_one.side_effect = PyMongoError("Query failed") | |
| mongodb._db = MagicMock() | |
| mongodb._db.__getitem__.return_value = mock_collection | |
| with pytest.raises(DatabaseError) as exc_info: | |
| await mongodb.get("test_collection", "test_id") | |
| assert "MongoDB get error" in str(exc_info.value) | |
| assert "Query failed" in str(exc_info.value) | |
| async def test_delete_error_handling(self, mongodb): | |
| """Test error handling during delete operation.""" | |
| from pymongo.errors import PyMongoError | |
| mock_collection = AsyncMock() | |
| mock_collection.delete_one.side_effect = PyMongoError("Delete failed") | |
| mongodb._db = MagicMock() | |
| mongodb._db.__getitem__.return_value = mock_collection | |
| with pytest.raises(DatabaseError) as exc_info: | |
| await mongodb.delete("test_collection", "test_id") | |
| assert "MongoDB delete error" in str(exc_info.value) | |
| assert "Delete failed" in str(exc_info.value) | |
| async def test_find_error_handling(self, mongodb): | |
| """Test error handling during find operation.""" | |
| from pymongo.errors import PyMongoError | |
| mock_collection = AsyncMock() | |
| mock_cursor = AsyncMock() | |
| mock_cursor.to_list.side_effect = PyMongoError("Find failed") | |
| mock_collection.find = MagicMock(return_value=mock_cursor) | |
| mongodb._db.__getitem__.return_value = mock_collection | |
| with pytest.raises(DatabaseError) as exc_info: | |
| await mongodb.find("test_collection", {"name": "test"}) | |
| assert "MongoDB find error" in str(exc_info.value) | |
| assert "Find failed" in str(exc_info.value) | |
| class TestMongoDBConnection: | |
| """Test MongoDB connection handling.""" | |
| async def test_connection_lazy_initialization(self): | |
| """Test that connection is only established when needed.""" | |
| with patch("jvspatial.db.mongodb.AsyncIOMotorClient") as mock_client_class: | |
| db = MongoDB(uri="mongodb://localhost:27017/test", db_name="test_db") | |
| # Connection should not be established yet | |
| assert db._client is None | |
| assert db._db is None | |
| # Mock the client and database | |
| mock_client = MagicMock() | |
| mock_db = MagicMock() | |
| mock_client_class.return_value = mock_client | |
| mock_client.__getitem__.return_value = mock_db | |
| # Mock collection operations | |
| mock_collection = AsyncMock() | |
| mock_collection.find_one.return_value = {"_id": "test", "name": "test"} | |
| mock_db.__getitem__.return_value = mock_collection | |
| # Now perform an operation that requires connection | |
| result = await db.get("test_collection", "test_id") | |
| # Connection should now be established | |
| assert db._client is not None | |
| assert db._db is not None | |
| mock_client_class.assert_called_once_with( | |
| "mongodb://localhost:27017/test", | |
| maxPoolSize=10, | |
| minPoolSize=0, | |
| maxIdleTimeMS=60000, | |
| ) | |
| assert result is not None | |
| async def test_multiple_operations_reuse_connection(self): | |
| """Test that multiple operations reuse the same connection.""" | |
| with patch("jvspatial.db.mongodb.AsyncIOMotorClient") as mock_client_class: | |
| db = MongoDB(uri="mongodb://localhost:27017/test", db_name="test_db") | |
| # Mock the client and database | |
| mock_client = MagicMock() | |
| mock_db = MagicMock() | |
| mock_client_class.return_value = mock_client | |
| mock_client.__getitem__.return_value = mock_db | |
| # Mock collection operations | |
| mock_collection = AsyncMock() | |
| mock_collection.find_one.return_value = {"_id": "test", "name": "test"} | |
| mock_collection.replace_one.return_value = AsyncMock() | |
| mock_db.__getitem__.return_value = mock_collection | |
| # Perform multiple operations | |
| await db.get("test_collection", "test_id") | |
| await db.save("test_collection", {"id": "test", "name": "test"}) | |
| # Client should only be created once | |
| assert mock_client_class.call_count == 1 | |
| assert db._client is not None | |
| assert db._db is not None | |
| class TestMongoDBIntegration: | |
| """Test MongoDB integration scenarios.""" | |
| def mongodb(self): | |
| """Create MongoDB instance for testing.""" | |
| with patch("jvspatial.db.mongodb.AsyncIOMotorClient") as mock_client_class: | |
| mock_client = MagicMock() | |
| mock_db = MagicMock() | |
| mock_client_class.return_value = mock_client | |
| mock_client.__getitem__.return_value = mock_db | |
| db = MongoDB(uri="mongodb://localhost:27017/test", db_name="test_db") | |
| db._client = mock_client | |
| db._db = mock_db | |
| return db | |
| def mock_collection(self, mongodb): | |
| """Create mock collection for testing.""" | |
| mock_collection = AsyncMock() | |
| mongodb._db.__getitem__.return_value = mock_collection | |
| return mock_collection | |
| async def test_crud_workflow(self, mongodb, mock_collection): | |
| """Test complete CRUD workflow.""" | |
| # Create | |
| test_data = {"id": "test_id", "name": "test_record", "value": 42} | |
| mock_collection.replace_one.return_value = AsyncMock() | |
| saved_data = await mongodb.save("test_collection", test_data) | |
| assert saved_data == test_data | |
| # Read | |
| mock_collection.find_one.return_value = test_data | |
| retrieved_data = await mongodb.get("test_collection", "test_id") | |
| assert retrieved_data == test_data | |
| # Update | |
| updated_data = {"id": "test_id", "name": "updated_record", "value": 100} | |
| mock_collection.replace_one.return_value = AsyncMock() | |
| saved_updated = await mongodb.save("test_collection", updated_data) | |
| assert saved_updated == updated_data | |
| # Delete | |
| mock_collection.delete_one.return_value = AsyncMock() | |
| await mongodb.delete("test_collection", "test_id") | |
| # Verify all operations were called | |
| assert mock_collection.replace_one.call_count == 2 | |
| assert mock_collection.find_one.call_count == 1 | |
| assert mock_collection.delete_one.call_count == 1 | |
| async def test_query_operations(self, mongodb, mock_collection): | |
| """Test various query operations.""" | |
| # Mock cursor and results | |
| mock_cursor = AsyncMock() | |
| test_results = [ | |
| {"_id": "id1", "name": "record1", "value": 10}, | |
| {"_id": "id2", "name": "record2", "value": 20}, | |
| {"_id": "id3", "name": "record3", "value": 30}, | |
| ] | |
| mock_cursor.to_list.return_value = test_results | |
| mock_collection.find = MagicMock(return_value=mock_cursor) | |
| # Test different query types | |
| queries = [ | |
| {}, # Find all | |
| {"name": "record1"}, # Exact match | |
| {"value": {"$gte": 20}}, # Range query | |
| {"name": {"$regex": "record"}}, # Regex query | |
| ] | |
| for query in queries: | |
| result = await mongodb.find("test_collection", query) | |
| assert result == test_results | |
| mock_collection.find.assert_called_with(query) | |
| # Verify find was called for each query | |
| assert mock_collection.find.call_count == len(queries) | |
| async def test_collection_isolation(self, mongodb, mock_collection): | |
| """Test that different collections are isolated.""" | |
| # Mock different collections | |
| mock_collection1 = AsyncMock() | |
| mock_collection2 = AsyncMock() | |
| mongodb._db = MagicMock() | |
| mongodb._db.__getitem__.side_effect = lambda name: { | |
| "collection1": mock_collection1, | |
| "collection2": mock_collection2, | |
| }[name] | |
| # Test operations on different collections | |
| mock_collection1.replace_one.return_value = AsyncMock() | |
| mock_collection2.replace_one.return_value = AsyncMock() | |
| await mongodb.save("collection1", {"id": "test1", "name": "test1"}) | |
| await mongodb.save("collection2", {"id": "test2", "name": "test2"}) | |
| # Verify each collection was accessed correctly | |
| mock_collection1.replace_one.assert_called_once() | |
| mock_collection2.replace_one.assert_called_once() | |
| class TestMongoDBFindOneAndDelete: | |
| """Test find_one_and_delete atomic operation.""" | |
| def mongodb(self): | |
| """Create MongoDB instance for testing.""" | |
| with patch("jvspatial.db.mongodb.AsyncIOMotorClient") as mock_client_class: | |
| mock_client = MagicMock() | |
| mock_db = MagicMock() | |
| mock_client_class.return_value = mock_client | |
| mock_client.__getitem__.return_value = mock_db | |
| db = MongoDB(uri="mongodb://localhost:27017/test", db_name="test_db") | |
| db._client = mock_client | |
| db._db = mock_db | |
| return db | |
| def mock_collection(self, mongodb): | |
| """Create mock collection for testing.""" | |
| mock_collection = AsyncMock() | |
| mongodb._db.__getitem__.return_value = mock_collection | |
| return mock_collection | |
| async def test_find_one_and_delete_returns_deleted_doc( | |
| self, mongodb, mock_collection | |
| ): | |
| """Test find_one_and_delete returns the deleted document.""" | |
| deleted_doc = {"_id": "sender_123", "media_items": [], "updated_at": 1234.5} | |
| mock_collection.find_one_and_delete.return_value = deleted_doc | |
| result = await mongodb.find_one_and_delete( | |
| "media_batches", {"_id": "sender_123"} | |
| ) | |
| assert result == deleted_doc | |
| mock_collection.find_one_and_delete.assert_called_once_with( | |
| {"_id": "sender_123"} | |
| ) | |
| async def test_find_one_and_delete_returns_none_when_not_found( | |
| self, mongodb, mock_collection | |
| ): | |
| """Test find_one_and_delete returns None when no document matches.""" | |
| mock_collection.find_one_and_delete.return_value = None | |
| result = await mongodb.find_one_and_delete( | |
| "media_batches", {"_id": "nonexistent"} | |
| ) | |
| assert result is None | |
| mock_collection.find_one_and_delete.assert_called_once_with( | |
| {"_id": "nonexistent"} | |
| ) | |
| async def test_find_one_and_delete_error_handling(self, mongodb): | |
| """Test find_one_and_delete raises DatabaseError on failure.""" | |
| from pymongo.errors import PyMongoError | |
| mock_collection = AsyncMock() | |
| mock_collection.find_one_and_delete.side_effect = PyMongoError("Delete failed") | |
| mongodb._db.__getitem__.return_value = mock_collection | |
| with pytest.raises(DatabaseError) as exc_info: | |
| await mongodb.find_one_and_delete("media_batches", {"_id": "sender_123"}) | |
| assert "find_one_and_delete" in str(exc_info.value) | |
| assert "Delete failed" in str(exc_info.value) | |
| class TestMongoDBFindOneAndUpdate: | |
| """Test find_one_and_update atomic operation.""" | |
| def mongodb(self): | |
| """Create MongoDB instance for testing.""" | |
| with patch("jvspatial.db.mongodb.AsyncIOMotorClient") as mock_client_class: | |
| mock_client = MagicMock() | |
| mock_db = MagicMock() | |
| mock_client_class.return_value = mock_client | |
| mock_client.__getitem__.return_value = mock_db | |
| db = MongoDB(uri="mongodb://localhost:27017/test", db_name="test_db") | |
| db._client = mock_client | |
| db._db = mock_db | |
| return db | |
| def mock_collection(self, mongodb): | |
| """Create mock collection for testing.""" | |
| mock_collection = AsyncMock() | |
| mongodb._db.__getitem__.return_value = mock_collection | |
| return mock_collection | |
| async def test_find_one_and_update_returns_updated_doc( | |
| self, mongodb, mock_collection | |
| ): | |
| """Test find_one_and_update returns the updated document (AFTER).""" | |
| from pymongo import ReturnDocument | |
| updated_doc = { | |
| "_id": "sender_123", | |
| "media_items": [{"url": "https://example.com/img.jpg", "utterance": None}], | |
| "updated_at": 1234.5, | |
| } | |
| mock_collection.find_one_and_update.return_value = updated_doc | |
| update = { | |
| "$push": {"media_items": {"url": "https://example.com/img.jpg"}}, | |
| "$set": {"updated_at": 1234.5}, | |
| } | |
| result = await mongodb.find_one_and_update( | |
| "media_batches", {"_id": "sender_123"}, update, upsert=True | |
| ) | |
| assert result == updated_doc | |
| mock_collection.find_one_and_update.assert_called_once() | |
| call_kwargs = mock_collection.find_one_and_update.call_args[1] | |
| assert call_kwargs["upsert"] is True | |
| assert call_kwargs["return_document"] == ReturnDocument.AFTER | |
| async def test_find_one_and_update_with_push_and_set( | |
| self, mongodb, mock_collection | |
| ): | |
| """Test find_one_and_update with $push and $set operators.""" | |
| updated_doc = { | |
| "_id": "sender_456", | |
| "media_items": [{"url": "img1.jpg"}, {"url": "img2.jpg"}], | |
| "updated_at": 5678.9, | |
| } | |
| mock_collection.find_one_and_update.return_value = updated_doc | |
| update = { | |
| "$push": {"media_items": {"url": "img2.jpg"}}, | |
| "$set": {"updated_at": 5678.9}, | |
| "$setOnInsert": {"entity_id": "entity_1", "created_at": 5678.9}, | |
| } | |
| result = await mongodb.find_one_and_update( | |
| "media_batches", {"_id": "sender_456"}, update, upsert=True | |
| ) | |
| assert result == updated_doc | |
| assert len(result["media_items"]) == 2 | |
| call_args = mock_collection.find_one_and_update.call_args[0] | |
| assert call_args[0] == {"_id": "sender_456"} # query | |
| assert call_args[1] == update # update | |
| async def test_find_one_and_update_returns_none_when_no_match_no_upsert( | |
| self, mongodb, mock_collection | |
| ): | |
| """Test find_one_and_update returns None when no match and upsert=False.""" | |
| mock_collection.find_one_and_update.return_value = None | |
| result = await mongodb.find_one_and_update( | |
| "media_batches", | |
| {"_id": "nonexistent"}, | |
| {"$set": {"updated_at": 1234.5}}, | |
| upsert=False, | |
| ) | |
| assert result is None | |
| async def test_find_one_and_update_error_handling(self, mongodb): | |
| """Test find_one_and_update raises DatabaseError on failure.""" | |
| from pymongo.errors import PyMongoError | |
| mock_collection = AsyncMock() | |
| mock_collection.find_one_and_update.side_effect = PyMongoError("Update failed") | |
| mongodb._db.__getitem__.return_value = mock_collection | |
| with pytest.raises(DatabaseError) as exc_info: | |
| await mongodb.find_one_and_update( | |
| "media_batches", | |
| {"_id": "sender_123"}, | |
| {"$set": {"updated_at": 1234.5}}, | |
| ) | |
| assert "find_one_and_update" in str(exc_info.value) | |
| assert "Update failed" in str(exc_info.value) | |
Xet Storage Details
- Size:
- 22.6 kB
- Xet hash:
- 88a0a7aeca8b5e80bf66ff00663004a07a2f6c9cbe7fdeb86d2091a32ccb4f55
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.