Пример #1
0
        public void Delete_Customer()
        {
            int result;
            int id;

            using (var context = CreateDbContext("Delete_Customer"))
            {
                var createdCustomer = new Customer
                {
                    Name    = "nurgul",
                    Surname = "dereli"
                };
                context.Set <Customer>().Add(createdCustomer);
                context.Set <Customer>().Add(new Customer {
                    Name = "Another Name", Surname = "Another Surname"
                });
                context.SaveChangesAsync();
                id = createdCustomer.Id;
            }

            using (var context = CreateDbContext("Delete_Customer"))
            {
                var repository = new RepositoryCustomer(new EfRepository(context));
                var customer   = repository.GetById(id);
                repository.Delete(customer);
            }

            // Simulate access from another context to verifiy that correct data was saved to database
            using (var context = CreateDbContext("Delete_Customer"))
            {
                (context.Set <Customer>().FirstOrDefault(x => x.Id == id)).Should().BeNull();
                context.Set <Customer>().ToList().Should().NotBeEmpty();
            }
        }
Пример #2
0
        public void GetById_nonexisting_customers(int id)
        {
            Customer customer = null;

            using (var context = CreateDbContext("GetById_nonexisting_customers"))
            {
                var repository = new RepositoryCustomer(new EfRepository(context));
                customer = repository.GetById(id);
            }

            customer.Should().BeNull();
        }
Пример #3
0
        public void Update_Customer()
        {
            int    id;
            string name = "";

            using (var context = CreateDbContext("Update_Customer"))
            {
                var createdCustomer = new Customer
                {
                    Name    = "nurgul",
                    Surname = "dereli"
                };
                context.Set <Customer>().Add(createdCustomer);
                context.Set <Customer>().Add(new Customer {
                    Name = "Another Name", Surname = "Another Surname"
                });
                context.SaveChanges();

                id   = createdCustomer.Id;
                name = createdCustomer.Name;
            }

            Customer updateCustomer;

            using (var context = CreateDbContext("Update_Customer"))
            {
                updateCustomer         = context.Set <Customer>().FirstOrDefault(x => x.Id == id);
                updateCustomer.Name    = "mehmet";
                updateCustomer.Surname = "yildiz";
                var repository = new RepositoryCustomer(new EfRepository(context));
                repository.Update(updateCustomer);
            }

            Customer savedCustomer;

            using (var context = CreateDbContext("Update_Customer"))
            {
                var repository = new RepositoryCustomer(new EfRepository(context));
                savedCustomer = repository.GetById(id);
            }

            savedCustomer.Name.Should().NotBeEquivalentTo(name);
            using (var context = CreateDbContext("Update_Customer"))
            {
                context.Customers.First(x => x.Id == updateCustomer.Id).Should().BeEquivalentTo(updateCustomer);
            }
        }
Пример #4
0
        public void GetById_existing_customers(int id)
        {
            using (var context = CreateDbContext("GetById_existing_customers"))
            {
                context.Set <Customer>().Add(new Customer {
                    Id = id
                });
                context.SaveChanges();
            }
            Customer customer = null;

            using (var context = CreateDbContext("GetById_existing_customers"))
            {
                var repository = new RepositoryCustomer(new EfRepository(context));
                customer = repository.GetById(id);
            }

            customer.Should().NotBeNull();
            customer.Id.Should().Be(id);
        }