Пример #1
0
        public async Task BoundFunctionCollection()
        {
            List <int> expectedResult;

            using (var dbContext = Fixture.CreateContext())
                expectedResult = dbContext.OrderItems.Where(i => i.Order.Name == "Order 1" || i.Order.Name == "Order 2").Select(i => i.Id).ToList();

            Db.OeDataAdapter dataAdapter = Fixture.EdmModel.GetDataAdapter(Fixture.EdmModel.EntityContainer);
            String           request     = $"Customers/{dataAdapter.DataContextType.Namespace}.BoundFunctionCollection(orderNames=['Order 1','Order 2'])?$expand=Customer,Items&$select=Name";

            ODataUri  odataUri = Fixture.ParseUri(request);
            IEdmModel edmModel = Fixture.EdmModel.GetEdmModel(odataUri.Path);
            var       parser   = new OeParser(odataUri.ServiceRoot, edmModel);
            var       uri      = new Uri(odataUri.ServiceRoot, request);

            var response = new MemoryStream();
            await parser.ExecuteQueryAsync(odataUri, OeRequestHeaders.JsonDefault, response, CancellationToken.None).ConfigureAwait(false);

            response.Position = 0;

            List <Object> fromOe = new ResponseReader(edmModel).Read(response).Cast <Object>().ToList();

            Assert.Equal(expectedResult, fromOe.SelectMany(c => (IEnumerable <dynamic>)((dynamic)c).Items).Select(i => (int)i.Id).OrderBy(id => id));
        }
Пример #2
0
        public async Task CountQueryParameterFilter()
        {
            String request = "OrderItems?$filter=OrderId eq 1&$count=true&$top=1";

            ODataUri  odataUri = Fixture.ParseUri(request);
            IEdmModel edmModel = Fixture.EdmModel.GetEdmModel(odataUri.Path);
            var       parser   = new OeParser(odataUri.ServiceRoot, edmModel);

            var response = new MemoryStream();
            await parser.ExecuteQueryAsync(odataUri, OeRequestHeaders.JsonDefault, response, CancellationToken.None).ConfigureAwait(false);

            response.Position = 0;

            var reader = new ResponseReader(edmModel);

            Assert.Single(reader.Read(response).Cast <Object>());

            int?expectedCount;

            using (var dbContext = Fixture.CreateContext())
                expectedCount = dbContext.OrderItems.Count(i => i.OrderId == 1);

            Assert.Equal(expectedCount, reader.ResourceSet.Count);
        }
Пример #3
0
        public async Task FillNextLinkProperties(OeParser parser, CancellationToken token)
        {
            using (var response = new MemoryStream())
                foreach (KeyValuePair <Object, Dictionary <PropertyInfo, NavigationInfo> > navigationPropertyEntity in NavigationInfoEntities)
                {
                    foreach (KeyValuePair <PropertyInfo, NavigationInfo> propertyResourceSet in navigationPropertyEntity.Value)
                    {
                        Uri requestUri = propertyResourceSet.Value.NextPageLink;
                        while (requestUri != null)
                        {
                            response.SetLength(0);
                            await parser.ExecuteGetAsync(requestUri, OeRequestHeaders.JsonDefault, response, token).ConfigureAwait(false);

                            response.Position = 0;

                            var navigationPropertyReader = new ResponseReader(EdmModel, _serviceProvider);
                            AddItems(navigationPropertyEntity.Key, propertyResourceSet.Key, navigationPropertyReader.Read(response));
                            await navigationPropertyReader.FillNextLinkProperties(parser, token).ConfigureAwait(false);

                            requestUri = navigationPropertyReader.ResourceSet.NextPageLink;
                        }
                    }
                }
        }
Пример #4
0
        public async Task NavigationNextPageLink()
        {
            String request = "Orders?$expand=Items($filter=Count gt 0 or Count eq null;$orderby=Id;$count=true)&$orderby=Id&$count=true";

            ODataUri         odataUri       = Fixture.ParseUri(request);
            var              parser         = new OeParser(odataUri.ServiceRoot, Fixture.OeDataAdapter, Fixture.EdmModel);
            var              requestUri     = new Uri(odataUri.ServiceRoot, request);
            Uri              uri            = requestUri;
            OeRequestHeaders requestHeaders = OeRequestHeaders.JsonDefault.SetMaxPageSize(1).SetNavigationNextLink(true);

            long count  = -1;
            var  fromOe = new List <Object>();

            do
            {
                var response = new MemoryStream();
                await parser.ExecuteGetAsync(uri, requestHeaders, response, CancellationToken.None).ConfigureAwait(false);

                response.Position = 0;

                var           reader = new ResponseReader(Fixture.EdmModel, Fixture.OeDataAdapter.EntitySetMetaAdapters);
                List <Object> result = reader.Read(response).Cast <Object>().ToList();
                Assert.InRange(result.Count, 0, requestHeaders.MaxPageSize);
                fromOe.AddRange(result);

                var navigationPropertyParser = new OeParser(odataUri.ServiceRoot, Fixture.OeDataAdapter, Fixture.EdmModel);
                foreach (dynamic order in result)
                {
                    var navigationProperty           = (IEnumerable)order.Items;
                    ODataResourceSetBase resourceSet = reader.GetResourceSet(navigationProperty);

                    var navigationPropertyResponse = new MemoryStream();
                    await navigationPropertyParser.ExecuteGetAsync(resourceSet.NextPageLink, OeRequestHeaders.JsonDefault, navigationPropertyResponse, CancellationToken.None).ConfigureAwait(false);

                    navigationPropertyResponse.Position = 0;

                    var navigationPropertyReader = new ResponseReader(Fixture.EdmModel, Fixture.OeDataAdapter.EntitySetMetaAdapters);
                    foreach (dynamic orderItem in navigationPropertyReader.Read(navigationPropertyResponse))
                    {
                        order.Items.Add(orderItem);
                    }

                    Assert.Equal(navigationPropertyReader.ResourceSet.Count, order.Items.Count);
                }

                if (count < 0)
                {
                    count = reader.ResourceSet.Count.GetValueOrDefault();
                }

                uri = reader.ResourceSet.NextPageLink;
            }while (uri != null);

            Assert.Equal(count, fromOe.Count);

            var exprectedResponse = new MemoryStream();
            var expectedParser    = new OeParser(odataUri.ServiceRoot, Fixture.OeDataAdapter, Fixture.EdmModel);
            await expectedParser.ExecuteGetAsync(requestUri, OeRequestHeaders.JsonDefault, exprectedResponse, CancellationToken.None).ConfigureAwait(false);

            exprectedResponse.Position = 0;

            var           exprectedReader = new ResponseReader(Fixture.EdmModel, Fixture.OeDataAdapter.EntitySetMetaAdapters);
            List <Object> expectedResult  = exprectedReader.Read(exprectedResponse).Cast <Object>().ToList();

            TestHelper.Compare(expectedResult, fromOe, null);
        }
Пример #5
0
        protected override Object CreateRootEntity(ODataResource resource, IReadOnlyList <NavigationInfo> navigationProperties, Type entityType)
        {
            var openType = new SortedDictionary <String, Object>(StringComparer.Ordinal);

            foreach (ODataProperty property in resource.Properties)
            {
                if (property.Value is ODataUntypedValue)
                {
                    openType.Add(property.Name, null);
                }
                else if (property.Value is ODataEnumValue enumValue)
                {
                    Type enumType = Type.GetType(enumValue.TypeName);
                    openType.Add(property.Name, Enum.Parse(enumType, enumValue.Value));
                }
                else
                {
                    openType.Add(property.Name, property.Value);
                }
            }

            Dictionary <PropertyInfo, NavigationInfo> propertyInfos = null;

            foreach (NavigationInfo navigationInfo in navigationProperties)
            {
                Object value = navigationInfo.Value;

                if (navigationInfo.Count != null || navigationInfo.NextPageLink != null)
                {
                    PropertyInfo clrProperty = entityType.GetProperty(navigationInfo.Name);
                    if (value == null && navigationInfo.NextPageLink != null)
                    {
                        value = ResponseReader.CreateCollection(clrProperty.PropertyType);
                    }

                    if (value is IEnumerable collection)
                    {
                        base.NavigationProperties.Add(collection, navigationInfo);

                        if (propertyInfos == null)
                        {
                            propertyInfos = new Dictionary <PropertyInfo, NavigationInfo>(navigationProperties.Count);
                            base.NavigationInfoEntities.Add(openType, propertyInfos);
                        }
                        propertyInfos.Add(clrProperty, navigationInfo);
                    }
                }

                if (value == null)
                {
                    PropertyInfo clrProprety = entityType.GetProperty(navigationInfo.Name);
                    Type         type        = Parsers.OeExpressionHelper.GetCollectionItemTypeOrNull(clrProprety.PropertyType);
                    if (type == null)
                    {
                        type = clrProprety.PropertyType;
                    }

                    if (Parsers.OeExpressionHelper.IsEntityType(type))
                    {
                        value = clrProprety.PropertyType;
                    }
                }

                openType.Add(navigationInfo.Name, value);
            }

            return(openType);
        }
Пример #6
0
        private static async Task <Object[]> Execute <T>(String request, Object requestData, Func <OrderContext, IEnumerable <T> > fromDbFunc)
        {
            var fixture = new RDBNull_DbFixtureInitDb();
            await fixture.Initalize();

            var parser         = new OeParser(new Uri("http://dummy/"), fixture.OeEdmModel);
            var responseStream = new MemoryStream();

            var requestUri = new Uri(@"http://dummy/" + request);

            if (requestData == null)
            {
                await parser.ExecuteGetAsync(requestUri, OeRequestHeaders.JsonDefault, responseStream, CancellationToken.None);
            }
            else
            {
                String data          = JsonConvert.SerializeObject(requestData);
                var    requestStream = new MemoryStream(Encoding.UTF8.GetBytes(data));
                await parser.ExecutePostAsync(requestUri, OeRequestHeaders.JsonDefault, requestStream, responseStream, CancellationToken.None);
            }

            ODataPath path   = OeParser.ParsePath(fixture.OeEdmModel, new Uri("http://dummy/"), requestUri);
            var       reader = new ResponseReader(fixture.OeEdmModel.GetEdmModel(path));

            responseStream.Position = 0;
            Object[] fromOe;
            if (typeof(T) == typeof(int))
            {
                String count = new StreamReader(responseStream).ReadToEnd();
                fromOe = count == "" ? null : new Object[] { int.Parse(count) };
            }
            else if (typeof(T) == typeof(String))
            {
                String json    = new StreamReader(responseStream).ReadToEnd();
                var    jobject = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(json);
                var    jarray  = (Newtonsoft.Json.Linq.JArray)jobject["value"];
                fromOe = jarray.Select(j => (String)j).ToArray();
            }
            else
            {
                fromOe = reader.Read(responseStream).Cast <Object>().ToArray();
            }

            if (fromDbFunc == null)
            {
                return(fromOe);
            }

            T[] fromDb;
            using (OrderContext orderContext = fixture.CreateContext())
                fromDb = fromDbFunc(orderContext).ToArray();

            var settings = new JsonSerializerSettings()
            {
                DateFormatString      = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff",
                DateTimeZoneHandling  = DateTimeZoneHandling.Utc,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                NullValueHandling     = NullValueHandling.Ignore
            };
            String jsonOe = JsonConvert.SerializeObject(fromOe, settings);
            String jsonDb = JsonConvert.SerializeObject(fromDb, settings);

            Console.WriteLine(requestUri);
            Assert.Equal(jsonDb, jsonOe);

            return(fromOe);
        }
Пример #7
0
        public async Task <IList> ExecuteOe <TResult>(String requestUri, bool navigationNextLink, int pageSize)
        {
            OeModelBoundProvider modelBoundProvider = ModelBoundProvider;

            if (modelBoundProvider == null)
            {
                var modelBoundProviderBuilder = new PageNextLinkModelBoundBuilder(OeEdmModel, IsSqlite);
                modelBoundProvider = modelBoundProviderBuilder.BuildProvider(pageSize, navigationNextLink);
            }

            OeParser         parser         = CreateParser(requestUri, modelBoundProvider);
            var              uri            = new Uri(parser.BaseUri, requestUri);
            OeRequestHeaders requestHeaders = OeRequestHeaders.JsonDefault.SetMaxPageSize(pageSize);

            long     count = -1;
            ODataUri odataUri;
            var      fromOe = new List <Object>();

            do
            {
                odataUri = OeParser.ParseUri(parser.EdmModel, parser.BaseUri, uri);
                var response = new MemoryStream();
                await parser.ExecuteQueryAsync(odataUri, requestHeaders, response, CancellationToken.None).ConfigureAwait(false);

                response.Position = 0;

                List <Object>  result;
                ResponseReader responseReader;
                if (typeof(TResult).IsPrimitive)
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(typeof(TResult));
                    return(new Object[] { converter.ConvertFromString(new StreamReader(response).ReadToEnd()) });
                }
                else if (typeof(TResult) == typeof(Object) && (requestUri.Contains("$apply=") || requestUri.Contains("$compute=")))
                {
                    responseReader = new OpenTypeResponseReader(TestHelper.GetEdmModel(DbEdmModel, odataUri.Path));
                    result         = responseReader.Read(response).Cast <Object>().ToList();
                }
                else
                {
                    responseReader = new ResponseReader(TestHelper.GetEdmModel(DbEdmModel, odataUri.Path));
                    result         = responseReader.Read(response).Cast <Object>().ToList();
                }

                if (pageSize > 0)
                {
                    Xunit.Assert.InRange(result.Count, 0, requestHeaders.MaxPageSize);
                }
                fromOe.AddRange(result);

                await responseReader.FillNextLinkProperties(parser, CancellationToken.None).ConfigureAwait(false);

                if (count < 0)
                {
                    count = responseReader.ResourceSet.Count.GetValueOrDefault();
                }

                uri = responseReader.ResourceSet.NextPageLink;
            }while (uri != null);

            if (odataUri.QueryCount != null)
            {
                Xunit.Assert.Equal(count, fromOe.Count);
            }

            return(fromOe);
        }
Пример #8
0
        public async Task <IList> ExecuteOe <TResult>(String requestUri, bool navigationNextLink, int pageSize)
        {
            ODataUri         odataUri       = ParseUri(requestUri);
            var              parser         = new OeParser(odataUri.ServiceRoot, OeDataAdapter, EdmModel);
            var              uri            = new Uri(odataUri.ServiceRoot, requestUri);
            OeRequestHeaders requestHeaders = OeRequestHeaders.JsonDefault.SetMaxPageSize(pageSize).SetNavigationNextLink(navigationNextLink);

            long count  = -1;
            var  fromOe = new List <Object>();

            do
            {
                var response = new MemoryStream();
                await parser.ExecuteGetAsync(uri, requestHeaders, response, CancellationToken.None).ConfigureAwait(false);

                response.Position = 0;

                List <Object>  result;
                ResponseReader responseReader;
                if (typeof(TResult).IsPrimitive)
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(typeof(TResult));
                    return(new Object[] { converter.ConvertFromString(new StreamReader(response).ReadToEnd()) });
                }
                else if (typeof(TResult) == typeof(Object))
                {
                    responseReader = new OpenTypeResponseReader(EdmModel, DbDataAdapter);
                    result         = responseReader.Read(response).Cast <Object>().ToList();
                }
                else
                {
                    responseReader = new ResponseReader(EdmModel, DbDataAdapter);
                    result         = responseReader.Read <TResult>(response).Cast <Object>().ToList();
                }

                if (pageSize > 0)
                {
                    Xunit.Assert.InRange(result.Count, 0, requestHeaders.MaxPageSize);
                }
                fromOe.AddRange(result);

                foreach (Object entity in fromOe)
                {
                    await responseReader.FillNextLinkProperties(odataUri.ServiceRoot, entity, CancellationToken.None).ConfigureAwait(false);
                }

                if (count < 0)
                {
                    count = responseReader.ResourceSet.Count.GetValueOrDefault();
                }

                uri = responseReader.ResourceSet.NextPageLink;
            }while (uri != null);

            if (odataUri.QueryCount != null)
            {
                Xunit.Assert.Equal(count, fromOe.Count);
            }

            return(fromOe);
        }
Пример #9
0
        internal static async Task <List <Object> > OrdersCountItemsCount(DbFixture fixture, String request, Func <Model.OrderItem, bool> orderItemPredicate,
                                                                          int maxPageSize, bool navigationNextLink)
        {
            Query.OeModelBoundProvider modelBoundProvider = fixture.ModelBoundProvider;
            if (modelBoundProvider == null)
            {
                var modelBoundProviderBuilder = new PageNextLinkModelBoundBuilder(fixture.OeEdmModel, fixture.IsSqlite);
                modelBoundProvider = modelBoundProviderBuilder.BuildProvider(maxPageSize, navigationNextLink);
            }

            OeParser parser = fixture.CreateParser(request, modelBoundProvider);
            Uri      uri    = new Uri(parser.BaseUri, request);

            int  expectedCount;
            long count  = -1;
            var  fromOe = new List <Object>();

            do
            {
                var response = new MemoryStream();
                await parser.ExecuteGetAsync(uri, OeRequestHeaders.JsonDefault.SetMaxPageSize(maxPageSize), response, CancellationToken.None).ConfigureAwait(false);

                response.Position = 0;

                var           reader = new ResponseReader(parser.EdmModel);
                List <Object> result = reader.Read(response).Cast <Object>().ToList();
                fromOe.AddRange(result);

                if (maxPageSize > 0)
                {
                    Assert.InRange(result.Count, 0, maxPageSize);
                }

                var navigationPropertyParser = new OeParser(parser.BaseUri, parser.EdmModel, fixture.ModelBoundProvider);
                foreach (dynamic order in result)
                {
                    using (var dbContext = fixture.CreateContext())
                    {
                        String orderName = order.Name;
                        expectedCount = dbContext.OrderItems.Where(i => i.Order.Name == orderName).ToList().Where(orderItemPredicate).Count();
                    }

                    var navigationProperty             = (IEnumerable)order.Items;
                    ResponseReader.NavigationInfo info = reader.GetNavigationInfo(navigationProperty);

                    if (!navigationNextLink && !uri.OriginalString.Contains("$skiptoken="))
                    {
                        Assert.Equal(expectedCount, info.Count);
                    }

                    var navigationPropertyResponse = new MemoryStream();
                    await navigationPropertyParser.ExecuteGetAsync(info.NextPageLink, OeRequestHeaders.JsonDefault, navigationPropertyResponse, CancellationToken.None).ConfigureAwait(false);

                    navigationPropertyResponse.Position = 0;

                    var navigationPropertyReader = new ResponseReader(parser.EdmModel);
                    foreach (dynamic orderItem in navigationPropertyReader.Read(navigationPropertyResponse))
                    {
                        order.Items.Add(orderItem);
                    }

                    if (navigationNextLink)
                    {
                        Assert.Equal(expectedCount, navigationPropertyReader.ResourceSet.Count);
                    }
                }

                if (count < 0)
                {
                    count = reader.ResourceSet.Count.GetValueOrDefault();
                }

                uri = reader.ResourceSet.NextPageLink;
            }while (uri != null);

            using (var dbContext = fixture.CreateContext())
                expectedCount = dbContext.Orders.Count();
            Assert.Equal(expectedCount, count);

            return(fromOe);
        }