public void By_Default_Dont_Use_Numbers_In_Generated_Values_On_CreateMany() { var workers = new Faker<Worker>().CreateMany(3); Assert.IsTrue(workers.Any(x => !x.Name.Contains("1"))); Assert.IsTrue(workers.Any(x => !x.Name.Contains("2"))); Assert.IsTrue(workers.Any(x => !x.Name.Contains("3"))); }
public void Seed() { Randomizer.Seed = new Random(1742); var faker = new Faker("de"); var subjects = new Subject[] { new Subject { Id = faker.Random.Guid(), Term = 3, Name = "POS", EducationType = "BIF" }, new Subject { Id = faker.Random.Guid(), Term = 3, Name = "DBI", EducationType = "BIF" }, new Subject { Id = faker.Random.Guid(), Term = 5, Name = "DBI", EducationType = "KIF" }, }; Subjects.AddRange(subjects); SaveChanges(); var students = new Faker <Student>("de").CustomInstantiator(f => { var lastname = f.Name.LastName(); var username = (lastname.Length < 3 ? lastname : lastname.Substring(0, 3)).ToLower() + f.Random.Int(100000, 999999).ToString(); return(new Student { Id = f.Random.Guid(), Firstname = f.Name.FirstName(), Lastname = lastname, Email = username.ToLower() + "@spengergasse.at", Username = username.ToUpper() }); }) .Generate(20) .GroupBy(s => s.Username).Select(g => g.First()) .ToList(); var coaches = new Faker <Coach>("de").CustomInstantiator(f => { var lastname = f.Name.LastName(); var username = (lastname.Length < 3 ? lastname : lastname.Substring(0, 3)).ToLower() + f.Random.Int(100000, 999999).ToString(); return(new Coach { Id = f.Random.Guid(), Firstname = f.Name.FirstName(), Lastname = lastname, Email = username.ToLower() + "@spengergasse.at", Username = username.ToUpper(), Phone = f.Random.Int(100000, 999999).ToString() }); }) .Generate(5) .GroupBy(s => s.Username).Select(g => g.First()) .Where(c => !students.Any(s => s.Username == c.Username)) .ToList(); Students.AddRange(students); Students.AddRange(coaches); SaveChanges(); var offers = new Faker <Offer>("de").CustomInstantiator(f => { var from = new DateTime(2021, 9, 1).AddDays(f.Random.Int(0, 120)); return(new Offer { Id = f.Random.Guid(), Teacher = f.Random.ListItem(coaches), Subject = f.Random.ListItem(subjects), From = from, To = from.AddDays(f.Random.Int(30, 120)) }); }) .Generate(20) .GroupBy(o => new { o.Teacher.Id, SId = o.Subject.Id }).Select(g => g.First()) .ToList(); Offers.AddRange(offers); SaveChanges(); var locations = new string[] { "C3.07", "C3.08", "C3.09", "C3.10", "C3.11" }; var appointments = new Faker <Appointment>("de").CustomInstantiator(f => { var offer = f.Random.ListItem(offers); return(new Appointment { Id = f.Random.Guid(), Offer = offer, Student = f.Random.ListItem(students), Date = offer.From.AddDays(f.Random.Int(0, (int)(offer.To - offer.From).TotalDays)), Location = f.Random.ListItem(locations).OrDefault(f, 0.5f), State = f.Random.Enum <AppointmentState>() }); }) .Generate(40) .ToList(); Appointments.AddRange(appointments); SaveChanges(); }
public void When_Set_UseSequenceNumbers_We_Have_Numbers_In_Generated_Values_On_CreateMany() { var workers = new Faker<Worker>(useSequenceNumbers: true).CreateMany(3); Assert.IsTrue(workers.Any(x => x.Name.Contains("1"))); Assert.IsTrue(workers.Any(x => x.Name.Contains("2"))); Assert.IsTrue(workers.Any(x => x.Name.Contains("3"))); }
public Task <GenerateExcelFileResource> Handle(GenerateExcelFileQuery request, CancellationToken cancellationToken) { string folderName = "TempExcels"; string webRootPath = hostingEnvironment.ContentRootPath; string newPath = Path.Combine(webRootPath, folderName); string sFileName = @"TestSample.xlsx"; string filePath = Path.Combine(newPath, sFileName); string URL = string.Format("{0}://{1}/{2}/{3}", httpContext.HttpContext.Request.Scheme, httpContext.HttpContext.Request.Host, folderName, sFileName); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } if (File.Exists(filePath)) { File.Delete(filePath); } var output = new GenerateExcelFileResource(); var memory = new MemoryStream(); using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { IWorkbook workbook; workbook = new XSSFWorkbook(); ISheet citySheet = workbook.CreateSheet("Cities"); IRow cityRow = citySheet.CreateRow(0); cityRow.CreateCell(0).SetCellValue("نام شهر"); var cities = new Faker <City> (locale: "fa") .RuleFor(c => c.Name, opt => opt.Lorem.Word()).Generate(1000); cities = cities.GroupBy(c => c.Name).Select(c => c.First()).ToList(); int cityRowCountr = 1; foreach (var city in cities) { cityRow = citySheet.CreateRow(cityRowCountr); cityRow.CreateCell(0).SetCellValue(city.Name); cityRowCountr++; } ISheet saleSheet = workbook.CreateSheet("Sales"); IRow saleRow = saleSheet.CreateRow(0); saleRow.CreateCell(0).SetCellValue("نام شهر"); saleRow.CreateCell(1).SetCellValue("نام فرد"); saleRow.CreateCell(2).SetCellValue("نام محصول"); saleRow.CreateCell(3).SetCellValue("کد محصول"); saleRow.CreateCell(4).SetCellValue("قیمت محصول"); var sale = new Faker <Sale> (locale: "fa") .RuleFor(s => s.UserName, opt => opt.Lorem.Word()) .RuleFor(s => s.ProductName, opt => opt.Lorem.Word()) .RuleFor(s => s.Price, opt => opt.Random.Long(1000, 50000)) .Generate(1040000); var faker = new Faker(); var groupSale = sale.GroupBy(s => s.ProductName); foreach (var item in groupSale) { var ProductId = faker.Random.Int(1, 1400000).ToString(); while (sale.Any(s => s.ProductId == ProductId)) { ProductId = faker.Random.Int(1, 1400000).ToString(); } sale.Where(s => s.ProductName == item.Key).ToList().ForEach(s => s.ProductId = ProductId); } int saleRowCounter = 1; var cityNames = cities.Select(c => c.Name).ToList(); foreach (var item in sale) { saleRow = saleSheet.CreateRow(saleRowCounter); saleRow.CreateCell(0).SetCellValue(faker.PickRandom(cityNames)); saleRow.CreateCell(1).SetCellValue(item.UserName); saleRow.CreateCell(2).SetCellValue(item.ProductName); saleRow.CreateCell(3).SetCellValue(item.ProductId); saleRow.CreateCell(4).SetCellValue(item.Price); saleRowCounter++; } workbook.Write(fs); } using (var stream = new FileStream(filePath, FileMode.Open)) { stream.CopyTo(memory); } memory.Position = 0; output.Url = URL; return(Task.FromResult(output)); }