Beispiel #1
0
        public static void FillComputedNPost(this SampleWebAppDb db, int totalOfEach)
        {
            //clear the current
            ClearDatabase(db);

            var tag1 = new Tag {
                Name = "Tag1", Slug = "tag1"
            };
            var tag2 = new Tag {
                Name = "Tag2", Slug = "tag2"
            };

            db.Tags.AddRange(new[] { tag1, tag2 });
            var blogger1 = new Blog()
            {
                Name = "Name1", EmailAddress = "Email1"
            };
            var blogger2 = new Blog()
            {
                Name = "Name2", EmailAddress = "Email2"
            };

            db.Blogs.AddRange(new[] { blogger1, blogger2 });
            var posts = BuildNPosts(tag2, blogger2, totalOfEach);

            db.Posts.AddRange(posts);

            var status = db.SaveChangesWithChecking();

            status.IsValid.ShouldEqual(true, status.Errors);
        }
Beispiel #2
0
        public static async Task DeletePostGenericDirectAsync(this SampleWebAppDb db, int postId)
        {
            var service = new DeleteServiceAsync(db);
            var status  = await service.DeleteAsync <Post>(postId);

            status.IsValid.ShouldEqual(true, status.Errors);
        }
Beispiel #3
0
        public async void Check06UpdateWithListDtoOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap         = new DbSnapShot(db);
                var firstPost    = db.Posts.Include(x => x.Tags).AsNoTracking().First();
                var service      = new UpdateServiceAsync <Post, SimplePostDtoAsync>(db);
                var setupService = new UpdateSetupServiceAsync <Post, SimplePostDtoAsync>(db);

                //ATTEMPT
                var setupStatus = await setupService.GetOriginalAsync(firstPost.PostId);

                setupStatus.IsValid.ShouldEqual(true, setupStatus.Errors);
                setupStatus.Result.Title = Guid.NewGuid().ToString();
                var status = await service.UpdateAsync(setupStatus.Result);

                setupStatus.Result.LogSpecificName("End");

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                status.SuccessMessage.ShouldEqual("Successfully updated Post.");
                snap.CheckSnapShot(db);
            }
        }
        public static void ResetBlogs(SampleWebAppDb context, TestDataSelection selection)
        {
            try
            {
                context.Posts.ToList().ForEach(x => context.Posts.Remove(x));
                context.Tags.ToList().ForEach(x => context.Tags.Remove(x));
                context.Blogs.ToList().ForEach(x => context.Blogs.Remove(x));
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                _logger.Critical("Exception when resetting the blogs", ex);
                throw;
            }

            var bloggers = LoadDbDataFromXml.FormBlogsWithPosts(XmlBlogsDataFileManifestPath[selection]);

            context.Blogs.AddRange(bloggers);
            var status = context.SaveChangesWithChecking();

            if (!status.IsValid)
            {
                _logger.CriticalFormat("Error when resetting courses data. Error:\n{0}",
                                       string.Join(",", status.Errors));
                throw new FormatException("problem writing to database. See log.");
            }
        }
        public void Check06UpdateWithKeyPropertiesInWrongOrderOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap         = new DbSnapShot(db);
                var firstGrade   = db.PostTagGrades.Include(x => x.TagPart).Include(x => x.PostPart).First();
                var service      = new UpdateService <PostTagGrade, SimpleTagPostGradeDto>(db);
                var setupService = new UpdateSetupService <PostTagGrade, SimpleTagPostGradeDto>(db);

                //ATTEMPT
                var setupStatus = setupService.GetOriginal(firstGrade.PostId, firstGrade.TagId);
                setupStatus.IsValid.ShouldEqual(true, setupStatus.Errors);
                setupStatus.Result.Grade = 999;
                var status = service.Update(setupStatus.Result);
                setupStatus.Result.LogSpecificName("End");

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                status.SuccessMessage.ShouldEqual("Successfully updated PostTagGrade.");
                snap.CheckSnapShot(db);
                var updatedfirstGrade = db.PostTagGrades.First();
                updatedfirstGrade.Grade.ShouldEqual(999);
            }
        }
        public void Check05CauseBothErrorsBad()
        {
            //NOTE: To test this I needed to comment out the ValidateEntity method in SampleWebAppDb

            var tagGuid = Guid.NewGuid().ToString("N");

            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var post = db.Posts.First();
                db.PostLinks.Add(new PostLink {
                    PostPart = post
                });
                db.Tags.Add(new Tag {
                    Name = tagGuid, Slug = tagGuid
                });
                db.SaveChanges();
            }

            using (var db = new SampleWebAppDb())
            {
                //ATTEMPT
                db.Posts.Remove(db.Posts.First());
                db.Tags.Add(new Tag {
                    Name = tagGuid, Slug = tagGuid
                });
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);         //for these two cases we only get one error
                status.Errors[0].ErrorMessage.ShouldEqual("One of the properties is marked as Unique index and there is already an entry with that value.");
            }
        }
        public void Check07UpdateWithListDtoCheckDateOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap             = new DbSnapShot(db);
                var firstPost        = db.Posts.Include(x => x.Tags).AsNoTracking().First();
                var originalDateTime = firstPost.LastUpdated;
                Thread.Sleep(400);

                var service      = new UpdateService <Post, SimplePostDto>(db);
                var setupService = new UpdateSetupService <Post, SimplePostDto>(db);

                //ATTEMPT
                var setupStatus = setupService.GetOriginal(firstPost.PostId);
                setupStatus.Result.Title = Guid.NewGuid().ToString();
                var status = service.Update(setupStatus.Result);
                setupStatus.Result.LogSpecificName("End");

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                status.SuccessMessage.ShouldEqual("Successfully updated Post.");
                snap.CheckSnapShot(db);
                Assert.GreaterOrEqual(db.Posts.First().LastUpdated.Subtract(originalDateTime).Milliseconds, 400);
            }
        }
Beispiel #8
0
        public async void Check07UpdateDirectPostCorrectOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var firstPostUntrackedNoIncludes   = db.Posts.AsNoTracking().First();
                var firstPostUntrackedWithIncludes = db.Posts.AsNoTracking().Include(x => x.Tags).First();
                var service = new UpdateServiceAsync <Post>(db);

                //ATTEMPT
                firstPostUntrackedNoIncludes.Title = Guid.NewGuid().ToString();
                var status = await service.UpdateAsync(firstPostUntrackedNoIncludes);

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db);
                var updatedPost = db.Posts.Include(x => x.Tags).First();
                updatedPost.Title.ShouldEqual(firstPostUntrackedNoIncludes.Title);
                updatedPost.Content.ShouldEqual(firstPostUntrackedWithIncludes.Content);
                updatedPost.Blogger.ShouldNotEqualNull();
                updatedPost.Blogger.Name.ShouldEqual(firstPostUntrackedWithIncludes.Blogger.Name);
                CollectionAssert.AreEqual(firstPostUntrackedWithIncludes.Tags.Select(x => x.TagId), updatedPost.Tags.Select(x => x.TagId));
            }
        }
Beispiel #9
0
 public DbSnapShot(SampleWebAppDb db)
 {
     NumBlogs        = db.Blogs.Count();
     NumPostTagLinks = db.Database.SqlQuery <int>("SELECT COUNT(*) FROM dbo.TagPosts").First();
     NumPosts        = db.Posts.Count();
     NumTags         = db.Tags.Count();
 }
Beispiel #10
0
        public void Perf22UpdatePostAddTagOk()
        {
            int postId;

            using (var db = new SampleWebAppDb())
                postId = db.Posts.Include(x => x.Tags).AsNoTracking().First().PostId;

            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var setupService  = new UpdateSetupService <Post, DetailPostDto>(db);
                var updateService = new UpdateService <Post, DetailPostDto>(db);

                //ATTEMPT
                var setupStatus = setupService.GetOriginal(postId);
                setupStatus.Result.Title = Guid.NewGuid().ToString();
                setupStatus.Result.Bloggers.SelectedValue        = db.Blogs.First().BlogId.ToString("D");
                setupStatus.Result.UserChosenTags.FinalSelection = db.Tags.Take(3).ToList().Select(x => x.TagId.ToString("D")).ToArray();
                var status = updateService.Update(setupStatus.Result);
                setupStatus.Result.LogSpecificName("End");

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                foreach (var log in setupStatus.Result.LogOfCalls)
                {
                    Console.WriteLine(log);
                }
            }
        }
Beispiel #11
0
        public void Perf11CreatePostOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP

                var service      = new CreateService <Post, DetailPostDto>(db);
                var setupService = new CreateSetupService <Post, DetailPostDto>(db);

                //ATTEMPT
                var dto = setupService.GetDto();
                dto.Title   = Guid.NewGuid().ToString();
                dto.Content = "something to fill it as can't be empty";
                dto.Bloggers.SelectedValue        = db.Blogs.First().BlogId.ToString("D");
                dto.UserChosenTags.FinalSelection = db.Tags.Take(2).ToList().Select(x => x.TagId.ToString("D")).ToArray();
                var status = service.Create(dto);
                dto.LogSpecificName("End");

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                foreach (var log in dto.LogOfCalls)
                {
                    Console.WriteLine(log);
                }
            }
        }
        public void Check02UniqueKeyErrorBad()
        {
            //NOTE: To test this I needed to comment out the ValidateEntity method in SampleWebAppDb

            var tagGuid = Guid.NewGuid().ToString("N");

            using (var db = new SampleWebAppDb())
            {
                //SETUP
                db.Tags.Add(new Tag {
                    Name = tagGuid, Slug = tagGuid
                });
                db.SaveChanges();
            }

            using (var db = new SampleWebAppDb())
            {
                //ATTEMPT
                db.Tags.Add(new Tag {
                    Name = tagGuid, Slug = tagGuid
                });
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("One of the properties is marked as Unique index and there is already an entry with that value.");
            }
        }
        public async void Perf06UpdateWithListDtoOk()
        {
            int postId;

            using (var db = new SampleWebAppDb())
                postId = db.Posts.Include(x => x.Tags).AsNoTracking().First().PostId;

            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var service      = new UpdateServiceAsync <Post, SimplePostDtoAsync>(db);
                var setupService = new UpdateSetupServiceAsync <Post, SimplePostDtoAsync>(db);

                //ATTEMPT
                var setupStatus = await setupService.GetOriginalAsync(postId);

                setupStatus.Result.Title = Guid.NewGuid().ToString();
                var status = await service.UpdateAsync(setupStatus.Result);

                setupStatus.Result.LogSpecificName("End");

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                foreach (var log in setupStatus.Result.LogOfCalls)
                {
                    Console.WriteLine(log);
                }
            }
        }
Beispiel #14
0
        public void Check21ValidatePostContentTwoErrors()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var existingTag     = db.Tags.First();
                var existingBlogger = db.Blogs.First();

                //ATTEMPT
                var newPost = new Post()
                {
                    Blogger = existingBlogger,
                    Title   = "Test post",
                    Content = "Should not end sentence with sheep. Nor end sentence with lamb.",
                    Tags    = new[] { existingTag }
                };
                db.Posts.Add(newPost);
                var status = db.SaveChangesWithChecking();;

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(2);
                status.Errors[0].ErrorMessage.ShouldEqual("Sorry. Not allowed to end a sentance with 'sheep'.");
                status.Errors[1].ErrorMessage.ShouldEqual("Sorry. Not allowed to end a sentance with 'lamb'.");
            }
        }
Beispiel #15
0
        public void Check16ValidatePostTitleError()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var existingTag     = db.Tags.First();
                var existingBlogger = db.Blogs.First();

                //ATTEMPT
                var newPost = new Post()
                {
                    Blogger = existingBlogger,
                    Title   = "Test post?",
                    Content = "Nothing special",
                    Tags    = new[] { existingTag }
                };
                db.Posts.Add(newPost);
                var status = db.SaveChangesWithChecking();;

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("Sorry, but you can't ask a question, i.e. the title can't end with '?'.");
            }
        }
        public async void Check11CreatePostOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap         = new DbSnapShot(db);
                var service      = new CreateServiceAsync <Post, DetailPostDtoAsync>(db);
                var setupService = new CreateSetupServiceAsync <Post, DetailPostDtoAsync>(db);

                //ATTEMPT
                var dto = await setupService.GetDtoAsync();

                dto.Title   = Guid.NewGuid().ToString();
                dto.Content = "something to fill it as can't be empty";
                dto.Bloggers.SelectedValue        = db.Blogs.First().BlogId.ToString("D");
                dto.UserChosenTags.FinalSelection = db.Tags.OrderBy(x => x.TagId).Take(2).ToList().Select(x => x.TagId.ToString("D")).ToArray();
                var status = await service.CreateAsync(dto);

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, 1, 2);
                var post = db.Posts.Include(x => x.Tags).OrderByDescending(x => x.PostId).First();
                post.Title.ShouldEqual(dto.Title);
                post.BlogId.ShouldEqual(db.Blogs.First().BlogId);
                CollectionAssert.AreEqual(db.Tags.OrderBy(x => x.TagId).Take(2).Select(x => x.TagId), post.Tags.Select(x => x.TagId));
            }
        }
        public void Check20AddPostOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);

                //ATTEMPT
                var uglyTag    = db.Tags.Single(x => x.Slug == "ugly");
                var jonBlogger = db.Blogs.First();
                var newPost    = new Post
                {
                    Blogger = jonBlogger,
                    Content = "a few simple words.",
                    Title   = "A new post",
                    Tags    = new[] { uglyTag }
                };

                db.Posts.Add(newPost);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, 1, 1);
                var uglyPosts = db.Tags.Include(x => x.Posts).Single(y => y.Slug == "ugly").Posts;
                uglyPosts.Count.ShouldEqual(3);
            }
        }
Beispiel #18
0
        public void Check10ValidatePostOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap            = new DbSnapShot(db);
                var existingTag     = db.Tags.First();
                var existingBlogger = db.Blogs.First();

                //ATTEMPT
                var newPost = new Post()
                {
                    Blogger = existingBlogger,
                    Title   = "Test post",
                    Content = "Nothing special",
                    Tags    = new[] { existingTag }
                };
                db.Posts.Add(newPost);
                var status = db.SaveChangesWithChecking();;

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, 1, 1);
            }
        }
        public async void Check22UpdatePostAddTagOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap          = new DbSnapShot(db);
                var setupService  = new UpdateSetupServiceAsync <Post, DetailPostDtoAsync>(db);
                var updateService = new UpdateServiceAsync <Post, DetailPostDtoAsync>(db);
                var firstPost     = db.Posts.First();

                //ATTEMPT
                var setupStatus = await setupService.GetOriginalAsync(firstPost.PostId);

                setupStatus.IsValid.ShouldEqual(true, setupStatus.Errors);
                setupStatus.Result.Title = Guid.NewGuid().ToString();
                setupStatus.Result.Bloggers.SelectedValue        = db.Blogs.First().BlogId.ToString("D");
                setupStatus.Result.UserChosenTags.FinalSelection = db.Tags.Take(3).ToList().Select(x => x.TagId.ToString("D")).ToArray();
                var status = await updateService.UpdateAsync(setupStatus.Result);

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, 0, 1);
                var post = db.Posts.Include(x => x.Tags).Single(x => x.PostId == firstPost.PostId);
                post.Title.ShouldEqual(setupStatus.Result.Title);
                post.BlogId.ShouldEqual(db.Blogs.First().BlogId);
                CollectionAssert.AreEquivalent(db.Tags.Take(3).Select(x => x.TagId), post.Tags.Select(x => x.TagId));
            }
        }
Beispiel #20
0
        private string ChangeTagsBasedOnMultiSelectList(SampleWebAppDb db, Post post = null)
        {
            var requiredTagIds = UserChosenTags.GetFinalSelectionAsInts();

            if (!requiredTagIds.Any())
            {
                return("You must select at least one tag for the post.");
            }

            if (requiredTagIds.Any(x => db.Tags.Find(x) == null))
            {
                return("Could not find one of the tags. Did another user delete it?");
            }

            if (post != null)
            {
                //This is an update so we need to load the tags
                db.Entry(post).Collection(p => p.Tags).Load();
            }

            var newTagsForPost = db.Tags.Where(x => requiredTagIds.Contains(x.TagId)).ToList();

            Tags = newTagsForPost;      //will be copied over by copyDtoToData

            return(null);
        }
Beispiel #21
0
        public void Check02DatabaseDataLinksOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                DataLayerInitialise.InitialiseThis();
                var filepath = TestFileHelpers.GetTestFileFilePath("DbContentSimple.xml");

                //ATTEMPT
                DataLayerInitialise.ResetDatabaseToTestData(db, filepath);

                //VERIFY
                var allPosts = db.Posts.Include(x => x.Blogger).Include(x => x.Tags).ToList();
                allPosts[0].Blogger.Name.ShouldEqual("Fred Bloggs");
                string.Join(",", allPosts[0].Tags.Select(x => x.Slug)).ShouldEqual("ugly,bad");
                allPosts[1].Blogger.Name.ShouldEqual("Jon Smith");
                string.Join(",", allPosts[1].Tags.Select(x => x.Slug)).ShouldEqual("good,ugly");
                allPosts[2].Blogger.Name.ShouldEqual("Jon Smith");
                string.Join(",", allPosts[2].Tags.Select(x => x.Slug)).ShouldEqual("bad");

                db.PostTagGrades.Count().ShouldEqual(2);
                db.PostTagGrades.ToList().All(x => x.PostId == allPosts[0].PostId).ShouldEqual(true);
                string.Join(",", db.PostTagGrades.Include(x => x.TagPart).Select(x => x.TagPart.Slug)).ShouldEqual("bad,ugly");
            }
        }
Beispiel #22
0
        public async void Check08UpdateWithListDtoCorrectOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var firstPost    = db.Posts.Include(x => x.Tags).AsNoTracking().First();
                var service      = new UpdateServiceAsync <Post, SimplePostDtoAsync>(db);
                var setupService = new UpdateSetupServiceAsync <Post, SimplePostDtoAsync>(db);

                //ATTEMPT
                var setupStatus = await setupService.GetOriginalAsync(firstPost.PostId);

                setupStatus.IsValid.ShouldEqual(true, setupStatus.Errors);
                setupStatus.Result.Title = Guid.NewGuid().ToString();
                var status = await service.UpdateAsync(setupStatus.Result);

                setupStatus.Result.LogSpecificName("End");

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                var updatedPost = db.Posts.Include(x => x.Tags).First();
                updatedPost.Title.ShouldEqual(setupStatus.Result.Title);
                updatedPost.Blogger.ShouldNotEqualNull();
                CollectionAssert.AreEqual(firstPost.Tags.Select(x => x.TagId), updatedPost.Tags.Select(x => x.TagId));
            }
        }
Beispiel #23
0
        //--------------------------

        public static void DeletePostGenericDirect(this SampleWebAppDb db, int postId)
        {
            var service = new DeleteService(db);
            var status  = service.Delete <Post>(postId);

            status.IsValid.ShouldEqual(true, status.Errors);
        }
Beispiel #24
0
 private static int ResetDatabaseNPost(int numToPutInDatabase)
 {
     using (var db = new SampleWebAppDb())
     {
         db.FillComputedNPost(numToPutInDatabase);
         return(db.Posts.Min(x => x.PostId));
     }
 }
Beispiel #25
0
 public void SetUpFixture()
 {
     using (var db = new SampleWebAppDb())
     {
         DataLayerInitialise.InitialiseThis(false, true);
         DataLayerInitialise.ResetBlogs(db, TestDataSelection.Small);
     }
 }
Beispiel #26
0
 private static void ClearDatabase(SampleWebAppDb db)
 {
     db.Posts.RemoveRange(db.Posts);
     db.Tags.RemoveRange(db.Tags);
     db.Blogs.RemoveRange(db.Blogs);
     db.PostTagGrades.RemoveRange(db.PostTagGrades);
     db.SaveChanges();
 }
 public void SetUpFixture()
 {
     using (var db = new SampleWebAppDb())
     {
         DataLayerInitialise.InitialiseThis();
         var filepath = TestFileHelpers.GetTestFileFilePath("DbContentSimple.xml");
         DataLayerInitialise.ResetDatabaseToTestData(db, filepath);
     }
 }
Beispiel #28
0
        public void CheckSnapShot(SampleWebAppDb db, int postsChange = 0, int postTagLinkChange = 0, int blogsChange = 0, int tagsChange = 0)
        {
            var newSnap = new DbSnapShot(db);

            newSnap.NumPosts.ShouldEqual(NumPosts + postsChange, "posts wrong");
            newSnap.NumPostTagLinks.ShouldEqual(NumPostTagLinks + postTagLinkChange, "posttaglinks wrong");
            newSnap.NumBlogs.ShouldEqual(NumBlogs + blogsChange, "blogs wrong");
            newSnap.NumTags.ShouldEqual(NumTags + tagsChange, "tags wrong");
        }
Beispiel #29
0
 static void Main(string[] args)
 {
     using (var db = new SampleWebAppDb())
     {
         var firstId = db.Posts.First().PostId;
         RunCommand1(db);
         RunCommand2(db);
     }
 }
Beispiel #30
0
 public static async Task ListPostEfViaDtoAsync(this SampleWebAppDb db, int id)
 {
     var list = await db.Set <Post>().AsNoTracking().Select(x => new SimplePostDto
     {
         PostId      = x.PostId,
         BloggerName = x.Blogger.Name,
         Title       = x.Title,
         Tags        = x.Tags,
         LastUpdated = x.LastUpdated
     }).ToListAsync();
 }