Skip to content

Commit e98c5af

Browse files
committed
Add prose test
1 parent 3cfa69c commit e98c5af

File tree

6 files changed

+32
-18
lines changed

6 files changed

+32
-18
lines changed

gridfs/asynchronous/grid_file.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -848,21 +848,20 @@ async def delete_by_name(
848848
await fs.upload_from_stream("test_file", "data I want to store!")
849849
await fs.delete_by_name("test_file")
850850
851-
Raises :exc:`~gridfs.errors.NoFile` if no file with filename exists.
851+
Raises :exc:`~gridfs.errors.NoFile` if no file with the given filename exists.
852852
853853
:param filename: The name of the file to be deleted.
854-
:param session: a
855-
:class:`~pymongo.client_session.AsyncClientSession`
854+
:param session: a :class:`~pymongo.client_session.AsyncClientSession`
856855
857-
.. versionadded:: 4.13
856+
.. versionadded:: 4.12
858857
"""
859858
_disallow_transactions(session)
860859
files = self._files.find({"filename": filename}, {"_id": 1}, session=session)
861860
file_ids = [file_id["_id"] async for file_id in files]
862861
res = await self._files.delete_many({"_id": {"$in": file_ids}}, session=session)
863862
await self._chunks.delete_many({"files_id": {"$in": file_ids}}, session=session)
864863
if not res.deleted_count:
865-
raise NoFile("no file could be deleted because none matched %s" % filename)
864+
raise NoFile(f"no file could be deleted because none matched filename {filename!r}")
866865

867866
def find(self, *args: Any, **kwargs: Any) -> AsyncGridOutCursor:
868867
"""Find and return the files collection documents that match ``filter``

gridfs/synchronous/grid_file.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,21 +842,20 @@ def delete_by_name(self, filename: str, session: Optional[ClientSession] = None)
842842
fs.upload_from_stream("test_file", "data I want to store!")
843843
fs.delete_by_name("test_file")
844844
845-
Raises :exc:`~gridfs.errors.NoFile` if no file with filename exists.
845+
Raises :exc:`~gridfs.errors.NoFile` if no file with the given filename exists.
846846
847847
:param filename: The name of the file to be deleted.
848-
:param session: a
849-
:class:`~pymongo.client_session.ClientSession`
848+
:param session: a :class:`~pymongo.client_session.ClientSession`
850849
851-
.. versionadded:: 4.13
850+
.. versionadded:: 4.12
852851
"""
853852
_disallow_transactions(session)
854853
files = self._files.find({"filename": filename}, {"_id": 1}, session=session)
855854
file_ids = [file_id["_id"] for file_id in files]
856855
res = self._files.delete_many({"_id": {"$in": file_ids}}, session=session)
857856
self._chunks.delete_many({"files_id": {"$in": file_ids}}, session=session)
858857
if not res.deleted_count:
859-
raise NoFile("no file could be deleted because none matched %s" % filename)
858+
raise NoFile(f"no file could be deleted because none matched filename {filename!r}")
860859

861860
def find(self, *args: Any, **kwargs: Any) -> GridOutCursor:
862861
"""Find and return the files collection documents that match ``filter``

test/asynchronous/test_gridfs_bucket.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ async def test_multi_chunk_delete(self):
115115
self.assertEqual(0, await self.db.fs.files.count_documents({}))
116116
self.assertEqual(0, await self.db.fs.chunks.count_documents({}))
117117

118+
async def test_delete_by_name(self):
119+
self.assertEqual(0, await self.db.fs.files.count_documents({}))
120+
self.assertEqual(0, await self.db.fs.chunks.count_documents({}))
121+
gfs = gridfs.AsyncGridFSBucket(self.db)
122+
await gfs.upload_from_stream("test_filename", b"hello", chunk_size_bytes=1)
123+
self.assertEqual(1, await self.db.fs.files.count_documents({}))
124+
self.assertEqual(5, await self.db.fs.chunks.count_documents({}))
125+
await gfs.delete_by_name("test_filename")
126+
self.assertEqual(0, await self.db.fs.files.count_documents({}))
127+
self.assertEqual(0, await self.db.fs.chunks.count_documents({}))
128+
118129
async def test_empty_file(self):
119130
oid = await self.fs.upload_from_stream("test_filename", b"")
120131
self.assertEqual(b"", await (await self.fs.open_download_stream(oid)).read())

test/asynchronous/unified_format.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,7 @@ def process_error(self, exception, spec):
628628
# Connection errors are considered client errors.
629629
if isinstance(error, ConnectionFailure):
630630
self.assertNotIsInstance(error, NotPrimaryError)
631-
elif isinstance(error, (InvalidOperation, ConfigurationError, EncryptionError)):
632-
pass
633-
# gridfs NoFile errors are considered client errors.
634-
elif isinstance(error, NoFile):
631+
elif isinstance(error, (InvalidOperation, ConfigurationError, EncryptionError, NoFile)):
635632
pass
636633
else:
637634
self.assertNotIsInstance(error, PyMongoError)

test/test_gridfs_bucket.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ def test_multi_chunk_delete(self):
115115
self.assertEqual(0, self.db.fs.files.count_documents({}))
116116
self.assertEqual(0, self.db.fs.chunks.count_documents({}))
117117

118+
def test_delete_by_name(self):
119+
self.assertEqual(0, self.db.fs.files.count_documents({}))
120+
self.assertEqual(0, self.db.fs.chunks.count_documents({}))
121+
gfs = gridfs.GridFSBucket(self.db)
122+
gfs.upload_from_stream("test_filename", b"hello", chunk_size_bytes=1)
123+
self.assertEqual(1, self.db.fs.files.count_documents({}))
124+
self.assertEqual(5, self.db.fs.chunks.count_documents({}))
125+
gfs.delete_by_name("test_filename")
126+
self.assertEqual(0, self.db.fs.files.count_documents({}))
127+
self.assertEqual(0, self.db.fs.chunks.count_documents({}))
128+
118129
def test_empty_file(self):
119130
oid = self.fs.upload_from_stream("test_filename", b"")
120131
self.assertEqual(b"", (self.fs.open_download_stream(oid)).read())

test/unified_format.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,7 @@ def process_error(self, exception, spec):
627627
# Connection errors are considered client errors.
628628
if isinstance(error, ConnectionFailure):
629629
self.assertNotIsInstance(error, NotPrimaryError)
630-
elif isinstance(error, (InvalidOperation, ConfigurationError, EncryptionError)):
631-
pass
632-
# gridfs NoFile errors are considered client errors.
633-
elif isinstance(error, NoFile):
630+
elif isinstance(error, (InvalidOperation, ConfigurationError, EncryptionError, NoFile)):
634631
pass
635632
else:
636633
self.assertNotIsInstance(error, PyMongoError)

0 commit comments

Comments
 (0)