Skip to content

Commit 9034168

Browse files
committed
Exception handling tests
1 parent a17c258 commit 9034168

File tree

1 file changed

+84
-3
lines changed

1 file changed

+84
-3
lines changed

tests/CouchDB.Driver.UnitTests/Client_Tests.cs

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using CouchDB.Driver.Types;
1+
using CouchDB.Driver.Exceptions;
2+
using CouchDB.Driver.Types;
23
using CouchDB.Driver.UnitTests.Models;
34
using Flurl.Http.Testing;
45
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Net;
58
using System.Net.Http;
69
using System.Threading.Tasks;
710
using Xunit;
@@ -110,7 +113,7 @@ public async Task IsUp()
110113

111114
using (var client = new CouchClient("http://localhost"))
112115
{
113-
var result = await client.IsUpAsync();
116+
var result = await client.IsUpAsync();
114117
Assert.True(result);
115118
}
116119
}
@@ -127,7 +130,7 @@ public async Task IsNotUp()
127130

128131
using (var client = new CouchClient("http://localhost"))
129132
{
130-
httpTest.RespondWith("Not found", 404);
133+
httpTest.RespondWith("Not found", 404);
131134
var result = await client.IsUpAsync();
132135
Assert.False(result);
133136
}
@@ -175,5 +178,83 @@ public async Task ActiveTasks()
175178
}
176179

177180
#endregion
181+
182+
#region Error Handling
183+
[Fact]
184+
public async Task ConflictException()
185+
{
186+
using (var httpTest = new HttpTest())
187+
{
188+
httpTest.RespondWith(status: (int)HttpStatusCode.Conflict);
189+
190+
using (var client = new CouchClient("http://localhost"))
191+
{
192+
await Assert.ThrowsAsync<CouchConflictException>(() => client.CreateDatabaseAsync<Rebel>());
193+
}
194+
}
195+
}
196+
197+
[Fact]
198+
public async Task NotFoundException()
199+
{
200+
using (var httpTest = new HttpTest())
201+
{
202+
httpTest.RespondWith(status: (int)HttpStatusCode.NotFound);
203+
204+
using (var client = new CouchClient("http://localhost"))
205+
{
206+
await Assert.ThrowsAsync<CouchNotFoundException>(() => client.DeleteDatabaseAsync<Rebel>());
207+
}
208+
}
209+
}
210+
211+
[Fact]
212+
public void BadRequestException()
213+
{
214+
using (var httpTest = new HttpTest())
215+
{
216+
httpTest.RespondWith(@"{error: ""no_usable_index""}", (int)HttpStatusCode.BadRequest);
217+
218+
using (var client = new CouchClient("http://localhost"))
219+
{
220+
var db = client.GetDatabase<Rebel>();
221+
Assert.Throws<CouchNoIndexException>(() => db.UseIndex("aoeu").ToList());
222+
}
223+
}
224+
}
225+
226+
[Fact]
227+
public async Task GenericExceptionWithMessage()
228+
{
229+
using (var httpTest = new HttpTest())
230+
{
231+
string message = "message text";
232+
string reason = "reason text";
233+
httpTest.RespondWith($"{{error: \"{message}\", reason: \"{reason}\"}}", (int)HttpStatusCode.InternalServerError);
234+
235+
using (var client = new CouchClient("http://localhost"))
236+
{
237+
var db = client.GetDatabase<Rebel>();
238+
var couchException = await Assert.ThrowsAsync<CouchException>(() => db.FindAsync("aoeu"));
239+
Assert.Equal(message, couchException.Message);
240+
}
241+
}
242+
}
243+
244+
[Fact]
245+
public async Task GenericExceptionNoMessage()
246+
{
247+
using (var httpTest = new HttpTest())
248+
{
249+
httpTest.RespondWith(status: (int)HttpStatusCode.InternalServerError);
250+
251+
using (var client = new CouchClient("http://localhost"))
252+
{
253+
var db = client.GetDatabase<Rebel>();
254+
var couchException = await Assert.ThrowsAsync<CouchException>(() => db.FindAsync("aoeu"));
255+
}
256+
}
257+
}
258+
#endregion
178259
}
179260
}

0 commit comments

Comments
 (0)