コード例 #1
0
        public void mongo_add_and_update_test()
        {
            var customerRepo = new MongoTestDB <Customer>();

            var customer = CreateCustomer();

            customerRepo.Insert(customer);

            customer.Id.Should().NotBeNull();
            var alreadyAddedCustomer = customerRepo.GetBy(c => c.FirstName == "Bob").Single();

            alreadyAddedCustomer.Should().NotBeNull();
            alreadyAddedCustomer.FirstName.Should().Be(customer.FirstName);
            alreadyAddedCustomer.HomeAddress.Address1.Should().Be(customer.HomeAddress.Address1);
            alreadyAddedCustomer.CreateDate.Should().BeBefore(DateTime.Now);

            alreadyAddedCustomer.Phone = "10110111";
            alreadyAddedCustomer.Email = "*****@*****.**";

            customerRepo.Update(alreadyAddedCustomer);

            var updatedCustomer = customerRepo.GetByKey(customer.Id);

            updatedCustomer.Should().NotBeNull();
            updatedCustomer.Phone.Should().Be(alreadyAddedCustomer.Phone);
            updatedCustomer.Email.Should().Be(alreadyAddedCustomer.Email);
            var isExists = customerRepo.Exists(c => c.HomeAddress.Country == "Alaska");

            isExists.Should().Be(true);
        }
コード例 #2
0
        public void mongo_long_id_generator_test()
        {
            var log = new OrderLog
            {
                Summary   = "test",
                Title     = "aa",
                OrderDate = DateTime.Now
            };

            var repo = new MongoTestDB <OrderLog, long>();

            repo.Insert(log);

            var added = repo.GetBy(p => p.Title == "aa").FirstOrDefault();

            added.Should().NotBeNull();
            added.Id.Should().Be(1);
            added.Summary.Should().Be("test");
        }
コード例 #3
0
        public void mongo_string_id_test()
        {
            var pInfo = new Product
            {
                Description = "XXX",
                Name        = "aa",
                Price       = 100
            };

            var repo = new MongoTestDB <Product>();

            repo.Insert(pInfo);

            var added = repo.GetBy(p => p.Name == "aa").FirstOrDefault();

            added.Should().NotBeNull();
            added.Id.Should().NotBeEmpty();
            added.Price.Should().Be(100);
        }
コード例 #4
0
        public void mongo_complex_entity_test()
        {
            var customerRepo = new MongoTestDB <Customer>();
            var productRepo  = new MongoTestDB <Product>();

            var customer = new Customer();

            customer.FirstName   = "Erik";
            customer.LastName    = "Swaun";
            customer.Phone       = "123 99 8767";
            customer.Email       = "*****@*****.**";
            customer.HomeAddress = new Address
            {
                Address1 = "Main bulevard",
                Address2 = "1 west way",
                PostCode = "89560",
                City     = "Tempare",
                Country  = "Arizona"
            };

            var order = new Order();

            order.PurchaseDate = DateTime.Now.AddDays(-2);
            var orderItems = new List <OrderItem>();

            var shampoo = new Product()
            {
                Name = "Palmolive Shampoo", Price = 5
            };

            productRepo.Insert(shampoo);
            var paste = new Product()
            {
                Name = "Mcleans Paste", Price = 4
            };

            productRepo.Insert(paste);


            var item1 = new OrderItem {
                Product = shampoo, Quantity = 1
            };
            var item2 = new OrderItem {
                Product = paste, Quantity = 2
            };

            orderItems.Add(item1);
            orderItems.Add(item2);

            order.Items = orderItems;

            customer.Orders = new List <Order>
            {
                order
            };

            customerRepo.Insert(customer);
            customer.Id.Should().NotBeNull();
            customer.Orders[0].Items[0].Product.Id.Should().NotBeNull();

            var theOrders     = customerRepo.GetBy(c => c.Id == customer.Id).Select(c => c.Orders).ToList();
            var theOrderItems = theOrders[0].Select(o => o.Items);

            theOrders.Should().NotBeNull();
            theOrderItems.Should().NotBeNull();
        }
コード例 #5
0
        public void mongo_batch_test()
        {
            var customerRepo = new MongoTestDB <Customer>();

            var custlist = new List <Customer>(new Customer[] {
                new Customer()
                {
                    FirstName = "Customer A"
                },
                new Customer()
                {
                    FirstName = "Client B"
                },
                new Customer()
                {
                    FirstName = "Customer C"
                },
                new Customer()
                {
                    FirstName = "Client D"
                },
                new Customer()
                {
                    FirstName = "Customer E"
                },
                new Customer()
                {
                    FirstName = "Client F"
                },
                new Customer()
                {
                    FirstName = "Customer G"
                },
            });

            //Insert batch
            customerRepo.Insert(custlist);
            var count = customerRepo.GetAll().Count();

            count.Should().Be(7);

            foreach (Customer c in custlist)
            {
                c.Id.Should().NotBe(new string('0', 24));
            }

            //Update batch
            foreach (Customer c in custlist)
            {
                c.LastName = c.FirstName;
            }
            customerRepo.Update(custlist);

            var updateList = customerRepo.GetAll();

            foreach (Customer c in updateList)
            {
                c.LastName.Should().Be(c.FirstName);
            }

            //Delete by criteria
            customerRepo.Delete(f => f.FirstName.StartsWith("Client"));

            count = customerRepo.GetAll().Count();
            count.Should().Be(4);

            //Delete specific object
            customerRepo.Delete(custlist[0]);

            //Test AsQueryable
            var selectedcustomers = customerRepo.GetBy(
                cust => cust.LastName.EndsWith("C") || cust.LastName.EndsWith("G"));

            selectedcustomers.ToList().Count.Should().Be(2);

            count = customerRepo.GetAll().Count();
            count.Should().Be(3);
        }