Пример #1
0
        public void CompanyNameGeneratorTest()
        {
            IGenerator <string> cng = new CompanyNameGenerator();
            var companyname         = (string)cng.Generate();

            Assert.IsNotNull(companyname);
        }
        public void Seed(SiteDbContext context, int sampleSize)
        {
            // Return if there are already suppliers
            if (context.Suppliers.Any())
                return;

            // Hydrate supplier
            var hydrator = new Hydrator<Supplier>()
                .WithAmericanAddress(e => e.Address)
                .WithAmericanCity(e => e.City)
                .WithAmericanState(e => e.Region)
                .WithCustomGenerator(e => e.Country, new CountryGenerator())
                .WithCustomGenerator(e => e.ContactName, new ContactNameGenerator())
                .WithEmailAddress(e => e.ContactEmail)
                .WithAmericanPostalCode(e => e.PostalCode, 4)
                .WithAmericanPhone(e => e.Phone)
                .WithCustomGenerator(e => e.ContactTitle, new TitleGenerator())
                .Ignoring(e => e.CompanyName)
                .Ignoring(e => e.SupplierId)
                .Ignoring(e => e.Products)
                .GetList(sampleSize)
                .ToList();

            // Make sure every company name is unique
            var usedCompanyNames = new List<string>();

            foreach (var supplier in hydrator)
            {
                string name;
                do
                {
                    name = new CompanyNameGenerator().Generate();
                } while (usedCompanyNames.Contains(name));
                usedCompanyNames.Add(name);
                supplier.CompanyName = name;

                // See if this works
                supplier.Products = new List<Product>();
                // ReSharper disable once LoopCanBePartlyConvertedToQuery
                foreach (var i in Enumerable.Range(0, 4))
                {
                    var product = new Product
                    {
                        ProductName = new CatchPhraseGenerator().Generate(),
                        QuantityPerUnit = new IntegerGenerator() {MaximumValue = 10, MinimumValue = 1}.Generate(),
                        UnitPrice = new IntegerGenerator() { MaximumValue = 200, MinimumValue = 1 }.Generate(),
                    };

                    // Add Product to supplier
                    supplier.Products.Add(product);
                }

                // Add supplier to context
                context.Suppliers.Add(supplier);
            }

            // Save the changes
            context.SaveChanges();
        }
        public void Seed(SiteDbContext context, int sampleSize)
        {
            if (context.Customers.Any())
                return;

            var hydrator = new Hydrator<CustomerViewModel>()
                .WithAmericanAddress(e => e.Address)
                .WithAmericanCity(e => e.City)
                .WithAmericanState(e => e.Region)
                .WithCustomGenerator(e => e.Country, new CountryGenerator())
                .WithCustomGenerator(e => e.ContactName, new ContactNameGenerator())
                .WithEmailAddress(e => e.ContactEmail)
                .WithAmericanPostalCode(e => e.PostalCode, 4)
                .WithAmericanPhone(e => e.Phone)
                .WithCustomGenerator( e => e.ContactTitle, new TitleGenerator())
                .WithDate(e => e.CreatedOn,DateTime.Now.AddYears(-10),DateTime.Now)
                .Ignoring(e => e.CustomerId)
                .Ignoring(e => e.CreatedBy)
                .Ignoring(e => e.CompanyName)
                .GetList(sampleSize)
                .ToList();

            // Map the view models to models
            var models = AutoMapper.Mapper.Map<List<CustomerViewModel>, List<Customer>>(hydrator);

            // Make sure the company name remains unique
            string name;    // Current Name
            var usedCompanyNames = new List<string>();  // List of used names
            var nameGenerator = new CompanyNameGenerator(); // Name generator

            // Attempt to get the second user ID
            var createdByUserId = context.Users.OrderBy(e => e.FirstName).Skip(1).FirstOrDefault()?.Id;

            // If there are no Users, something went wrong
            if (createdByUserId == null)
            {
                throw new DatabaseSeedException();
            }

            // Go through every model
            models.ForEach(e =>
            {
                do
                {
                    // Generate a random company name
                    name = nameGenerator.Generate();
                }

                // Continue until we get a unused name
                while (usedCompanyNames.Contains(name));

                // Add the name to the list of used names
                usedCompanyNames.Add(name);

                // Assign the name to the Entity
                e.CompanyName = name;

                // Generate a slug for the Entity Based on the name
                e.Slug = name.GenerateSlug();

                // Set the Created By Id
                e.CreatedById = createdByUserId;

                // Add Customer to context
                context.Customers.AddOrUpdate(e);
            });

            // Save Changes
            context.SaveChanges();
        }
Пример #4
0
 public void CompanyNameGeneratorTest()
 {
     IGenerator<string> cng = new CompanyNameGenerator();
     var companyname = (string) cng.Generate();
     Assert.IsNotNull(companyname);
 }