Example #1
0
        //Arrange
        public void LoginProperlyReturnsACustomerType()
        {
            var options = new DbContextOptionsBuilder <SGDB2Context>()
                          .UseInMemoryDatabase(databaseName: "SGDB2")
                          .Options;

            using (var context = new SGDB2Context(options))
            {
                using (var sw = new StringWriter())
                {
                    using (var sr = new StringReader("abe\nyup"))
                    {
                        context.Add(new Customers
                        {
                            UserName = "******",
                            Pword    = "yup"
                        });

                        context.SaveChanges();

                        //Act
                        Console.SetOut(sw);
                        Console.SetIn(sr);
                        Customers    customer     = new Customers();
                        StoreMethods storeMethods = new StoreMethods();
                        customer = storeMethods.Login(context);


                        //Assert
                        Assert.IsType <Customers>(customer);
                    }
                }
            }
        }//End Test 1
Example #2
0
        //Arrange
        public void SearchCustomerReturnsCustomerName()
        {
            var options = new DbContextOptionsBuilder <SGDB2Context>()
                          .UseInMemoryDatabase(databaseName: "SGDB2")
                          .Options;

            using (var context = new SGDB2Context(options))
            {
                using (var sw = new StringWriter())
                {
                    using (var sr = new StringReader("a"))
                    {
                        Customers custy = new Customers {
                            FirstName = "al", LastName = "ali"
                        };
                        context.Add(custy);
                        context.SaveChanges();

                        //Act
                        Console.SetOut(sw);
                        Console.SetIn(sr);
                        Customers    customer     = new Customers();
                        StoreMethods storeMethods = new StoreMethods();
                        storeMethods.SearchForUser(context);


                        //Assert
                        Assert.Contains("al\r\n", sw.ToString());
                    }
                }
            }
        }//Test 9
        /// <summary>
        /// Takes the user input and updates the customers default store in db and cache
        /// </summary>
        /// <param name="storeSelection"></param>
        /// <returns>Updated customer object</returns>
        public IActionResult SelectDefaultStore(Stores store)
        {
            int storeSelection = store.StoreId;
            //Update the default store in the cache
            var loggedIn = _cache.Get("loggedInCustomer");

            loggedInCustomer = (Customers)loggedIn;
            loggedInCustomer.DefaultStore = storeSelection;
            _cache.Set("loggedInCustomer", loggedInCustomer);
            _logger.LogInformation("Logged in customer updated on cache");

            //Update the default store in the database
            var dbCust = StoreMethods.SetDefaultStore(_db, loggedInCustomer);

            dbCust.DefaultStore = storeSelection;

            _db.SaveChanges();
            _logger.LogInformation("Logged in customer updated on database");

            return(View("_StoreMenu"));
        }
        public void SelectListOfStoreCreatesPopulatedList()
        {
            var options = new DbContextOptionsBuilder <SGDB2Context>()
                          .UseInMemoryDatabase(databaseName: "SGDB2")
                          .Options;

            using (var context = new SGDB2Context(options))
            {
                Stores store = new Stores();
                context.Stores.Add(store);
                context.SaveChanges();
                var list    = StoreMethods.SelectListOfStores(context);
                int isEmpty = list.Count;
                Assert.True(isEmpty > 0);
            }
        }//12
        public void SelectAllProductsReturnsPopulatedList()
        {
            var options = new DbContextOptionsBuilder <SGDB2Context>()
                          .UseInMemoryDatabase(databaseName: "SGDB2")
                          .Options;

            using (var context = new SGDB2Context(options))
            {
                Products item1 = new Products {
                };
                context.Products.Add(item1);
                context.SaveChanges();
                var list    = StoreMethods.GetAllProducts(context);
                int isEmpty = list.Count;
                Assert.True(isEmpty > 0);
            }
        }//20
        public void SelectDefaultStoreReturnsACustomerType()
        {
            var options = new DbContextOptionsBuilder <SGDB2Context>()
                          .UseInMemoryDatabase(databaseName: "SGDB2")
                          .Options;

            using (var context = new SGDB2Context(options))
            {
                Customers loggedIn = new Customers {
                    FirstName = "kk", LastName = "kk", CustomerId = 1, DefaultStore = 5
                };
                context.Customers.Add(loggedIn);
                context.SaveChanges();
                var result = StoreMethods.SetDefaultStore(context, loggedIn);

                Assert.IsType <Customers>(result);
            }
        }//13
        public void GetCustomerIDReturnsPopulatedList()
        {
            var options = new DbContextOptionsBuilder <SGDB2Context>()
                          .UseInMemoryDatabase(databaseName: "SGDB2")
                          .Options;

            using (var context = new SGDB2Context(options))
            {
                Customers customer = new Customers {
                    CustomerId = 200
                };
                context.Customers.Add(customer);
                context.SaveChanges();
                List <int> result  = StoreMethods.GetCustomerIDs(context);
                int        isEmpty = result.Count;
                Assert.True(result.Count > 0);
            }
        }//7
        public void SearchCustomerReturnsPopulatedListOfCustomers()
        {
            var options = new DbContextOptionsBuilder <SGDB2Context>()
                          .UseInMemoryDatabase(databaseName: "SGDB2")
                          .Options;

            using (var context = new SGDB2Context(options))
            {
                Customers customer = new Customers {
                    FirstName = "aaron", LastName = "aaronson"
                };
                context.Customers.Add(customer);
                context.SaveChanges();

                List <Customers> result = StoreMethods.GetSearchResults("a", context);
                int isEmpty             = result.Count;
                Assert.False(isEmpty == 0);
            }
        }//3
Example #9
0
        //Arrange
        public void StoreSelectionThrowsExceptionOnNull()
        {
            var options = new DbContextOptionsBuilder <SGDB2Context>()
                          .UseInMemoryDatabase(databaseName: "SGDB2")
                          .Options;

            using (var context = new SGDB2Context(options))
            {
                using (var sw = new StringWriter())
                {
                    using (var sr = new StringReader("0\n"))
                    {
                        Customers custy = new Customers
                        {
                            UserName     = "******",
                            Pword        = "yup",
                            DefaultStore = 1
                        };
                        context.Add(custy);
                        context.SaveChanges();

                        //Act
                        bool thrown = false;
                        try
                        {
                            Console.SetOut(sw);
                            Console.SetIn(sr);
                            Customers    customer     = new Customers();
                            StoreMethods storeMethods = new StoreMethods();
                            storeMethods.SetSelectedStore(customer, context);
                        }
                        catch (NullReferenceException)
                        {
                            thrown = true;
                        }
                        //Assert
                        Assert.True(thrown);
                        sw.Close();
                    }
                }
            }
        }//Test 10
Example #10
0
        public IActionResult SignUpCustomer([Bind("FirstName,LastName,UserName,Pword, City, State, Street, Zip, AptNum, DefaultStore")] Customers customer)
        {
            foreach (var cust in _db.Customers)
            {
                if (customer.UserName == cust.UserName)
                {
                    ModelState.AddModelError("Username", "Username is taken");
                    return(View("_SignUpCustomer"));
                }
            }
            if (customer.FirstName == null)
            {
                ModelState.AddModelError("FirstName", "Entry can't be empty");
                return(View("_SignUpCustomer"));
            }
            else if (customer.LastName == null)
            {
                ModelState.AddModelError("LastName", "Entry can't be empty");
                return(View("_SignUpCustomer"));
            }
            else if (customer.UserName == null)
            {
                ModelState.AddModelError("UserName", "Entry can't be empty");
                return(View("_SignUpCustomer"));
            }
            else if (customer.Pword == null)
            {
                ModelState.AddModelError("Pword", "Entry can't be empty");
                return(View("_SignUpCustomer"));
            }
            else if (customer.City == null)
            {
                ModelState.AddModelError("City", "Entry can't be empty");
                return(View("_SignUpCustomer"));
            }
            else if (customer.State == null)
            {
                ModelState.AddModelError("State", "Entry can't be empty");
                return(View("_SignUpCustomer"));
            }
            else if (customer.Street == null)
            {
                ModelState.AddModelError("Street", "Entry can't be empty");
                return(View("_SignUpCustomer"));
            }
            else if (customer.Zip <= 0 || customer.Zip > 99999)
            {
                ModelState.AddModelError("Zip", "Invalid entry");
                return(View("_SignUpCustomer"));
            }
            else
            {
                _cache.Set("loggedInCustomer", customer); //Add as currentlylogged in for future use
                _db.Customers.Add(customer);              //Add the new customer to the db
                _db.SaveChanges();

                return(View("_StoreMenu"));
            }

            //var custId = StoreMethods.GetCustomerIDs(_db);
            //customer.CustomerId = StoreMethods.GetNewCustID(custId);
        }