public void Test_CreateCode()
 {
     GeneralTreeCodeGenerate.CreateCode().ShouldBe(null);
     GeneralTreeCodeGenerate.CreateCode(42).ShouldBe("00042");
     GeneralTreeCodeGenerate.CreateCode(1, 2).ShouldBe("00001.00002");
     GeneralTreeCodeGenerate.CreateCode(1, 2, 3).ShouldBe("00001.00002.00003");
 }
        public async Task Move_Test()
        {
            //Act
            var beijing = await CreateRegion("beijing");

            await CreateRegion("dongcheng", beijing.Id);
            await CreateRegion("xicheng", beijing.Id);

            var hebei = await CreateRegion("hebei");

            await CreateRegion("shijiazhuang", hebei.Id);

            var chengde = await CreateRegion("chengde", hebei.Id);

            await CreateRegion("shaungqiao", chengde.Id);
            await CreateRegion("shaungluan", chengde.Id);

            var beijingLastChild = GetRegion("xicheng");

            beijingLastChild.ShouldNotBeNull();
            await _generalRegion2TreeManager.MoveAsync(chengde.Id, beijing.Id);

            //Assert
            var cd = GetRegion(chengde.Name);

            cd.ShouldNotBeNull();
            cd.FullName.ShouldBe(beijing.FullName + "-" + chengde.Name);
            cd.ParentId.ShouldBe(beijing.Id);
            cd.Level.ShouldBe(beijing.Level + 1);
            cd.Code.ShouldBe(GeneralTreeCodeGenerate.GetNextCode(beijingLastChild.Code));
        }
        public async Task BulkCreate_Test()
        {
            //Act
            var beijing = new Region2
            {
                Name = "beijing"
            };

            var xicheng = new Region2
            {
                Name     = "xicheng",
                ParentId = beijing.Id
            };

            var dongcheng = new Region2
            {
                Name     = "dongcheng",
                ParentId = beijing.Id
            };

            beijing.Children = new List <Region2>
            {
                xicheng,
                dongcheng
            };

            await _generalRegion2TreeManager.BulkCreateAsync(beijing);

            //Assert
            var bj = GetRegion("beijing");

            bj.ShouldNotBeNull();
            bj.Name.ShouldBe("beijing");
            bj.FullName.ShouldBe("beijing");
            bj.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1));
            bj.Level.ShouldBe(1);
            bj.ParentId.ShouldBeNull();

            var xc = GetRegion("xicheng");

            xc.ShouldNotBeNull();
            xc.Name.ShouldBe("xicheng");
            xc.FullName.ShouldBe("beijing-xicheng");
            xc.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 1));
            xc.Level.ShouldBe(beijing.Level + 1);
            xc.ParentId.ShouldBe(beijing.Id);

            var dc = GetRegion("dongcheng");

            dc.ShouldNotBeNull();
            dc.Name.ShouldBe("dongcheng");
            dc.FullName.ShouldBe("beijing-dongcheng");
            dc.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 2));
            dc.Level.ShouldBe(beijing.Level + 1);
            dc.ParentId.ShouldBe(beijing.Id);
        }
Exemple #4
0
        public void Test_CreateCode_With_Length()
        {
            var generate = new GeneralTreeCodeGenerate(new GeneralTreeCodeGenerateConfiguration()
            {
                CodeLength = 3
            });

            generate.CreateCode().ShouldBe(null);
            generate.CreateCode(42).ShouldBe("042");
            generate.CreateCode(1, 2).ShouldBe("001.002");
            generate.CreateCode(1, 2, 3).ShouldBe("001.002.003");
        }
Exemple #5
0
        public async Task CheckSameNameExpression_Test()
        {
            var uowManager = LocalIocManager.Resolve <IUnitOfWorkManager>();

            using (var uow = uowManager.Begin())
            {
                var repository = LocalIocManager.Resolve <IRepository <Region, long> >();
                var config     = new GeneralTreeConfiguration <Region, long>
                {
                    CheckSameNameExpression = (regionThis, regionCheck) =>
                                              regionThis.SomeForeignKey == regionCheck.SomeForeignKey
                };

                var codeGenerate = new GeneralTreeCodeGenerate(new GeneralTreeCodeGenerateConfiguration());

                var manager =
                    new GeneralTreeManager <Region, long>(codeGenerate, repository, config);

                //Act
                await manager.CreateAsync(new Region
                {
                    Name           = "beijing",
                    SomeForeignKey = 1
                });

                uowManager.Current.SaveChanges();

                //Act
                await manager.CreateAsync(new Region
                {
                    Name           = "beijing",
                    SomeForeignKey = 2
                });

                uowManager.Current.SaveChanges();

                //Assert
                var beijing1 = repository.FirstOrDefault(x => x.Name == "beijing" && x.SomeForeignKey == 1);
                beijing1.ShouldNotBeNull();

                var beijing2 = repository.FirstOrDefault(x => x.Name == "beijing" && x.SomeForeignKey == 2);
                beijing2.ShouldNotBeNull();

                uow.Complete();
            }
        }
Exemple #6
0
        public async Task FullName_Hyphen_Test()
        {
            var uowManager = LocalIocManager.Resolve <IUnitOfWorkManager>();

            using (var uow = uowManager.Begin())
            {
                var repository = LocalIocManager.Resolve <IRepository <Region, long> >();
                var config     = new GeneralTreeConfiguration <Region, long>
                {
                    Hyphen = "->"
                };

                var codeGenerate = new GeneralTreeCodeGenerate(new GeneralTreeCodeGenerateConfiguration());

                var manager =
                    new GeneralTreeManager <Region, long>(codeGenerate, repository, config);

                //Act
                var beijing = new Region
                {
                    Name = "beijing"
                };
                await manager.CreateAsync(beijing);

                uowManager.Current.SaveChanges();

                var xicheng = new Region
                {
                    Name     = "xicheng",
                    ParentId = beijing.Id
                };
                await manager.CreateAsync(xicheng);

                uowManager.Current.SaveChanges();

                //Assert
                var xc = GetRegion("xicheng");
                xc.ShouldNotBeNull();
                xc.Name.ShouldBe("xicheng");
                xc.FullName.ShouldBe("beijing->xicheng");

                uow.Complete();
            }
        }
        public async Task Create_Test()
        {
            //Act
            var beijing = new Region2
            {
                Name = "beijing"
            };
            await _generalRegion2TreeManager.CreateAsync(beijing);

            //Assert
            var xc = GetRegion("beijing");

            xc.ShouldNotBeNull();
            xc.Name.ShouldBe("beijing");
            xc.FullName.ShouldBe("beijing");
            xc.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1));
            xc.Level.ShouldBe(1);
            xc.ParentId.ShouldBeNull();
        }
        public async Task Create_Children_Test()
        {
            //Act
            var beijing = new Region
            {
                Name = "beijing"
            };
            await _generalRegionTreeManager.CreateAsync(beijing);

            var xicheng = new Region
            {
                Name     = "xicheng",
                ParentId = beijing.Id
            };
            await _generalRegionTreeManager.CreateAsync(xicheng);

            var dongcheng = new Region
            {
                Name     = "dongcheng",
                ParentId = beijing.Id
            };
            await _generalRegionTreeManager.CreateAsync(dongcheng);

            //Assert
            var xc = GetRegion("xicheng");

            xc.ShouldNotBeNull();
            xc.Name.ShouldBe("xicheng");
            xc.FullName.ShouldBe("beijing-xicheng");
            xc.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 1));
            xc.Level.ShouldBe(beijing.Level + 1);
            xc.ParentId.ShouldBe(beijing.Id);

            var dc = GetRegion("dongcheng");

            dc.ShouldNotBeNull();
            dc.Name.ShouldBe("dongcheng");
            dc.FullName.ShouldBe("beijing-dongcheng");
            dc.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 2));
            dc.Level.ShouldBe(beijing.Level + 1);
            dc.ParentId.ShouldBe(beijing.Id);
        }
Exemple #9
0
        public async Task ExceptionMessageFactory_Test()
        {
            var uowManager = LocalIocManager.Resolve <IUnitOfWorkManager>();

            using (var uow = uowManager.Begin())
            {
                var repository = LocalIocManager.Resolve <IRepository <Region, long> >();
                var config     = new GeneralTreeConfiguration <Region, long>
                {
                    ExceptionMessageFactory =
                        tree => $"{tree.Name}已经存在"
                };

                var codeGenerate = new GeneralTreeCodeGenerate(new GeneralTreeCodeGenerateConfiguration());

                var manager =
                    new GeneralTreeManager <Region, long>(codeGenerate, repository, config);

                //Act
                await manager.CreateAsync(new Region
                {
                    Name = "beijing"
                });

                uowManager.Current.SaveChanges();

                //Assert
                var exception = await Record.ExceptionAsync(async() => await manager.CreateAsync(
                                                                new Region
                {
                    Name = "beijing"
                }
                                                                ));

                exception.ShouldNotBeNull();
                exception.ShouldBeOfType <UserFriendlyException>();
                exception.Message.ShouldBe("beijing已经存在");

                uow.Complete();
            }
        }
        public async Task BulkCreate_ExistTree_Test()
        {
            //Arrange
            var hebei = new Region2
            {
                Name     = "hebei",
                FullName = "hebei",
                Code     = "00001",
                Level    = 1
            };

            await UsingDbContextAsync(async context =>
            {
                context.Region2.Add(hebei);
                await context.SaveChangesAsync();

                context.Region2.Add(new Region2
                {
                    Name     = "shijiazhuang",
                    FullName = "hebei-shijiazhuang",
                    Code     = "00001.00001",
                    Level    = 2,
                    ParentId = hebei.Id
                });
                await context.SaveChangesAsync();
            });

            var chengde = new Region2
            {
                Name     = "chengde",
                ParentId = hebei.Id,
                Children = new List <Region2>
                {
                    new Region2
                    {
                        Name = "shuangqiaoqu"
                    },
                    new Region2
                    {
                        Name = "shuangluanqu"
                    }
                }
            };

            await _generalRegion2TreeManager.BulkCreateAsync(chengde);

            //Assert
            chengde = GetRegion("chengde");
            chengde.ShouldNotBeNull();
            chengde.Name.ShouldBe("chengde");
            chengde.FullName.ShouldBe("hebei-chengde");
            chengde.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 2));
            chengde.Level.ShouldBe(2);
            chengde.ParentId.ShouldBe(hebei.Id);

            var shuangqiaoqu = GetRegion("shuangqiaoqu");

            shuangqiaoqu.ShouldNotBeNull();
            shuangqiaoqu.Name.ShouldBe("shuangqiaoqu");
            shuangqiaoqu.FullName.ShouldBe("hebei-chengde-shuangqiaoqu");
            shuangqiaoqu.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 2, 1));
            shuangqiaoqu.Level.ShouldBe(chengde.Level + 1);
            shuangqiaoqu.ParentId.ShouldBe(chengde.Id);

            var shuangluanqu = GetRegion("shuangluanqu");

            shuangluanqu.ShouldNotBeNull();
            shuangluanqu.Name.ShouldBe("shuangluanqu");
            shuangluanqu.FullName.ShouldBe("hebei-chengde-shuangluanqu");
            shuangluanqu.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 2, 2));
            shuangluanqu.Level.ShouldBe(chengde.Level + 1);
            shuangluanqu.ParentId.ShouldBe(chengde.Id);
        }
        public async Task FillUp_Test()
        {
            //Act
            var beijing = new Region2
            {
                Name = "beijing"
            };

            var xicheng = new Region2
            {
                Name     = "xicheng",
                ParentId = beijing.Id
            };

            var dongcheng = new Region2
            {
                Name     = "dongcheng",
                ParentId = beijing.Id
            };

            var balizhuang = new Region2
            {
                Name     = "balizhuang",
                ParentId = dongcheng.Id
            };

            dongcheng.Children = new List <Region2>
            {
                balizhuang
            };

            beijing.Children = new List <Region2>
            {
                xicheng,
                dongcheng
            };

            await _generalRegion2TreeManager.FillUpAsync(beijing);

            //Assert
            beijing.FullName.ShouldBe("beijing");
            beijing.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1));
            beijing.Level.ShouldBe(1);
            beijing.ParentId.ShouldBeNull();
            beijing.Children.Count.ShouldBe(2);

            xicheng.FullName.ShouldBe("beijing-xicheng");
            xicheng.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 1));
            xicheng.Level.ShouldBe(beijing.Level + 1);
            xicheng.ParentId.ShouldBe(beijing.Id);

            dongcheng.FullName.ShouldBe("beijing-dongcheng");
            dongcheng.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 2));
            dongcheng.Level.ShouldBe(beijing.Level + 1);
            dongcheng.ParentId.ShouldBe(beijing.Id);

            balizhuang.FullName.ShouldBe("beijing-dongcheng-balizhuang");
            balizhuang.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 2, 1));
            balizhuang.Level.ShouldBe(dongcheng.Level + 1);
            balizhuang.ParentId.ShouldBe(dongcheng.Id);
        }
 public void Test_GetNextCode()
 {
     GeneralTreeCodeGenerate.GetNextCode("00001").ShouldBe("00002");
     GeneralTreeCodeGenerate.GetNextCode("00001.00001").ShouldBe("00001.00002");
 }
 public void Test_RemoveParentCode()
 {
     GeneralTreeCodeGenerate.RemoveParentCode("00001.00002.00003", "00001").ShouldBe("00002.00003");
     GeneralTreeCodeGenerate.RemoveParentCode("00001.00002.00003", null).ShouldBe("00001.00002.00003");
 }
 public void Test_MergeFullName()
 {
     GeneralTreeCodeGenerate.MergeFullName(null, "beijing").ShouldBe("beijing");
     GeneralTreeCodeGenerate.MergeFullName("beijing", "xicheng").ShouldBe("beijing-xicheng");
 }
 public void Test_MergeCode()
 {
     GeneralTreeCodeGenerate.MergeCode(null, "00002").ShouldBe("00002");
     GeneralTreeCodeGenerate.MergeCode("00001", "00002").ShouldBe("00001.00002");
 }
 public void Test_GetParentCode()
 {
     GeneralTreeCodeGenerate.GetParentCode("00001").ShouldBe(null);
     GeneralTreeCodeGenerate.GetParentCode("00001.00002.00003").ShouldBe("00001.00002");
 }
        public async Task FillUp_Test()
        {
            //Act
            var beijing = new Region
            {
                Name = "beijing"
            };

            var xicheng = new Region
            {
                Name     = "xicheng",
                ParentId = beijing.Id
            };

            var dongcheng = new Region
            {
                Name     = "dongcheng",
                ParentId = beijing.Id
            };

            var balizhuang = new Region
            {
                Name     = "balizhuang",
                ParentId = dongcheng.Id
            };

            dongcheng.Children = new List <Region>
            {
                balizhuang
            };

            beijing.Children = new List <Region>
            {
                xicheng,
                dongcheng
            };

            var uowManager = LocalIocManager.Resolve <IUnitOfWorkManager>();

            using (var uow = uowManager.Begin())
            {
                await _generalRegionTreeManager.FillUpAsync(beijing);

                uow.Complete();
            }

            //Assert
            beijing.FullName.ShouldBe("beijing");
            beijing.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1));
            beijing.Level.ShouldBe(1);
            beijing.ParentId.ShouldBeNull();
            beijing.Children.Count.ShouldBe(2);

            xicheng.FullName.ShouldBe("beijing-xicheng");
            xicheng.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 1));
            xicheng.Level.ShouldBe(beijing.Level + 1);
            xicheng.ParentId.ShouldBe(beijing.Id);

            dongcheng.FullName.ShouldBe("beijing-dongcheng");
            dongcheng.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 2));
            dongcheng.Level.ShouldBe(beijing.Level + 1);
            dongcheng.ParentId.ShouldBe(beijing.Id);

            balizhuang.FullName.ShouldBe("beijing-dongcheng-balizhuang");
            balizhuang.Code.ShouldBe(GeneralTreeCodeGenerate.CreateCode(1, 2, 1));
            balizhuang.Level.ShouldBe(dongcheng.Level + 1);
            balizhuang.ParentId.ShouldBe(dongcheng.Id);
        }
 public void Test_GetLastCode()
 {
     GeneralTreeCodeGenerate.GetLastCode("00001").ShouldBe("00001");
     GeneralTreeCodeGenerate.GetLastCode("00001.00002.00003").ShouldBe("00003");
 }