Ejemplo n.º 1
0
        public async Task LazyLoadQueryTest()
        {
            UnitTestEnvironment env = new UnitTestEnvironment();
            await env.Run(async sp =>
            {
                var database        = sp.GetService <IDatabaseContext>();
                var categoryContext = database.GetContext <Category>();
                var category        = categoryContext.Create();
                category.Name       = "Test";
                categoryContext.Add(category);
                var userContext = database.GetContext <User>();
                var user        = userContext.Create();
                user.Username   = "******";
                user.Category   = category;
                userContext.Add(user);
                await database.SaveAsync();
            });

            await env.Run(async sp =>
            {
                var database        = sp.GetService <IDatabaseContext>();
                var categoryContext = database.GetContext <Category>();
                var category        = (await categoryContext.ToArrayAsync(categoryContext.Query()))[0];
                var userContext     = database.GetContext <User>();
                Assert.Null(category.Users);
                var collection = await category.LoadAsync(t => t.Users);
                Assert.Equal(1, collection.Count);
                var users = await userContext.ToArrayAsync(collection);
                Assert.Equal("TestUser", users[0].Username);
            });
        }
Ejemplo n.º 2
0
        public async Task LazyLoadEntityTest()
        {
            UnitTestEnvironment env = new UnitTestEnvironment();
            await env.Run(async sp =>
            {
                var database        = sp.GetService <IDatabaseContext>();
                var categoryContext = database.GetContext <Category>();
                var category        = categoryContext.Create();
                category.Name       = "Test";
                categoryContext.Add(category);
                var userContext = database.GetContext <User>();
                var user        = userContext.Create();
                user.Username   = "******";
                user.Category   = category;
                userContext.Add(user);
                await database.SaveAsync();
            });

            await env.Run(async sp =>
            {
                var database    = sp.GetService <IDatabaseContext>();
                var userContext = database.GetContext <User>();
                var user        = await userContext.Query().FirstOrDefaultAsync();
                Assert.Null(user.Category);
                var dbContext = ((DatabaseContext)database).InnerContext;
                var category  = await user.LoadAsync(t => t.Category);
                Assert.NotNull(category);
                Assert.Equal("Test", category.Name);
            });
        }
Ejemplo n.º 3
0
        public async Task List()
        {
            var env = new UnitTestEnvironment();
            await env.Run(async sp =>
            {
                var databaseContext = sp.GetRequiredService <IDatabaseContext>();
                await DataCommon.DataInitAsync(databaseContext);
            });

            Guid   id       = Guid.Empty;
            string username = null;
            await env.Run(async sp =>
            {
                MockController controller = new MockController(sp);
                var model = await controller.ExecuteAsync <EntityDomainService <User>, IEntityViewModel <User> >("List");
                Assert.Equal(2, model.Items.Length);
                id       = model.Items[0].Index;
                username = model.Items[0].Username;
            });

            await env.Run(async sp =>
            {
                MockController controller = new MockController(sp);
                var model = await controller.ExecuteAsync <EntityDomainService <User>, IEntityEditModel <User> >(context =>
                {
                    context.ValueProvider.SetAlias("id", "Index");
                    context.ValueProvider.SetValue("id", id);
                }, "Detail");
                Assert.Equal(username, model.Item.Username);
            });
        }
Ejemplo n.º 4
0
        public async Task AddAndRemoveTest()
        {
            UnitTestEnvironment env = new UnitTestEnvironment();
            await env.Run(async sp =>
            {
                var database        = sp.GetService <IDatabaseContext>();
                var categoryContext = database.GetWrappedContext <ICategory>();
                var userContext     = database.GetWrappedContext <IUser>();
                Assert.Equal(0, await categoryContext.CountAsync(categoryContext.Query()));
                var category  = categoryContext.Create();
                category.Name = "Test";
                categoryContext.Add(category);
                await database.SaveAsync();

                var user      = userContext.Create();
                user.Username = "******";
                user.Category = category;
                userContext.Add(user);
                await database.SaveAsync();

                Assert.Equal(1, await userContext.CountAsync(userContext.Query().Where(t => t.Category.Name == "Test")));

                user = await userContext.GetAsync(user.Index);
                Assert.NotNull(user);
            });
        }
Ejemplo n.º 5
0
        public async Task Test()
        {
            UnitTestEnvironment env      = new UnitTestEnvironment();
            Category            category = null;
            await env.Run(async sp =>
            {
                var context = (DataContext)sp.GetRequiredService <Microsoft.EntityFrameworkCore.DbContext>();

                category = new Category {
                    Index = Guid.NewGuid(), CreateDate = DateTime.Now, EditDate = DateTime.Now, Name = "Test"
                };
                context.Category.Add(category);
                await context.SaveChangesAsync();
            });

            await env.Run(async sp =>
            {
                var databaseContext = sp.GetService <Wodsoft.ComBoost.Data.Entity.IDatabaseContext>();
                var context         = (DataContext)((Wodsoft.ComBoost.Data.Entity.DatabaseContext)databaseContext).InnerContext;
                Assert.Equal(1, await context.Category.CountAsync());
                var categoryContext = databaseContext.GetContext <Category>();
                var category2       = await categoryContext.GetAsync(category.Index);

                var user = new User {
                    Index = Guid.NewGuid(), Category = category2, CreateDate = DateTime.Now, EditDate = DateTime.Now, Username = "******"
                };
                databaseContext.GetContext <User>().Add(user);
                Assert.Equal(Microsoft.EntityFrameworkCore.EntityState.Unchanged, context.Entry(category2).State);
                //category2.Name = "Changed";
                await context.SaveChangesAsync();
            });

            await env.Run(async sp =>
            {
                var databaseContext = sp.GetService <Wodsoft.ComBoost.Data.Entity.IDatabaseContext>();
                var categoryContext = databaseContext.GetContext <Category>();
                var category3       = await categoryContext.GetAsync(category.Index);
                Assert.Equal("Test", category3.Name);

                var users = await category3.LoadAsync(t => t.Users);
                var user  = new User {
                    Index = Guid.NewGuid(), CreateDate = DateTime.Now, EditDate = DateTime.Now, Username = "******"
                };
                users.Add(user);
                Assert.Equal(category3, user.Category);
            });
        }
Ejemplo n.º 6
0
        public async Task AddAndRemoveTest()
        {
            UnitTestEnvironment env = new UnitTestEnvironment();
            await env.Run(async sp =>
            {
                var database        = sp.GetService <IDatabaseContext>();
                var categoryContext = database.GetContext <Category>();
                Assert.Equal(0, await categoryContext.CountAsync(categoryContext.Query()));
                var category  = categoryContext.Create();
                category.Name = "Test";
                categoryContext.Add(category);
                Assert.Equal(0, await categoryContext.CountAsync(categoryContext.Query()));
                await database.SaveAsync();
                Assert.Equal(1, await categoryContext.CountAsync(categoryContext.Query()));
                categoryContext.Remove(category);
                Assert.Equal(1, await categoryContext.CountAsync(categoryContext.Query()));
                await database.SaveAsync();
                Assert.Equal(0, await categoryContext.CountAsync(categoryContext.Query()));
            });

            await env.Run(async sp =>
            {
                var database        = sp.GetService <IDatabaseContext>();
                var categoryContext = database.GetContext <Category>();
                Assert.Equal(0, await categoryContext.CountAsync(categoryContext.Query()));
                var category  = categoryContext.Create();
                category.Name = "Parent";
                categoryContext.Add(category);
                await database.SaveAsync();
            });

            await env.Run(async sp =>
            {
                var database        = sp.GetService <IDatabaseContext>();
                var categoryContext = database.GetContext <Category>();
                Assert.Equal(1, await categoryContext.CountAsync(categoryContext.Query()));
                var parent      = await categoryContext.Query().FirstOrDefaultAsync(t => t.Name == "Parent");
                var category    = categoryContext.Create();
                category.Name   = "Test";
                category.Parent = parent;
                categoryContext.Add(category);
                await database.SaveAsync();
                Assert.Equal(2, await categoryContext.CountAsync(categoryContext.Query()));
            });
        }
Ejemplo n.º 7
0
 public async Task OrderTest()
 {
     UnitTestEnvironment env = new UnitTestEnvironment();
     await env.Run(async sp =>
     {
         var database = sp.GetService <IDatabaseContext>();
         await DataCommon.DataInitAsync(database);
         var userContext = database.GetContext <User>();
         var user        = await userContext.FirstOrDefaultAsync(userContext.Order());
         Assert.Equal("TestUser1", user.Username);
     });
 }
Ejemplo n.º 8
0
        public async Task ReferenceTest()
        {
            UnitTestEnvironment env = new UnitTestEnvironment();
            await env.Run(async sp =>
            {
                var database        = sp.GetService <IDatabaseContext>();
                var categoryContext = database.GetContext <Category>();
                var category        = categoryContext.Create();
                category.Name       = "Test";
                categoryContext.Add(category);
                var userContext = database.GetContext <User>();
                var user        = userContext.Create();
                user.Username   = "******";
                userContext.Add(user);
                await database.SaveAsync();
            });

            await env.Run(async sp =>
            {
                var database        = sp.GetService <IDatabaseContext>();
                var userContext     = database.GetContext <User>();
                var user            = await userContext.FirstOrDefaultAsync(userContext.Query());
                var categoryContext = database.GetContext <Category>();
                var category        = await categoryContext.FirstOrDefaultAsync(categoryContext.Query());
                user.Category       = category;
                userContext.Update(user);
                //var refCategory = ((DatabaseContext)database).InnerContext.Entry(user).Reference(t => t.Category);
                await database.SaveAsync();
            });

            await env.Run(async sp =>
            {
                var database    = sp.GetService <IDatabaseContext>();
                var userContext = database.GetContext <User>();
                var user        = await userContext.FirstOrDefaultAsync(userContext.Include(userContext.Query(), t => t.Category));
                Assert.NotNull(user.Category);
            });
        }
Ejemplo n.º 9
0
        public async Task List2()
        {
            var env = new UnitTestEnvironment();
            await env.Run(async sp =>
            {
                MockController controller = new MockController(sp);
                await controller.ExecuteAsync <DataCommonService>("DataInit");
            });

            Guid   id;
            string username;
            await env.Run(async sp =>
            {
                MockController controller = new MockController(sp);
                var model = await controller.ExecuteAsync <EntityDomainService <User>, IEntityViewModel <User> >("List");
                Assert.Equal(2, model.Items.Length);
                id       = model.Items[0].Index;
                username = model.Items[0].Username;

                var category      = await model.Items[0].LoadAsync(t => t.Category);
                var userQueryable = await category.LoadAsync(t => t.Users);
                var users         = userQueryable.ToArrayAsync();
            });
        }