Beispiel #1
0
        public void Loop_Test()
        {
            using (var tmp = new TempFile())
            {
                using (var db = new LiteEngine(tmp.Filename))
                {
                    db.Insert("col", new BsonDocument { { "Number", 1 } });
                    db.Insert("col", new BsonDocument { { "Number", 2 } });
                    db.Insert("col", new BsonDocument { { "Number", 3 } });
                    db.Insert("col", new BsonDocument { { "Number", 4 } });
                }

                using (var db = new LiteEngine(tmp.Filename))
                {
                    foreach (var doc in db.Find("col", Query.All()))
                    {
                        doc["Name"] = "John";
                        db.Update("col", doc);
                    }

                    db.EnsureIndex("col", "Name");
                    var all = db.Find("col", Query.EQ("Name", "John"));

                    Assert.AreEqual(4, all.Count());
                }
            }
        }
Beispiel #2
0
        public void Thread_InsertQuery_Test()
        {
            const int N = 3000;
            var running = true;

            using (var file = new TempFile())
            using (var db = new LiteEngine(file.Filename))
            {
                db.Insert("col", new BsonDocument());

                // insert basic document
                var ta = Task.Factory.StartNew(() =>
                {
                    for (var i = 0; i < N; i++)
                    {
                        var doc = new BsonDocument { { "_id", i } };

                        db.Insert("col", doc);
                    }
                    running = false;
                });

                // query while insert
                var tb = Task.Factory.StartNew(() =>
                {
                    while (running)
                    {
                        db.Find("col", Query.All()).ToList();
                    }
                });

                Task.WaitAll(ta, tb);

                Assert.AreEqual(N + 1, db.Count("col", Query.All()));
            }
        }
Beispiel #3
0
        public void ReadOnlyFirst_Test()
        {
            using (var file = new TempFile())
            {
                // just create file
                using (var c = new LiteEngine(file.Filename))
                {
                    c.Insert("col", new BsonDocument {
                        { "_id", 1 }
                    });
                }

                // here there is no open datafile

                // open as read-only
                using (var r = new LiteEngine(new FileDiskService(file.Filename, new FileOptions {
                    ReadOnly = true
                })))
                {
                    // just query
                    var d = r.Find("col", Query.EQ("_id", 1)).FirstOrDefault();
                    Assert.AreEqual(1, d["_id"].AsInt32);

                    // open database in read/write mode
                    Task.Factory.StartNew(() =>
                    {
                        using (var rw = new LiteEngine(file.Filename))
                        {
                            rw.Insert("col", new BsonDocument {
                                { "_id", 2 }
                            });
                        }
                    }).Wait();
                }
            }
        }
Beispiel #4
0
        public void ULongList_Test()
        {
            using (var file = new TempFile())
                using (var db = new LiteDatabase(file.Filename))
                {
                    var col = db.GetCollection <Gang>();

                    col.Insert(new Gang
                    {
                        GuildId  = 1,
                        LeaderId = 2,
                        Members  = new List <ulong> {
                            5, 6, 7
                        }
                    });

                    ulong userId  = 5;
                    ulong guildId = 1;

                    var e = col.Exists(x => x.GuildId == guildId && (x.LeaderId == userId));

                    //Assert.IsTrue(e);
                }
        }
Beispiel #5
0
        public void Engine_Insert_Test()
        {
            using (var file = new TempFile())
            {
                using (var db = new LiteEngine(file.Filename))
                {
                    db.Insert("col", new BsonDocument {
                        { "_id", 1 }, { "name", "John" }
                    });
                    db.Insert("col", new BsonDocument {
                        { "_id", 2 }, { "name", "Doe" }
                    });
                }

                using (var db = new LiteEngine(file.Filename))
                {
                    var john = db.Find("col", Query.EQ("_id", 1)).FirstOrDefault();
                    var doe  = db.Find("col", Query.EQ("_id", 2)).FirstOrDefault();

                    Assert.AreEqual("John", john["name"].AsString);
                    Assert.AreEqual("Doe", doe["name"].AsString);
                }
            }
        }
Beispiel #6
0
        public void ReadOnly_Test()
        {
            using (var file = new TempFile())
            {
                // open database with read/write
                using (var db = new LiteEngine(file.Filename))
                {
                    // here datafile are in Read/Write
                    db.Insert("col", new BsonDocument {
                        { "_id", 1 }
                    });
                }

                using (var r = new LiteEngine(new FileDiskService(file.Filename, new FileOptions {
                    FileMode = FileMode.ReadOnly
                })))
                {
                    var doc = r.Find("col", Query.EQ("_id", 1)).FirstOrDefault();

                    Assert.AreEqual(1, doc["_id"].AsInt32);

                    // do not support write operation
                    try
                    {
                        r.Insert("doc", new BsonDocument {
                            { "_id", 2 }
                        });

                        Assert.Fail("Do not accept write operation in readonly database");
                    }
                    catch (NotSupportedException)
                    {
                    }
                }
            }
        }
Beispiel #7
0
        public void Include_Test()
        {
            var mapper = new BsonMapper();

            mapper.Entity <Order>()
            .DbRef(x => x.Products, "products")
            .DbRef(x => x.ProductArray, "products")
            .DbRef(x => x.ProductColl, "products")
            .DbRef(x => x.ProductEmpty, "products")
            .DbRef(x => x.ProductsNull, "products")
            .DbRef(x => x.Customer, "customers")
            .DbRef(x => x.CustomerNull, "customers");

            mapper.Entity <Customer>()
            .DbRef(x => x.MainAddress, "addresses");

            mapper.Entity <Product>()
            .DbRef(x => x.SupplierAddress, "addresses");

            using (var file = new TempFile())
                using (var db = new LiteDatabase(file.Filename, mapper))
                {
                    var address = new Address {
                        StreetName = "3600 S Las Vegas Blvd"
                    };
                    var customer = new Customer {
                        Name = "John Doe", MainAddress = address
                    };

                    var product1 = new Product {
                        Name = "TV", Price = 800, SupplierAddress = address
                    };
                    var product2 = new Product {
                        Name = "DVD", Price = 200
                    };

                    var customers = db.GetCollection <Customer>("customers");
                    var addresses = db.GetCollection <Address>("addresses");
                    var products  = db.GetCollection <Product>("products");
                    var orders    = db.GetCollection <Order>("orders");

                    // insert ref documents
                    addresses.Insert(address);
                    customers.Insert(customer);
                    products.Insert(new Product[] { product1, product2 });

                    var order = new Order
                    {
                        Customer     = customer,
                        CustomerNull = null,
                        Products     = new List <Product>()
                        {
                            product1, product2
                        },
                        ProductArray = new Product[] { product1 },
                        ProductColl  = new List <Product>()
                        {
                            product2
                        },
                        ProductEmpty = new List <Product>(),
                        ProductsNull = null
                    };

                    orders.Insert(order);

                    var query = orders
                                .Include(x => x.Customer)
                                .Include(x => x.Customer.MainAddress)
                                .Include(x => x.CustomerNull)
                                .Include(x => x.Products)
                                .Include(x => x.ProductArray)
                                .Include(x => x.ProductColl)
                                .Include(x => x.ProductsNull)
                                .FindAll()
                                .FirstOrDefault();

                    Assert.AreEqual(customer.Name, query.Customer.Name);
                    Assert.AreEqual(customer.MainAddress.StreetName, query.Customer.MainAddress.StreetName);
                    Assert.AreEqual(product1.Price, query.Products[0].Price);
                    Assert.AreEqual(product2.Name, query.Products[1].Name);
                    Assert.AreEqual(product1.Name, query.ProductArray[0].Name);
                    Assert.AreEqual(product2.Price, query.ProductColl.ElementAt(0).Price);
                    Assert.AreEqual(null, query.ProductsNull);
                    Assert.AreEqual(0, query.ProductEmpty.Count);
                }
        }
Beispiel #8
0
        public void ReadOnly_Test()
        {
            using (var file = new TempFile())
            {
                // open database with read/write
                using (var db = new LiteEngine(file.Filename))
                {
                    // here datafile are in Read/Write
                    db.Insert("col", new BsonDocument {
                        { "_id", 1 }
                    });

                    // open datafile as readonly mode
                    Task.Factory.StartNew(() =>
                    {
                        using (var r = new LiteEngine(new FileDiskService(file.Filename, new FileOptions {
                            ReadOnly = true
                        })))
                        {
                            var doc = r.Find("col", Query.EQ("_id", 1)).FirstOrDefault();

                            Assert.AreEqual(1, doc["_id"].AsInt32);

                            // do not support write operation
                            try
                            {
                                r.Insert("doc", new BsonDocument {
                                    { "_id", 2 }
                                });

                                Assert.Fail("Do not accept write operation in readonly database");
                            }
                            catch (LiteException ex)
                            {
                                if (ex.ErrorCode != LiteException.READ_ONLY_DATABASE)
                                {
                                    Assert.Fail("Wrong exception");
                                }
                            }
                        }
                    }).Wait();

                    // try open second datafile in write mode (must throw exception)
                    Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            // try open datafile as read/write
                            using (var rw = new LiteEngine(new FileDiskService(file.Filename, new FileOptions {
                                Timeout = TimeSpan.FromSeconds(3)
                            })))
                            {
                                Assert.Fail("Do not open this datafile");
                            }
                        }
                        catch (LiteException ex)
                        {
                            if (ex.ErrorCode != LiteException.LOCK_TIMEOUT)
                            {
                                Assert.Fail("Wrong exception");
                            }
                        }
                    }).Wait();
                }
            }
        }
Beispiel #9
0
        public void ShrinkTest_Test()
        {
            // do some tests
            Action <LiteEngine> DoTest = (db) =>
            {
                Assert.AreEqual(1, db.Count("col", null));
                Assert.AreEqual(99, db.UserVersion);
                Assert.IsNotNull(db.GetIndexes("col").FirstOrDefault(x => x.Field == "name"));
                Assert.IsTrue(db.GetIndexes("col").FirstOrDefault(x => x.Field == "name").Unique);
            };

            using (var file = new TempFile())
            {
                using (var db = new LiteEngine(file.Filename))
                {
                    db.UserVersion = 99;
                    db.EnsureIndex("col", "name", true);
                    db.Insert("col", GetDocs(1, 40000));
                    db.Delete("col", Query.GT("_id", 1)); // delete 29.999 docs

                    Assert.AreEqual(1, db.Count("col", null));

                    // file still large than 20mb (even with only 1 document)
                    Assert.IsTrue(file.Size > 20 * 1024 * 1024);

                    // reduce datafile
                    db.Shrink();

                    // now file are small than 50kb
                    Assert.IsTrue(file.Size < 50 * 1024);

                    DoTest(db);
                }

                // re-open datafile to check if is ok
                using (var db = new LiteEngine(file.Filename))
                {
                    // still 1 doc and 1 name unique index
                    DoTest(db);

                    // shrink again but now with password
                    var reduced = db.Shrink("abc123");

                    // file still same size (but now are encrypted)
                    Assert.AreEqual(0, reduced);

                    // still 1 doc and 1 name unique index
                    DoTest(db);
                }

                // re-open, again, but now with password
                using (var db = new LiteEngine(file.Filename, "abc123"))
                {
                    DoTest(db);

                    // now, remove password
                    db.Shrink();

                    // test again
                    DoTest(db);
                }
            }
        }
Beispiel #10
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);
                }
        }
        public void Linq_Test()
        {
            using (var file = new TempFile())
                using (var db = new LiteDatabase(file.Filename))
                {
                    var c1 = new User {
                        Id = 1, Name = "Mauricio", Active = true, Domain = new UserDomain {
                            DomainName = "Numeria"
                        }, OS = PlatformID.Xbox
                    };
                    var c2 = new User {
                        Id = 2, Name = "Malatruco", Active = false, Domain = new UserDomain {
                            DomainName = "Numeria"
                        }, OS = PlatformID.Win32NT
                    };
                    var c3 = new User {
                        Id = 3, Name = "Chris", Domain = new UserDomain {
                            DomainName = "Numeria"
                        }, OS = PlatformID.Win32NT
                    };
                    var c4 = new User {
                        Id = 4, Name = "Juliane", OS = PlatformID.Win32NT
                    };

                    var col = db.GetCollection <User>("Customer");

                    col.EnsureIndex(x => x.Name, true);
                    col.EnsureIndex(x => x.OS, false);
                    col.EnsureIndex(x => x.Domains.Select(z => z.DomainName), false);
                    col.EnsureIndex(x => x.Domains[0].Age, false);

                    var idx = col.GetIndexes().Select(x => x.Field).ToArray();

                    Assert.AreEqual("Name", idx[1]);
                    Assert.AreEqual("OS", idx[2]);
                    Assert.AreEqual("Domains.DomainName", idx[3]);
                    Assert.AreEqual("Domains.Age", idx[4]);

                    col.Insert(new User[] { c1, c2, c3, c4 });

                    // a simple lambda function to returns string "Numeria"
                    Func <string> GetNumeria = () => "Numeria";
                    var           strNumeria = GetNumeria();


                    // == !=
                    Assert.AreEqual(1, col.Count(x => x.Id == 1));
                    Assert.AreEqual(3, col.Count(x => x.Id != 1));

                    // member booleans
                    Assert.AreEqual(3, col.Count(x => !x.Active));
                    Assert.AreEqual(1, col.Count(x => x.Active));

                    // > >= < <=
                    Assert.AreEqual(1, col.Count(x => x.Id > 3));
                    Assert.AreEqual(1, col.Count(x => x.Id >= 4));
                    Assert.AreEqual(1, col.Count(x => x.Id < 2));
                    Assert.AreEqual(1, col.Count(x => x.Id <= 1));

                    // sub-class
                    Assert.AreEqual(3, col.Count(x => x.Domain.DomainName == "Numeria"));
                    Assert.AreEqual(3, col.Count(x => x.Domain.DomainName == GetNumeria()));
                    Assert.AreEqual(3, col.Count(x => x.Domain.DomainName == strNumeria));

                    // methods
                    Assert.AreEqual(1, col.Count(x => x.Name.StartsWith("Mal")));
                    Assert.AreEqual(1, col.Count(x => x.Name.Equals("Mauricio")));
                    Assert.AreEqual(1, col.Count(x => x.Name.Contains("cio")));

                    // enum
                    Assert.AreEqual(1, col.Count(x => x.OS == PlatformID.Xbox));
                    Assert.AreEqual(1, col.Count(x => x.OS == (PlatformID)5)); // Xbox
                    Assert.AreEqual(1, col.Count(x => x.OS == (PlatformID)Enum.Parse(typeof(PlatformID), "Xbox")));
                    Assert.AreEqual(3, col.Count(x => x.OS == PlatformID.Win32NT));

                    // doesnt works... must be a better linq provider
                    //var Platforms = new PlatformID[] { PlatformID.Xbox, PlatformID.Win32NT };
                    //Assert.AreEqual(4, col.Count(x => Platforms.Contains(x.OS)));


                    // and/or
                    Assert.AreEqual(1, col.Count(x => x.Id > 0 && x.Name == "Mauricio"));
                    Assert.AreEqual(2, col.Count(x => x.Name == "Malatruco" || x.Name == "Mauricio"));
                }
        }