public async Task Add()
        {
            var fixture = new RDBNull_DbFixtureInitDb();
            await fixture.Initalize().ConfigureAwait(false);

            using (var orderContext = fixture.CreateContext())
            {
                Assert.Equal(8, orderContext.Categories.Count());
                Assert.Equal(4, orderContext.Customers.Count());
                Assert.Equal(4, orderContext.Orders.Count());
                Assert.Equal(7, orderContext.OrderItems.Count());
                Assert.Equal(5, orderContext.ShippingAddresses.Count());
                Assert.Equal(5, orderContext.CustomerShippingAddress.Count());

                var category = orderContext.Categories.Single(t => t.Name == "jackets");
                Assert.Equal("clothes", orderContext.Categories.Single(t => t.Id == category.ParentId).Name);
                Assert.Equal(2, orderContext.Categories.AsQueryable().Where(t => t.ParentId == category.Id).Count());

                var order1 = orderContext.Orders.Include(t => t.Items).Single(t => t.Name == "Order 1");
                Assert.Equal(3, order1.Items.Count());

                var order2 = orderContext.Orders.Include(t => t.Items).Single(t => t.Name == "Order 2");
                Assert.Equal(2, order2.Items.Count());

                var order3 = orderContext.Orders.Include(t => t.Items).Single(t => t.Name == "Order unknown");
                Assert.Equal(2, order3.Items.Count());
            }
        }
        private static async Task <Object[]> Execute <T>(String request, Object requestData, Func <OrderContext, IEnumerable <T> > fromDbFunc)
        {
            var fixture = new RDBNull_DbFixtureInitDb();
            await fixture.Initalize().ConfigureAwait(false);

            var responseStream = new MemoryStream();

            OeParser parser     = fixture.CreateParser(request, null);
            ODataUri odataUri   = fixture.ParseUri(request);
            var      requestUri = new Uri(parser.BaseUri, request);

            if (requestData == null)
            {
                await parser.ExecuteOperationAsync(odataUri, OeRequestHeaders.JsonDefault, null, responseStream, CancellationToken.None).ConfigureAwait(false);
            }
            else
            {
                String data          = fixture.SerializeRequestData(requestData);
                var    requestStream = new MemoryStream(Encoding.UTF8.GetBytes(data));
                await parser.ExecuteOperationAsync(odataUri, OeRequestHeaders.JsonDefault, requestStream, responseStream, CancellationToken.None).ConfigureAwait(false);
            }

            ODataPath path   = OeParser.ParsePath(fixture.DbEdmModel, parser.BaseUri, requestUri);
            var       reader = new ResponseReader(fixture.DbEdmModel.GetEdmModel(path), fixture.ServiceProvider);

            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();

            TestHelper.Compare(fromDb, fromOe, null);
            return(fromOe);
        }
Exemple #3
0
        public async Task ScalarFunctionWithParameters_get()
        {
            String request = "dbo.ScalarFunctionWithParameters(name='Order 1',id=1,status=null)";

            Object[] result = await Execute <int>(request, null, null);

            var fixture = new RDBNull_DbFixtureInitDb();

            using (OrderContext orderContext = fixture.CreateContext())
            {
                int count = orderContext.ScalarFunctionWithParameters(1, "Order 1", null);
                Assert.Equal(count, (int)result[0]);
            }
        }
Exemple #4
0
        public async Task ScalarFunction_get()
        {
            String request = "dbo.ScalarFunction";

            Object[] result = await Execute <int>(request, null, null);

            var fixture = new RDBNull_DbFixtureInitDb();

            using (OrderContext orderContext = fixture.CreateContext())
            {
                int count = orderContext.ScalarFunction();
                Assert.Equal(count, (int)result[0]);
            }
        }
Exemple #5
0
        public async Task ScalarFunctionWithParameters_post()
        {
            String request     = "dbo.ScalarFunctionWithParameters";
            var    requestData = new { id = (int?)1, name = "Order 1", status = (OrderStatus?)null };

            Object[] result = await Execute <int>(request, requestData, null);

            var fixture = new RDBNull_DbFixtureInitDb();

            using (OrderContext orderContext = fixture.CreateContext())
            {
                int count = orderContext.ScalarFunctionWithParameters(1, "Order 1", null);
                Assert.Equal(count, (int)result[0]);
            }
        }
Exemple #6
0
        public async Task ResetDb_post()
        {
            String request = "ResetDb";

            await Execute <int>(request, "", null);

            var fixture = new RDBNull_DbFixtureInitDb();

            using (OrderContext orderContext = fixture.CreateContext())
            {
                int count = orderContext.Categories.Count() +
                            orderContext.Customers.Count() +
                            orderContext.Orders.Count() +
                            orderContext.OrderItems.Count();
                Assert.Equal(0, count);
            }
        }
        public async Task Delete()
        {
            var fixture = new RDBNull_DbFixtureInitDb();
            await fixture.Initalize().ConfigureAwait(false);

            await DbFixture.ExecuteBatchAsync(fixture.OeEdmModel, "Delete").ConfigureAwait(false);

            using (var orderContext = fixture.CreateContext())
            {
                Assert.Equal(5, orderContext.Categories.Count());
                Assert.Equal(4, orderContext.Customers.Count());
                Assert.Equal(3, orderContext.Orders.Count());
                Assert.Equal(3, orderContext.OrderItems.Count());
                Assert.Equal(2, orderContext.ShippingAddresses.Count());
                Assert.Equal(2, orderContext.CustomerShippingAddress.Count());

                var order1 = orderContext.Orders.Include(t => t.Items).Single(t => t.Name == "Order 1");
                Assert.Equal("Product order 1 item 3", order1.Items.Single().Product);
            }
        }
        public void Test()
        {
            SelectTestDefinition[] requestMethodNames = SelectTestDefinition.GetSelectTestDefinitions();
            requestMethodNames = requestMethodNames.Where(t => t.MethodName == "FilterEnum" || t.MethodName == "FilterEnumNull").ToArray();

            var fixture = new RDBNull_DbFixtureInitDb();

            for (int i = 0; i < requestMethodNames.Length; i++)
            {
                var queryContext1 = new OeQueryContext(fixture.OeEdmModel, fixture.ParseUri(requestMethodNames[i].Request));
                var queryContext2 = new OeQueryContext(fixture.OeEdmModel, fixture.ParseUri(requestMethodNames[i].Request));

                var constantToParameterMapper = new FakeReadOnlyDictionary <ConstantNode, OeQueryCacheDbParameterDefinition>();
                if (queryContext1.ODataUri.Skip != null)
                {
                    var constantNode = OeCacheComparerParameterValues.CreateSkipConstantNode((int)queryContext1.ODataUri.Skip.Value, queryContext1.ODataUri.Path);
                    constantToParameterMapper.Add(constantNode, new OeQueryCacheDbParameterDefinition("p_0", typeof(int)));
                }
                if (queryContext1.ODataUri.Top != null)
                {
                    var constantNode = OeCacheComparerParameterValues.CreateTopConstantNode((int)queryContext1.ODataUri.Top.Value, queryContext1.ODataUri.Path);
                    constantToParameterMapper.Add(constantNode, new OeQueryCacheDbParameterDefinition($"p_{constantToParameterMapper.Count}", typeof(int)));
                }

                OeCacheContext cacheContext1 = queryContext1.CreateCacheContext();
                OeCacheContext cacheContext2 = queryContext2.CreateCacheContext();
                bool           result        = new OeCacheComparer(constantToParameterMapper).Compare(cacheContext1, cacheContext2);
                Assert.True(result);

                for (int j = i + 1; j < requestMethodNames.Length; j++)
                {
                    queryContext2 = new OeQueryContext(fixture.OeEdmModel, fixture.ParseUri(requestMethodNames[j].Request));

                    constantToParameterMapper = new FakeReadOnlyDictionary <ConstantNode, OeQueryCacheDbParameterDefinition>();
                    result = new OeCacheComparer(constantToParameterMapper).Compare(cacheContext1, cacheContext2);
                    Assert.False(result);
                }
            }
        }
        //[Fact]
        public void CacheCode()
        {
            var hashes = new Dictionary <int, List <String> >();

            SelectTestDefinition[] requestMethodNames = SelectTestDefinition.GetSelectTestDefinitions();

            var fixture = new RDBNull_DbFixtureInitDb();

            for (int i = 0; i < requestMethodNames.Length; i++)
            {
                var queryContext = new OeQueryContext(fixture.OeEdmModel, fixture.ParseUri(requestMethodNames[i].Request));
                int hash         = OeCacheComparer.GetCacheCode(queryContext.CreateCacheContext());
                if (!hashes.TryGetValue(hash, out List <String> value))
                {
                    value = new List <String>();
                    hashes.Add(hash, value);
                }
                value.Add(requestMethodNames[i].MethodName);
            }

            var duplicate = hashes.Where(p => p.Value.Count > 1).Select(p => p.Value).ToArray();
        }
        public async Task Update()
        {
            var fixture = new RDBNull_DbFixtureInitDb();
            await fixture.Initalize().ConfigureAwait(false);

            await DbFixture.ExecuteBatchAsync(fixture.OeEdmModel, "Update").ConfigureAwait(false);

            using (var orderContext = fixture.CreateInMemoryContext())
            {
                var category = orderContext.Categories.Single(t => t.Name == "sombrero jacket");
                Assert.Equal("jackets", orderContext.Categories.Single(t => t.Id == category.ParentId).Name);

                Assert.Equal(4, orderContext.Customers.Count());
                Assert.Equal(4, orderContext.Orders.Count());
                Assert.Equal(7, orderContext.OrderItems.Count());

                Assert.Equal("New Order 1", orderContext.Orders.Single(t => t.Id == 1).Name);
                Assert.Equal("New Product order 1 item 3", orderContext.OrderItems.Single(t => t.OrderId == 1 && t.Id == 3).Product);

                Assert.Equal(Sex.Female, orderContext.Customers.Single(c => c.Country == "RU" && c.Id == 1).Sex);
                Assert.Null(orderContext.Customers.Single(c => c.Country == "EN" && c.Id == 1).Sex);
            }
        }
Exemple #11
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);
        }