Exemple #1
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 Check11CreatePostOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap         = new DbSnapShot(db);
                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.OrderBy(x => x.TagId).Take(2).ToList().Select(x => x.TagId.ToString("D")).ToArray();
                var status = service.Create(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));
            }
        }
Exemple #3
0
        public void Test05AddressCreateViaServiceOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var setupService = new CreateSetupService(db);
                var service      = new CreateService(db);
                var lastCustomer = db.Customers.AsNoTracking().Include(x => x.CustomerAddresses).OrderByDescending(x => x.CustomerID).First();

                //ATTEMPT
                var dto =
                    setupService.GetDto <CrudCustomerAddressDto>()
                    .SetCustomerIdWhenCreatingNewEntry(lastCustomer.CustomerID);
                dto.AddressType           = "Unit Test";
                dto.Address.AddressLine1  = "Some street";
                dto.Address.AddressLine2  = Guid.NewGuid().ToString("D");
                dto.Address.City          = "some town";
                dto.Address.StateProvince = "a state";
                dto.Address.CountryRegion = "the world";
                dto.Address.PostalCode    = "XXX 111";
                var status = service.Create(dto);

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                var newLastCustomer = db.Customers.AsNoTracking().Include(x => x.CustomerAddresses.Select(y => y.Address)).OrderByDescending(x => x.CustomerID).First();
                newLastCustomer.CustomerAddresses.Count.ShouldEqual(lastCustomer.CustomerAddresses.Count + 1);
                newLastCustomer.CustomerAddresses.OrderByDescending(x => x.AddressID).First().Address.AddressLine2.ShouldEqual(dto.Address.AddressLine2);
            }
        }
Exemple #4
0
        public void Check08CreateWithListDtoBad()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var service = new CreateService <Post, SimplePostDto>(db);

                //ATTEMPT
                var dto    = new SimplePostDto();
                var status = service.Create(dto);
                dto.LogSpecificName("End");

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("Create of a new Post is not supported in this mode.");
            }
        }
Exemple #5
0
        //--------

        public static void CreatePostGSelectDirect(this SampleWebAppDb db, int id)
        {
            var guidString = Guid.NewGuid().ToString("N");
            var postClass  = new Post
            {
                Title   = guidString,
                Content = guidString,
                Blogger = db.Blogs.First(),
                Tags    = new Collection <Tag> {
                    db.Tags.First()
                }
            };

            var service = new CreateService(db);
            var status  = service.Create(postClass);

            status.IsValid.ShouldEqual(true, status.Errors);
        }
        //----------------------------------

        private CreateLineItemDto AddLineItem(AdventureWorksLt2012 db, short quantity = 1)
        {
            var service  = new CreateService(db);
            var newOrder = new CreateLineItemDto
            {
                SalesOrderID = _salesOrderId,
                CustomerID   = _customerId,
                ProductID    = _productToUse.ProductID,
                OrderQty     = quantity
            };

            //ATTEMPT
            var status = service.Create(newOrder);

            //VERIFY
            status.ShouldBeValid();
            return(newOrder);
        }
Exemple #7
0
        public ActionResult CreatePost(MainModel mainmodel)
        {
            try
            //Try code for exception handling of Database Errors
            {
                if (!ModelState.IsValid)
                {
                    var errors = ModelState.Select(x => x.Value.Errors).Where(y => y.Count > 0).ToList();
                    mainmodel.Errors = new List <string>();
                    foreach (var item in errors)
                    {
                        mainmodel.Errors.Add(item.FirstOrDefault().ErrorMessage);
                    }
                    return(PartialView("_ErrorView", mainmodel));
                }
                //Could also be before try if you know the exception occurs in SaveChanges
                _employeeDetailsService.Create(mainmodel);
            }

            //Catch code for exception handling of Database Errors
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(" The validation errors are: ", fullErrorMessage);
                mainmodel.Errors = new List <string>();

                mainmodel.Errors.Add(exceptionMessage);

                //return View(exceptionMessage);
                return(PartialView("_ErrorView", mainmodel));
            }


            //returns to Index page after saving data;
            return(Content("<script type=text/javascript>alert('Employee Details Added');window.location.href='/Home/Index'</script>"));
        }
        public void Check02CreateFailOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var service = new CreateService<Tag, SimpleTagDto>(db);
                var dto = new SimpleTagDto();
                dto.SetSupportedFunctions( CrudFunctions.None);

                //ATTEMPT
                var status = service.Create(dto);

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEndWith("is not supported in this mode.");
                dto.FunctionsCalledCommaDelimited.ShouldEqual("");
            }
        }
Exemple #9
0
        public void Check02CreateFailOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var service = new CreateService <Tag, SimpleTagDto>(db);
                var dto     = new SimpleTagDto();
                dto.SetSupportedFunctions(CrudFunctions.None);

                //ATTEMPT
                var status = service.Create(dto);

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEndWith("is not supported in this mode.");
                dto.FunctionsCalledCommaDelimited.ShouldEqual("");
            }
        }
Exemple #10
0
        public void Check02CreateFlow(InstrumentedOpFlags errorFlag, bool isValid, string expectedFunctionsCalled)
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var service = new CreateService <Tag, SimpleTagDto>(db);
                var dto     = new SimpleTagDto(errorFlag)
                {
                    Name = "Test Name",
                    Slug = Guid.NewGuid().ToString("N")
                };

                //ATTEMPT
                var status = service.Create(dto);

                //VERIFY
                status.IsValid.ShouldEqual(isValid);
                dto.FunctionsCalledCommaDelimited.ShouldEqual(expectedFunctionsCalled);
            }
        }
        public void FixtureSetup()
        {
            GenericServicesConfig.UseDelegateDecompilerWhereNeeded = true;

            //we create a SaleOrderHeader to use in these tests
            using (var db = new AdventureWorksLt2012())
            {
                _customerId = db.Customers.OrderBy(x => x.CustomerID).First().CustomerID;
                _productToUse = db.Products.OrderByDescending(x => x.ListPrice).First();
                var dto = new CreateSetupService(db).GetDto<NewOrderDto>();
                dto.CustomerID = _customerId;
                dto.ShipMethod = "Unit Test";
                dto.PurchaseOrderNumber = "Unit Test";
                dto.AccountNumber = "Unit Test";

                var service = new CreateService(db);
                var status = service.Create(dto);
                status.ShouldBeValid();
                _salesOrderId = dto.SalesOrderID;
            }
        }
        public void FixtureSetup()
        {
            GenericServicesConfig.UseDelegateDecompilerWhereNeeded = true;

            //we create a SaleOrderHeader to use in these tests
            using (var db = new AdventureWorksLt2012())
            {
                _customerId   = db.Customers.OrderBy(x => x.CustomerID).First().CustomerID;
                _productToUse = db.Products.OrderByDescending(x => x.ListPrice).First();
                var dto = new CreateSetupService(db).GetDto <NewOrderDto>();
                dto.CustomerID          = _customerId;
                dto.ShipMethod          = "Unit Test";
                dto.PurchaseOrderNumber = "Unit Test";
                dto.AccountNumber       = "Unit Test";

                var service = new CreateService(db);
                var status  = service.Create(dto);
                status.ShouldBeValid();
                _salesOrderId = dto.SalesOrderID;
            }
        }
        public void Check13CreateFailRunsSetupSecondaryDataAgainOk()
        {
            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 = null;                                 //this will fail
                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);

                //VERIFY
                status.IsValid.ShouldEqual(false);
                dto.Bloggers.KeyValueList.Count.ShouldEqual(db.Blogs.Count() + 1);
                dto.UserChosenTags.AllPossibleOptions.Count.ShouldEqual(db.Tags.Count());
            }
        }
Exemple #14
0
        public void Check11CreateDirectOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap               = new DbSnapShot(db);
                var service            = new CreateService(db);
                var firstPostUntracked = db.Posts.Include(x => x.Tags).AsNoTracking().First();
                var tagsTracked        = db.Tags.ToList().Where(x => firstPostUntracked.Tags.Any(y => y.TagId == x.TagId)).ToList();

                //ATTEMPT
                firstPostUntracked.Title = Guid.NewGuid().ToString();
                firstPostUntracked.Tags  = tagsTracked;
                var status = service.Create(firstPostUntracked);

                //VERIFY
                status.IsValid.ShouldEqual(true);
                snap.CheckSnapShot(db, 1, 2);
                var updatedPost = db.Posts.OrderByDescending(x => x.PostId).Include(x => x.Tags).First();
                updatedPost.Title.ShouldEqual(firstPostUntracked.Title);
                updatedPost.BlogId.ShouldEqual(firstPostUntracked.BlogId);
                CollectionAssert.AreEqual(firstPostUntracked.Tags.Select(x => x.TagId), updatedPost.Tags.Select(x => x.TagId));
            }
        }
        public void Check02CreateFlow(InstrumentedOpFlags errorFlag, bool isValid, string expectedFunctionsCalled)
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var service = new CreateService<Tag, SimpleTagDto>(db);
                var dto = new SimpleTagDto(errorFlag)
                {
                    Name = "Test Name",
                    Slug = Guid.NewGuid().ToString("N")
                };

                //ATTEMPT
                var status = service.Create(dto);

                //VERIFY
                status.IsValid.ShouldEqual(isValid);
                dto.FunctionsCalledCommaDelimited.ShouldEqual(expectedFunctionsCalled);
            }
        }  
        public void Check11CreateDirectOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var service = new CreateService(db);
                var firstPostUntracked = db.Posts.Include(x => x.Tags).AsNoTracking().First();
                var tagsTracked = db.Tags.ToList().Where(x => firstPostUntracked.Tags.Any(y => y.TagId == x.TagId)).ToList();

                //ATTEMPT
                firstPostUntracked.Title = Guid.NewGuid().ToString();
                firstPostUntracked.Tags = tagsTracked;
                var status = service.Create(firstPostUntracked);

                //VERIFY
                status.IsValid.ShouldEqual(true);
                snap.CheckSnapShot(db, 1, 2);
                var updatedPost = db.Posts.OrderByDescending(x => x.PostId).Include(x => x.Tags).First();
                updatedPost.Title.ShouldEqual(firstPostUntracked.Title);
                updatedPost.BlogId.ShouldEqual(firstPostUntracked.BlogId);
                CollectionAssert.AreEqual(firstPostUntracked.Tags.Select(x => x.TagId), updatedPost.Tags.Select(x => x.TagId));
            }
        }
        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 Check13CreateFailRunsSetupSecondaryDataAgainOk()
        {

            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 = null;                                 //this will fail 
                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);

                //VERIFY
                status.IsValid.ShouldEqual(false);
                dto.Bloggers.KeyValueList.Count.ShouldEqual(db.Blogs.Count() + 1);
                dto.UserChosenTags.AllPossibleOptions.Count.ShouldEqual(db.Tags.Count());
            }
        }
        public void Test05AddressCreateViaServiceOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var setupService = new CreateSetupService(db);
                var service = new CreateService(db);
                var lastCustomer = db.Customers.AsNoTracking().Include(x => x.CustomerAddresses).OrderByDescending(x => x.CustomerID).First();

                //ATTEMPT
                var dto =
                    setupService.GetDto<CrudCustomerAddressDto>()
                        .SetCustomerIdWhenCreatingNewEntry(lastCustomer.CustomerID);
                dto.AddressType = "Unit Test";
                dto.Address.AddressLine1 = "Some street";
                dto.Address.AddressLine2 = Guid.NewGuid().ToString("D");
                dto.Address.City = "some town";
                dto.Address.StateProvince = "a state";
                dto.Address.CountryRegion = "the world";
                dto.Address.PostalCode = "XXX 111";
                var status = service.Create(dto);

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                var newLastCustomer = db.Customers.AsNoTracking().Include( x => x.CustomerAddresses.Select( y => y.Address)).OrderByDescending(x => x.CustomerID).First();
                newLastCustomer.CustomerAddresses.Count.ShouldEqual(lastCustomer.CustomerAddresses.Count+1);
                newLastCustomer.CustomerAddresses.OrderByDescending(x => x.AddressID).First().Address.AddressLine2.ShouldEqual(dto.Address.AddressLine2);
            }
        }
        public virtual IActionResult Post([FromBody] TCreateModel dto)
        {
            var result = CreateService.Create(dto);

            return(Ok(result));
        }
        public void Check10CreateWithListDtoBad()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var service = new CreateService<Post, SimplePostDto>(db);

                //ATTEMPT
                var dto = new SimplePostDto();
                var status = service.Create(dto);
                dto.LogSpecificName("End");

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("Create of a new Post is not supported in this mode.");

            }
        }
        //----------------------------------
        private CreateLineItemDto AddLineItem(AdventureWorksLt2012 db, short quantity = 1)
        {
            var service = new CreateService(db);
            var newOrder = new CreateLineItemDto
            {
                SalesOrderID = _salesOrderId,
                CustomerID = _customerId,
                ProductID = _productToUse.ProductID,
                OrderQty = quantity
            };

            //ATTEMPT
            var status = service.Create(newOrder);

            //VERIFY
            status.ShouldBeValid();
            return newOrder;
        }
        public void Check12CreatePostCopyBackKeyOk()
        {

            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.OrderBy(x => x.TagId).Take(2).ToList().Select(x => x.TagId.ToString("D")).ToArray();
                var status = service.Create(dto);

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                var post = db.Posts.OrderByDescending(x => x.PostId).First();
                dto.PostId.ShouldEqual(post.PostId);
            }
        }