public void should_save_a_document_attachment() { using (var session = new TemporarySession()) { var config = Documents.Configure <TestDocUnique>(); config.TypeName = "testdocunique"; var testObject = new TestDocUnique { Name = "hello world" }; var result = session.Documents.Save <TestDocUnique>(testObject); var data64String = "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="; var bytes = Convert.FromBase64String(data64String); string attachmentName = "testAttachment"; result = session.Documents.PutAttachment(result.Id, result.Revision, attachmentName, "text/plain", bytes); var bytesStored = session.Documents.GetAttachment(result.Id, attachmentName); var doc = Convert.ToBase64String(bytesStored); Console.WriteLine(Encoding.UTF8.GetString(bytesStored)); Assert.That(doc, Is.EqualTo(data64String)); session.Documents.DeleteAttachment(result.Id, result.Revision, attachmentName); Assert.That(session.Documents.GetAttachment(result.Id, attachmentName), Is.Null); } }
public void should_be_able_to_delete_a_documents_and_add_another_with_same_key() { using (var session = new TemporarySession()) { Documents.Configure <TestDocUnique>() .UniqueConstraint = unique => unique.Name; var testObject = new TestDocUnique { Name = "hello world" }; var result = session.Documents.Save <TestDocUnique>(testObject); Console.Write(result.Id); session.Delete(testObject); var conflictDOc = new TestDocUnique { Name = "hello world" }; var result2 = session.Documents.Save(conflictDOc); Assert.AreNotEqual(result.Id, result2.Id); } }
public void should_be_able_hook_into_pre_and_post_save_events() { using (var session = new TemporarySession()) { var config = Documents.Configure <TestDocUnique>(); int counter = 0, onSavingCounter = 0, onSavedCounter = 0; string onSavedId = String.Empty, onSavingId = String.Empty, onSavingRevision = String.Empty, onSavedRevision = String.Empty; TestDocUnique onSavingObj = null, onSavedObj = null; config.OnSaving = (id, rev, obj) => { onSavingCounter = counter++; onSavingId = id; onSavingRevision = rev; onSavingObj = obj; }; config.OnSaved = (id, rev, obj) => { onSavedCounter = counter++; onSavedId = id; onSavedRevision = rev; onSavedObj = obj; }; var testObject = new TestDocUnique { Name = "hello world" }; var result = session.Documents.Save <TestDocUnique>(testObject); result.Id.ShouldBe(onSavedId); result.Id.ShouldBe(onSavingId); onSavingRevision.ShouldBe(null); result.Revision.ShouldBe(onSavedRevision); testObject.ShouldBe(onSavingObj); testObject.ShouldBe(onSavedObj); } }
public void should_be_able_to_soft_delete_a_document() { using (var session = new TemporarySession()) { var config = Documents.Configure <TestDocUnique>(); config.TypeName = "testdocunique"; var testObject = new TestDocUnique { Name = "hello world" }; var result = session.Documents.Save <TestDocUnique>(testObject); Console.Write(result.Id); session.Documents.Delete(testObject); Console.WriteLine("x"); } }
public void should_be_able_to_use_a_custom_documet_type() { using (var session = new TemporarySession()) { var config = Documents.Configure <TestDocUnique>(); config.TypeName = "testdocunique"; var testObject = new TestDocUnique { Name = "hello world" }; var result = session.Documents.Save <TestDocUnique>(testObject); Console.Write(result.Id); var testObjectFromDb = session.Documents.Get <JObject>(result.Id); Assert.AreEqual("testdocunique", testObjectFromDb["type"].ToString()); } }
public void should_be_able_to_use_a_slight_of_hand_with_my_types_id() { using (var session = new TemporarySession()) { var config = Documents.Configure <TestDocUnique>(); config.CreateId = unique => "TestDocUnique-" + unique.Name; config.ResolveId = s => "TestDocUnique-" + s; var testObject = new TestDocUnique { Name = "hello world" }; var result = session.Documents.Save <TestDocUnique>(testObject); Console.Write(result.Id); var testObjectFromDb = session.Documents.Get <TestDocUnique>("hello world"); Assert.NotNull(testObjectFromDb); } }
public void should_throw_an_exception_if_unique_constraint_violated() { using (var session = new TemporarySession()) { Documents.Configure <TestDocUnique>() .UniqueConstraint = unique => unique.Name; var testObject = new TestDocUnique { Name = "hello world" }; var result = session.Documents.Save <TestDocUnique>(testObject); Console.Write(result.Id); var conflictDOc = new TestDocUnique { Name = "hello world" }; Assert.Throws <ArgumentException>(() => session.Documents.Save(conflictDOc)); } }
public void should_save_a_document_version_history_when_enabled_and_document_doesnt_have_attachment_property() { using (var session = new TemporarySession()) { var config = Documents.Configure <TestDocUnique>(); var testObject = new TestDocUnique { Name = "hello world" }; var result = session.Documents.Save <TestDocUnique>(testObject); config.KeepHistory = 10; testObject = new TestDocUnique { Name = "hello world" }; result = session.Documents.Save <TestDocUnique>(testObject); testObject.Name = "Hello World 2"; result = session.Documents.Save <TestDocUnique>(testObject); var jObject = session.Documents.Get <JObject>(result.Id); Assert.That(jObject["_attachments"], Is.Not.Null); Assert.That(jObject["_attachments"].Children().Count(), Is.EqualTo(1)); testObject.Name = "Rock Out"; result = session.Documents.Save <TestDocUnique>(testObject); jObject = session.Documents.Get <JObject>(result.Id); Assert.That(jObject["_attachments"], Is.Not.Null); Assert.That(jObject["_attachments"].Children().Count(), Is.EqualTo(2)); } }
public void history_should_be_limited_to_keep_history_length() { using (var session = new TemporarySession()) { var config = Documents.Configure <TestDocUnique>(); var testObject = new TestDocUnique { Name = "hello world" }; var result = session.Documents.Save <TestDocUnique>(testObject); config.KeepHistory = 2; testObject = new TestDocUnique { Name = "hello world" }; result = session.Documents.Save <TestDocUnique>(testObject); testObject.Name = "Hello World 2"; result = session.Documents.Save <TestDocUnique>(testObject); testObject.Name = "Hello World 3"; result = session.Documents.Save <TestDocUnique>(testObject); testObject.Name = "Hello World 4"; result = session.Documents.Save <TestDocUnique>(testObject); var jObject = session.Documents.Get <JObject>(result.Id); Assert.That(jObject["_attachments"], Is.Not.Null); Assert.That(jObject["_attachments"].Children().Count(), Is.EqualTo(2)); } }