diff --git a/LoveSeat.IntegrationTest/CouchClientTest.cs b/LoveSeat.IntegrationTest/CouchClientTest.cs index d44e3ea..b4cfc7e 100644 --- a/LoveSeat.IntegrationTest/CouchClientTest.cs +++ b/LoveSeat.IntegrationTest/CouchClientTest.cs @@ -15,33 +15,38 @@ namespace LoveSeat.IntegrationTest { [TestFixture] - public class CouchClientTest - { - private CouchClient client; - private const string baseDatabase = "love-seat-test-base"; + public class CouchClientTest + { + private CouchClient client; + private const string baseDatabase = "love-seat-test-base"; private const string replicateDatabase = "love-seat-test-repli"; + private const string userDatabase = "_users"; - private readonly string host = ConfigurationManager.AppSettings["Host"]; - private readonly int port = int.Parse(ConfigurationManager.AppSettings["Port"]); - private readonly string username = ConfigurationManager.AppSettings["UserName"]; - private readonly string password = ConfigurationManager.AppSettings["Password"]; + private readonly string host = ConfigurationManager.AppSettings["Host"]; + private readonly int port = int.Parse(ConfigurationManager.AppSettings["Port"]); + private readonly string username = ConfigurationManager.AppSettings["UserName"]; + private readonly string password = ConfigurationManager.AppSettings["Password"]; - [SetUp] - public void Setup() - { - client = new CouchClient(host, port, username, password, false,AuthenticationType.Cookie); - if (!client.HasDatabase(baseDatabase)) - { - client.CreateDatabase(baseDatabase); - } + [SetUp] + public void Setup() + { + client = new CouchClient(host, port, username, password, false, AuthenticationType.Cookie); + if (!client.HasDatabase(baseDatabase)) + { + client.CreateDatabase(baseDatabase); + } if (!client.HasDatabase(replicateDatabase)) { client.CreateDatabase(replicateDatabase); } - } - [TearDown] - public void TearDown() - { + if (!client.HasDatabase(userDatabase)) + { + client.CreateDatabase(userDatabase); + } + } + [TearDown] + public void TearDown() + { //delete the test database if (client.HasDatabase(baseDatabase)) { client.DeleteDatabase(baseDatabase); @@ -52,15 +57,18 @@ public void TearDown() if (client.HasUser("Leela")) { client.DeleteAdminUser("Leela"); } - } + if (client.HasUser("Fry")) { + client.DeleteUser("Fry"); + } + } - [Test] - public void Should_Trigger_Replication() - { - var obj = client.TriggerReplication("http://Professor:Farnsworth@"+ host+":5984/" +replicateDatabase, baseDatabase); - Assert.IsTrue(obj != null); - } - public class Bunny : IBaseObject{ + [Test] + public void Should_Trigger_Replication() + { + var obj = client.TriggerReplication("http://Professor:Farnsworth@" + host + ":5984/" + replicateDatabase, baseDatabase); + Assert.IsTrue(obj != null); + } + public class Bunny : IBaseObject { public Bunny() { } public string Name { get; set; } public string Id { get; set; } @@ -78,15 +86,15 @@ public void Creating_A_Document_Should_Keep_Id_If_Supplied() Assert.IsNotNull(savedDoc, "Saved doc should be able to be retrieved by the same id"); } - [Test] - public void Should_Create_Document_From_String() - { - string obj = @"{""test"": ""prop""}"; - var db = client.GetDatabase(baseDatabase); - var result = db.CreateDocument("fdas", obj); - var document = db.GetDocument("fdas"); - Assert.IsNotNull(document); - } + [Test] + public void Should_Create_Document_From_String() + { + string obj = @"{""test"": ""prop""}"; + var db = client.GetDatabase(baseDatabase); + var result = db.CreateDocument("fdas", obj); + var document = db.GetDocument("fdas"); + Assert.IsNotNull(document); + } [Test] public void Should_Save_Existing_Document() { @@ -96,54 +104,54 @@ public void Should_Save_Existing_Document() var doc = db.GetDocument("fdas"); doc.JObject["test"] = "newprop"; db.SaveDocument(doc); - var newresult= db.GetDocument("fdas"); + var newresult = db.GetDocument("fdas"); Assert.AreEqual(newresult.JObject.Value("test"), "newprop"); } - [Test] - public void Should_Delete_Document() - { - var db = client.GetDatabase(baseDatabase); - db.CreateDocument("asdf", "{}"); - var doc = db.GetDocument("asdf"); - var result = db.DeleteDocument(doc.Id, doc.Rev); - Assert.IsNull(db.GetDocument("asdf")); - } + [Test] + public void Should_Delete_Document() + { + var db = client.GetDatabase(baseDatabase); + db.CreateDocument("asdf", "{}"); + var doc = db.GetDocument("asdf"); + var result = db.DeleteDocument(doc.Id, doc.Rev); + Assert.IsNull(db.GetDocument("asdf")); + } [Test] public void Should_Save_And_Retrieve_A_Document_With_Generics() { var id = Guid.NewGuid().ToString(); var db = client.GetDatabase(baseDatabase); - var bunny = new Bunny {Id=id, Name = "Hippity Hop"}; + var bunny = new Bunny { Id = id, Name = "Hippity Hop" }; var doc = new Document(bunny); byte[] attachment = File.ReadAllBytes("../../Files/martin.jpg"); doc.AddAttachment("martin.jpg", attachment); db.SaveDocument(doc); - var persistedBunny= db.GetDocument(id); + var persistedBunny = db.GetDocument(id); Assert.IsTrue(persistedBunny.HasAttachment); } - [Test] - public void Should_Determine_If_Doc_Has_Attachment() - { - var db = client.GetDatabase(baseDatabase); - db.CreateDocument(@"{""_id"":""fdsa""}"); - byte[] attachment = File.ReadAllBytes("../../Files/martin.jpg"); - db.AddAttachment("fdsa" , attachment,"martin.jpg", "image/jpeg"); - var doc = db.GetDocument("fdsa"); - Assert.IsTrue(doc.HasAttachment); - } - [Test] - public void Should_Return_Attachment_Names() - { - var db = client.GetDatabase(baseDatabase); - db.CreateDocument(@"{""_id"":""upload""}"); - var attachment = File.ReadAllBytes("../../Files/martin.jpg"); - db.AddAttachment("upload", attachment, "martin.jpg", "image/jpeg"); - var doc = db.GetDocument("upload"); - Assert.IsTrue(doc.GetAttachmentNames().Contains("martin.jpg")); - } + [Test] + public void Should_Determine_If_Doc_Has_Attachment() + { + var db = client.GetDatabase(baseDatabase); + db.CreateDocument(@"{""_id"":""fdsa""}"); + byte[] attachment = File.ReadAllBytes("../../Files/martin.jpg"); + db.AddAttachment("fdsa", attachment, "martin.jpg", "image/jpeg"); + var doc = db.GetDocument("fdsa"); + Assert.IsTrue(doc.HasAttachment); + } + [Test] + public void Should_Return_Attachment_Names() + { + var db = client.GetDatabase(baseDatabase); + db.CreateDocument(@"{""_id"":""upload""}"); + var attachment = File.ReadAllBytes("../../Files/martin.jpg"); + db.AddAttachment("upload", attachment, "martin.jpg", "image/jpeg"); + var doc = db.GetDocument("upload"); + Assert.IsTrue(doc.GetAttachmentNames().Contains("martin.jpg")); + } [Test] public void Should_Return_Docs_On_View() @@ -153,9 +161,9 @@ public void Should_Return_Docs_On_View() JObject.Parse( "{\"_id\": \"_design/Patient\",\"views\": {\"all\": {\"map\": \"function (doc) {\n emit(doc._id, null);\n }\"}},\"type\": \"designdoc\"}"); db.CreateDocument(jobj.ToString()); - var bunny = new Bunny {Name = "Roger"}; + var bunny = new Bunny { Name = "Roger" }; db.CreateDocument(new Document(bunny)); - var result = db.View("all", new ViewOptions {IncludeDocs = true}, "Patient"); + var result = db.View("all", new ViewOptions { IncludeDocs = true }, "Patient"); Assert.IsTrue(result.Docs.Any()); } @@ -171,7 +179,21 @@ public void Should_Delete_Admin_User() client.DeleteAdminUser("Leela"); } - [Test] + [Test] + public void Should_Create_User() + { + client.CreateUser("Fry", "Philip"); + } + + [Test] + public void Should_Delete_User() + { + client.CreateUser("Fry", "Philip"); + client.DeleteUser("Fry"); + Assert.IsFalse(client.HasUser("Fry")); + } + + [Test] public void Should_Get_Attachment() { var db = client.GetDatabase(baseDatabase); diff --git a/LoveSeat.IntegrationTest/CouchDatabaseTest.cs b/LoveSeat.IntegrationTest/CouchDatabaseTest.cs new file mode 100644 index 0000000..726ae47 --- /dev/null +++ b/LoveSeat.IntegrationTest/CouchDatabaseTest.cs @@ -0,0 +1,53 @@ +using NUnit.Framework; +using System; +using System.Configuration; + +namespace LoveSeat.IntegrationTest +{ + [TestFixture] + public class CouchDatabaseTest + { + private CouchDatabase db; + private CouchClient client; + private const string baseDatabase = "love-seat-test-base"; + + private readonly string host = ConfigurationManager.AppSettings["Host"]; + private readonly int port = int.Parse(ConfigurationManager.AppSettings["Port"]); + private readonly string username = ConfigurationManager.AppSettings["UserName"]; + private readonly string password = ConfigurationManager.AppSettings["Password"]; + + [SetUp] + public void Setup() + { + client = new CouchClient(host, port, username, password, false, AuthenticationType.Cookie); + if (!client.HasDatabase(baseDatabase)) + { + client.CreateDatabase(baseDatabase); + } + + var uriBuilder = new UriBuilder("http", host, port, baseDatabase); + db = new CouchDatabase(uriBuilder.Uri); + } + + [TearDown] + public void TearDown() + { + //delete the test database + if (client.HasDatabase(baseDatabase)) + { + client.DeleteDatabase(baseDatabase); + } + } + + [Test] + public void Should_Save_Multiple_Documents() + { + var pets = new Documents(); + pets.Values.Add(new Document(@"{ ""name"": ""Santa's Little Helper"" }")); + pets.Values.Add(new Document(@"{ ""name"": ""Snowball V"" }")); + pets.Values.Add(new Document(@"{ ""name"": ""Strangles"" }")); + + db.SaveDocuments(pets, true); + } + } +} diff --git a/LoveSeat.IntegrationTest/LoveSeat.IntegrationTest.csproj b/LoveSeat.IntegrationTest/LoveSeat.IntegrationTest.csproj index de1041c..51c781c 100644 --- a/LoveSeat.IntegrationTest/LoveSeat.IntegrationTest.csproj +++ b/LoveSeat.IntegrationTest/LoveSeat.IntegrationTest.csproj @@ -61,6 +61,7 @@ + diff --git a/LoveSeat/CouchClient.cs b/LoveSeat/CouchClient.cs index e96d1a9..ecbcdc8 100644 --- a/LoveSeat/CouchClient.cs +++ b/LoveSeat/CouchClient.cs @@ -248,7 +248,7 @@ public CouchResponseObject CreateUser(string usernameToCreate, string passwordTo var docResult = GetRequest(baseUri + "_users/org.couchdb.user:" + HttpUtility.UrlEncode(usernameToCreate)) .Put().Json().Data(user).GetCouchResponse(); - if (docResult.StatusCode == HttpStatusCode.Created) + if (docResult.StatusCode == HttpStatusCode.Created || docResult.StatusCode == HttpStatusCode.Accepted) { return docResult.GetJObject(); } diff --git a/LoveSeat/CouchDatabase.cs b/LoveSeat/CouchDatabase.cs index 59b1640..2b2f7e5 100644 --- a/LoveSeat/CouchDatabase.cs +++ b/LoveSeat/CouchDatabase.cs @@ -232,7 +232,7 @@ public BulkDocumentResponses SaveDocuments(Documents docs, bool all_or_nothing) throw new System.Exception("Response returned null."); } - if (resp.StatusCode != HttpStatusCode.Created) + if (resp.StatusCode != HttpStatusCode.Created && resp.StatusCode != HttpStatusCode.Accepted) { throw new System.Exception("Response returned with a HTTP status code of " + resp.StatusCode + " - " + resp.StatusDescription); }