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 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);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Deserializes the query.
        /// </summary>
        /// <param name="template">The <see cref="IQueryable"/> to run.</param>
        /// <param name="json">The stream with json info.</param>
        /// <param name="logger">The logger.</param>
        /// <returns>The <see cref="IQueryable"/> result.</returns>
        public async Task <QueryResult> DeserializeAsync(
            IQueryable template,
            Stream json,
            ILogger logger = null)
        {
            Ensure.NotNull(() => template);
            Ensure.NotNull(() => json);

            var request = await JsonSerializer.DeserializeAsync <SerializationPayload>(json);

            Ensure.VariableNotNull(() => request);
            Ensure.NotNullOrWhitespace(() => request.Json);

            if (logger != null)
            {
                logger.LogInformation($"Query payload: {request.Json}");
            }

            var options = jsonOptions.Value();
            var root    = jsonSerializer.Value.ToSerializationRoot(request.Json, options);
            var query   = QueryExprSerializer.DeserializeQuery(template, root);

            if (logger != null)
            {
                logger.LogInformation($"Deserialized query: {query.Expression}");
            }

            return(new QueryResult
            {
                Query = query,
                QueryType = request.GetQueryType(),
            });
        }
        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 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());
        }