public void BuildFilter_ValidData_EqualToExpected()
        {
            var repository = new Repository <Phone, int, AppDbContextTests>(AppDbContextTests.GetContextWithData());

            Expression <Func <Phone, bool> > filter = p => p.ContactInfoId == 1 && p.ContactInfo.StudentId == 1 && p.ContactInfo.Student.SchoolId == 1;
            var expectedData = GetData(repository, filter);

            var routeFilterBuilder = GetRouteFilterBuilder("schoolid:1#studentid:1#contactinfoid:1");
            var builtFilter        = (Expression <Func <Phone, bool> >)routeFilterBuilder.BuildFilter(typeof(PhonesController));
            var builtData          = GetData(repository, builtFilter);

            Assert.Equal(expectedData.ToJson(), builtData.ToJson());
        }
Exemple #2
0
        public ShaperTests()
        {
            var services = new ServiceCollection();

            services.AddTransient <IShapePropertyWalker, ShapePropertyWalker>();
            services.AddTransient <IShapePropertyWalkerVisitor, ShapePropertyWalkerVisitor>();
            services.AddSingleton <IPropertyProvider, PropertyProvider>();
            services.AddSingleton <IFieldNameResolver, FieldNameResolver>();
            services.AddSingleton <IOptions <MvcJsonOptions>, MvcJsonOptionsTests>();

            var walker  = new ShapePropertyWalker(new PropertyProvider());
            var visitor = new ShapePropertyWalkerVisitor(services.BuildServiceProvider(), new FieldNameResolver(new MvcJsonOptionsTests()));

            _shaper = new Shaper(walker, visitor);

            var mapper = new Mapper(new MapperConfiguration(config => config.AddProfile(new AppProfile())));

            _school = mapper.DefaultContext.Mapper.Map <SchoolOutDto>(AppDbContextTests.GetContextWithData().Schools.FirstOrDefault());
        }
 public PagedListTests()
 {
     _repository = new Repository <Student, int, AppDbContextTests>(AppDbContextTests.GetContextWithData());
     _middlePage = new Random().Next(2, TOTAL_PAGES - 1);
 }
        private IServiceCollection GetRepositoryServices()
        {
            var services = new ServiceCollection();

            services.AddSingleton <IRepository <Book, int> >(new Repository <Book, int, AppDbContextTests>(AppDbContextTests.GetContextWithData()));
            services.AddSingleton <IRepository <ContactInfo, int> >(new Repository <ContactInfo, int, AppDbContextTests>(AppDbContextTests.GetContextWithData()));
            services.AddSingleton <IRepository <Lesson, string> >(new Repository <Lesson, string, AppDbContextTests>(AppDbContextTests.GetContextWithData()));
            services.AddSingleton <IRepository <Phone, int> >(new Repository <Phone, int, AppDbContextTests>(AppDbContextTests.GetContextWithData()));
            services.AddSingleton <IRepository <School, int> >(new Repository <School, int, AppDbContextTests>(AppDbContextTests.GetContextWithData()));
            services.AddSingleton <IRepository <Student, int> >(new Repository <Student, int, AppDbContextTests>(AppDbContextTests.GetContextWithData()));

            return(services);
        }
Exemple #5
0
        public async Task Build_ValidData_EqualToExpected()
        {
            var schoolsRepository = new Repository <School, int, AppDbContextTests>(AppDbContextTests.GetContextWithData());

            var expectedDataQuery = schoolsRepository.Query
                                    .Where(school => school.Name.Contains("of") == true)                                                           // Search
                                    .Where(school => school.YearOfEstablishment > 1739 &&                                                          // Greater
                                           school.YearOfEstablishment < 1852 ||                                                                    // Less
                                           school.Lessons.Any(lesson => lesson.Books.Any(book => book.Name == "The art of computer programming"))) // Equal
                                    .OrderByDescending(school => school.YearOfEstablishment)
                                    .Select(school => new School
            {
                Name = school.Name,
                YearOfEstablishment = school.YearOfEstablishment,
                Lessons             = school.Lessons
                                      .Where(lesson => new List <object> {
                    2, 3, 4, 5
                }.Contains(lesson.WeekHours) == true)                                                        // In
                                      .OrderBy(lesson => lesson.Name)
                                      .Select(lesson => new Lesson
                {
                    Id    = lesson.Id,
                    Name  = lesson.Name,
                    Books = lesson.Books
                            .Where(book => book.Name.Contains("The") == true &&     // Like
                                   book.Name.Contains("math") == false)             // NotLike
                            .OrderByDescending(book => book.Name)
                            .Select(book => new Book
                    {
                        Id       = book.Id,
                        Name     = book.Name,
                        LessonId = book.LessonId
                    })
                            .ToList()
                })
                                      .ToList(),
                Students = school.Students
                           .Where(student => student.DateOfBirth >= new DateTime(1996, 1, 1) && // GreaterOrEqual
                                  student.DateOfBirth <= new DateTime(2000, 12, 31) &&          // LessOrEqual
                                  student.ContactInfo.Email != null ||                          // NotNull
                                  student.ContactInfo.Address == null)                          // Null
                           .OrderBy(student => student.FirstName)
                           .ThenByDescending(student => student.LastName)
                           .Select(student => new Student
                {
                    FirstName   = student.FirstName,
                    LastName    = student.LastName,
                    ContactInfo = student.ContactInfo != default(ContactInfo) ? new ContactInfo
                    {
                        Email  = student.ContactInfo.Email,
                        Phones = student.ContactInfo.Phones
                                 .Where(phone => phone.Number != "608-555-0122" &&    // NotEqual
                                        new List <object> {
                            "405-555-0176", "302-555-0147"
                        }.Contains(phone.Number) == false)                                                                               // NotIn
                                 .OrderBy(phone => phone.Number)
                                 .Select(phone => new Phone
                        {
                            Number = phone.Number
                        })
                                 .ToList()
                    } : default
                })
Exemple #6
0
        public void Build_SearchKeyOnNonStringProperty_ExceptionThrown()
        {
            var invalidEntitiesRepository = new Repository <InvalidEntity, int, AppDbContextTests>(AppDbContextTests.GetContextWithData());
            var builder = GetQueryBuilder <InvalidEntity>(new ServiceCollection());

            Assert.Throws <InvalidOperationException>(() => builder.Build(invalidEntitiesRepository.Query, new QueryString(new CoreOptions())
            {
                QueryParams = new QueryParams
                {
                    Search = "anything"
                }
            }));
        }