Exemplo n.º 1
0
 public void SaveCategoryB()
 {
     using (var repo = RepositoryContext.GetRepository())
     {
         var b = new Category() { Name = "SaveB" };
         repo.Add(b);
         repo.SaveChanges();
     }
 }
Exemplo n.º 2
0
        public void SaveCategory()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var a = new Category() { Name = "SaveA" };
                repo.Add(a);

                SaveCategoryB();

                repo.Add(new Category() { Name = "SaveC" });
                repo.SaveChanges();
            }
        }
Exemplo n.º 3
0
 public void LinqDistinctTest()
 {
     using (var repo = RepositoryContext.GetRepository())
     {
         var category = new Category() {Name = "LinqDistinctTest", Description = "Description"};
         var category2 = new Category() {Name = "LinqDistinctTest", Description = "Description"};
         repo.Add(category);
         repo.Add(category2);
         repo.SaveChanges();
         var result = (from item in repo.Query<Category>()
                       select item.Name).Distinct().ToList();
         Assert.IsNotNull(result);
         Assert.IsTrue(result.Count == 1);
     }
 }
Exemplo n.º 4
0
        public void LinqJoinTest()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var category = new Category() {Name = "LinqGroupTest", Description = "LinqGroupTest_Description"};
                repo.Add(category);
                repo.Add(new Product() {Name = "LinqGroupTest", Category = category});
                repo.SaveChanges();
                var result = (from ca in repo.Query<Category>()
                              join pr in repo.Query<Product>() on ca.ID equals pr.Category.ID into os
                              select new {ca, os}).ToList();

                Assert.IsNotNull(result);
                Assert.IsTrue(result.Count > 0);
                Assert.IsTrue(result[0].ca.Name == category.Name);
            }
        }
Exemplo n.º 5
0
 public void Status_Test()
 {
     var repo = RepositoryContext.GetRepository();
      Category category = new Category();
      repo.Add(category);
      Assert.AreEqual(category.SysState, EntityStateEnum.Inserting);
      repo.Update(category);
      Assert.AreEqual(category.SysState, EntityStateEnum.Inserting);
      repo.Remove(category);
      Assert.AreEqual(category.SysState, EntityStateEnum.Deleting);
 }
Exemplo n.º 6
0
        public void LinqLimitationTest()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var category = new Category() {Name = "LinqLimitationTest", Description = "Description"};
                var category2 = new Category() {Name = "LinqLimitationTest2", Description = "Description"};
                repo.Add(category);
                repo.Add(category2);
                repo.SaveChanges();

                Assert.IsTrue(repo.Query<Category>().Any(p => p.Name == category.Name));
                Assert.IsTrue(repo.Query<Category>().Any(p => p.Name == category2.Name));
                Assert.IsNotNull(repo.Query<Category>().Any(p => p.Name == "NotExist"));
            }
        }
Exemplo n.º 7
0
        public void Projection_Test()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var date = DateTime.Now.AddHours(-1);
                var category = new Category()
                                   {
                                       Name = "Projection_Test",
                                       Description = "Projection_Test"
                                   };
                repo.Add(category);
                repo.SaveChanges();
                var result = (from item in repo.Query<Category>()
                              select new {item.Name, item.Size, item.ID}).ToList();

                Assert.IsNotNull(result);
                Assert.IsTrue(result.Count == 1);
                Assert.IsTrue(result[0].ID == category.ID);
            }
        }
Exemplo n.º 8
0
        public void Linq_Sum_Test()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var date = DateTime.Now.AddHours(-1);
                var category = new Category()
                                   {
                                       Name = "Linq_Sum_Test",
                                       Description = "Linq_Sum_Test_Description",
                                       Size = 9

                                   };
                var category2 = new Category()
                                    {
                                        Name = "Linq_Sum_Test",
                                        Description = "Linq_Sum_Test_Description",
                                        Size = 1
                                    };

                repo.Add(category);
                repo.Add(category2);
                repo.SaveChanges();

                var sum = repo.Query<Category>().Sum(P => P.Size);
                var count = repo.Query<Category>().Count();
                var min = repo.Query<Category>().Min(P => P.Size);
                var max = repo.Query<Category>().Max(P => P.Size);
                Assert.IsTrue(sum == (category.Size + category2.Size));
                Assert.IsTrue(count == 2);
                Assert.IsTrue(max == category.Size);
                Assert.IsTrue(min == category2.Size);

                var result = from item in repo.Query<Category>()
                             group item by item.Name
                             into ss
                             select new {A = ss.Key, TotalPrice = ss.Sum(P => P.Size)}.TotalPrice;
                Assert.IsTrue(sum == (category.Size + category2.Size));
            }
        }
Exemplo n.º 9
0
        public void Pagination_Test()
        {
            using (var repo = RepositoryContext.GetRepository())
            {

                var date = DateTime.Now.AddHours(-1);
                var category1 = new Category()
                                    {
                                        Name = "Pagination_Test1",
                                        Description = "Pagination_Test",
                                        Size = 1
                                    };

                var category2 = new Category()
                                    {
                                        Name = "Pagination_Test2",
                                        Description = "Pagination_Test",
                                        Size = 2
                                    };
                var category3 = new Category()
                                    {
                                        Name = "Pagination_Test3",
                                        Description = "Pagination_Test",
                                        Size = 3
                                    };
                repo.Add(category1);
                repo.Add(category2);
                repo.Add(category3);
                repo.SaveChanges();
                var result = (from item in repo.Query<Category>()
                              orderby item.Size
                              select item).Take(2).Skip(1).ToList();

                Assert.IsNotNull(result);
                Assert.IsTrue(result.Count == 2);
                Assert.IsTrue(result[0].Name == category2.Name);
                Assert.IsTrue(result[1].Name == category3.Name);
            }
        }
Exemplo n.º 10
0
        public void Linq_MaxMin_Test()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var date = DateTime.Now.AddHours(-1);
                var idString = "Linq_MaxMin_Test" + DateTime.Now.ToString("yymmdd:hhMMss");
                var category1 = new Category()
                                    {
                                        Name = "Linq_MaxMin_Test",
                                        Description = idString,
                                        Size = 2
                                    };

                var category2 = new Category()
                                    {
                                        Name = "Linq_MaxMin_Test",
                                        Description = idString,
                                        Size = 2
                                    };
                var category3 = new Category()
                                    {
                                        Name = "Linq_MaxMin_Test",
                                        Description = idString,
                                        Size = 1
                                    };
                var category4 = new Category()
                                    {
                                        Name = "Linq_MaxMin_Test",
                                        Description = idString,
                                        Size = 1
                                    };
                repo.Add(category1);
                repo.Add(category2);
                repo.Add(category3);
                repo.Add(category4);
                repo.SaveChanges();
                var maxResult = (from item in repo.Query<Category>()
                                 where item.Description == idString
                                 select item.Size).Max();
                var minResult = (from item in repo.Query<Category>()
                                 where item.Description == idString
                                 select item.Size).Min();

                Assert.IsTrue(maxResult >= 2 && minResult <= 1);
            }
        }
Exemplo n.º 11
0
        public void Linq_OrderBy_Test()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var date = DateTime.Now.AddHours(-1);
                var idString = "Linq_OrderBy_Test_Description" + DateTime.Now.ToString("yymmdd:hhMMss");
                var category1 = new Category()
                                    {
                                        Name = "Linq_OrderBy_Test",
                                        Description = idString,
                                        Size = 2
                                    };

                var category2 = new Category()
                                    {
                                        Name = "ALinq_OrderBy_Test",
                                        Description = idString,
                                        Size = 2
                                    };
                var category3 = new Category()
                                    {
                                        Name = "Linq_OrderBy_Test",
                                        Description = idString,
                                        Size = 1
                                    };
                var category4 = new Category()
                                    {
                                        Name = "ALinq_OrderBy_Test",
                                        Description = idString,
                                        Size = 1
                                    };
                repo.Add(category1);
                repo.Add(category2);
                repo.Add(category3);
                repo.Add(category4);
                repo.SaveChanges();
                var result = (from item in repo.Query<Category>()
                              where item.Description == idString
                              orderby item.Size descending
                              select item).ThenBy(p => p.Name).ToList<Category>();

                Assert.IsNotNull(result);
                Assert.IsTrue(result.Count == 4);
                Assert.IsTrue(result[0].ID == category2.ID);
                Assert.IsTrue(result[1].ID == category1.ID);
                Assert.IsTrue(result[2].ID == category4.ID);
                Assert.IsTrue(result[3].ID == category3.ID);

                var result2 =
                    repo.Query<Category>().Where(T => T.Description == idString).OrderByDescending(T => T.Size).ThenBy(
                        T => T.Name).ToList();
                for (int i = 0; i < result.Count; i++)
                {
                    Assert.IsTrue(result[i].ID == result2[i].ID);
                }
            }
        }
Exemplo n.º 12
0
        public void Linq_Like_Test()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var date = DateTime.Now.AddHours(-1);
                var category = new Category()
                {
                    Name = "Linq_Like_Test",
                    Description = "Linq_Like_Test_Description"
                };
                repo.Add(category);
                repo.SaveChanges();
                var result = (from item in repo.Query<Category>()
                              where item.ID == category.ID && item.Name.ToUpper().IndexOf("_LIKE_TEST") > 0
                              select item).ToList<Category>();

                Assert.IsNotNull(result);
                Assert.IsTrue(result.Count == 1);
                Assert.IsTrue(result[0].ID == category.ID);
            }
        }
Exemplo n.º 13
0
        public void Linq_GroupBy_Test()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var date = DateTime.Now.AddHours(-1);
                var category = new Category()
                                   {
                                       Name = "Linq_GroupBy_Test",
                                       Description = "Linq_GroupBy_Test_Description",
                                   };

                var category1 = new Category()
                                    {
                                        Name = "Linq_GroupBy_Test",
                                        Description = "Linq_GroupBy_Test_Description",
                                        Size = 9
                                    };
                repo.Add(category);
                repo.Add(category1);
                repo.SaveChanges();

                var groupquery = (from item in repo.Query<Category>()
                                  group item by item.Name
                                  into g
                                  select new
                                             {
                                                 g.Key,
                                                 Age = g.Sum(p => p.Size)
                                             }).ToList();
                var groupquery2 = repo.Query<Category>().GroupBy(o => o.Name)
                    .Select(o => new {o.Key, Age = o.Sum(p => p.Size)}).ToList();

                Assert.IsNotNull(groupquery);
                Assert.IsTrue(groupquery.Count > 0);
                Assert.IsNotNull(groupquery2);
                Assert.IsTrue(groupquery2.Count > 0);
                Assert.IsTrue(groupquery.Count == groupquery2.Count);
                for (int i = 0; i < groupquery.Count; i++)
                {
                    Assert.IsTrue(groupquery[i].Age == groupquery2[i].Age);
                }

            }
        }
Exemplo n.º 14
0
        public void Linq_Element_Test()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var date = DateTime.Now.AddHours(-1);
                var category = new Category()
                                   {
                                       Name = "Linq_Element_Test",
                                       Description = "Linq_Element_Test_Description",
                                   };

                var category1 = new Category()
                                    {
                                        Name = "Linq_Element_Test",
                                        Description = "Linq_Element_Test_Description",
                                    };
                repo.Add(category);
                repo.Add(category1);
                repo.SaveChanges();

                var firstquery = repo.Query<Category>().First(u => u.Name == "Linq_Element_Test");
                var firstOrDefaultquery = repo.Query<Category>().FirstOrDefault(u => u.Name == "Linq_Element_Test");

                var singlequery = repo.Query<Category>().Single(u => u.Name == "Linq_Element_Test");//此处抛出异常,因为Single方法只能根据条件返回一条数据
                var singleOrDefaultquery = repo.Query<Category>().SingleOrDefault(u => u.Name == "Linq_Element_Test");
                Assert.IsTrue(firstquery.Name == "Linq_Element_Test");
                Assert.IsTrue(firstOrDefaultquery.Name == "Linq_Element_Test");

                repo.Remove(category);
                repo.SaveChanges();
                Assert.IsTrue(singleOrDefaultquery.Name == "Linq_Element_Test");
                Assert.IsTrue(singlequery.Name == "Linq_Element_Test");
            }
        }
Exemplo n.º 15
0
        public void Linq_BetweenAnd_Test()
        {
            using (var repo = RepositoryContext.GetRepository())
            {
                var date = DateTime.Now.AddHours(-1);
                var category = new Category()
                                   {
                                       Name = "Linq_BetweenAnd_OrderByTest",
                                       Description = "Linq_BetweenAnd_OrderByTest_Description"
                                   };
                repo.Add(category);
                repo.SaveChanges();
                var result = (from item in repo.Query<Category>()
                              where item.CreatedOn <= DateTime.Now && item.CreatedOn >= date
                              orderby item.Name descending
                              orderby item.Description ascending
                              select item).ToList<Category>();

                var addedCategory = result.Find(t => t.ID == category.ID);
                Assert.IsNotNull(result);
                Assert.IsTrue(result.Count > 0);
                Assert.IsTrue(addedCategory.CreatedOn >= date && addedCategory.CreatedOn <= DateTime.Now);
            }
        }
Exemplo n.º 16
0
 public void LinqNormalTest()
 {
     using (var repo = RepositoryContext.GetRepository())
     {
         var category = new Category() {Name = "LinqNormalTest", Description = "Description"};
         repo.Add(category);
         repo.SaveChanges();
         var result = (from item in repo.Query<Category>()
                       where item.Name != null && item.Description.IndexOf("ption") > 0 && item.ID == category.ID
                             && item.CreatedOn <= DateTime.Now && item.CreatedOn.Year == 2012
                       orderby item.Name descending
                       orderby item.Description ascending
                       select item).ToList<Category>();
         Assert.IsNotNull(result);
         Assert.IsTrue(result.Count == 1);
     }
 }