public void PersistingAListOfProductsAndCategories() { const int numProducts = 500; const int numCategories = 50; const int numCategoriesForEachProduct = 5; var builder = new Builder(builderSettings); var categories = builder .CreateListOfSize <Category>(numCategories) .Persist(); builder .CreateListOfSize <Product>(numProducts) .All() .With(x => x.Categories = Pick <Category> . UniqueRandomList(With.Exactly(numCategoriesForEachProduct).Elements) .From(categories) .ToList()) .Persist(); // NB: Persistence is setup in the RepositoryBuilderSetup class var productsTable = this.Repositories.Products.GetAll(); var categoriesTable = this.Repositories.Categories.GetAll(); productsTable.Count.ShouldBe(numProducts, "products"); categoriesTable.Count.ShouldBe(numCategories, "categories"); }
public void PersistingAListOfProductsAndCategories() { var builderSetup = new SetupFixture().SetUp(); Database.Clear(); const int numProducts = 500; const int numCategories = 50; const int numCategoriesForEachProduct = 5; var categories = new Builder <Category>(builderSetup).CreateListOfSize(numCategories).Persist(); new Builder <Product>(builderSetup) .CreateListOfSize(numProducts) .All() .With(x => x.Categories = Pick <Category> .UniqueRandomList(With.Exactly(numCategoriesForEachProduct).Elements).From(categories)) .Persist(); // NB: Persistence is setup in the SetupFixture class DataTable productsTable = Database.GetContentsOf(Database.Tables.Product); DataTable categoriesTable = Database.GetContentsOf(Database.Tables.Category); DataTable productCategoriesTable = Database.GetContentsOf(Database.Tables.ProductCategory); Assert.That(productsTable.Rows.Count, Is.EqualTo(numProducts)); Assert.That(categoriesTable.Rows.Count, Is.EqualTo(numCategories)); Assert.That(productCategoriesTable.Rows.Count, Is.EqualTo(numCategoriesForEachProduct * numProducts)); }
public void PersistingAListOfProductsAndCategories() { const int numProducts = 500; const int numCategories = 50; const int numCategoriesForEachProduct = 5; var builder = new Builder(builderSettings); var categories = builder .CreateListOfSize <Category>(numCategories) .Persist(); builder .CreateListOfSize <Product>(numProducts) .All() .With(x => x.Categories = Pick <Category> . UniqueRandomList(With.Exactly(numCategoriesForEachProduct).Elements) .From(categories) .ToList()) .Persist(); // NB: Persistence is setup in the RepositoryBuilderSetup class DataTable productsTable = Database.GetContentsOf(Database.Tables.Product); DataTable categoriesTable = Database.GetContentsOf(Database.Tables.Category); DataTable productCategoriesTable = Database.GetContentsOf(Database.Tables.ProductCategory); Assert.That(productsTable.Rows.Count, Is.EqualTo(numProducts), "products"); Assert.That(categoriesTable.Rows.Count, Is.EqualTo(numCategories), "categories"); Assert.That(productCategoriesTable.Rows.Count, Is.EqualTo(numCategoriesForEachProduct * numProducts)); }
private static void Seed(this BloggingContext context) { if (context.Blogs.Any()) { return; } var blogs = Builder <Blog> .CreateListOfSize(100) .All() .With(blog => blog.Id = 0) .With(blog => blog.Url = $"http://{Faker.Internet.DomainWord()}.{Faker.Internet.DomainSuffix()}") .With(blog => blog.Rating = Faker.RandomNumber.Next(1, 5)) .Build(); var posts = Builder <Post> .CreateListOfSize(100) .All() .With(post => post.Blog = Pick <Blog> .RandomItemFrom(blogs)) .With(post => post.Id = 0) .With(post => post.Title = Faker.Lorem.GetFirstWord()) .With(post => post.Content = Faker.Lorem.Paragraph()) .Build(); var tags = Builder <Tag> .CreateListOfSize(30) .All() .With(tag => tag.Id = 0) .Build(); context.Blogs.AddRange(blogs); context.Posts.AddRange(posts); var randTags = Pick <Tag> .UniqueRandomList(With.Exactly(15).Elements).From(tags); var randPosts = Pick <Post> .UniqueRandomList(With.Exactly(15).Elements).From(posts); for (var i = 0; i < 15; i++) { context.PostTags.Add(new PostTag { Post = randPosts[i], Tag = randTags[i] }); } context.SaveChanges(); }
public void ShouldBeAbleToUseWith_Exactly() { var constraint = With.Exactly(1); constraint.ShouldBeOfType <ExactlyConstraint>(); }
static void Seed(UniversityContext context) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); int quantity = 33; // Generate departments var depNames = DepartmentNames.GetListOfNames(); int depQ = Math.Min(quantity, depNames.Count()); var selectedD = Pick <String> .UniqueRandomList(With.Exactly(depQ).Elements).From(depNames); var iterD = new Stack <String>(selectedD); var departments = Builder <Department> .CreateListOfSize(depQ) .All() .With(d => d.Name = iterD.Pop()) .Build(); foreach (var department in departments) { context.Add(department); } context.SaveChanges(); // Generate courses /*int coursesPerDepartment = 10; * var allCourses = new List<Course>(); * foreach (var dep in departments) * { * var courses = Builder<Course>.CreateListOfSize(Faker.RandomNumber.Next((int)(coursesPerDepartment*0.8), (int)(coursesPerDepartment *1.2))) * .All() * .With(d => d.Department = dep) * .With(d => d.Name = Faker.Company.CatchPhrase()) * .With(d => d.ECTS = Faker.RandomNumber.Next(1, 8)) * .Build(); * allCourses.AddRange(courses); * } * * foreach (var course in allCourses) * { * context.Add(course); * } * context.SaveChanges();*/ int coursesPerDepartment = 10; int quantOfCourses = departments.Count() * Faker.RandomNumber.Next((int)(coursesPerDepartment * 0.8), (int)(coursesPerDepartment * 1.2)); var courses = Builder <Course> .CreateListOfSize(quantOfCourses) .All() .With(d => d.Department = Pick <Department> .RandomItemFrom(departments)) .With(d => d.Name = Faker.Company.CatchPhrase()) .With(d => d.ECTS = Faker.RandomNumber.Next(1, 8)) .Build(); foreach (var course in courses) { context.Add(course); } context.SaveChanges(); // Generate employees /*int employeesPerDepartment = 10; * var allEmployees = new List<Employee>(); * foreach (var dep in departments) * { * var employees = Builder<Employee>.CreateListOfSize(employeesPerDepartment) * .All() * .With(d => d.Name = Faker.Name.First()) * .With(d => d.Surname = Faker.Name.Last()) * .With(d => d.Address = Faker.Address.StreetAddress()) * .With(d => d.City = Faker.Address.City()) * .With(d => d.Country = Faker.Address.Country()) * .With(d => d.Phone = Faker.Phone.Number()) * .With(d => d.Email = Faker.Internet.Email()) * .With(d => d.Salary = Faker.RandomNumber.Next(2000, 6000)) * .With(d => d.Department = dep) * .Build(); * allEmployees.AddRange(employees); * } * * foreach (var employee in allEmployees) * { * context.Add(employee); * } * context.SaveChanges();*/ int employeesPerDepartment = 9; int quantOfEmployees = departments.Count() * Faker.RandomNumber.Next((int)(employeesPerDepartment * 0.8), (int)(employeesPerDepartment * 1.2)); var employees = Builder <Employee> .CreateListOfSize(quantOfEmployees) .All() .With(d => d.Name = Faker.Name.First()) .With(d => d.Surname = Faker.Name.Last()) .With(d => d.Address = Faker.Address.StreetAddress()) .With(d => d.City = Faker.Address.City()) .With(d => d.Country = Faker.Address.Country()) .With(d => d.Phone = Faker.Phone.Number()) .With(d => d.Email = Faker.Internet.Email()) .With(d => d.Salary = Faker.RandomNumber.Next(2000, 6000)) .With(d => d.Department = Pick <Department> .RandomItemFrom(departments)) .Build(); foreach (var employee in employees) { context.Add(employee); } context.SaveChanges(); // Generate students int studentsPerDepartment = 100; int quantOfStudents = departments.Count() * Faker.RandomNumber.Next((int)(studentsPerDepartment * 0.8), (int)(studentsPerDepartment * 1.2)); var students = Builder <Student> .CreateListOfSize(quantOfStudents) .All() .With(d => d.Name = Faker.Name.First()) .With(d => d.Surname = Faker.Name.Last()) .With(d => d.Address = Faker.Address.StreetAddress()) .With(d => d.City = Faker.Address.City()) .With(d => d.Country = Faker.Address.Country()) .With(d => d.Phone = Faker.Phone.Number()) .With(d => d.Email = Faker.Internet.Email()) .With(d => d.GraduationYear = Faker.RandomNumber.Next(2010, 2025)) .Build(); foreach (var student in students) { context.Add(student); } context.SaveChanges(); // Generate sections /* var allSections = new List<Section>(); * foreach (var cou in courses) * { * int sectionsPerCourse = Faker.RandomNumber.Next(3, 10); * int employeesPerCourse = Faker.RandomNumber.Next(1, 4); * var selectedE = Pick<Employee>.UniqueRandomList(With.Exactly(employeesPerCourse).Elements).From(employees); * var sections = Builder<Section>.CreateListOfSize(sectionsPerCourse) * .All() * .With(d => d.Course = cou) * .With(d => d.Employee = selectedE[Faker.RandomNumber.Next(0, selectedE.Count())]) * .With(d => d.Day = Faker.RandomNumber.Next(1, 5)) * .With(d => d.StartTime = Faker.RandomNumber.Next(8, 19).ToString() + ":" + (Faker.RandomNumber.Next(0, 3) * 15).ToString()) + .With(d => d.Length = Faker.RandomNumber.Next(1, 4) * 45) + .With(d => d.Capacity = Faker.RandomNumber.Next(1, 4) * 10) + .Build(); + allSections.AddRange(sections); + } + + foreach (var section in allSections) + { + context.Add(section); + } + context.SaveChanges();*/ int sectionsPerCourse = 7; int quantOfSections = courses.Count() * Faker.RandomNumber.Next((int)(sectionsPerCourse * 0.6), (int)(sectionsPerCourse * 1.3)); var sections = Builder <Section> .CreateListOfSize(quantOfSections) .All() .With(d => d.Course = Pick <Course> .RandomItemFrom(courses)) .With(d => d.Employee = Pick <Employee> .RandomItemFrom(employees)) .With(d => d.Day = Faker.RandomNumber.Next(1, 5)) .With(d => d.StartTime = Faker.RandomNumber.Next(8, 19).ToString("00") + ":" + (Faker.RandomNumber.Next(0, 3) * 15).ToString("00")) .With(d => d.Length = Faker.RandomNumber.Next(1, 4) * 45) .With(d => d.Capacity = Faker.RandomNumber.Next(1, 4) * 10) .Build(); foreach (var section in sections) { context.Add(section); } context.SaveChanges(); // Generate students grades List <Student> oldStuds = students.Where(s => DateTime.Now.Year - s.GraduationYear > -4).ToList(); var allGrades = new List <Grade>(); foreach (var s in oldStuds) { var studYearEnded = Math.Min(DateTime.Now.Year - s.GraduationYear + 5, 6); var coursesEnded = Math.Min(10 * studYearEnded, courses.Count()); var selectedC = Pick <Course> .UniqueRandomList(With.Exactly(coursesEnded).Elements).From(courses); var iter = new Stack <Course>(selectedC); var grades = Builder <Grade> .CreateListOfSize(Math.Min(Faker.RandomNumber.Next(8 * studYearEnded, 10 * studYearEnded), selectedC.Count())) .All() .With(d => d.StudentID = s.StudentID) .With(d => d.CourseID = iter.Pop().CourseID) .With(d => d.Year = Faker.RandomNumber.Next(1, studYearEnded)) .With(d => d.Semester = d.Year * 2 + Faker.RandomNumber.Next(0, 1)) .With(d => d.Score = Faker.RandomNumber.Next(2, 5)) .Build(); allGrades.AddRange(grades); } foreach (var grade in allGrades) { context.Add(grade); } context.SaveChanges(); // Generate students enrolments var allEnrolments = new List <Enrollment>(); foreach (var s in students) { var selectedS = Pick <Section> .UniqueRandomList(With.Exactly(15).Elements).From(sections); var iter = new Stack <Section>(selectedS); var enrolments = Builder <Enrollment> .CreateListOfSize(Faker.RandomNumber.Next(5, 15)) .All() .With(d => d.SectionID = iter.Pop().SectionID) .With(d => d.StudentID = s.StudentID) .Build(); allEnrolments.AddRange(enrolments); } foreach (var enrolment in allEnrolments) { /*var section = (Section)context.Sections.Where(e => e.SectionID == enrolment.SectionID).FirstOrDefault(); * int freePlaces = section.Capacity - Section.CountStudsOnTmpDB(section, context); * if (freePlaces > 0) * {*/ context.Add(enrolment); /* context.SaveChanges(); * }*/ } context.SaveChanges(); // Correction foreach (var section in sections) { int freePlaces = section.Capacity - Section.CountStudentsOnSection(section, context); if (freePlaces < 0) { section.Capacity -= (freePlaces + freePlaces % 10); } } context.SaveChanges(); }
public void ShouldBeAbleToUseWith_Exactly() { var constraint = With.Exactly(1); Assert.That(constraint, Is.TypeOf(typeof(ExactlyConstraint))); }
public void ShouldBeAbleToPickUsingExactlyConstraint() { var simpleClasses = Builder <SimpleClass> .CreateListOfSize(10).Build(); var products = Builder <MyClass> .CreateListOfSize(10) .All() .With(x => x.SimpleClasses = Pick <SimpleClass> .UniqueRandomList(With.Exactly(1).Elements).From(simpleClasses)).Build(); for (int i = 0; i < products.Count; i++) { Assert.That(products[i].SimpleClasses.Count, Is.EqualTo(1)); } }