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 Check02XmlFileLoadBad() { //SETUP //ATTEMPT var ex = Assert.Throws <NullReferenceException>(() => LoadDbDataFromXml.FormBlogsWithPosts("badname.xml")); //VERIFY ex.Message.ShouldStartWith("Could not find the xml file you asked for."); }
public void Check01XmlFileLoadOk() { //SETUP //ATTEMPT var bloggers = LoadDbDataFromXml.FormBlogsWithPosts("DataLayer.Startup.Internal.BlogsContentSimple.xml").ToList(); //VERIFY bloggers.Count().ShouldEqual(2); bloggers.SelectMany(x => x.Posts).Count().ShouldEqual(3); bloggers.SelectMany(x => x.Posts.SelectMany(y => y.Tags)).Distinct().Count().ShouldEqual(3); }
public static void ResetDatabaseToTestData(SampleWebAppDb context, string filepathOfXmlFile) { context.Posts.RemoveRange(context.Posts); context.Tags.RemoveRange(context.Tags); context.Blogs.RemoveRange(context.Blogs); context.PostTagGrades.RemoveRange(context.PostTagGrades); context.PostLinks.RemoveRange(context.PostLinks); context.SaveChanges(); var loader = new LoadDbDataFromXml(filepathOfXmlFile); context.Blogs.AddRange(loader.Bloggers); //note: The order things appear in the database are not obvious //have to add these by hand context.PostTagGrades.AddRange(loader.PostTagGrades); context.SaveChanges(); }