Example #1
0
        public void should_use_configuration_database_when_specified()
        {
            using (var session = new TemporarySession())
            {
                using (var anotherSession = new TemporarySession())
                {
                    Documents.Configure <TestDoc>(database: anotherSession.DatabaseName);

                    var testObject = new TestDoc {
                        Text = "hello world"
                    };

                    var info = session.Save(testObject, "test");

                    var fromDb = session.Get <JObject>(info.Id);
                    fromDb.ShouldBe(null);


                    session.DatabaseName.ShouldBe(session.DefaultDatabase);

                    var fromDb2 = session.Get <TestDoc>(info.Id);
                    fromDb2.ShouldNotBe(null);

                    var info2 = session.Documents.Save(testObject);

                    info2.Id.ShouldBe(info.Id);

                    session.DatabaseName.ShouldBe(session.DefaultDatabase);
                }
            }
        }
Example #2
0
        public void test_update_handlers_with_document()
        {
            using (var session = new TemporarySession())
            {
                var designDoc = new SimpleDesignDocument();
                designDoc.Updates["test1"] = @"
function(doc,req)
{
   
    var resp = {
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': req.body ? toJSON( JSON.parse(req.body) ) : toJSON({m: 'null body'})
    };
    
    return [doc, resp];

}".Trim();

                session.Design("test1").Put(designDoc);

                var response = session.Design("test1").Update <JToken>("test1", "x", payload: new { Hello = "World" });

                Console.WriteLine(response);
            }
        }
Example #3
0
        public void test_update_handlers()
        {
            using (var session = new TemporarySession())
            {
                var designDoc = new SimpleDesignDocument();
                designDoc.Updates["test"] = @"
function(doc,req)
{
   
    var resp = {
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': toJSON( JSON.parse(req.body) )
    };
    
    return [doc, resp];

}".Trim();

                session.Design("test").Put(designDoc);

                var response = session.Design("test").Update <TestDoc, JObject>("test", new TestDoc()
                {
                    Text = "brand new"
                }, Guid.NewGuid().ToString());

                Console.WriteLine(response);
            }
        }
Example #4
0
        public void should_save_only_create_history_if_something_changes()
        {
            using (var session = new TemporarySession())
            {
                var config = Documents.Configure <TestDocUniqueVersioned>();

                var testObject = new TestDocUniqueVersioned {
                    Name = "hello world"
                };

                var result = session.Documents.Save <TestDocUniqueVersioned>(testObject);

                config.KeepHistory = 10;

                result = session.Documents.Save <TestDocUniqueVersioned>(testObject);

                result = session.Documents.Save <TestDocUniqueVersioned>(testObject);

                testObject = session.Documents.Get <TestDocUniqueVersioned>(result.Id);

                Assert.That(testObject._Attachments, Is.Null);

                testObject.Name = "Rock Out";

                result = session.Documents.Save <TestDocUniqueVersioned>(testObject);

                testObject = session.Documents.Get <TestDocUniqueVersioned>(result.Id);

                Assert.That(testObject._Attachments, Is.Not.Null);
                Assert.That(testObject._Attachments.Count, Is.EqualTo(1));
            }
        }
Example #5
0
        public void attached_version_document_should_not_have_history()
        {
            using (var session = new TemporarySession())
            {
                var config = Documents.Configure <TestDocUniqueVersioned>();

                var testObject = new TestDocUniqueVersioned {
                    Name = "hello world 1"
                };

                config.KeepHistory = 10;

                var result = session.Documents.Save(testObject);

                testObject.Name = "Hello World 2";

                result = session.Documents.Save(testObject);

                testObject.Name = "Hello World 3";

                result = session.Documents.Save(testObject);

                testObject = session.Documents.Get <TestDocUniqueVersioned>(result.Id);

                Assert.That(testObject._Attachments, Is.Not.Null);
                Assert.That(testObject._Attachments.Count, Is.EqualTo(2));

                var version1 = session.Documents.GetDocumentAttachment <TestDocUniqueVersioned>(result.Id, testObject._Attachments.First().Key);
                version1._Attachments.ShouldBe(null);

                var version2 = session.Documents.GetDocumentAttachment <TestDocUniqueVersioned>(result.Id, testObject._Attachments.Skip(1).First().Key);
                version2._Attachments.ShouldBe(null);
            }
        }
Example #6
0
        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);
            }
        }
Example #7
0
        public void test_filters()
        {
            using (var session = new TemporarySession())
            {
                var designDoc = new SimpleDesignDocument();
                designDoc.Filters["testFilter"] = @"
function(doc,req)
{
   
    return doc.filter;

}".Trim();

                session.Design("test").Put(designDoc);

                session.Documents.Save(new { item = "1", filter = false });
                session.Documents.Save(new { item = "2", filter = true });
                session.Documents.Save(new { item = "3", filter = true });

                var results = session.Changes.Poll(new ChangesQuery()
                {
                    Filter = "test/testFilter"
                });

                Assert.That(results.Results.Length, Is.EqualTo(2));

                results = session.Changes.Poll(new ChangesQuery()
                {
                });

                Assert.That(results.Results.Length, Is.EqualTo(4));
            }
        }
Example #8
0
        public void TestContinous()
        {
            using (var session = new TemporarySession())
            {
                var run = true;
                using (var task = Task.Run(() =>
                {
                    int id = 1;
                    while (run)
                    {
                        session.Documents.Save(new { item = "item_" + (id++), filter = false });
                        Thread.Sleep(500);
                    }
                }))
                {
                    Console.WriteLine("DB: " + session.DatabaseName);

                    Console.WriteLine(""); Console.WriteLine("");

                    Thread.Sleep(1000);


                    session.Changes.Stream(Console.WriteLine, null, pair => Console.WriteLine("Header: {0}:{1}", pair.Key, pair.Value));


                    run = false;

                    Thread.Sleep(700);
                }
            }
        }
Example #9
0
        public void should_be_able_insert_typed_documents()
        {
            using (var session = new TemporarySession())
            {
                var testObject1 = new TestDoc {
                    Text = "hello world 1"
                };
                var testObject2 = new TestDoc {
                    Text = "hello world 2"
                };
                var testObject3 = new TestDoc {
                    Text = "hello world 3"
                };



                session.Bulk.Update(new[] { testObject1, testObject2, testObject3 });

                var result = session.Bulk.All(new ViewQuery()
                {
                    IncludeDocs = true
                });

                result.Rows.Length.ShouldBe(3);
                result.Documents.First().ToObject <TestDoc>().Text.ShouldBe("hello world 1");
            }
        }
Example #10
0
        public void db_put_get_delete()
        {
            using (var session = new TemporarySession())
            {
                var testObject = new TestDoc {
                    Text = "hello world"
                };

                var result = session.Get <TestDoc>("test");

                Assert.That(result, Is.Null);

                var info = session.Save(testObject, "test");

                testObject.Text = "hello world chagned";

                session.Save(testObject);

                testObject.Text = "hello world changed";

                info = session.Save(testObject);

                testObject.Text = "hello world changed 2";

                session.Save(testObject, info);

                session.Delete(testObject);

                result = session.Get <TestDoc>("test");

                Assert.That(result, Is.Null);
            }
        }
Example #11
0
        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);
            }
        }
Example #12
0
        public void should_be_able_to_get_delete_all_docs_using_helper()
        {
            using (var session = new TemporarySession())
            {
                var testObject1 = new TestDoc {
                    Text = "hello world"
                };
                var testObject2 = new TestDoc {
                    Text = "hello world"
                };
                var testObject3 = new TestDoc {
                    Text = "hello world"
                };

                var info1 = session.Save(testObject1);
                var info2 = session.Save(testObject2);
                var info3 = session.Save(testObject3);

                var result = session.Bulk.All(new ViewQuery()
                {
                    IncludeDocs = true
                });
                result.Rows.Length.ShouldBe(3);

                session.Bulk.Delete(result.Documents.ToArray());

                result = session.Bulk.All(new ViewQuery()
                {
                    IncludeDocs = true
                });
                result.Rows.Length.ShouldBe(0);
            }
        }
Example #13
0
        public void should_be_able_to_create_a_user()
        {
            using (var session = new TemporarySession())
            {
                var user = new User()
                {
                    Name = Guid.NewGuid().ToString()
                };

                session.Users.Save(user);

                Console.WriteLine(session.DatabaseName);
            }
        }
Example #14
0
        public void should_be_able_to_create_and_get_a_user()
        {
            using (var session = new TemporarySession())
            {
                var user = new User()
                {
                    Name = Guid.NewGuid().ToString()
                };

                session.Users.Save(user);

                var fromDb = session.Users.Get <User>(user.Name);

                Assert.IsNotNull(fromDb);
            }
        }
Example #15
0
        public void db_put_get_delete()
        {
            using (var session = new TemporarySession())
            {
                var db = session.Db.Get();

                Assert.That(db, Is.Not.Null);
                Assert.That(db.Name, Is.EqualTo(session.DatabaseName));

                session.Db.Delete();

                db = session.Db.Get();

                Assert.That(db, Is.Null);
            }
        }
Example #16
0
        public void TestNormalPoll()
        {
            using (var session = new TemporarySession())
            {
                session.Documents.Save(new { item = "1", filter = false });
                session.Documents.Save(new { item = "2", filter = true });
                session.Documents.Save(new { item = "3", filter = true });

                var results = session.Changes.Poll(new ChangesQuery()
                {
                    IncludeDocs = true
                });

                Assert.That(results.Results.Length, Is.EqualTo(3));
            }
        }
Example #17
0
        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);
            }
        }
Example #18
0
        public void TestLongPoll()
        {
            using (var session = new TemporarySession())
            {
                session.Documents.Save(new { item = "1", filter = false });
                session.Documents.Save(new { item = "2", filter = true });
                session.Documents.Save(new { item = "3", filter = true });

                Console.WriteLine("YOU NEED TO CHANGE 1 DOCUMENT FO THIS TEST TO WORK, DB: " + session.DatabaseName);
                var results = session.Changes.LongPoll();

                results = session.Changes.LongPoll(new ChangesQuery()
                {
                    Since = results.LastSequence
                });

                Assert.That(results.Results.Length, Is.EqualTo(1));
            }
        }
Example #19
0
        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");
            }
        }
Example #20
0
        public void should_be_able_to_set_password_and_authenticate_2()
        {
            using (var session = new TemporarySession())
            {
                var user = new User()
                {
                    Name = Guid.NewGuid().ToString(),
                };

                var password = Guid.NewGuid().ToString();

                session.Users.Save(user);

                session.Users.SetPasword(user.Name, password);

                var result = session.Users.Authenticate <User>(user.Name, password);

                Assert.IsTrue(result.OK);
            }
        }
Example #21
0
        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());
            }
        }
Example #22
0
        public void create_design_doc_with_a_update_handler_create_new_doc()
        {
            using (var session = new TemporarySession())
            {
                var designDoc = new SimpleDesignDocument();
                designDoc.Updates["test"] = @"
function(doc,req)
{
    if (!doc) {
        if (req.id)
            return [{ _id : req.id}, Text= toJSON('New World')]
        return [null, toJSON('Empty World')];
    }
    doc.world = 'hello';
    doc.edited_by = req.userCtx;
    return [doc, toJSON('Existing World')];
}".Trim();

                session.Design("test").Put(designDoc);

                var testObject = new TestDoc {
                    Text = "hello world"
                };
                var results = session.Design("test").Update <TestDoc, string>("test", testObject);

                Assert.That(results, Is.EqualTo("New World"));

                results = session.Design("test").Update <TestDoc, string>("test", testObject);

                Assert.That(results, Is.EqualTo("Existing World"));

                var orginalInfo = session.Documents.GetInfo(testObject);

                results = session.Design("test").Update <TestDoc, string>("test", new TestDoc()
                {
                    Text = "brand new"
                }, orginalInfo.Id);

                Assert.That(results, Is.EqualTo("Existing World"));
            }
        }
Example #23
0
        public void create_design_doc_with_a_view_and_query()
        {
            using (var session = new TemporarySession())
            {
                var testObject = new TestDoc {
                    Text = "hello world"
                };

                var info1 = session.Documents.Save(testObject, "test1");
                var info2 = session.Documents.Save(testObject, "test2");


                var designDoc = new SimpleDesignDocument();
                designDoc.Views["test"] = new SimpleDesignDocument.View()
                {
                    Map = "function(doc){ emit(doc._id); }"
                };

                session.Design("test").Put(designDoc);


                var results = session.Design("test").View <object, TestDoc>("test");


                Assert.That(results.Rows.Length, Is.EqualTo(2));

                results = session.Design("test").ViewDocs <TestDoc>("test");
                Assert.That(results.Documents.Length, Is.GreaterThan(0));
                Assert.That(results.Documents[0], Is.Not.Null);


                results = session.Design("test").ViewDocs <TestDoc>("test", track: true);

                var doc1 = results.Documents[0];

                doc1.Text = "arg I got changed from a view result";

                Assert.That(session.Documents.GetInfo(doc1), Is.Not.Null);
                session.Documents.Save(doc1);
            }
        }
Example #24
0
        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);
            }
        }
Example #25
0
        public void should_save_a_document_version_history_when_enabled()
        {
            using (var session = new TemporarySession())
            {
                var config = Documents.Configure <TestDocUniqueVersioned>();

                var testObject = new TestDocUniqueVersioned {
                    Name = "hello world"
                };

                var result = session.Documents.Save <TestDocUniqueVersioned>(testObject);

                config.KeepHistory = 10;

                testObject = new TestDocUniqueVersioned {
                    Name = "hello world 1"
                };

                result = session.Documents.Save <TestDocUniqueVersioned>(testObject);

                testObject.Name = "hello world";

                result = session.Documents.Save <TestDocUniqueVersioned>(testObject);

                testObject = session.Documents.Get <TestDocUniqueVersioned>(result.Id);

                Assert.That(testObject._Attachments, Is.Not.Null);
                Assert.That(testObject._Attachments.Count, Is.EqualTo(1));

                testObject.Name = "Rock Out";

                result = session.Documents.Save <TestDocUniqueVersioned>(testObject);


                testObject = session.Documents.Get <TestDocUniqueVersioned>(result.Id);

                Assert.That(testObject._Attachments, Is.Not.Null);
                Assert.That(testObject._Attachments.Count, Is.EqualTo(2));
            }
        }
Example #26
0
        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));
            }
        }
Example #27
0
        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));
            }
        }
Example #28
0
        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));
            }
        }
Example #29
0
        public void should_be_able_to_get_all_docs()
        {
            using (var session = new TemporarySession())
            {
                var testObject1 = new TestDoc {
                    Text = "hello world"
                };
                var testObject2 = new TestDoc {
                    Text = "hello world"
                };
                var testObject3 = new TestDoc {
                    Text = "hello world"
                };

                var info1 = session.Save(testObject1);
                var info2 = session.Save(testObject2);
                var info3 = session.Save(testObject3);

                var result = session.Bulk.All();

                result.Rows.Length.ShouldBe(3);
            }
        }
Example #30
0
        public void should_be_able_to_set_password_and_authenticate()
        {
            using (var session = new TemporarySession())
            {
                var user = new User()
                {
                    Name = Guid.NewGuid().ToString(),

                    Roles = new[] { "jedi" }
                };

                var password = Guid.NewGuid().ToString();

                session.Users.Save(user);

                session.Users.SetPasword(user.Name, password);

                var result = session.Users.Authenticate <User>(user.Name, password);

                Assert.IsTrue(result.OK);
                Assert.That(result.User.Roles[0], Is.EqualTo("jedi"));
            }
        }