Пример #1
0
        public async Task AddAndRemoveTest()
        {
            var serviceProvider = DataCommon.GetServiceProvider();
            var database        = serviceProvider.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);
        }
Пример #2
0
        public async Task LazyLoadQueryTest()
        {
            var serviceProvider = DataCommon.GetServiceProvider();
            var database        = serviceProvider.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();

            database        = serviceProvider.GetService <IDatabaseContext>();
            categoryContext = database.GetContext <Category>();
            category        = (await categoryContext.ToArrayAsync(categoryContext.Query()))[0];
            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);
        }
Пример #3
0
        public async Task LazyLoadEntityTest()
        {
            var serviceProvider = DataCommon.GetServiceProvider();
            var database        = serviceProvider.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();

            database    = serviceProvider.GetService <IDatabaseContext>();
            userContext = database.GetContext <User>();
            user        = await userContext.FirstOrDefaultAsync(userContext.Query());

            Assert.Null(user.Category);
            var dbContext = ((DatabaseContext)database).InnerContext;

            category = await user.LoadAsync(t => t.Category);

            Assert.NotNull(category);
            Assert.Equal("Test", category.Name);
        }
Пример #4
0
        public async Task Test()
        {
            var context = new DataContext(new DbContextOptionsBuilder <DataContext>().UseInMemoryDatabase().Options.WithExtension(new ComBoostOptionExtension()));

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

            context.Category.Add(category);
            await context.SaveChangesAsync();

            var databaseContext = DataCommon.GetServiceProvider().GetService <IDatabaseContext>();

            context = (DataContext)((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(EntityState.Unchanged, context.Entry(category2).State);
            //category2.Name = "Changed";
            await context.SaveChangesAsync();

            databaseContext = DataCommon.GetServiceProvider().GetService <IDatabaseContext>();
            var category3 = await categoryContext.GetAsync(category.Index);

            Assert.Equal("Test", category3.Name);
        }
Пример #5
0
        public async Task OrderTest()
        {
            var serviceProvider = DataCommon.GetServiceProvider();
            var database        = serviceProvider.GetService <IDatabaseContext>();
            await DataCommon.DataInitAsync(database);

            var userContext = database.GetContext <User>();
            var user        = await userContext.FirstOrDefaultAsync(userContext.Order());

            Assert.Equal("TestUser1", user.Username);
        }
Пример #6
0
        public async Task AddAndRemoveTest()
        {
            var serviceProvider = DataCommon.GetServiceProvider();
            var database        = serviceProvider.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()));
        }