コード例 #1
0
        public void Get_error_if_delete_a_category_when_post_has_a_foreign_key_in_database_via_anonym_context()
        {
            Category category = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Name        = "post-category-unit-testing-relationship",
                Description = "post-category-unit-testing-relationship",
                Active      = false
            };

            Post post = new Post
            {
                PostId         = Guid.NewGuid().ToString(),
                UserId         = "",
                CategoryId     = category.CategoryId,
                Title          = "post-category-unit-testing",
                Text           = "post-category-unit-testing",
                CreatePostDate = DateTime.Now,
                IsActive       = false
            };

            try
            {
                using (var context = new AnonymContext(DbOptionsFactory.DbContextOptions))
                {
                    var addedCategory = context.Entry(category);
                    addedCategory.State = EntityState.Added;

                    var addedPost = context.Entry(post);
                    addedPost.State = EntityState.Added;
                    context.SaveChanges();

                    var deletedCategory = context.Entry(category);
                    deletedCategory.State = EntityState.Deleted;
                    context.SaveChanges();
                }

                Assert.IsTrue(true);
            }
            catch (Exception)
            {
                // Rollback
                using (var context = new AnonymContext(DbOptionsFactory.DbContextOptions))
                {
                    var deletedPost = context.Entry(post);
                    deletedPost.State = EntityState.Deleted;
                    context.SaveChanges();

                    var deletedCategory = context.Entry(category);
                    deletedCategory.State = EntityState.Deleted;
                    context.SaveChanges();
                }

                throw;
            }
        }
コード例 #2
0
        public void Create_a_category_in_database_via_anonym_context()
        {
            try
            {
                Category category = new Category
                {
                    CategoryId  = Guid.NewGuid().ToString(),
                    Name        = "category-unit-testing",
                    Description = "category-unit-testing",
                    Active      = false
                };

                using (var context = new AnonymContext(DbOptionsFactory.DbContextOptions))
                {
                    var addedCategory = context.Entry(category);
                    addedCategory.State = EntityState.Added;
                    context.SaveChanges();
                }

                Assert.IsTrue(true);
            }
            catch (Exception exception)
            {
                Assert.Fail(exception.Message);
            }
        }
コード例 #3
0
ファイル: ChatRoomTests.cs プロジェクト: cetinkesepara/Anonym
        public void Delete_a_chatroom_in_database_via_anonym_context()
        {
            try
            {
                ChatRoom chatRoom = new ChatRoom();
                Post     post     = new Post();
                Category category = new Category();

                using (var context = new AnonymContext(DbOptionsFactory.DbContextOptions))
                {
                    chatRoom = context.Set <ChatRoom>().SingleOrDefault(c => c.ReviewerName == "chatroom-unit-testing");

                    post = context.Set <Post>().SingleOrDefault(c => c.Title == "chatroom-unit-testing-relationship");

                    category = context.Set <Category>().SingleOrDefault(c => c.Name == "chatroom-unit-testing-relationship");

                    var deletedCategory = context.Entry(category);
                    deletedCategory.State = EntityState.Deleted;

                    var deletedPost = context.Entry(post);
                    deletedPost.State = EntityState.Deleted;

                    var deletedChatRoom = context.Entry(chatRoom);
                    deletedChatRoom.State = EntityState.Deleted;
                    context.SaveChanges();
                }

                Assert.IsTrue(true);
            }
            catch (Exception exception)
            {
                Assert.Fail(exception.Message);
            }
        }
コード例 #4
0
ファイル: ChatRoomTests.cs プロジェクト: cetinkesepara/Anonym
        public void Create_a_chatroom_in_database_via_anonym_context()
        {
            try
            {
                Category category = new Category
                {
                    CategoryId  = Guid.NewGuid().ToString(),
                    Name        = "chatroom-unit-testing-relationship",
                    Description = "chatroom-unit-testing-relationship",
                    Active      = false
                };

                Post post = new Post
                {
                    PostId         = Guid.NewGuid().ToString(),
                    UserId         = "",
                    CategoryId     = category.CategoryId,
                    Title          = "chatroom-unit-testing-relationship",
                    Text           = "chatroom-unit-testing-relationship",
                    CreatePostDate = DateTime.Now,
                    IsActive       = false
                };

                ChatRoom chatRoom = new ChatRoom
                {
                    ChatRoomId         = Guid.NewGuid().ToString(),
                    ReviewerUserId     = "",
                    PostId             = post.PostId,
                    PublisherName      = "chatroom-unit-testing",
                    ReviewerName       = "chatroom-unit-testing",
                    PublisherCommented = false,
                    Active             = false,
                    CreateDate         = DateTime.Now,
                };

                using (var context = new AnonymContext(DbOptionsFactory.DbContextOptions))
                {
                    var addedCategory = context.Entry(category);
                    addedCategory.State = EntityState.Added;

                    var addedPost = context.Entry(post);
                    addedPost.State = EntityState.Added;

                    var addedChatRoom = context.Entry(chatRoom);
                    addedChatRoom.State = EntityState.Added;
                    context.SaveChanges();
                }

                Assert.IsTrue(true);
            }
            catch (Exception exception)
            {
                Assert.Fail(exception.Message);
            }
        }
コード例 #5
0
        public void Delete_a_category_in_database_via_anonym_context()
        {
            try
            {
                Category category = new Category();

                using (var context = new AnonymContext(DbOptionsFactory.DbContextOptions))
                {
                    category = context.Set <Category>().SingleOrDefault(c => c.Name == "category-unit-testing");

                    var deletedCategory = context.Entry(category);
                    deletedCategory.State = EntityState.Deleted;
                    context.SaveChanges();
                }

                Assert.IsTrue(true);
            }
            catch (Exception exception)
            {
                Assert.Fail(exception.Message);
            }
        }
コード例 #6
0
ファイル: ChatRoomTests.cs プロジェクト: cetinkesepara/Anonym
        public void Update_a_chatroom_in_database_via_anonym_context()
        {
            try
            {
                ChatRoom chatRoom = new ChatRoom();

                using (var context = new AnonymContext(DbOptionsFactory.DbContextOptions))
                {
                    chatRoom = context.Set <ChatRoom>().SingleOrDefault(c => c.ReviewerName == "chatroom-unit-testing");

                    chatRoom.Active = true;

                    var updatedChatRoom = context.Entry(chatRoom);
                    updatedChatRoom.State = EntityState.Modified;
                    context.SaveChanges();
                }

                Assert.AreEqual(true, chatRoom.Active);
            }
            catch (Exception exception)
            {
                Assert.Fail(exception.Message);
            }
        }
コード例 #7
0
        public void Update_a_post_in_database_via_anonym_context()
        {
            try
            {
                Post post = new Post();

                using (var context = new AnonymContext(DbOptionsFactory.DbContextOptions))
                {
                    post = context.Set <Post>().SingleOrDefault(c => c.Title == "post-unit-testing");

                    post.IsActive = true;

                    var updatedPost = context.Entry(post);
                    updatedPost.State = EntityState.Modified;
                    context.SaveChanges();
                }

                Assert.AreEqual(true, post.IsActive);
            }
            catch (Exception exception)
            {
                Assert.Fail(exception.Message);
            }
        }
コード例 #8
0
        public void Update_a_chatmessage_in_database_via_anonym_context()
        {
            try
            {
                ChatMessage chatMessage = new ChatMessage();

                using (var context = new AnonymContext(DbOptionsFactory.DbContextOptions))
                {
                    chatMessage = context.Set <ChatMessage>().SingleOrDefault(c => c.DisplayUserName == "chatmessage-unit-testing");

                    chatMessage.IsPublisherMessage = true;

                    var updatedChatMessage = context.Entry(chatMessage);
                    updatedChatMessage.State = EntityState.Modified;
                    context.SaveChanges();
                }

                Assert.AreEqual(true, chatMessage.IsPublisherMessage);
            }
            catch (Exception exception)
            {
                Assert.Fail(exception.Message);
            }
        }
コード例 #9
0
        public void Delete_as_cascade_if_delete_a_post_when_chatroom_and_chatmessage_has_foreign_key_in_database_via_anonym_context()
        {
            Category category = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Name        = "post-chatroom-unit-testing-relationship",
                Description = "post-chatroom-unit-testing-relationship",
                Active      = false
            };

            Post post = new Post
            {
                PostId         = Guid.NewGuid().ToString(),
                UserId         = "",
                CategoryId     = category.CategoryId,
                Title          = "post-chatroom-unit-testing-relationship",
                Text           = "post-chatroom-unit-testing-relationship",
                CreatePostDate = DateTime.Now,
                IsActive       = false
            };

            ChatRoom chatRoom = new ChatRoom
            {
                ChatRoomId         = Guid.NewGuid().ToString(),
                ReviewerUserId     = "",
                PostId             = post.PostId,
                PublisherName      = "post-chatroom-unit-testing",
                ReviewerName       = "post-chatroom-unit-testing",
                PublisherCommented = false,
                Active             = false,
                CreateDate         = DateTime.Now,
            };

            ChatMessage chatMessage = new ChatMessage
            {
                ChatMessageId      = Guid.NewGuid().ToString(),
                ChatRoomId         = chatRoom.ChatRoomId,
                DisplayUserName    = "******",
                Message            = "chatmessage-chatroom-unit-testing",
                IsPublisherMessage = false,
                CreateDate         = DateTime.Now
            };

            ChatMessage chatMessageExpected = new ChatMessage();

            using (var context = new AnonymContext(DbOptionsFactory.DbContextOptions))
            {
                var addedCategory = context.Entry(category);
                addedCategory.State = EntityState.Added;

                var addedPost = context.Entry(post);
                addedPost.State = EntityState.Added;

                var addedChatRoom = context.Entry(chatRoom);
                addedChatRoom.State = EntityState.Added;

                var addedChatMessage = context.Entry(chatMessage);
                addedChatMessage.State = EntityState.Added;
                context.SaveChanges();

                var deletedPost = context.Entry(post);
                deletedPost.State = EntityState.Deleted;

                var deletedCategory = context.Entry(category);
                deletedCategory.State = EntityState.Deleted;
                context.SaveChanges();

                chatMessageExpected = context.Set <ChatMessage>().SingleOrDefault(c => c.DisplayUserName == "chatmessage-chatroom-unit-testing");
            }

            Assert.IsNull(chatMessageExpected);
        }