예제 #1
0
        public async void CrudViewsInDb()
        {
            DbContextOptions <WorkGramDbContext> options = new DbContextOptionsBuilder <WorkGramDbContext>()
                                                           .UseInMemoryDatabase("GetViewsname")
                                                           .Options;

            using (WorkGramDbContext context = new WorkGramDbContext(options))
            {
                //Arrange  (CREATE)
                WorkGram workGram = new WorkGram();
                workGram.Views = 1;

                context.WorkGrams.Add(workGram);
                context.SaveChanges();

                //Act (READ)
                var viewsName = await context.WorkGrams.FirstOrDefaultAsync(x => x.Views == workGram.Views);

                //Assert
                Assert.Equal(1, workGram.Views);

                //UPDATE
                workGram.Views = 456;
                context.Update(viewsName);
                context.SaveChanges();
                var updateViews = await context.WorkGrams.FirstOrDefaultAsync(x => x.Views == workGram.Views);

                Assert.Equal(456, updateViews.Views);

                //DELETE
                context.WorkGrams.Remove(updateViews);
                context.SaveChanges();
                var deleteViews = await context.WorkGrams.FirstOrDefaultAsync(x => x.Views == workGram.Views);

                Assert.True(deleteViews == null);
            }
        }
예제 #2
0
        public async void CrudUsersInDb()
        {
            DbContextOptions <WorkGramDbContext> options = new DbContextOptionsBuilder <WorkGramDbContext>()
                                                           .UseInMemoryDatabase("GetUserName")
                                                           .Options;

            using (WorkGramDbContext context = new WorkGramDbContext(options))
            {
                //Arrange  (CREATE)
                WorkGram workGram = new WorkGram();
                workGram.UserName = "******";

                context.WorkGrams.Add(workGram);
                context.SaveChanges();

                //Act (READ)
                var userName = await context.WorkGrams.FirstOrDefaultAsync(x => x.UserName == workGram.UserName);

                //Assert
                Assert.Equal("UserOne", workGram.UserName);

                //UPDATE
                workGram.UserName = "******";
                context.Update(userName);
                context.SaveChanges();
                var updateUser = await context.WorkGrams.FirstOrDefaultAsync(x => x.UserName == workGram.UserName);

                Assert.Equal("updated User", updateUser.UserName);

                //DELETE
                context.WorkGrams.Remove(updateUser);
                context.SaveChanges();
                var deleteUser = await context.WorkGrams.FirstOrDefaultAsync(x => x.UserName == workGram.UserName);

                Assert.True(deleteUser == null);
            }
        }
예제 #3
0
        public async void CrudURLInDb()
        {
            DbContextOptions <WorkGramDbContext> options = new DbContextOptionsBuilder <WorkGramDbContext>()
                                                           .UseInMemoryDatabase("GetURLname")
                                                           .Options;

            using (WorkGramDbContext context = new WorkGramDbContext(options))
            {
                //Arrange  (CREATE)
                WorkGram workGram = new WorkGram();
                workGram.URL = "https://via.placeholder.com/250";

                context.WorkGrams.Add(workGram);
                context.SaveChanges();

                //Act (READ)
                var urlName = await context.WorkGrams.FirstOrDefaultAsync(x => x.URL == workGram.URL);

                //Assert
                Assert.Equal("https://via.placeholder.com/250", workGram.URL);

                //UPDATE
                workGram.URL = "https://via.placeholder.com/150";
                context.Update(urlName);
                context.SaveChanges();
                var updateURL = await context.WorkGrams.FirstOrDefaultAsync(x => x.URL == workGram.URL);

                Assert.Equal("https://via.placeholder.com/150", updateURL.URL);

                //DELETE
                context.WorkGrams.Remove(updateURL);
                context.SaveChanges();
                var deleteURL = await context.WorkGrams.FirstOrDefaultAsync(x => x.URL == workGram.URL);

                Assert.True(deleteURL == null);
            }
        }