Skip to content

Commit 824691a

Browse files
committed
CSHARP-1450: Add sync version of the new 2.x GridFS API.
1 parent d0c5aad commit 824691a

23 files changed

+1505
-427
lines changed

src/MongoDB.Driver.GridFS.Tests/GridFSBucketTests.cs

Lines changed: 223 additions & 48 deletions
Large diffs are not rendered by default.

src/MongoDB.Driver.GridFS.Tests/MongoDB.Driver.GridFS.Tests.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@
6868
<Compile Include="GridFSUploadOptionsTests.cs" />
6969
<Compile Include="Properties\AssemblyInfo.cs" />
7070
<Compile Include="SetupFixture.cs" />
71-
<Compile Include="Specifications\gridfs\GridFSDeleteAsyncTest.cs" />
72-
<Compile Include="Specifications\gridfs\GridFSDeleteAsyncTestFactory.cs" />
73-
<Compile Include="Specifications\gridfs\GridFSDownloadAsBytesByNameAsyncTest.cs" />
74-
<Compile Include="Specifications\gridfs\GridFSDownloadAsBytesByNameAsyncTestFactory.cs" />
75-
<Compile Include="Specifications\gridfs\GridFSUploadFromBytesAsyncTestFactory.cs" />
76-
<Compile Include="Specifications\gridfs\GridFSDownloadAsBytesAsyncTest.cs" />
77-
<Compile Include="Specifications\gridfs\GridFSDownloadAsBytesAsyncTestFactory.cs" />
78-
<Compile Include="Specifications\gridfs\GridFSUploadFromBytesAsyncTest.cs" />
71+
<Compile Include="Specifications\gridfs\GridFSDeleteTest.cs" />
72+
<Compile Include="Specifications\gridfs\GridFSDeleteTestFactory.cs" />
73+
<Compile Include="Specifications\gridfs\GridFSDownloadAsBytesByNameTest.cs" />
74+
<Compile Include="Specifications\gridfs\GridFSDownloadAsBytesByNameTestFactory.cs" />
75+
<Compile Include="Specifications\gridfs\GridFSUploadFromBytesTestFactory.cs" />
76+
<Compile Include="Specifications\gridfs\GridFSDownloadAsBytesTest.cs" />
77+
<Compile Include="Specifications\gridfs\GridFSDownloadAsBytesTestFactory.cs" />
78+
<Compile Include="Specifications\gridfs\GridFSUploadFromBytesTest.cs" />
7979
<Compile Include="Specifications\gridfs\GridFSTestBase.cs" />
8080
<Compile Include="Specifications\gridfs\IGridFSTest.cs" />
8181
<Compile Include="Specifications\gridfs\GridFSTestFactory.cs" />

src/MongoDB.Driver.GridFS.Tests/Specifications/gridfs/GridFSDeleteAsyncTest.cs renamed to src/MongoDB.Driver.GridFS.Tests/Specifications/gridfs/GridFSDeleteTest.cs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
namespace MongoDB.Driver.GridFS.Tests.Specifications.gridfs
2323
{
24-
public abstract class GridFSDeleteAsyncTestBase : GridFSTestBase
24+
public abstract class GridFSDeleteTestBase : GridFSTestBase
2525
{
2626
// fields
2727
protected ObjectId _id;
2828

2929
// constructors
30-
public GridFSDeleteAsyncTestBase(BsonDocument data, BsonDocument testDefinition)
30+
public GridFSDeleteTestBase(BsonDocument data, BsonDocument testDefinition)
3131
: base(data, testDefinition)
3232
{
3333
var operationName = testDefinition["act"]["operation"].AsString;
@@ -39,6 +39,11 @@ public GridFSDeleteAsyncTestBase(BsonDocument data, BsonDocument testDefinition)
3939
}
4040

4141
// protected methods
42+
protected void InvokeMethod(GridFSBucket bucket)
43+
{
44+
bucket.Delete(_id);
45+
}
46+
4247
protected Task InvokeMethodAsync(GridFSBucket bucket)
4348
{
4449
return bucket.DeleteAsync(_id);
@@ -62,43 +67,56 @@ private void ParseArguments(IEnumerable<BsonElement> arguments)
6267
}
6368
}
6469

65-
public class GridFSDeleteAsyncTest : GridFSDeleteAsyncTestBase
70+
public class GridFSDeleteTest : GridFSDeleteTestBase
6671
{
6772
// constructors
68-
public GridFSDeleteAsyncTest(BsonDocument data, BsonDocument testDefinition)
73+
public GridFSDeleteTest(BsonDocument data, BsonDocument testDefinition)
6974
: base(data, testDefinition)
7075
{
7176
}
7277

7378
// protected methods
74-
protected override async Task ActAsync(GridFSBucket bucket)
79+
protected override void Act(GridFSBucket bucket, bool async)
7580
{
76-
await InvokeMethodAsync(bucket);
81+
if (async)
82+
{
83+
InvokeMethodAsync(bucket).GetAwaiter().GetResult();
84+
}
85+
else
86+
{
87+
InvokeMethod(bucket);
88+
}
7789
}
7890
}
7991

80-
public class GridFSDeleteAsyncTest<TException> : GridFSDeleteAsyncTestBase where TException : Exception
92+
public class GridFSDeleteTest<TException> : GridFSDeleteTestBase where TException : Exception
8193
{
8294
// fields
83-
private Func<Task> _action;
95+
private Action _action;
8496

8597
// constructors
86-
public GridFSDeleteAsyncTest(BsonDocument data, BsonDocument testDefinition)
98+
public GridFSDeleteTest(BsonDocument data, BsonDocument testDefinition)
8799
: base(data, testDefinition)
88100
{
89101
}
90102

91103
// protected methods
92-
protected override Task ActAsync(GridFSBucket bucket)
104+
protected override void Act(GridFSBucket bucket, bool async)
93105
{
94-
_action = () => InvokeMethodAsync(bucket);
95-
return Task.FromResult(true);
106+
if (async)
107+
{
108+
_action = () => InvokeMethodAsync(bucket).GetAwaiter().GetResult();
109+
}
110+
else
111+
{
112+
_action = () => InvokeMethod(bucket);
113+
}
96114
}
97115

98-
protected override Task AssertAsync(GridFSBucket bucket)
116+
protected override void Assert(GridFSBucket bucket)
99117
{
100118
_action.ShouldThrow<TException>();
101-
return base.AssertAsync(bucket);
119+
base.Assert(bucket);
102120
}
103121
}
104122
}

src/MongoDB.Driver.GridFS.Tests/Specifications/gridfs/GridFSDeleteAsyncTestFactory.cs renamed to src/MongoDB.Driver.GridFS.Tests/Specifications/gridfs/GridFSDeleteTestFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919

2020
namespace MongoDB.Driver.GridFS.Tests.Specifications.gridfs
2121
{
22-
public static class GridFSDeleteAsyncTestFactory
22+
public static class GridFSDeleteTestFactory
2323
{
2424
// static public methods
2525
public static IGridFSTest CreateTest(BsonDocument data, BsonDocument testDefinition)
2626
{
2727
if (testDefinition["assert"].AsBsonDocument.Contains("result"))
2828
{
29-
return new GridFSDeleteAsyncTest(data, testDefinition);
29+
return new GridFSDeleteTest(data, testDefinition);
3030
}
3131

3232
var error = testDefinition["assert"]["error"].AsString;
3333
switch (error)
3434
{
3535
case "FileNotFound":
36-
return new GridFSDeleteAsyncTest<GridFSFileNotFoundException>(data, testDefinition);
36+
return new GridFSDeleteTest<GridFSFileNotFoundException>(data, testDefinition);
3737
default:
3838
throw new NotSupportedException(string.Format("Invalid error: {0}.", error));
3939
}

src/MongoDB.Driver.GridFS.Tests/Specifications/gridfs/GridFSDownloadAsBytesByNameAsyncTest.cs renamed to src/MongoDB.Driver.GridFS.Tests/Specifications/gridfs/GridFSDownloadAsBytesByNameTest.cs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121

2222
namespace MongoDB.Driver.GridFS.Tests.Specifications.gridfs
2323
{
24-
public abstract class GridFSGetByNameAsyncTestBase : GridFSTestBase
24+
public abstract class GridFSDownloadAsBytesByNameTestBase : GridFSTestBase
2525
{
2626
// fields
2727
protected string _filename;
2828
protected GridFSDownloadByNameOptions _options = null;
2929

3030
// constructors
31-
public GridFSGetByNameAsyncTestBase(BsonDocument data, BsonDocument testDefinition)
31+
public GridFSDownloadAsBytesByNameTestBase(BsonDocument data, BsonDocument testDefinition)
3232
: base(data, testDefinition)
3333
{
3434
var operationName = testDefinition["act"]["operation"].AsString;
@@ -40,6 +40,11 @@ public GridFSGetByNameAsyncTestBase(BsonDocument data, BsonDocument testDefiniti
4040
}
4141

4242
// protected methods
43+
protected byte[] InvokeMethod(GridFSBucket bucket)
44+
{
45+
return bucket.DownloadAsBytesByName(_filename, _options);
46+
}
47+
4348
protected Task<byte[]> InvokeMethodAsync(GridFSBucket bucket)
4449
{
4550
return bucket.DownloadAsBytesByNameAsync(_filename, _options);
@@ -88,54 +93,67 @@ private void ParseOptions(BsonDocument options)
8893
}
8994
}
9095

91-
public class GridFSDownloadAsBytesByNameAsyncTest : GridFSGetByNameAsyncTestBase
96+
public class GridFSDownloadAsBytesByNameTest : GridFSDownloadAsBytesByNameTestBase
9297
{
9398
// fields
9499
private readonly byte[] _expectedResult;
95100
private byte[] _result;
96101

97102
// constructors
98-
public GridFSDownloadAsBytesByNameAsyncTest(BsonDocument data, BsonDocument testDefinition)
103+
public GridFSDownloadAsBytesByNameTest(BsonDocument data, BsonDocument testDefinition)
99104
: base(data, testDefinition)
100105
{
101106
_expectedResult = testDefinition["assert"]["result"].AsByteArray;
102107
}
103108

104109
// protected methods
105-
protected override async Task ActAsync(GridFSBucket bucket)
110+
protected override void Act(GridFSBucket bucket, bool async)
106111
{
107-
_result = await InvokeMethodAsync(bucket);
112+
if (async)
113+
{
114+
_result = InvokeMethodAsync(bucket).GetAwaiter().GetResult();
115+
}
116+
else
117+
{
118+
_result = InvokeMethod(bucket);
119+
}
108120
}
109121

110-
protected override Task AssertAsync(GridFSBucket bucket)
122+
protected override void Assert(GridFSBucket bucket)
111123
{
112124
_result.Should().Equal(_expectedResult);
113-
return Task.FromResult(true); // don't call base.AssertAsync
125+
// don't call base.Assert
114126
}
115127
}
116128

117-
public class GridFSGetByNameAsyncTest<TException> : GridFSGetByNameAsyncTestBase where TException : Exception
129+
public class GridFSDownloadAsBytesByNameTest<TException> : GridFSDownloadAsBytesByNameTestBase where TException : Exception
118130
{
119131
// fields
120-
private Func<Task> _action;
132+
private Action _action;
121133

122134
// constructors
123-
public GridFSGetByNameAsyncTest(BsonDocument data, BsonDocument testDefinition)
135+
public GridFSDownloadAsBytesByNameTest(BsonDocument data, BsonDocument testDefinition)
124136
: base(data, testDefinition)
125137
{
126138
}
127139

128140
// protected methods
129-
protected override Task ActAsync(GridFSBucket bucket)
141+
protected override void Act(GridFSBucket bucket, bool async)
130142
{
131-
_action = () => InvokeMethodAsync(bucket);
132-
return Task.FromResult(true);
143+
if (async)
144+
{
145+
_action = () => InvokeMethodAsync(bucket).GetAwaiter().GetResult();
146+
}
147+
else
148+
{
149+
_action = () => InvokeMethod(bucket);
150+
}
133151
}
134152

135-
protected override Task AssertAsync(GridFSBucket bucket)
153+
protected override void Assert(GridFSBucket bucket)
136154
{
137155
_action.ShouldThrow<TException>();
138-
return Task.FromResult(true); // don't call base.AssertAsync
156+
// don't call base.Assert
139157
}
140158
}
141159
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@
1919

2020
namespace MongoDB.Driver.GridFS.Tests.Specifications.gridfs
2121
{
22-
public static class GridFSDownloadAsBytesByNameAsyncTestFactory
22+
public static class GridFSDownloadAsBytesByNameTestFactory
2323
{
2424
// static public methods
2525
public static IGridFSTest CreateTest(BsonDocument data, BsonDocument testDefinition)
2626
{
2727
if (testDefinition["assert"].AsBsonDocument.Contains("result"))
2828
{
29-
return new GridFSDownloadAsBytesByNameAsyncTest(data, testDefinition);
29+
return new GridFSDownloadAsBytesByNameTest(data, testDefinition);
3030
}
3131

3232
var error = testDefinition["assert"]["error"].AsString;
3333
switch (error)
3434
{
3535
case "FileNotFound":
3636
case "RevisionNotFound":
37-
return new GridFSGetByNameAsyncTest<GridFSFileNotFoundException>(data, testDefinition);
37+
return new GridFSDownloadAsBytesByNameTest<GridFSFileNotFoundException>(data, testDefinition);
3838
default:
3939
throw new NotSupportedException(string.Format("Invalid error: {0}.", error));
4040
}

src/MongoDB.Driver.GridFS.Tests/Specifications/gridfs/GridFSDownloadAsBytesAsyncTest.cs renamed to src/MongoDB.Driver.GridFS.Tests/Specifications/gridfs/GridFSDownloadAsBytesTest.cs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121

2222
namespace MongoDB.Driver.GridFS.Tests.Specifications.gridfs
2323
{
24-
public abstract class GridFSDownloadAsyncTestBase : GridFSTestBase
24+
public abstract class GridFSDownloadAsBytesTestBase : GridFSTestBase
2525
{
2626
// fields
2727
protected ObjectId _id;
2828
protected GridFSDownloadOptions _options = null;
2929

3030
// constructors
31-
public GridFSDownloadAsyncTestBase(BsonDocument data, BsonDocument testDefinition)
31+
public GridFSDownloadAsBytesTestBase(BsonDocument data, BsonDocument testDefinition)
3232
: base(data, testDefinition)
3333
{
3434
var operationName = testDefinition["act"]["operation"].AsString;
@@ -40,6 +40,11 @@ public GridFSDownloadAsyncTestBase(BsonDocument data, BsonDocument testDefinitio
4040
}
4141

4242
// protected methods
43+
protected byte[] InvokeMethod(GridFSBucket bucket)
44+
{
45+
return bucket.DownloadAsBytes(_id, _options);
46+
}
47+
4348
protected Task<byte[]> InvokeMethodAsync(GridFSBucket bucket)
4449
{
4550
return bucket.DownloadAsBytesAsync(_id, _options);
@@ -84,54 +89,67 @@ private void ParseOptions(BsonDocument options)
8489
}
8590
}
8691

87-
public class GridFSDownloadAsBytesAsyncTest : GridFSDownloadAsyncTestBase
92+
public class GridFSDownloadAsBytesTest : GridFSDownloadAsBytesTestBase
8893
{
8994
// fields
9095
private readonly byte[] _expectedResult;
9196
private byte[] _result;
9297

9398
// constructors
94-
public GridFSDownloadAsBytesAsyncTest(BsonDocument data, BsonDocument testDefinition)
99+
public GridFSDownloadAsBytesTest(BsonDocument data, BsonDocument testDefinition)
95100
: base(data, testDefinition)
96101
{
97102
_expectedResult = testDefinition["assert"]["result"].AsByteArray;
98103
}
99104

100105
// protected methods
101-
protected override async Task ActAsync(GridFSBucket bucket)
106+
protected override void Act(GridFSBucket bucket, bool async)
102107
{
103-
_result = await InvokeMethodAsync(bucket);
108+
if (async)
109+
{
110+
_result = InvokeMethodAsync(bucket).GetAwaiter().GetResult();
111+
}
112+
else
113+
{
114+
_result = InvokeMethod(bucket);
115+
}
104116
}
105117

106-
protected override Task AssertAsync(GridFSBucket bucket)
118+
protected override void Assert(GridFSBucket bucket)
107119
{
108120
_result.Should().Equal(_expectedResult);
109-
return Task.FromResult(true); // don't call base.AssertAsync
121+
// don't call base.Assert
110122
}
111123
}
112124

113-
public class GridFSDownloadAsyncTest<TException> : GridFSDownloadAsyncTestBase where TException : Exception
125+
public class GridFSDownloadAsBytesTest<TException> : GridFSDownloadAsBytesTestBase where TException : Exception
114126
{
115127
// fields
116-
private Func<Task> _action;
128+
private Action _action;
117129

118130
// constructors
119-
public GridFSDownloadAsyncTest(BsonDocument data, BsonDocument testDefinition)
131+
public GridFSDownloadAsBytesTest(BsonDocument data, BsonDocument testDefinition)
120132
: base(data, testDefinition)
121133
{
122134
}
123135

124136
// protected methods
125-
protected override Task ActAsync(GridFSBucket bucket)
137+
protected override void Act(GridFSBucket bucket, bool async)
126138
{
127-
_action = () => InvokeMethodAsync(bucket);
128-
return Task.FromResult(true);
139+
if (async)
140+
{
141+
_action = () => InvokeMethodAsync(bucket).GetAwaiter().GetResult();
142+
}
143+
else
144+
{
145+
_action = () => InvokeMethod(bucket);
146+
}
129147
}
130148

131-
protected override Task AssertAsync(GridFSBucket bucket)
149+
protected override void Assert(GridFSBucket bucket)
132150
{
133151
_action.ShouldThrow<TException>();
134-
return Task.FromResult(true); // don't call base.AssertAsync
152+
// don't call base.Assert
135153
}
136154
}
137155
}

0 commit comments

Comments
 (0)