Esempio n. 1
0
        public void MapperObviousTests()
        {
            Assert.Throws <ArgumentNullException>(() => new RddObjectsMapper <DTOCat, Cat>(null, _fixture.Mapper));
            Assert.Throws <ArgumentNullException>(() => new RddObjectsMapper <DTOCat, Cat>(new ExpressionParser(), null));

            var mapper = new RddObjectsMapper <DTOCat, Cat>(new ExpressionParser(), _fixture.Mapper);

            Assert.Null(mapper.Map((Query <DTOCat>)null));
            Assert.Null(mapper.Map((ISelection <Cat>)null));
            Assert.Null(mapper.Map((Cat)null));
        }
Esempio n. 2
0
        public void MapperTests()
        {
            var mapper = new RddObjectsMapper <DTOCat, Cat>(new ExpressionParser(), _fixture.Mapper);
            var cat    = new Cat
            {
                Age  = 22,
                Name = "bob"
            };

            var dto = mapper.Map(cat);

            Assert.Equal(cat.Age, dto.Age);
            Assert.Equal(cat.Id, dto.Id);
            Assert.Equal(cat.Name, dto.NickName);

            var input = new Selection <Cat>(new List <Cat> {
                cat
            }, 45);
            var dtoSelection = mapper.Map(input);

            Assert.Equal(input.Count, dtoSelection.Count);
            Assert.Single(dtoSelection.Items);
            Assert.Equal(cat.Age, dtoSelection.Items.First().Age);
            Assert.Equal(cat.Id, dtoSelection.Items.First().Id);
            Assert.Equal(cat.Name, dtoSelection.Items.First().NickName);

            var expressionParser = new ExpressionParser();
            var query            = new Query <DTOCat>
            {
                Filter   = new Filter <DTOCat>(f => f.NickName.StartsWith("titi") && f.Age != 10),
                Fields   = expressionParser.ParseTree <DTOCat>("nickname,age"),
                OrderBys = new List <OrderBy <DTOCat> > {
                    new OrderBy <DTOCat>(expressionParser.Parse <DTOCat>("nickname").ToLambdaExpression(), SortDirection.Ascending)
                }
            };
            var bddQuery = mapper.Map(query);

            var comparer = new ExpressionEqualityComparer();
            Expression <Func <Cat, bool> > result = f => f.Name.StartsWith("titi") && f.Age != 10;

            Assert.True(comparer.Equals(bddQuery.Filter, result));

            Assert.Equal(bddQuery.Fields.ToString(), expressionParser.ParseTree <Cat>("name,age").ToString());

            Assert.Single(bddQuery.OrderBys);
            Assert.Equal(SortDirection.Ascending, bddQuery.OrderBys.FirstOrDefault().Direction);
            Expression <Func <Cat, string> > orderBy = f => f.Name;

            Assert.True(comparer.Equals(bddQuery.OrderBys.FirstOrDefault().LambdaExpression, orderBy));
        }