Ejemplo n.º 1
0
        public async void CreateTag_Creates_A_Tag_In_Db()
        {
            // Arrange
            var tag = new Tag {
                Slug = "tag", Title = "Tag"
            };

            // Act
            await _tagRepo.CreateAsync(tag);

            // Assert
            Assert.NotNull(_db.Set <Tag>().SingleOrDefault(c => c.Title == "Tag"));
        }
Ejemplo n.º 2
0
        public async void UpdatePost_With_Tags_Updated()
        {
            // Arrange: given 1 post with 2 tags
            SeedTestPost();
            // and another new tag
            var tagRepo = new SqlTagRepository(_db);
            var tagJava = await tagRepo.CreateAsync(new Tag { Title = "Java", Slug = "java" });

            // prep a list of tags
            List <string> tagTitles = new List <string> {
                TAG2_TITLE, "Java"
            };
            // get the post
            var post = await _postRepo.GetAsync(1, EPostType.BlogPost);

            // Act: when user updated the post by removing AspNet tag and adding Java tag

            // this won't work, the PostTag is still being tracked
            //post.PostTags.Clear();
            //post.PostTags.Add(new PostTag { Post = post, Tag = tagJava });
            //post.PostTags.Add(new PostTag { Post = post, Tag = tagCs });

            // instead we have to
            List <string> tagTitlesCurrent = post.PostTags.Select(pt => pt.Tag.Title).ToList();
            var           tagsToRemove     = tagTitlesCurrent.Except(tagTitles);

            foreach (var t in tagsToRemove)
            {
                post.PostTags.Remove(post.PostTags.Single(pt => pt.Tag.Title == t));
            }
            post.PostTags.Add(new PostTag {
                Post = post, Tag = tagJava
            });

            await _postRepo.UpdateAsync(post);

            // Assert: then the post's tags are updated
            var postAgain = await _postRepo.GetAsync(1, EPostType.BlogPost);

            Assert.Equal(2, postAgain.PostTags.Count());
            Assert.True(post.PostTags.ToList()[0].Tag.Slug == "cs");
            Assert.True(post.PostTags.ToList()[1].Tag.Slug == "java");
        }
Ejemplo n.º 3
0
        public async void UpdatePost_Can_Add_None_Tracked_Tag()
        {
            // Given 1 post with 2 tags
            Seed_1BlogPost_with_1Category_2Tags();
            // and a non-tracked tag
            var tagRepo = new SqlTagRepository(_db);
            var tag     = await tagRepo.CreateAsync(new Tag { Title = "Java", Slug = "java" });

            // Act: when user updates post by adding the non-tracked tag
            var post = _db.Set <Post>().Include(p => p.PostTags).Single(p => p.Slug == POST_SLUG);

            //post.PostTags.Add(new PostTag { Post = post, Tag = tagNonTracked });
            post.PostTags.Add(new PostTag {
                Post = post, Tag = tag
            });
            await _postRepo.UpdateAsync(post);

            // Assert: now post has 3 tags
            var postAgain = _db.Set <Post>().Include(p => p.PostTags).Single(p => p.Id == post.Id);

            Assert.Equal(3, postAgain.PostTags.Count());
        }