public void GivenSerializeWhenCalledWithNullQueryThenShouldThrowArgumentNull()
        {
            IQueryable query = null;

            Assert.Throws <ArgumentNullException>(() =>
                                                  QueryExprSerializer.Serialize(query));
        }
        public void MultipleSelectManyProjectionWorks()
        {
            var dataQuery = TestableThing.MakeQuery(5);

            IQueryable <Tuple <string, string, string> > SelectMany(IQueryable <TestableThing> query) =>
            query.SelectMany(t => t.ChildThings, (t, c) => new
            {
                parentId = t.Id,
                childId  = c.Id,
                children = c.ChildThings
            }).SelectMany(tc => tc.children, (tc, tcc) => new
            {
                tc.parentId,
                tc.childId,
                grandChildId = tcc.Id
            }).Select(t => Tuple.Create(t.parentId, t.childId, t.grandChildId));

            var query = SelectMany(IQueryableExtensions.CreateQueryTemplate <TestableThing>());

            var root = QueryExprSerializer.Serialize(query);
            var json = wrapper.FromSerializationRoot(root);
            // make sure we're not just pulling from the cache
            var deserializedRoot = wrapper.ToSerializationRoot(json);
            var newQuery         = QueryExprSerializer.DeserializeQuery <Tuple <string, string, string> >(deserializedRoot, dataQuery);

            var expected = SelectMany(dataQuery).ToList();
            var actual   = newQuery.ToList();

            Assert.NotNull(actual);
            Assert.Equal(expected, actual);
        }
        public void GivenSerializeWhenCalledWithNullExpressionThenShouldThrowArgumentNull()
        {
            Expression expression = null;

            Assert.Throws <ArgumentNullException>(() =>
                                                  QueryExprSerializer.Serialize(expression));
        }
        public void GivenQueryWhenSerializeCalledThenShouldDeserialize(
            IQueryable query,
            Queries type)
        {
            var json = QueryExprSerializer.Serialize(query);

            // make sure we're not just pulling from the cache
            Reset();

            IQueryable queryHost = TestableThing.MakeQuery(10);

            var newQuery = QueryExprSerializer.DeserializeQuery(queryHost, json);

            // can't do equivalency check for anonymous types
            if (!query.AsEnumerableExpression().OfType <NewExpression>()
                .Any(t => t.Type.IsAnonymousType()))
            {
                Assert.True(query.IsEquivalentTo(newQuery));
            }

            if (newQuery is IQueryable <ExpandoObject> anonymousQuery)
            {
                var list = anonymousQuery.ToList();
                Assert.NotNull(list);
            }
            else if (newQuery is IQueryable <TestableThing> thingQuery)
            {
                var list = thingQuery.ToList();
                ValidateQuery(list, type);
            }
        }
 private string queryJson(
     bool isCount    = false,
     bool useProduct = false,
     bool isSingle   = false) => JsonSerializer.Serialize(
     new SerializationPayload(isCount ? PayloadType.Count : (isSingle ? PayloadType.Single : PayloadType.Array))
 {
     Json = jsonWrapper.FromSerializationRoot(
         QueryExprSerializer.Serialize(altQuery ?? (useProduct ? productsQuery : query)))
 });
        public void DeserializeWithConfigurationUsesConfiguration()
        {
            var expr         = 42.AsConstantExpression();
            var serialized   = QueryExprSerializer.Serialize(expr);
            var deserialized = QueryExprSerializer.Deserialize(
                serialized,
                null,
                config => config.CompressExpressionTree(false),
                null);

            Assert.NotNull(deserialized);
        }
        public void GivenQueryCanSerializeAndDeserialize(IQueryable query, QueryExprSerializerTests.Queries queryType)
        {
            var root             = QueryExprSerializer.Serialize(query, config => config.CompressExpressionTree(false));
            var json             = wrapper.FromSerializationRoot(root);
            var deserializedRoot = wrapper.ToSerializationRoot(json);

            Assert.NotNull(deserializedRoot);
            IQueryable queryHost         = TestableThing.MakeQuery(10);
            var        deserializedQuery = QueryExprSerializer.DeserializeQuery(queryHost, deserializedRoot);

            Assert.NotNull(deserializedQuery);
            Assert.True(query.Expression.IsEquivalentTo(deserializedQuery.Expression));
        }
        public void GivenQueryWhenSerializeCalledThenShouldDeserializeForType(
            IQueryable <TestableThing> query,
            Queries type)
        {
            var json = QueryExprSerializer.Serialize(query);

            Reset();

            var queryHost = TestableThing.MakeQuery(100);
            var newQuery  = QueryExprSerializer.DeserializeQuery <TestableThing>(json, queryHost);

            Assert.True(query.IsEquivalentTo(newQuery));
            ValidateQuery(newQuery.ToList(), type);
        }
        public void HandlesElementInits()
        {
            Expression <Func <DataHost> > elemInit = () => new DataHost {
                Elems = { new DataElem() }
            };
            var root             = QueryExprSerializer.Serialize(elemInit, config => config.CompressExpressionTree(false));
            var json             = wrapper.FromSerializationRoot(root);
            var deserializedRoot = wrapper.ToSerializationRoot(json);

            QueryExprSerializer.ConfigureRules(rules => rules.RuleForType <DataHost>()
                                               .RuleForType <DataElem>());
            var deserializedExpr = QueryExprSerializer.Deserialize <LambdaExpression>(deserializedRoot);

            Assert.True(elemInit.IsEquivalentTo(deserializedExpr));
        }
        public void HandlesInterfaces()
        {
            var expr = Expression.Constant(new IdContainer
            {
                Id   = Guid.NewGuid().ToString(),
                Name = nameof(IdContainer),
            }, typeof(IHaveId));
            var root             = QueryExprSerializer.Serialize(expr, config => config.CompressExpressionTree(false));
            var json             = wrapper.FromSerializationRoot(root);
            var deserializedRoot = wrapper.ToSerializationRoot(json);
            var deserializedExpr = QueryExprSerializer.Deserialize <ConstantExpression>(deserializedRoot);

            Assert.Equal(typeof(IHaveId), deserializedExpr.Type);
            Assert.True(deserializedExpr.Value is IdContainer);
            Assert.Equal(nameof(IdContainer), ((IdContainer)deserializedExpr.Value).Name);
        }
        public void HandlesNullAnonymousTypes()
        {
            var expected = new
            {
                Id      = 1,
                SubAnon = default(object)
            };

            var expr             = expected.AsConstantExpression();
            var root             = QueryExprSerializer.Serialize(expr);
            var json             = wrapper.FromSerializationRoot(root);
            var deserializedRoot = wrapper.ToSerializationRoot(json);
            var deserializedExpr = QueryExprSerializer.Deserialize <ConstantExpression>(deserializedRoot);

            Assert.True(expressionEvaluator.Value.AnonymousValuesAreEquivalent(expr.Value, deserializedExpr.Value));
        }
        public void SerializesAndDeserializesAnonymousTypes()
        {
            var expected = new
            {
                Id      = 1,
                SubAnon = new
                {
                    Id  = 2,
                    Two = 2.ToString()
                }
            };

            var expr             = expected.AsConstantExpression();
            var root             = QueryExprSerializer.Serialize(expr);
            var json             = wrapper.FromSerializationRoot(root);
            var deserializedRoot = wrapper.ToSerializationRoot(json);
            var deserializedExpr = QueryExprSerializer.Deserialize <ConstantExpression>(deserializedRoot);

            Assert.True(expressionEvaluator.Value.AnonymousValuesAreEquivalent(expr.Value, deserializedExpr.Value));
        }
        public void NewArrayExpressionShouldDeserialize()
        {
            var newArray = Expression.NewArrayInit(
                typeof(int),
                Expression.Constant(1),
                Expression.Constant(2));

            Func <IConfigurationBuilder, IConfigurationBuilder> config = builder =>
                                                                         builder.CompressTypes(false).CompressExpressionTree(false);
            var expr = QueryExprSerializer.Serialize(newArray, cfg => config(cfg));

            var state = config(ServiceHost.GetService <IConfigurationBuilder>()).Configure();

            var deserialized = serializer.Deserialize(expr.Expression as NewArray, state);

            Assert.Equal(newArray.Type, deserialized.Type);
            Assert.Equal(
                newArray.Expressions.OfType <ConstantExpression>().Select(ce => ce.Value),
                deserialized.Expressions.OfType <ConstantExpression>().Select(ce => ce.Value));
        }
Ejemplo n.º 14
0
 private string GetJson(bool isCount = false) =>
 JsonSerializer.Serialize(
     new SerializationPayload(isCount ? PayloadType.Count : PayloadType.Array)
 {
     Json = jsonWrapper.FromSerializationRoot(QueryExprSerializer.Serialize(query)),
 });
        public void AlternateSelectManyWorks()
        {
            QueryExprSerializer.ConfigureRules(
                rules => rules.RuleForType <RelatedThing>());

            var db = typeof(JsonWrapperTests).Assembly.GetTypes()
                     .Select(t => new MockType
            {
                Id      = t.ToString(),
                Name    = t.FullName,
                Methods = t.GetMethods().Select(
                    m => new MockMethod
                {
                    Id         = m.ToString(),
                    Name       = m.Name,
                    Parameters = m.GetParameters()
                                 .Select(p => new MockParameter
                    {
                        Id   = $"{p.Position}-{p.ParameterType}",
                        Name = p.Name,
                    }).ToList()
                }).ToList()
            });

            Func <IQueryable <MockType>, IQueryable <RelatedThing> > makeQuery = q =>
                                                                                 q
                                                                                 .Where(t => t.Name != null)
                                                                                 .SelectMany(
                t =>
                t.Methods, (t, m) =>
                new
            {
                t.Name,
                MethodName = m.Name,
                m.Parameters,
            })
                                                                                 .SelectMany(
                tm => tm.Parameters,
                (tm, p) =>
                new RelatedThing
            {
                TypeName      = tm.Name,
                MethodName    = tm.MethodName,
                ParameterName = p.Name,
            })
                                                                                 .OrderBy(r => r.TypeName)
                                                                                 .ThenBy(r => r.MethodName)
                                                                                 .ThenBy(r => r.ParameterName);

            var queryToSerialize = makeQuery(new List <MockType>().AsQueryable());

            var root = QueryExprSerializer.Serialize(queryToSerialize);
            var json = wrapper.FromSerializationRoot(root);

            var deserializedRoot = wrapper.ToSerializationRoot(json);
            var newQuery         = QueryExprSerializer.DeserializeQuery <RelatedThing>(deserializedRoot, db.AsQueryable());

            var result   = makeQuery(db.AsQueryable());
            var expected = result.ToList();
            var actual   = newQuery.ToList();

            Assert.NotNull(actual);
            Assert.Equal(expected.Count(), actual.Count());
        }
 private string queryJson() => JsonSerializer.Serialize(
     new SerializationPayload
 {
     Json = jsonWrapper.FromSerializationRoot(QueryExprSerializer.Serialize(query))
 });