Skip to content

Commit 7208462

Browse files
authored
RUST-2080 spec tests (and fix) for gridfs rename (#1366)
1 parent 6fb56ba commit 7208462

File tree

6 files changed

+350
-34
lines changed

6 files changed

+350
-34
lines changed

src/action/gridfs/rename.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,21 @@ impl<'a> Action for Rename<'a> {
7272
type Future = RenameFuture;
7373

7474
async fn execute(self) -> Result<()> {
75-
self.bucket
75+
let count = self
76+
.bucket
7677
.files()
7778
.update_one(
78-
doc! { "_id": self.id },
79+
doc! { "_id": self.id.clone() },
7980
doc! { "$set": { "filename": self.new_filename } },
8081
)
81-
.await?;
82+
.await?
83+
.matched_count;
84+
if count == 0 {
85+
return Err(ErrorKind::GridFs(GridFsErrorKind::FileNotFound {
86+
identifier: GridFsFileIdentifier::Id(self.id),
87+
})
88+
.into());
89+
}
8290

8391
Ok(())
8492
}

src/test/spec/json/gridfs/rename.json

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
{
2+
"description": "gridfs-rename",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
},
10+
{
11+
"database": {
12+
"id": "database0",
13+
"client": "client0",
14+
"databaseName": "gridfs-tests"
15+
}
16+
},
17+
{
18+
"bucket": {
19+
"id": "bucket0",
20+
"database": "database0"
21+
}
22+
},
23+
{
24+
"collection": {
25+
"id": "bucket0_files_collection",
26+
"database": "database0",
27+
"collectionName": "fs.files"
28+
}
29+
},
30+
{
31+
"collection": {
32+
"id": "bucket0_chunks_collection",
33+
"database": "database0",
34+
"collectionName": "fs.chunks"
35+
}
36+
}
37+
],
38+
"initialData": [
39+
{
40+
"collectionName": "fs.files",
41+
"databaseName": "gridfs-tests",
42+
"documents": [
43+
{
44+
"_id": {
45+
"$oid": "000000000000000000000001"
46+
},
47+
"length": 0,
48+
"chunkSize": 4,
49+
"uploadDate": {
50+
"$date": "1970-01-01T00:00:00.000Z"
51+
},
52+
"filename": "filename",
53+
"metadata": {}
54+
},
55+
{
56+
"_id": {
57+
"$oid": "000000000000000000000002"
58+
},
59+
"length": 0,
60+
"chunkSize": 4,
61+
"uploadDate": {
62+
"$date": "1970-01-01T00:00:00.000Z"
63+
},
64+
"filename": "filename",
65+
"metadata": {}
66+
}
67+
]
68+
},
69+
{
70+
"collectionName": "fs.chunks",
71+
"databaseName": "gridfs-tests",
72+
"documents": [
73+
{
74+
"_id": {
75+
"$oid": "000000000000000000000001"
76+
},
77+
"files_id": {
78+
"$oid": "000000000000000000000002"
79+
},
80+
"n": 0,
81+
"data": {
82+
"$binary": {
83+
"base64": "",
84+
"subType": "00"
85+
}
86+
}
87+
}
88+
]
89+
}
90+
],
91+
"tests": [
92+
{
93+
"description": "rename by id",
94+
"operations": [
95+
{
96+
"name": "rename",
97+
"object": "bucket0",
98+
"arguments": {
99+
"id": {
100+
"$oid": "000000000000000000000001"
101+
},
102+
"newFilename": "newfilename"
103+
}
104+
}
105+
],
106+
"outcome": [
107+
{
108+
"collectionName": "fs.files",
109+
"databaseName": "gridfs-tests",
110+
"documents": [
111+
{
112+
"_id": {
113+
"$oid": "000000000000000000000001"
114+
},
115+
"length": 0,
116+
"chunkSize": 4,
117+
"uploadDate": {
118+
"$date": "1970-01-01T00:00:00.000Z"
119+
},
120+
"filename": "newfilename",
121+
"metadata": {}
122+
},
123+
{
124+
"_id": {
125+
"$oid": "000000000000000000000002"
126+
},
127+
"length": 0,
128+
"chunkSize": 4,
129+
"uploadDate": {
130+
"$date": "1970-01-01T00:00:00.000Z"
131+
},
132+
"filename": "filename",
133+
"metadata": {}
134+
}
135+
]
136+
},
137+
{
138+
"collectionName": "fs.chunks",
139+
"databaseName": "gridfs-tests",
140+
"documents": [
141+
{
142+
"_id": {
143+
"$oid": "000000000000000000000001"
144+
},
145+
"files_id": {
146+
"$oid": "000000000000000000000002"
147+
},
148+
"n": 0,
149+
"data": {
150+
"$binary": {
151+
"base64": "",
152+
"subType": "00"
153+
}
154+
}
155+
}
156+
]
157+
}
158+
]
159+
},
160+
{
161+
"description": "rename when file id does not exist",
162+
"operations": [
163+
{
164+
"name": "rename",
165+
"object": "bucket0",
166+
"arguments": {
167+
"id": {
168+
"$oid": "000000000000000000000003"
169+
},
170+
"newFilename": "newfilename"
171+
},
172+
"expectError": {
173+
"isClientError": true
174+
}
175+
}
176+
]
177+
}
178+
]
179+
}

src/test/spec/json/gridfs/rename.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
description: "gridfs-rename"
2+
3+
schemaVersion: "1.0"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 client0
8+
- database:
9+
id: &database0 database0
10+
client: *client0
11+
databaseName: &database0Name gridfs-tests
12+
- bucket:
13+
id: &bucket0 bucket0
14+
database: *database0
15+
- collection:
16+
id: &bucket0_files_collection bucket0_files_collection
17+
database: *database0
18+
collectionName: &bucket0_files_collectionName fs.files
19+
- collection:
20+
id: &bucket0_chunks_collection bucket0_chunks_collection
21+
database: *database0
22+
collectionName: &bucket0_chunks_collectionName fs.chunks
23+
24+
initialData:
25+
- collectionName: *bucket0_files_collectionName
26+
databaseName: *database0Name
27+
documents:
28+
- &file1
29+
_id: { "$oid": "000000000000000000000001" }
30+
length: 0
31+
chunkSize: 4
32+
uploadDate: { "$date": "1970-01-01T00:00:00.000Z" }
33+
filename: "filename"
34+
metadata: {}
35+
- &file2
36+
_id: { "$oid": "000000000000000000000002" }
37+
length: 0
38+
chunkSize: 4
39+
uploadDate: { "$date": "1970-01-01T00:00:00.000Z" }
40+
filename: "filename"
41+
metadata: {}
42+
- collectionName: *bucket0_chunks_collectionName
43+
databaseName: *database0Name
44+
documents:
45+
- &file2_chunk0
46+
_id: { "$oid": "000000000000000000000001" }
47+
files_id: { "$oid": "000000000000000000000002" }
48+
n: 0
49+
data: { "$binary": { "base64": "", "subType": "00" } }
50+
51+
tests:
52+
- description: "rename by id"
53+
operations:
54+
- name: rename
55+
object: *bucket0
56+
arguments:
57+
id: { "$oid": "000000000000000000000001" }
58+
newFilename: newfilename
59+
outcome:
60+
- collectionName: *bucket0_files_collectionName
61+
databaseName: *database0Name
62+
documents:
63+
- <<: *file1
64+
filename: newfilename
65+
- <<: *file2
66+
filename: filename
67+
- collectionName: *bucket0_chunks_collectionName
68+
databaseName: *database0Name
69+
documents:
70+
- *file2_chunk0
71+
- description: "rename when file id does not exist"
72+
operations:
73+
- name: rename
74+
object: *bucket0
75+
arguments:
76+
id: { "$oid": "000000000000000000000003" }
77+
newFilename: newfilename
78+
expectError: { isClientError: true } # FileNotFound

src/test/spec/unified_runner/operation.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod index;
1313
mod insert;
1414
mod iteration;
1515
mod list;
16+
mod rename;
1617
mod search_index;
1718
mod session;
1819
mod thread;
@@ -37,7 +38,6 @@ use collection::{
3738
AssertCollectionNotExists,
3839
CreateCollection,
3940
DropCollection,
40-
RenameCollection,
4141
};
4242
use command::{CreateCommandCursor, RunCommand, RunCursorCommand};
4343
use connection::{AssertNumberConnectionsCheckedOut, Close};
@@ -65,6 +65,7 @@ use index::{
6565
use insert::{InsertMany, InsertOne};
6666
use iteration::{IterateOnce, IterateUntilDocumentOrError};
6767
use list::{ListCollectionNames, ListCollections, ListDatabaseNames, ListDatabases};
68+
use rename::Rename;
6869
use serde::{
6970
de::{DeserializeOwned, Deserializer},
7071
Deserialize,
@@ -408,7 +409,7 @@ impl<'de> Deserialize<'de> for Operation {
408409
}
409410
"close" => deserialize_op::<Close>(definition.arguments),
410411
"createChangeStream" => deserialize_op::<CreateChangeStream>(definition.arguments),
411-
"rename" => deserialize_op::<RenameCollection>(definition.arguments),
412+
"rename" => deserialize_op::<Rename>(definition.arguments),
412413
"loop" => deserialize_op::<Loop>(definition.arguments),
413414
"waitForEvent" => deserialize_op::<WaitForEvent>(definition.arguments),
414415
"assertEventCount" => deserialize_op::<AssertEventCount>(definition.arguments),

src/test/spec/unified_runner/operation/collection.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -119,35 +119,6 @@ impl TestOperation for DropCollection {
119119
}
120120
}
121121

122-
#[derive(Debug, Deserialize)]
123-
#[serde(rename_all = "camelCase", deny_unknown_fields)]
124-
pub(super) struct RenameCollection {
125-
to: String,
126-
}
127-
128-
impl TestOperation for RenameCollection {
129-
fn execute_entity_operation<'a>(
130-
&'a self,
131-
id: &'a str,
132-
test_runner: &'a TestRunner,
133-
) -> BoxFuture<'a, Result<Option<Entity>>> {
134-
async move {
135-
let target = test_runner.get_collection(id).await;
136-
let ns = target.namespace();
137-
let mut to_ns = ns.clone();
138-
to_ns.coll.clone_from(&self.to);
139-
let cmd = doc! {
140-
"renameCollection": crate::bson::to_bson(&ns)?,
141-
"to": crate::bson::to_bson(&to_ns)?,
142-
};
143-
let admin = test_runner.internal_client.database("admin");
144-
admin.run_command(cmd).await?;
145-
Ok(None)
146-
}
147-
.boxed()
148-
}
149-
}
150-
151122
#[derive(Debug, Deserialize)]
152123
pub(super) struct Aggregate {
153124
pipeline: Vec<Document>,

0 commit comments

Comments
 (0)