示例#1
0
        public void TestBsonDictRemove()
        {
            var collection = GetTestCollection <BsonDocument>();

            Role role = new Role {
                RoleID = 1
            };

            role.Texts[11] = "abc";
            role.ClearDirty();
            collection.InsertOne((BsonDocument)role);

            UpdateContext context = new UpdateContext();

            role.Texts.Remove(11);
            role.Update(context);

            var bsonUpdate = context.Build();

            Assert.AreEqual(BsonDocument.Parse("{$unset:{'Texts': 1}}"), bsonUpdate);

            collection.UpdateOne(new BsonDocument {
                { "_id", 1 }
            }, bsonUpdate);

            var actual     = collection.Find(new BsonDocument()).First();
            var roleActual = (Role)actual;

            Assert.AreEqual(0, roleActual.Texts.Count);
        }
示例#2
0
        public void TestBsonList_IndexSet()
        {
            var collection = GetTestCollection <BsonDocument>();

            Role role = new Role {
                RoleID = 1
            };

            role.ListInt.Add(10);
            role.ListInt.Add(11);
            role.ListInt.Add(12);
            role.ClearDirty();

            collection.InsertOne((BsonDocument)role);

            UpdateContext context = new UpdateContext();

            role.ListInt[1] = 21;
            role.Update(context);

            collection.UpdateOne(new BsonDocument {
                { "_id", 1 }
            }, context.Build());

            var actual     = collection.Find(new BsonDocument()).First();
            var roleActual = (Role)actual;

            Assert.AreEqual(10, roleActual.ListInt[0]);
            Assert.AreEqual(21, roleActual.ListInt[1]);
            Assert.AreEqual(12, roleActual.ListInt[2]);
        }
示例#3
0
        public void TestBsonListClear()
        {
            var collection = GetTestCollection <BsonDocument>();

            Role role = new Role {
                RoleID = 1
            };

            role.ListInt.Add(10);
            role.ListInt.Add(11);
            role.ListInt.Add(12);
            role.ClearDirty();

            collection.InsertOne((BsonDocument)role);

            UpdateContext context = new UpdateContext();

            role.ListInt.Clear();
            role.Update(context);

            var bsonUpdate = context.Build();

            Assert.AreEqual(BsonDocument.Parse("{'$unset': {'ListInt': 1}}"), bsonUpdate);

            collection.UpdateOne(new BsonDocument {
                { "_id", 1 }
            }, context.Build());

            var actual     = collection.Find(new BsonDocument()).First();
            var roleActual = (Role)actual;

            Assert.AreEqual(0, roleActual.ListInt.Count);
        }
示例#4
0
        public void SetSubDocumentString()
        {
            Role role = CreateRole();

            role.Texts.Add(2, "test2");

            role.Update(context);
            Assert.AreEqual(BsonDocument.Parse("{$set:{'Texts.2':'test2'}}"), context.Build());

            role.ClearDirty();
        }
示例#5
0
        public void UnsetDocumentPrimary()
        {
            Role role = CreateRole();

            role.Nums.Add(1, 1);
            role.Nums.Add(2, 2);
            role.ClearDirty();
            role.Nums.Remove(2);

            role.Update(context);
            Assert.AreEqual(BsonDocument.Parse("{$unset:{'Nums.2':1}}"), context.Build());
        }
示例#6
0
        public void UnsetDocumentString()
        {
            Role role = CreateRole();

            role.Texts.Add(1, "test1");
            role.Texts.Add(2, "test2");
            role.ClearDirty();
            role.Texts.Remove(2);

            role.Update(context);
            Assert.AreEqual(BsonDocument.Parse("{$unset:{'Texts.2':1}}"), context.Build());
        }
示例#7
0
        public void UnsetDocumentClass()
        {
            Role role = CreateRole();

            role.Friends.Add(1, null);
            role.Friends.Add(2, new Friend {
                RoleID = 2, RoleName = "test2"
            });
            role.ClearDirty();
            role.Friends.Remove(2);

            role.Update(context);
            Assert.AreEqual(BsonDocument.Parse("{$unset:{'Friends.2':1}}"), context.Build());
        }
示例#8
0
        public void SetProperty()
        {
            Role role = CreateRole();

            role.RoleName = "test";

            role.Update(context);
            Assert.AreEqual(BsonDocument.Parse("{$set:{'RoleName':'test'}}"), context.Build());

            role.ClearDirty();
            context.Clear();

            role.RoleName = null;
            role.Update(context);
            Assert.AreEqual(BsonDocument.Parse("{$unset:{'RoleName':1}}"), context.Build());
        }
示例#9
0
        public void TestBsonList_RemoveAt_Middle()
        {
            var collection = GetTestCollection <BsonDocument>();

            Role role = new Role {
                RoleID = 1
            };

            role.ListInt.Add(10);
            role.ListInt.Add(11);
            role.ListInt.Add(12);
            role.ClearDirty();

            collection.InsertOne((BsonDocument)role);

            UpdateContext context = new UpdateContext();

            role.ListInt.RemoveAt(1);
            role.Update(context);

            var bsonUpdate = context.Build();

            Assert.AreEqual(new BsonDocument {
                {
                    "$set",
                    new BsonDocument {
                        { "ListInt.3.PrevKey", 1 }
                    }
                },
                {
                    "$unset",
                    new BsonDocument {
                        { "ListInt.2", 1 }
                    }
                }
            }, bsonUpdate);

            collection.UpdateOne(new BsonDocument {
                { "_id", 1 }
            }, context.Build());

            var actual     = collection.Find(new BsonDocument()).First();
            var roleActual = (Role)actual;

            Assert.AreEqual(10, roleActual.ListInt[0]);
            Assert.AreEqual(12, roleActual.ListInt[1]);
        }
示例#10
0
        public void TestUpdateVsReplace()
        {
            var collection           = GetTestCollection <BsonDocument>();
            BulkWriteContext context = new BulkWriteContext();

            Dictionary <long, Role> Roles = new Dictionary <long, Role>();

            for (int i = 0; i < 100; i++)
            {
                var role = new Role {
                    RoleID = i, RoleName = i.ToString()
                };
                for (int itemIndex = 0; itemIndex < 50; itemIndex++)
                {
                    role.Items.Add(itemIndex, new Item {
                        ItemID = itemIndex, ItemUID = itemIndex
                    });
                }
                role.ClearDirty();
                Roles.Add(i, role);
                context.Insert((BsonDocument)role);
            }
            context.Execute(collection);
            context.Clear();

            Stopwatch watch = new Stopwatch();

            List <ReplaceOneModel <BsonDocument> > replaces = new List <ReplaceOneModel <BsonDocument> >();

            watch.Start();
            foreach (var pair in Roles)
            {
                replaces.Add(new ReplaceOneModel <BsonDocument>(new BsonDocument {
                    { "_id", pair.Key }
                }, (BsonDocument)pair.Value));
            }
            collection.BulkWrite(replaces);
            watch.Stop();
            var time1 = watch.ElapsedMilliseconds;

            watch.Reset();

            for (int i = 0; i < 100; i++)
            {
                Roles[i].RoleName += i;
                for (int itemIndex = 0; itemIndex < 10; itemIndex++)
                {
                    Roles[i].Items[itemIndex].ItemID += 1;
                }
                Roles[i].Items.Add(51, new Item {
                    ItemID = 51, ItemUID = 51
                });
            }

            UpdateContext update = new UpdateContext();

            watch.Start();
            foreach (var pair in Roles)
            {
                update.Clear();
                pair.Value.Update(update);
                context.Update(new BsonDocument {
                    { "_id", pair.Key }
                }, update.Build());
            }
            context.Execute(collection);
            context.Clear();
            watch.Stop();
            var time2 = watch.ElapsedMilliseconds;

            Assert.IsTrue(time1 > time2);
        }