public void MongoRepository_Collection_Name_Attribute()
        {
            const string connectionString = "mongodb://127.0.0.1/test";

            if (!MongoDbRepositoryManager.ServerIsRunning(connectionString))
            {
                AssertIgnores.MongoServerIsNotRunning();
            }

            var cli = new MongoClient(connectionString);

            cli.DropDatabase("test");

            var repo = new MongoDbRepository <Order, string>(connectionString);

            // Create
            var create = new Order {
                Name = "Big sale"
            };

            repo.Add(create);

            var repoExt = new MongoDbRepository <OrderExtension, string>(connectionString);

            // Create
            var create2 = new OrderExtension {
                Name = "Big sale"
            };

            repoExt.Add(create2);

            Assert.IsTrue(cli.GetDatabase("test").ListCollections().ToList().Any(x => x["name"] == "Order"));
            Assert.IsTrue(cli.GetDatabase("test").ListCollections().ToList().Any(x => x["name"] == "OrderCustomName"));
            Assert.IsFalse(cli.GetDatabase("test").ListCollections().ToList().Any(x => x["name"] == "OrderExtended"));
        }
        public void MongoRepository_Generate_Query_With_AllowDiskSave()
        {
            const string connectionString = "mongodb://127.0.0.1/test";

            if (!MongoDbRepositoryManager.ServerIsRunning(connectionString))
            {
                AssertIgnores.MongoServerIsNotRunning();
            }

            var repo = new MongoDbRepository <Order, string>(connectionString);

            // Create
            var create = new Order {
                Name = "Big sale"
            };

            repo.Add(create);

            var fetchStrategy = new MongoDbFetchStrategy <Order>
            {
                AllowDiskUse = true
            };

            // Read
            var read = repo.Get(create.OrderId, fetchStrategy);

            read.Name.ShouldBe(create.Name);

            var all  = repo.GetAll(fetchStrategy);
            var all2 = repo.AsQueryable(fetchStrategy);
        }
示例#3
0
        public void MongoServer_Is_Running()
        {
            string connectionString = MongoDbConnectionStringFactory.Build("Test");

            if (MongoDbRepositoryManager.ServerIsRunning(connectionString))
            {
                MongoDbRepositoryManager.DropDatabase(connectionString);
                Assert.Pass("MongoServer is running");
            }
            else
            {
                AssertIgnores.MongoServerIsNotRunning();
            }
        }
        public void MongoRepository_Supports_Basic_Crud_Operations()
        {
            const string connectionString = "mongodb://127.0.0.1/test";

            if (!MongoDbRepositoryManager.ServerIsRunning(connectionString))
            {
                AssertIgnores.MongoServerIsNotRunning();
            }

            var repo = new MongoDbRepository <Order, string>(connectionString);

            // Create
            var create = new Order {
                Name = "Big sale"
            };

            repo.Add(create);

            // Read
            var read = repo.Get(create.OrderId);

            read.Name.ShouldBe(create.Name);

            // Update
            read.Name = "Really big sale";
            repo.Update(read);

            var all = repo.GetAll();

            var update = repo.Get(read.OrderId);

            update.OrderId.ShouldBe(read.OrderId);
            update.Name.ShouldBe(read.Name);

            // Delete
            repo.Delete(update);
            var delete = repo.Get(read.OrderId);

            delete.ShouldBeNull();
        }
        public void MongoDb_Supports_Basic_Crud_Operations()
        {
            const string connectionString = "mongodb://localhost/Order";

            if (!MongoDbRepositoryManager.ServerIsRunning(connectionString))
            {
                AssertIgnores.MongoServerIsNotRunning();
            }

            var cli           = new MongoClient(connectionString);
            var databaseNames = cli.ListDatabases().ToList();

            foreach (var db in databaseNames)
            {
                if (db["name"].AsString != "admin")
                {
                    cli.DropDatabase(db["name"].AsString);
                }
            }

            var database = cli.GetDatabase("Order");
            var orders   = database.GetCollection <Order>("Order");

            Console.WriteLine("* CREATE *");

            var create = new Order {
                Name = "Big sale"
            };

            database.GetCollection <Order>("Order").InsertOne(create);

            foreach (var order in database.GetCollection <Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            Console.WriteLine("* READ *");

            var filter = Builders <Order> .Filter.Eq(o => o.OrderId, create.OrderId);

            var read = orders.Find(filter).ToList().FirstOrDefault();

            read.Name.ShouldBe(create.Name);

            Console.WriteLine("* UPDATE *");

            var update = Builders <Order> .Update.Set(o => o.Name, "Really big sale");

            orders.UpdateOne(filter, update);

            foreach (var order in database.GetCollection <Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            var read_updated = orders.Find(filter).ToList().FirstOrDefault();

            read_updated.OrderId.ShouldBe(read.OrderId);
            read_updated.Name.ShouldBe("Really big sale");

            Console.WriteLine("* DELETE *");

            orders.DeleteOne(filter);

            foreach (var order in database.GetCollection <Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            orders.Count(filter).ShouldBe(0);

            Console.WriteLine("* DELETE ALL *");
            orders.DeleteMany(new BsonDocument());

            foreach (var order in database.GetCollection <Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            orders.AsQueryable().Count().ShouldBe(0);

            cli.DropDatabase("Order");
        }
        public void MongoDb_Supports_Basic_Crud_Operations()
        {
            const string connectionString = "mongodb://localhost";

            if (!MongoDbRepositoryManager.ServerIsRunning(connectionString))
            {
                AssertIgnores.MongoServerIsNotRunning();
            }

            var server        = new MongoClient(connectionString).GetServer();
            var databaseNames = server.GetDatabaseNames();

            foreach (var db in databaseNames)
            {
                server.DropDatabase(db);
            }

            var database = server.GetDatabase("Order");
            var orders   = database.GetCollection <Order>("Order");

            Console.WriteLine("* CREATE *");

            var create = new Order {
                Name = "Big sale"
            };

            database.GetCollection <Order>("Order").Insert(create);

            foreach (var order in database.GetCollection <Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            Console.WriteLine("* READ *");

            var read = orders.AsQueryable().FirstOrDefault(e => e.OrderId == create.OrderId);

            read.Name.ShouldEqual(create.Name);

            Console.WriteLine("* UPDATE *");

            read.Name = "Really big sale";
            database.GetCollection <Order>("Order").Save(read);

            foreach (var order in database.GetCollection <Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            var update = database.GetCollection <Order>("Order").AsQueryable().FirstOrDefault(e => e.OrderId == read.OrderId);

            update.OrderId.ShouldEqual(read.OrderId);
            update.Name.ShouldEqual(read.Name);

            Console.WriteLine("* DELETE *");

            var delete = database.GetCollection <Order>("Order").AsQueryable().FirstOrDefault(e => e.OrderId == update.OrderId);

            database.GetCollection <Order>("Order").Remove(Query.EQ("OrderId", delete.OrderId));

            foreach (var order in database.GetCollection <Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            database.GetCollection <Order>("Order").RemoveAll();

            Console.WriteLine("* DELETE ALL *");

            foreach (var order in database.GetCollection <Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            server.DropDatabase("Order");
        }