Beispiel #1
0
        public void TestCreateDictionary()
        {
            var address = new MutableDictionaryObject();

            address.Count.Should().Be(0, "because the dictionary is empty");
            address.ToDictionary().Should().BeEmpty("because the dictionary is empty");

            var doc1 = new MutableDocument("doc1");

            doc1.SetDictionary("address", address);
            doc1.GetDictionary("address")
            .Should()
            .BeSameAs(address, "because the document should return the same instance");

            Db.Save(doc1);
            var gotDoc = Db.GetDocument("doc1");

            gotDoc.GetDictionary("address").ToDictionary().Should().BeEmpty("because the content should not have changed");
        }
Beispiel #2
0
        public void TestConflictHandlerReturnsFalse()
        {
            using (var doc1 = new MutableDocument("doc1")) {
                doc1.SetString("name", "Jim");
                Db.Save(doc1, (updated, current) => {
                    return(false);
                });

                Db.GetDocument(doc1.Id).GetString("name").Should().Be("Jim");

                var doc1a = new MutableDocument(doc1.Id);
                doc1a.SetString("name", "Kim");
                Db.Save(doc1a, (updated, current) => {
                    return(false);
                });

                Db.GetDocument("doc1").GetString("name").Should().Be("Jim");
            }
        }
Beispiel #3
0
        public void TestSaveConcurrencyControl()
        {
            using (var doc1a = new MutableDocument("doc1"))
                using (var doc1b = new MutableDocument("doc1")) {
                    doc1a.SetString("name", "Jim");
                    Db.Save(doc1a);
                    doc1b.SetString("name", "Tim");
                    Db.Save(doc1b, ConcurrencyControl.FailOnConflict).Should()
                    .BeFalse("beacuse a conflict should not be allowed in this mode");
                    using (var gotDoc = Db.GetDocument(doc1a.Id)) {
                        gotDoc.GetString("name").Should().Be("Jim");
                    }

                    Db.Save(doc1b, ConcurrencyControl.LastWriteWins);
                    using (var gotDoc = Db.GetDocument(doc1a.Id)) {
                        gotDoc.GetString("name").Should().Be("Tim");
                    }
                }
        }
Beispiel #4
0
        public void TestOverwriteDocWithNewDocInstance()
        {
            using (var mDoc1 = new MutableDocument("abc"))
                using (var mDoc2 = new MutableDocument("abc")) {
                    mDoc1.SetString("somekey", "someVar");
                    mDoc2.SetString("somekey", "newVar");

                    // This causes a conflict, the default conflict resolver should be applied
                    Db.Save(mDoc1);
                    Db.Save(mDoc2);

                    // NOTE: Both doc1 and doc2 are generation 1.  Last write should win.
                    Db.Count.Should().Be(1UL);
                    using (var doc = Db.GetDocument("abc")) {
                        doc.Should().NotBeNull();
                        doc.GetString("somekey").Should().Be("newVar", "because the last write should win");
                    }
                }
        }
        public void TestSetNestedDictionaries()
        {
            var doc    = new MutableDocument("doc1");
            var level1 = new MutableDictionaryObject();

            level1.SetString("name", "n1");
            doc.SetDictionary("level1", level1);

            var level2 = new MutableDictionaryObject();

            level2.SetString("name", "n2");
            level1.SetDictionary("level2", level2);

            var level3 = new MutableDictionaryObject();

            level3.SetString("name", "n3");
            level2.SetDictionary("level3", level3);

            doc.GetDictionary("level1").ShouldBeEquivalentTo(level1, "because that is what was inserted");
            level1.GetDictionary("level2").ShouldBeEquivalentTo(level2, "because that is what was inserted");
            level2.GetDictionary("level3").ShouldBeEquivalentTo(level3, "because that is what was inserted");
            var dict = new Dictionary <string, object> {
                ["level1"] = new Dictionary <string, object> {
                    ["name"]   = "n1",
                    ["level2"] = new Dictionary <string, object> {
                        ["name"]   = "n2",
                        ["level3"] = new Dictionary <string, object> {
                            ["name"] = "n3"
                        }
                    }
                }
            };

            doc.ToDictionary().ShouldBeEquivalentTo(dict, "because otherwise the document's contents are incorrect");

            Db.Save(doc);
            var gotDoc = Db.GetDocument("doc1");

            gotDoc.GetDictionary("level1").Should().NotBeSameAs(level1);
            gotDoc.ToDictionary().ShouldBeEquivalentTo(dict);
        }
Beispiel #6
0
        public void TestGetContent6MBFile()
        {
            byte[] bytes = null;
            using (var stream = typeof(BlobTest).GetTypeInfo().Assembly.GetManifestResourceStream("iTunesMusicLibrary.json"))
                using (var sr = new BinaryReader(stream)) {
                    bytes = sr.ReadBytes((int)stream.Length);
                }

            var blob = new Blob("application/json", bytes);

            using (var mDoc = new MutableDocument("doc1")) {
                mDoc.SetBlob("blob", blob);
                Db.Save(mDoc);
                using (var doc = Db.GetDocument(mDoc.Id)) {
                    var savedBlob = doc.GetBlob("blob");
                    savedBlob.Should().NotBeNull();
                    savedBlob.ContentType.Should().Be("application/json");
                    savedBlob.Content.Should().Equal(bytes);
                }
            }
        }
Beispiel #7
0
        public void TestTypesInDictionaryToJSON()
        {
            var dic = PopulateDictData();
            var md  = new MutableDictionaryObject();

            foreach (var item in dic)
            {
                md.SetValue(item.Key, item.Value); // platform dictionary and list or array will be converted into Couchbase object in SetValue method
            }

            using (var doc = new MutableDocument("doc1")) {
                doc.SetDictionary("dict", md);
                Db.Save(doc);
            }

            using (var doc = Db.GetDocument("doc1")) {
                var dict = doc.GetDictionary("dict");
                var json = dict.ToJSON();
                ValidateToJsonValues(json, dic);
            }
        }
        public void TestExternalChanges()
        {
            using (var db2 = new Database(Db)) {
                var countdownDB = new CountdownEvent(1);
                db2.AddChangeListener((sender, args) =>
                {
                    args.Should().NotBeNull();
                    args.DocumentIDs.Count.Should().Be(10);
                    countdownDB.CurrentCount.Should().Be(1);
                    countdownDB.Signal();
                });


                var countdownDoc = new CountdownEvent(1);
                db2.AddDocumentChangeListener("doc-6", (sender, args) =>
                {
                    args.Should().NotBeNull();
                    args.DocumentID.Should().Be("doc-6");
                    using (var doc = Db.GetDocument(args.DocumentID)) {
                        doc.GetString("type").Should().Be("demo");
                        countdownDoc.CurrentCount.Should().Be(1);
                        countdownDoc.Signal();
                    }
                });

                Db.InBatch(() =>
                {
                    for (var i = 0; i < 10; i++)
                    {
                        using (var doc = new MutableDocument($"doc-{i}")) {
                            doc.SetString("type", "demo");
                            Db.Save(doc);
                        }
                    }
                });

                countdownDB.Wait(TimeSpan.FromSeconds(5)).Should().BeTrue();
                countdownDoc.Wait(TimeSpan.FromSeconds(5)).Should().BeTrue();
            }
        }
Beispiel #9
0
        public void TestDocIDFilter()
        {
            var doc1 = new MutableDocument("doc1");

            doc1.SetString("species", "Tiger");
            Db.Save(doc1);
            doc1.SetString("name", "Hobbes");
            Db.Save(doc1);

            var doc2 = new MutableDocument("doc2");

            doc2.SetString("species", "Tiger");
            Db.Save(doc2);
            doc2.SetString("pattern", "striped");
            Db.Save(doc2);

            var doc3 = new MutableDocument("doc3");

            doc3.SetString("species", "Tiger");
            _otherDB.Save(doc3);
            doc3.SetString("name", "Hobbes");
            _otherDB.Save(doc3);

            var doc4 = new MutableDocument("doc4");

            doc4.SetString("species", "Tiger");
            _otherDB.Save(doc4);
            doc4.SetString("pattern", "striped");
            _otherDB.Save(doc4);

            var config = CreateConfig(true, true, false);

            config.DocumentIDs = new[] { "doc1", "doc3" };
            RunReplication(config, 0, 0);
            Db.Count.Should().Be(3, "because only one document should have been pulled");
            Db.GetDocument("doc3").Should().NotBeNull();
            _otherDB.Count.Should().Be(3, "because only one document should have been pushed");
            _otherDB.GetDocument("doc1").Should().NotBeNull();
        }
        public void TestDelete()
        {
            var stopwatch = Stopwatch.StartNew();

            const int    n   = 2000;
            const string tag = "Delete";

            for (var i = 0; i < n; i++)
            {
                var docID = $"doc-{i:D10}";
                CreateDocumentNSave(docID, tag);
                Db.Count.Should().Be(1);
                using (var doc = Db.GetDocument(docID)) {
                    doc.GetString("tag").Should().Be(tag);
                    Db.Delete(doc);
                    Db.Count.Should().Be(0);
                }
            }

            stopwatch.Stop();
            LogPerformanceStats("TestDelete()", stopwatch.Elapsed);
        }
        public void TestRead()
        {
            var stopwatch = Stopwatch.StartNew();

            const int    n     = 2000;
            const string docID = "doc1";
            const string tag   = "Read";

            CreateDocumentNSave(docID, tag);

            for (var i = 0; i < n; i++)
            {
                using (var doc = Db.GetDocument(docID)) {
                    doc.Should().NotBeNull();
                    doc.Id.Should().Be(docID);
                    doc.GetString("tag").Should().Be(tag);
                }
            }

            stopwatch.Stop();
            LogPerformanceStats("TestRead()", stopwatch.Elapsed);
        }
        public void TestPullDoc()
        {
            using (var doc1 = new MutableDocument("doc1")) {
                doc1.SetString("name", "Tiger");
                Db.Save(doc1).Dispose();
                Db.Count.Should().Be(1, "because only one document was saved so far");
            }

            using (var doc2 = new MutableDocument("doc2")) {
                doc2.SetString("name", "Cat");
                _otherDB.Save(doc2).Dispose();
            }

            var config = CreateConfig(false, true, false);

            RunReplication(config, 0, 0);

            Db.Count.Should().Be(2, "because the replicator should have pulled doc2 from the other DB");
            using (var doc2 = Db.GetDocument("doc2")) {
                doc2.GetString("name").Should().Be("Cat");
            }
        }
        private void RunTestWithNumbers(IList <int> expectedResultCount,
                                        IList <Tuple <LinqExpression, Func <NumbersModel, object, bool>, object, ParameterExpression> > validator)
        {
            int index     = 0;
            var queryable = new DatabaseQueryable <NumbersModel>(Db);

            foreach (var c in validator)
            {
                var q = queryable.Where(LinqExpression.Lambda <Func <NumbersModel, bool> >(c.Item1, c.Item4)).Select(x => x.Id());

                var lastN = 0;
                VerifyQuery(q, (n, row) =>
                {
                    var doc = Db.GetDocument(row).ToModel <NumbersModel>();
                    c.Item2(doc, c.Item3).Should().BeTrue("because otherwise the row failed validation");
                    lastN = n;
                });

                lastN.Should()
                .Be(expectedResultCount[index++], "because otherwise there was an incorrect number of rows");
            }
        }
        public void TestPullConflictNoBaseRevision()
        {
            // Create the conflicting docs separately in each database.  They have the same base revID
            // because the contents are identical, but because the DB never pushed revision 1, it doesn't
            // think it needs to preserve the body; so when it pulls a conflict, there won't be a base
            // revision for the resolver.

            var doc1 = new MutableDocument("doc");

            doc1.SetString("species", "tiger");
            var saved = Db.Save(doc1);

            Misc.SafeSwap(ref doc1, saved.ToMutable());
            doc1.SetString("name", "Hobbes");
            Db.Save(doc1);

            var doc2 = new MutableDocument("doc");

            doc2.SetString("species", "Tiger");
            saved = _otherDB.Save(doc2);
            Misc.SafeSwap(ref doc2, saved.ToMutable());
            doc2.SetString("pattern", "striped");
            _otherDB.Save(doc2);

            var config = CreateConfig(false, true, false);

            config.ConflictResolver = new MergeThenTheirsWins();
            RunReplication(config, 0, 0);

            Db.Count.Should().Be(1, "because the document in otherDB has the same ID");
            var gotDoc1 = Db.GetDocument("doc");

            gotDoc1.ToDictionary().ShouldBeEquivalentTo(new Dictionary <string, object> {
                ["species"] = "Tiger",
                ["name"]    = "Hobbes",
                ["pattern"] = "striped"
            });
        }
Beispiel #15
0
        public void TestPullDocContinuous()
        {
            using (var doc1 = new MutableDocument("doc1")) {
                doc1.SetString("name", "Tiger");
                Db.Save(doc1);
                Db.Count.Should().Be(1, "because only one document was saved so far");
            }

            using (var doc2 = new MutableDocument("doc2")) {
                doc2.SetString("name", "Cat");
                _otherDB.Save(doc2);
            }

            var config = CreateConfig(false, true, true);

            config.CheckpointInterval = TimeSpan.FromSeconds(1);
            RunReplication(config, 0, 0);

            Db.Count.Should().Be(2, "because the replicator should have pulled doc2 from the other DB");
            using (var doc2 = Db.GetDocument("doc2")) {
                doc2.GetString("name").Should().Be("Cat");
            }
        }
        public void TestReplaceDictionaryDifferentType()
        {
            var doc      = new MutableDocument("doc1");
            var profile1 = new MutableDictionaryObject();

            profile1.SetString("name", "Scott Tiger");
            doc.SetDictionary("profile", profile1);
            doc.GetDictionary("profile").ShouldBeEquivalentTo(profile1, "because that is what was set");

            doc.SetString("profile", "Daniel Tiger");
            doc.GetString("profile").Should().Be("Daniel Tiger", "because that is what was set");

            profile1.SetInt("age", 20);
            profile1.GetString("name").Should().Be("Scott Tiger", "because profile1 should be detached now");
            profile1.GetInt("age").Should().Be(20, "because profile1 should be detached now");

            doc.GetString("profile").Should().Be("Daniel Tiger", "because profile1 should not affect the new value");

            Db.Save(doc);
            var gotDoc = Db.GetDocument("doc1");

            gotDoc.GetString("profile").Should().Be("Daniel Tiger", "because that is what was saved");
        }
Beispiel #17
0
        public void TestResetCheckpoint()
        {
            using (var doc1 = new MutableDocument("doc1")) {
                doc1.SetString("species", "Tiger");
                doc1.SetString("name", "Hobbes");
                Db.Save(doc1);
            }

            using (var doc2 = new MutableDocument("doc2")) {
                doc2.SetString("species", "Tiger");
                doc2.SetString("pattern", "striped");
                Db.Save(doc2);
            }

            var config = CreateConfig(true, false, false);

            RunReplication(config, 0, 0);
            config = CreateConfig(false, true, false);
            RunReplication(config, 0, 0);

            _otherDB.Count.Should().Be(2UL);
            using (var doc = Db.GetDocument("doc1")) {
                Db.Purge(doc);
            }

            using (var doc = Db.GetDocument("doc2")) {
                Db.Purge(doc);
            }

            Db.Count.Should().Be(0UL, "because the documents were purged");
            RunReplication(config, 0, 0);

            Db.Count.Should().Be(0UL, "because the documents were purged and the replicator is already past them");
            RunReplication(config, 0, 0, true);

            Db.Count.Should().Be(2UL, "because the replicator was reset");
        }
        public void TestConflictWithoutCommonAncestor()
        {
            ConflictResolver = new NoCommonAncestorValidator();
            ReopenDB();

            var props = new Dictionary <string, object> {
                ["hello"] = "world"
            };

            var doc = new MutableDocument("doc1", props);

            Db.Save(doc);

            doc = Db.GetDocument(doc.Id).ToMutable();
            doc.SetInt("university", 1);
            Db.Save(doc);

            // Create a conflict
            doc = new MutableDocument(doc.Id, props);
            doc.SetInt("university", 2);

            Db.Invoking(d => d.Save(doc)).ShouldThrow <LiteCoreException>().Which.Error.Should()
            .Be(new C4Error(C4ErrorCode.Conflict));
        }
        public void TestGetValueFromNewEmptyDictionary()
        {
            DictionaryObject dict = new MutableDictionaryObject();

            dict.GetInt("key").Should().Be(0, "because that is the default value");
            dict.GetLong("key").Should().Be(0L, "because that is the default value");
            dict.GetDouble("key").Should().Be(0.0, "because that is the default value");
            dict.GetBoolean("key").Should().Be(false, "because that is the default value");
            dict.GetDate("key").Should().Be(DateTimeOffset.MinValue, "because that is the default value");
            dict.GetBlob("key").Should().BeNull("because that is the default value");
            dict.GetValue("key").Should().BeNull("because that is the default value");
            dict.GetString("key").Should().BeNull("because that is the default value");
            dict.GetDictionary("key").Should().BeNull("because that is the default value");
            dict.GetArray("key").Should().BeNull("because that is the default value");
            dict.ToDictionary().Should().BeEmpty("because the dictionary is empty");

            var doc = new MutableDocument("doc1");

            doc.SetDictionary("dict", dict);

            Db.Save(doc);
            var gotDoc = Db.GetDocument("doc1");

            dict = gotDoc.GetDictionary("dict");
            dict.GetInt("key").Should().Be(0, "because that is the default value");
            dict.GetLong("key").Should().Be(0L, "because that is the default value");
            dict.GetDouble("key").Should().Be(0.0, "because that is the default value");
            dict.GetBoolean("key").Should().Be(false, "because that is the default value");
            dict.GetDate("key").Should().Be(DateTimeOffset.MinValue, "because that is the default value");
            dict.GetBlob("key").Should().BeNull("because that is the default value");
            dict.GetValue("key").Should().BeNull("because that is the default value");
            dict.GetString("key").Should().BeNull("because that is the default value");
            dict.GetDictionary("key").Should().BeNull("because that is the default value");
            dict.GetArray("key").Should().BeNull("because that is the default value");
            dict.ToDictionary().Should().BeEmpty("because the dictionary is empty");
        }
 public void TestGetNonExistingDocWithID()
 {
     Db.GetDocument("non-exist").Should().BeNull("because it doesn't exist");
 }
Beispiel #21
0
        public void TestBlobJsonStringInLayersOfMutableDict()
        {
            var blob = ArrayTestBlob();

            Db.SaveBlob(blob);
            var nestedBlob = new Blob("text/plain", Encoding.UTF8.GetBytes("abcde"));

            Db.SaveBlob(nestedBlob);
            var b1 = new Blob("text/plain", Encoding.UTF8.GetBytes("alpha"));

            Db.SaveBlob(b1);
            var b2 = new Blob("text/plain", Encoding.UTF8.GetBytes("beta"));

            Db.SaveBlob(b2);
            var b3 = new Blob("text/plain", Encoding.UTF8.GetBytes("omega"));

            Db.SaveBlob(b3);

            var KeyValueDictionary = new Dictionary <string, object>()
            {
                { "blob", blob },
                { "blobUnderDict", new Dictionary <string, object>()
                  {
                      { "nestedBlob", nestedBlob }
                  } },
                { "blobUnderArr", new List <object>()
                  {
                      b1, b2, b3
                  } }
            };

            var dicJson = JsonConvert.SerializeObject(KeyValueDictionary);
            var md      = new MutableDictionaryObject(dicJson);

            using (var mdoc = new MutableDocument("doc1")) {
                mdoc.SetDictionary("dict", md);
                Db.Save(mdoc);
            }

            var dic = Db.GetDocument("doc1").GetDictionary("dict");

            var blob1 = dic.GetBlob("blob");

            blob1.Content.Should().NotBeNull();
            blob1.Should().BeEquivalentTo(KeyValueDictionary["blob"]);

            var blob2 = dic.GetDictionary("blobUnderDict").GetBlob("nestedBlob");

            blob2.Content.Should().NotBeNull();
            var d = (Dictionary <string, object>)KeyValueDictionary["blobUnderDict"];

            blob2.Should().BeEquivalentTo(d["nestedBlob"]);

            var blobs    = dic.GetArray("blobUnderArr");
            var cnt      = blobs.Count;
            var blobList = (List <object>)KeyValueDictionary["blobUnderArr"];

            for (int i = 0; i < cnt; i++)
            {
                var b = blobs.GetBlob(i);
                b.Content.Should().NotBeNull();
                b.Should().BeEquivalentTo(blobList[i]);
            }
        }
Beispiel #22
0
        public void TestShortP2P()
        {
            var testNo = 1;

            foreach (var protocolType in new[] { ProtocolType.ByteStream, ProtocolType.MessageStream })
            {
                using (var mdoc = new MutableDocument("livesindb")) {
                    mdoc.SetString("name", "db");
                    Db.Save(mdoc);
                }

                using (var mdoc = new MutableDocument("livesinotherdb")) {
                    mdoc.SetString("name", "otherdb");
                    _otherDB.Save(mdoc);
                }


                // PUSH
                var listener = new MessageEndpointListener(new MessageEndpointListenerConfiguration(_otherDB, protocolType));
                var server   = new MockServerConnection(listener, protocolType);
                var config   = new ReplicatorConfiguration(Db,
                                                           new MessageEndpoint($"p2ptest{testNo++}", server, protocolType,
                                                                               new MockConnectionFactory(null)))
                {
                    ReplicatorType = ReplicatorType.Push,
                    Continuous     = false
                };
                RunReplication(config, 0, 0);
                _otherDB.Count.Should().Be(2UL, "because it contains the original and new");
                Db.Count.Should().Be(1UL, "because there is no pull, so the first db should only have the original");

                // PULL
                server = new MockServerConnection(listener, protocolType);
                config = new ReplicatorConfiguration(Db,
                                                     new MessageEndpoint($"p2ptest{testNo++}", server, protocolType,
                                                                         new MockConnectionFactory(null)))
                {
                    ReplicatorType = ReplicatorType.Pull,
                    Continuous     = false
                };

                RunReplication(config, 0, 0);
                Db.Count.Should().Be(2UL, "because the pull should add the document from otherDB");

                using (var savedDoc = Db.GetDocument("livesinotherdb"))
                    using (var mdoc = savedDoc.ToMutable()) {
                        mdoc.SetBoolean("modified", true);
                        Db.Save(mdoc);
                    }

                using (var savedDoc = _otherDB.GetDocument("livesindb"))
                    using (var mdoc = savedDoc.ToMutable()) {
                        mdoc.SetBoolean("modified", true);
                        _otherDB.Save(mdoc);
                    }

                // PUSH & PULL
                server = new MockServerConnection(listener, protocolType);
                config = new ReplicatorConfiguration(Db,
                                                     new MessageEndpoint($"p2ptest{testNo++}", server, protocolType,
                                                                         new MockConnectionFactory(null)))
                {
                    Continuous = false
                };

                RunReplication(config, 0, 0);
                Db.Count.Should().Be(2UL, "because no new documents were added");

                using (var savedDoc = Db.GetDocument("livesindb")) {
                    savedDoc.GetBoolean("modified").Should()
                    .BeTrue("because the property change should have come from the other DB");
                }

                using (var savedDoc = _otherDB.GetDocument("livesinotherdb")) {
                    savedDoc.GetBoolean("modified").Should()
                    .BeTrue("because the proeprty change should come from the original DB");
                }

                Db.Delete();
                ReopenDB();
                _otherDB.Delete();
                _otherDB.Dispose();
                _otherDB = OpenDB(_otherDB.Name);
            }
        }