Beispiel #1
0
        public void AutoId_Test()
        {
            using (var db = new LiteDatabase(new MemoryStream()))
            {
                var cs_int = db.GetCollection<EntityInt>("int");
                var cs_guid = db.GetCollection<EntityGuid>("guid");
                var cs_oid = db.GetCollection<EntityOid>("oid");
                var cs_str = db.GetCollection<EntityString>("str");

                // int32
                var cint_1 = new EntityInt { Name = "Using Int 1" };
                var cint_2 = new EntityInt { Name = "Using Int 2" };
                var cint_5 = new EntityInt { Id = 5, Name = "Using Int 5" }; // set Id, do not generate (jump 3 and 4)!
                var cint_6 = new EntityInt { Id = 0, Name = "Using Int 6" }; // for int, 0 is empty

                // guid
                var guid = Guid.NewGuid();

                var cguid_1 = new EntityGuid { Id = guid, Name = "Using Guid" };
                var cguid_2 = new EntityGuid { Name = "Using Guid" };

                // oid
                var oid = ObjectId.NewObjectId();

                var coid_1 = new EntityOid { Name = "ObjectId-1" };
                var coid_2 = new EntityOid { Id = oid, Name = "ObjectId-2" };

                // string - there is no AutoId for string
                var cstr_1 = new EntityString { Name = "Object using String" };

                cs_int.Insert(cint_1);
                cs_int.Insert(cint_2);
                cs_int.Insert(cint_5);
                cs_int.Insert(cint_6);

                cs_guid.Insert(cguid_1);
                cs_guid.Insert(cguid_2);

                cs_oid.Insert(coid_1);
                cs_oid.Insert(coid_2);

                try
                {
                    cs_str.Insert(cstr_1);
                    Assert.Fail();
                }
                catch (LiteException)
                {
                    // must fail because EntityString class has a defined Id
                    //   but has no value and no auto_id funtion - issue #43
                }

                // test for int
                Assert.AreEqual(cint_1.Id, 1);
                Assert.AreEqual(cint_2.Id, 2);
                Assert.AreEqual(cint_5.Id, 5);
                Assert.AreEqual(cint_6.Id, 6);

                // test for guid
                Assert.AreEqual(cguid_1.Id, guid);
                Assert.AreNotEqual(cguid_2.Id, Guid.Empty);
                Assert.AreNotEqual(cguid_1.Id, cguid_2.Id);

                // test for oid
                Assert.AreNotEqual(coid_1, ObjectId.Empty);
                Assert.AreEqual(coid_2.Id, oid);
            }
        }
Beispiel #2
0
        public void AutoId_Test()
        {
            using (var db = new LiteDatabase(new MemoryStream()))
            {
                var cs_int  = db.GetCollection <EntityInt>("int");
                var cs_guid = db.GetCollection <EntityGuid>("guid");
                var cs_oid  = db.GetCollection <EntityOid>("oid");
                var cs_str  = db.GetCollection <EntityString>("str");

                // int32
                var cint_1 = new EntityInt {
                    Name = "Using Int 1"
                };
                var cint_2 = new EntityInt {
                    Name = "Using Int 2"
                };
                var cint_5 = new EntityInt {
                    Id = 5, Name = "Using Int 5"
                };                                                           // set Id, do not generate (jump 3 and 4)!
                var cint_6 = new EntityInt {
                    Id = 0, Name = "Using Int 6"
                };                                                           // for int, 0 is empty

                // guid
                var guid = Guid.NewGuid();

                var cguid_1 = new EntityGuid {
                    Id = guid, Name = "Using Guid"
                };
                var cguid_2 = new EntityGuid {
                    Name = "Using Guid"
                };

                // oid
                var oid = ObjectId.NewObjectId();

                var coid_1 = new EntityOid {
                    Name = "ObjectId-1"
                };
                var coid_2 = new EntityOid {
                    Id = oid, Name = "ObjectId-2"
                };

                // string - there is no AutoId for string
                var cstr_1 = new EntityString {
                    Name = "Object using String"
                };

                cs_int.Insert(cint_1);
                cs_int.Insert(cint_2);
                cs_int.Insert(cint_5);
                cs_int.Insert(cint_6);

                cs_guid.Insert(cguid_1);
                cs_guid.Insert(cguid_2);

                cs_oid.Insert(coid_1);
                cs_oid.Insert(coid_2);

                try
                {
                    cs_str.Insert(cstr_1);
                    Assert.Fail();
                }
                catch (LiteException)
                {
                    // must fail because EntityString class has a defined Id
                    //   but has no value and no auto_id funtion - issue #43
                }

                // test for int
                Assert.AreEqual(cint_1.Id, 1);
                Assert.AreEqual(cint_2.Id, 2);
                Assert.AreEqual(cint_5.Id, 5);
                Assert.AreEqual(cint_6.Id, 6);

                // test for guid
                Assert.AreEqual(cguid_1.Id, guid);
                Assert.AreNotEqual(cguid_2.Id, Guid.Empty);
                Assert.AreNotEqual(cguid_1.Id, cguid_2.Id);

                // test for oid
                Assert.AreNotEqual(coid_1, ObjectId.Empty);
                Assert.AreEqual(coid_2.Id, oid);
            }
        }
Beispiel #3
0
        public void AutoId_Test()
        {
            var mapper = new BsonMapper();

            mapper.RegisterAutoId(
                (s) => s == null,
                (c) => "doc-" + c.Count()
                );

            using (var file = new TempFile())
                using (var db = new LiteDatabase(file.Filename, mapper))
                {
                    var cs_int  = db.GetCollection <EntityInt>("int");
                    var cs_guid = db.GetCollection <EntityGuid>("guid");
                    var cs_oid  = db.GetCollection <EntityOid>("oid");
                    var cs_str  = db.GetCollection <EntityString>("str");

                    // int32
                    var cint_1 = new EntityInt {
                    };
                    var cint_2 = new EntityInt {
                    };
                    var cint_5 = new EntityInt {
                        Id = 5
                    };                                 // set Id, do not generate (jump 3 and 4)!
                    var cint_6 = new EntityInt {
                        Id = 0
                    };                                 // for int, 0 is empty

                    // guid
                    var guid = Guid.NewGuid();

                    var cguid_1 = new EntityGuid {
                        Id = guid
                    };
                    var cguid_2 = new EntityGuid {
                    };

                    // oid
                    var oid = ObjectId.NewObjectId();

                    var coid_1 = new EntityOid {
                    };
                    var coid_2 = new EntityOid {
                        Id = oid
                    };

                    // string - there is no AutoId for string
                    var cstr_1 = new EntityString {
                    };
                    var cstr_2 = new EntityString {
                        Id = "mydoc2"
                    };

                    cs_int.Insert(cint_1);
                    cs_int.Insert(cint_2);
                    cs_int.Insert(cint_5);
                    cs_int.Insert(cint_6);

                    cs_guid.Insert(cguid_1);
                    cs_guid.Insert(cguid_2);

                    cs_oid.Insert(coid_1);
                    cs_oid.Insert(coid_2);

                    cs_str.Insert(cstr_1);

                    // test for int
                    Assert.AreEqual(1, cint_1.Id);
                    Assert.AreEqual(2, cint_2.Id);
                    Assert.AreEqual(5, cint_5.Id);
                    Assert.AreEqual(6, cint_6.Id);

                    // test for guid
                    Assert.AreEqual(guid, cguid_1.Id);
                    Assert.AreNotEqual(Guid.Empty, cguid_2.Id);
                    Assert.AreNotEqual(cguid_2.Id, cguid_1.Id);

                    // test for oid
                    Assert.AreNotEqual(ObjectId.Empty, coid_1.Id);
                    Assert.AreEqual(oid, coid_2.Id);

                    // test for string
                    Assert.AreEqual("doc-0", cstr_1.Id);
                    Assert.AreEqual("mydoc2", cstr_2.Id);
                }
        }
Beispiel #4
0
        public void AutoId_Test()
        {
            var mapper = new BsonMapper();

            mapper.RegisterAutoId(
                (s) => s == null,
                (db, col) => "doc-" + db.Count(col, null)
                );

            // register for "long" - use an collection to manage
            mapper.RegisterAutoId <long>(
                v => v == 0,
                (db, col) =>
            {
                var seq = "_sequence";
                db.BeginTrans();
                var next = 0L;
                try
                {
                    var current = db.Find(seq, Query.EQ("_id", col)).FirstOrDefault();

                    if (current == null)
                    {
                        db.Insert(seq, new BsonDocument {
                            { "_id", col }, { "value", 1L }
                        });
                        next = 1;
                    }
                    else
                    {
                        next = current["value"] = current["value"].AsInt64 + 1;
                        db.Update(seq, current);
                    }

                    db.Commit();
                }
                catch (Exception)
                {
                    db.Rollback();
                    throw;
                }

                return(next);
            }
                );

            using (var file = new TempFile())
                using (var db = new LiteDatabase(file.Filename, mapper))
                {
                    var cs_int  = db.GetCollection <EntityInt>("int");
                    var cs_long = db.GetCollection <EntityLong>("long");
                    var cs_guid = db.GetCollection <EntityGuid>("guid");
                    var cs_oid  = db.GetCollection <EntityOid>("oid");
                    var cs_str  = db.GetCollection <EntityString>("str");

                    // int32
                    var cint_1 = new EntityInt {
                    };
                    var cint_2 = new EntityInt {
                    };
                    var cint_5 = new EntityInt {
                        Id = 5
                    };                                 // set Id, do not generate (jump 3 and 4)!
                    var cint_6 = new EntityInt {
                        Id = 0
                    };                                 // for int, 0 is empty

                    // long
                    var clong_1 = new EntityLong {
                    };
                    var clong_2 = new EntityLong {
                    };
                    var clong_3 = new EntityLong {
                    };
                    var clong_4 = new EntityLong {
                    };

                    // guid
                    var guid = Guid.NewGuid();

                    var cguid_1 = new EntityGuid {
                        Id = guid
                    };
                    var cguid_2 = new EntityGuid {
                    };

                    // oid
                    var oid = ObjectId.NewObjectId();

                    var coid_1 = new EntityOid {
                    };
                    var coid_2 = new EntityOid {
                        Id = oid
                    };

                    // string - there is no AutoId for string
                    var cstr_1 = new EntityString {
                    };
                    var cstr_2 = new EntityString {
                        Id = "mydoc2"
                    };

                    cs_int.Insert(cint_1);
                    cs_int.Insert(cint_2);
                    cs_int.Insert(cint_5);
                    cs_int.Insert(cint_6);

                    cs_long.Insert(clong_1);
                    cs_long.Insert(clong_2);
                    // delete 1 and 2 and will not re-used
                    cs_long.Delete(Query.In("_id", 1, 2));
                    cs_long.Insert(clong_3);
                    cs_long.Insert(clong_4);

                    cs_guid.Insert(cguid_1);
                    cs_guid.Insert(cguid_2);

                    cs_oid.Insert(coid_1);
                    cs_oid.Insert(coid_2);

                    cs_str.Insert(cstr_1);

                    // test for int
                    Assert.AreEqual(1, cint_1.Id);
                    Assert.AreEqual(2, cint_2.Id);
                    Assert.AreEqual(5, cint_5.Id);
                    Assert.AreEqual(6, cint_6.Id);

                    // test for long
                    Assert.AreEqual(3, clong_3.Id);
                    Assert.AreEqual(4, clong_4.Id);

                    // test for guid
                    Assert.AreEqual(guid, cguid_1.Id);
                    Assert.AreNotEqual(Guid.Empty, cguid_2.Id);
                    Assert.AreNotEqual(cguid_2.Id, cguid_1.Id);

                    // test for oid
                    Assert.AreNotEqual(ObjectId.Empty, coid_1.Id);
                    Assert.AreEqual(oid, coid_2.Id);

                    // test for string
                    Assert.AreEqual("doc-0", cstr_1.Id);
                    Assert.AreEqual("mydoc2", cstr_2.Id);
                }
        }
Beispiel #5
0
        public void AutoId_Test()
        {
            string test_name = "AutoId_Test";

            var mapper = new BsonMapper();

            mapper.RegisterAutoId(
                (s) => s == null,
                (c) => "doc-" + c.Count()
                );

            using (var db = new LiteDatabase(new MemoryStream(), mapper))
            {
                var cs_int  = db.GetCollection <EntityInt>("int");
                var cs_guid = db.GetCollection <EntityGuid>("guid");
                var cs_oid  = db.GetCollection <EntityOid>("oid");
                var cs_str  = db.GetCollection <EntityString>("str");

                // int32
                var cint_1 = new EntityInt {
                };
                var cint_2 = new EntityInt {
                };
                var cint_5 = new EntityInt {
                    Id = 5
                };                                     // set Id, do not generate (jump 3 and 4)!
                var cint_6 = new EntityInt {
                    Id = 0
                };                                     // for int, 0 is empty

                // guid
                var guid = Guid.NewGuid();

                var cguid_1 = new EntityGuid {
                    Id = guid
                };
                var cguid_2 = new EntityGuid {
                };

                // oid
                var oid = ObjectId.NewObjectId();

                var coid_1 = new EntityOid {
                };
                var coid_2 = new EntityOid {
                    Id = oid
                };

                // string - there is no AutoId for string
                var cstr_1 = new EntityString {
                };
                var cstr_2 = new EntityString {
                    Id = "mydoc2"
                };

                cs_int.Insert(cint_1);
                cs_int.Insert(cint_2);
                cs_int.Insert(cint_5);
                cs_int.Insert(cint_6);

                cs_guid.Insert(cguid_1);
                cs_guid.Insert(cguid_2);

                cs_oid.Insert(coid_1);
                cs_oid.Insert(coid_2);

                cs_str.Insert(cstr_1);

                // test for int
                Helper.AssertIsTrue(test_name, 0, 1 == cint_1.Id);
                Helper.AssertIsTrue(test_name, 1, 2 == cint_2.Id);
                Helper.AssertIsTrue(test_name, 2, 5 == cint_5.Id);
                Helper.AssertIsTrue(test_name, 3, 6 == cint_6.Id);

                // test for guid
                Helper.AssertIsTrue(test_name, 4, guid == cguid_1.Id);
                Helper.AssertIsTrue(test_name, 5, Guid.Empty != cguid_2.Id);
                Helper.AssertIsTrue(test_name, 6, cguid_2.Id != cguid_1.Id);

                // test for oid
                Helper.AssertIsTrue(test_name, 7, ObjectId.Empty != coid_1.Id);
                Helper.AssertIsTrue(test_name, 8, oid == coid_2.Id);

                // test for string
                Helper.AssertIsTrue(test_name, 9, "doc-0" == cstr_1.Id);
                Helper.AssertIsTrue(test_name, 10, "mydoc2" == cstr_2.Id);
            }
        }
Beispiel #6
0
        public void AutoId_Test()
        {
            var mapper = new BsonMapper();

            mapper.RegisterAutoId(
                (s) => s == null,
                (c) => "doc-" + c.Count()
            );

            using (var file = new TempFile())
            using (var db = new LiteDatabase(file.Filename, mapper))
            {
                var cs_int = db.GetCollection<EntityInt>("int");
                var cs_guid = db.GetCollection<EntityGuid>("guid");
                var cs_oid = db.GetCollection<EntityOid>("oid");
                var cs_str = db.GetCollection<EntityString>("str");

                // int32
                var cint_1 = new EntityInt { };
                var cint_2 = new EntityInt { };
                var cint_5 = new EntityInt { Id = 5 }; // set Id, do not generate (jump 3 and 4)!
                var cint_6 = new EntityInt { Id = 0 }; // for int, 0 is empty

                // guid
                var guid = Guid.NewGuid();

                var cguid_1 = new EntityGuid { Id = guid };
                var cguid_2 = new EntityGuid { };

                // oid
                var oid = ObjectId.NewObjectId();

                var coid_1 = new EntityOid { };
                var coid_2 = new EntityOid { Id = oid };

                // string - there is no AutoId for string
                var cstr_1 = new EntityString { };
                var cstr_2 = new EntityString { Id = "mydoc2" };

                cs_int.Insert(cint_1);
                cs_int.Insert(cint_2);
                cs_int.Insert(cint_5);
                cs_int.Insert(cint_6);

                cs_guid.Insert(cguid_1);
                cs_guid.Insert(cguid_2);

                cs_oid.Insert(coid_1);
                cs_oid.Insert(coid_2);

                cs_str.Insert(cstr_1);

                // test for int
                Assert.AreEqual(1, cint_1.Id);
                Assert.AreEqual(2, cint_2.Id);
                Assert.AreEqual(5, cint_5.Id);
                Assert.AreEqual(6, cint_6.Id);

                // test for guid
                Assert.AreEqual(guid, cguid_1.Id);
                Assert.AreNotEqual(Guid.Empty, cguid_2.Id);
                Assert.AreNotEqual(cguid_2.Id, cguid_1.Id);

                // test for oid
                Assert.AreNotEqual(ObjectId.Empty, coid_1.Id);
                Assert.AreEqual(oid, coid_2.Id);

                // test for string
                Assert.AreEqual("doc-0", cstr_1.Id);
                Assert.AreEqual("mydoc2", cstr_2.Id);
            }
        }