Пример #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 CustomerData()
        {
            var dataFixture = new DataFixture();

            dataFixture.Fixture().ConfigureAwait(false).GetAwaiter().GetResult();

            // Initialize the list of objects and save them into database table.
            string filePath     = "\\Data\\customers.json";
            var    customersDTO = IOHelpers.ReadFromFile <Customer, CustomerDTO>(filePath, true);

            var repositoryCustomer = new RepositoryCustomer();
            var customers          = IOHelpers.EntitiesList <Customer, CustomerDTO>(customersDTO, repositoryCustomer.BusinessToDomainObjectPropertyMap());

            var customersNew = new List <Customer>();

            using (var dbContext = new BankAccountContext())
            {
                var repository = new RepositoryBaseEF <Customer>(dbContext);

                customersNew = repository.Get();
            }

            dataFixture.Dispose().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.NotNull(customersNew);

            var count = customers.Count;

            Assert.Equal(customers[count - 1].BirthDate, customersNew[count - 1].BirthDate);
        }
Пример #3
0
 public EShopUnitOfWork(ShopContext context)
 {
     this.context = context;
     RepostiryCustomer = new RepositoryCustomer(context);
     RepositoryBook = new RepositoryBook(context);
     RepositoryOrder = new RepositoryOrder(context);
     RepositoryGenre = new RepositoryGenre(context);
     RepositoryAutor = new RepositoryAutor(context);
 }
Пример #4
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();
        }
Пример #5
0
        public void CreateCustomer()
        {
            // need to create an instance of the repository
            var repository = new RepositoryCustomer();
            var dto        = new CustomerDto
            {
                FirstName = "Joe",
                LastName  = "Bloggs",
                Telephone = "9999-8888"
            };

            var customer = Customer.Create(repository, dto);

            Assert.IsFalse(customer.Id == 0, "Customer Id should have been updated");
            Assert.AreSame(customer.FirstName, dto.FirstName, "First Name are different");
        }
Пример #6
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);
            }
        }
Пример #7
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);
        }
Пример #8
0
        public void GetAll_customers(int count)
        {
            using (var context = CreateDbContext($"GetAll_customers{count}"))
            {
                for (var i = 0; i < count; i++)
                {
                    context.Set <Customer>().Add(new Customer());
                }
                context.SaveChanges();
            }
            List <Customer> customers = null;

            using (var context = CreateDbContext($"GetAll_customers{count}"))
            {
                var repository = new RepositoryCustomer(new EfRepository(context));
                customers = repository.GetAll().ToList();
            }
            customers.Should().NotBeNull();
            customers.Count.Should().Be(count);
        }
        public void RetrevieValid()
        {
            var repositoryCustomer = new RepositoryCustomer();
            var expected           = new Customer(1)
            {
                Email       = "*****@*****.**",
                FirstName   = "Bilbo",
                LastName    = "Baggins",
                AddressList = new List <Address>()
                {
                    new Address()
                    {
                        AddressType = 1,
                        StreetLine1 = "os. 700-lecia",
                        StreetLine2 = "44/22",
                        City        = "Zywiec",
                        State       = "Slaskie",
                        Country     = "Poland",
                        PostalCode  = 34300
                    },
                    new Address()
                    {
                        AddressType = 2,
                        StreetLine1 = "os. 1000-lecia",
                        StreetLine2 = "12/45",
                        City        = "Zywiec",
                        State       = "Slaskie",
                        Country     = "Poland",
                        PostalCode  = 34300
                    }
                }
            };

            var actual = repositoryCustomer.Retrive(1);

            Assert.AreEqual(expected.Email, actual.Email);
            for (int i = 0; i <= 1; i++)
            {
                Assert.AreEqual(expected.AddressList[i].AddressType, actual.AddressList[i].AddressType);
            }
        }
Пример #10
0
        public void Create_Customer()
        {
            var entity = new Customer
            {
                Name    = "nurgul",
                Surname = "dereli"
            };

            using (var context = CreateDbContext("Create_Customer"))
            {
                var repository = new RepositoryCustomer(new EfRepository(context));
                repository.Insert(entity);
            }
            entity.Id.Should().BeGreaterThan(0);

            using (var context = CreateDbContext("Create_Customer"))
            {
                context.Customers.Count().Should().Be(1);
                context.Customers.First().Should().BeEquivalentTo(entity);
            }
        }
Пример #11
0
        /// <summary>
        /// Initialize the list of objects and save them into database table and do the transactions process.
        /// </summary>
        public static async Task SaveDataAsync(CancellationToken cancellationToken = default)
        {
            try
            {
                // Initialize the list of objects and save them into database table.
                string filePath     = "\\Data\\customers.json";
                var    customersDTO = IOHelpers.ReadFromFile <Customer, CustomerDTO>(filePath);

                var repositoryCustomer = new RepositoryCustomer();
                var customers          = IOHelpers.EntitiesList <Customer, CustomerDTO>(customersDTO, repositoryCustomer.BusinessToDomainObjectPropertyMap());

                using (var dbContext = new BankAccountContext())
                {
                    var repository = new RepositoryBaseEF <Customer>(dbContext);

                    await repository.CreateEntities(customers).ConfigureAwait(false);

                    await repository.SaveAsync().ConfigureAwait(false);
                }

                filePath = "\\Data\\accounts.json";
                var accountsDTO = IOHelpers.ReadFromFile <Account, AccountDTO>(filePath);

                var repositoryAccount = new RepositoryAccount();
                var accounts          = IOHelpers.EntitiesList <Account, AccountDTO>(accountsDTO, repositoryAccount.BusinessToDomainObjectPropertyMap());

                using (var dbContext = new BankAccountContext())
                {
                    var repository = new RepositoryBaseEF <Account>(dbContext);

                    await repository.CreateEntities(accounts).ConfigureAwait(false);

                    await repository.SaveAsync().ConfigureAwait(false);
                }

                if (accountsDTO.Count > 0)
                {
                    await repositoryCustomer.CreateCustomerAccounts(accountsDTO);
                }

                filePath = "\\Data\\phones.json";
                var phonesDTO = IOHelpers.ReadFromFile <PhoneNumber, PhoneNumberDTO>(filePath);

                var repositoryPhone = new RepositoryPhone();
                var phones          = IOHelpers.EntitiesList <PhoneNumber, PhoneNumberDTO>(phonesDTO, repositoryPhone.BusinessToDomainObjectPropertyMap());

                if (phonesDTO.Count > 0 && customersDTO.Count > 0)
                {
                    repositoryPhone.UpdateCustomerIds(customersDTO, phones, phonesDTO);
                }

                using (var dbContext = new BankAccountContext())
                {
                    var repository = new RepositoryBaseEF <PhoneNumber>(dbContext);

                    await repository.CreateEntities(phones).ConfigureAwait(false);

                    await repository.SaveAsync().ConfigureAwait(false);
                }
            }
            catch (DbException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }