Exemplo n.º 1
0
        public void RepositoryUpdate()
        {
            //arrange
            var connection = DbConnectionFactory.CreateTransient();
            var stubDB     = new GroceryStoreContext(connection);
            var userRepo   = new EfGenericRepository <User>(stubDB);

            var user = new User
            {
                FirstName   = "Pesho",
                LastName    = "Pesho",
                Username    = "******",
                Password    = "******",
                PhoneNumber = "Pesho"
            };

            userRepo.Add(user);
            stubDB.SaveChanges();

            //act
            user.Password = "******";
            userRepo.Update(user);

            string actual = userRepo.GetById(1).Password;

            //assert
            Assert.AreEqual("newPassword", actual);
        }
Exemplo n.º 2
0
        public void RepositoryGetById()
        {
            //arrange
            var connection = DbConnectionFactory.CreateTransient();
            var stubDB     = new GroceryStoreContext(connection);
            var userRepo   = new EfGenericRepository <User>(stubDB);

            var userToAdd = new User
            {
                FirstName   = "Pesho",
                LastName    = "Pesho",
                Username    = "******",
                Password    = "******",
                PhoneNumber = "Pesho"
            };

            userRepo.Add(userToAdd);
            stubDB.SaveChanges();

            //act
            var actual = userRepo.GetById(1);

            //assert
            Assert.AreEqual(userToAdd.Username, actual.Username);
        }
        // Seed database through GroceryStoreAPI project's Startup class
        // TODO: Find a better way to seed database
        public static void SeedDatabase()
        {
            using (StreamReader r = new StreamReader("database.json"))
            {
                string json     = r.ReadToEnd();
                var    jsonData = JsonConvert.DeserializeObject <RootObject>(json);

                using (var dataContext = new GroceryStoreContext())
                {
                    foreach (Customer customer in jsonData.customers)
                    {
                        dataContext.Customers.Add(customer);
                    }
                    foreach (Order order in jsonData.orders)
                    {
                        dataContext.Orders.Add(order);
                    }
                    foreach (Product product in jsonData.products)
                    {
                        dataContext.Products.Add(product);
                    }
                    dataContext.SaveChanges();
                }
            }
        }
 public static void UpdateCustomer(Customer customer)
 {
     using (var dataContext = new GroceryStoreContext())
     {
         dataContext.Customers.Update(customer);
         dataContext.SaveChanges();
     }
 }
 public static void UpdateProduct(Product product)
 {
     using (var dataContext = new GroceryStoreContext())
     {
         dataContext.Products.Update(product);
         dataContext.SaveChanges();
     }
 }
 public static int AddProduct(Product product)
 {
     using (var dataContext = new GroceryStoreContext())
     {
         dataContext.Products.Add(product);
         dataContext.SaveChanges();
         return(product.id);
     }
 }
        public static int AddCustomer(string customerName)
        {
            var customer = new Customer();

            customer.name = customerName;
            using (var dataContext = new GroceryStoreContext())
            {
                dataContext.Customers.Add(customer);
                dataContext.SaveChanges();
                return(customer.id);
            }
        }
Exemplo n.º 8
0
        public void RepositoryAll()
        {
            //arrange
            var connection = DbConnectionFactory.CreateTransient();
            var stubDB     = new GroceryStoreContext(connection);
            var userRepo   = new EfGenericRepository <User>(stubDB);

            var userToAdd = new User
            {
                FirstName   = "Pesho",
                LastName    = "Pesho",
                Username    = "******",
                Password    = "******",
                PhoneNumber = "Pesho"
            };
            var userToAdd1 = new User
            {
                FirstName   = "Pesho1",
                LastName    = "Pesho1",
                Username    = "******",
                Password    = "******",
                PhoneNumber = "Pesho1"
            };
            var userToAdd2 = new User
            {
                FirstName   = "Pesho2",
                LastName    = "Pesho2",
                Username    = "******",
                Password    = "******",
                PhoneNumber = "Pesho2"
            };

            stubDB.Users.Add(userToAdd);
            stubDB.Users.Add(userToAdd1);
            stubDB.Users.Add(userToAdd2);
            stubDB.SaveChanges();

            //act
            var allUsers = userRepo.All;

            //assert
            Assert.AreEqual(3, allUsers.Count());
            Assert.IsInstanceOfType(allUsers, typeof(IQueryable));
        }
Exemplo n.º 9
0
        public void RepositoryAddEntity()
        {
            //arrange
            var connection = DbConnectionFactory.CreateTransient();
            var stubDB     = new GroceryStoreContext(connection);
            var userRepo   = new EfGenericRepository <User>(stubDB);

            var userToAdd = new User
            {
                FirstName   = "Pesho",
                LastName    = "Pesho",
                Username    = "******",
                Password    = "******",
                PhoneNumber = "Pesho"
            };

            //act
            userRepo.Add(userToAdd);
            stubDB.SaveChanges();

            //assert
            Assert.AreEqual(1, stubDB.Users.Count());
        }
Exemplo n.º 10
0
        public void RepositoryAddCollection()
        {
            //arrange
            var connection = DbConnectionFactory.CreateTransient();
            var stubDB     = new GroceryStoreContext(connection);
            var userRepo   = new EfGenericRepository <User>(stubDB);

            var user1 = new User
            {
                FirstName   = "Pesho",
                LastName    = "Peshev",
                Username    = "******",
                Password    = "******",
                PhoneNumber = "12345678"
            };

            var user2 = new User
            {
                FirstName   = "Gosho",
                LastName    = "Goshev",
                Username    = "******",
                Password    = "******",
                PhoneNumber = "09876543"
            };

            var collection = new List <User>()
            {
                user1, user2
            };

            //act
            userRepo.Add(collection);
            stubDB.SaveChanges();

            //assert
            Assert.AreEqual(2, stubDB.Users.Count());
        }