def test_duplicate_key_error(message, index_name): err = DuplicateKeyError(message) assert isinstance(err, pymongo.errors.DuplicateKeyError) assert str(err) == message assert err.index_name == index_name
async def insert_one(self, *args, **kwargs): """Insert one document. Returns: Inserted ``_id`` value. Raises: aiomongodel.errors.DuplicateKeyError: On duplicate key error. """ try: res = await self.collection.insert_one(*args, **kwargs) except pymongo.errors.DuplicateKeyError as e: raise DuplicateKeyError(str(e)) from e return res.inserted_id if res.acknowledged else None
async def update_many(self, query, *args, **kwargs): """Update many documents. Returns: int: Number of modified documents. Raises: aiomongodel.errors.DuplicateKeyError: On duplicate key error. """ try: res = await self.collection.update_many(self._update_query(query), *args, **kwargs) except pymongo.errors.DuplicateKeyError as e: raise DuplicateKeyError(str(e)) from e return res.modified_count if res.acknowledged else None