Ejemplo n.º 1
0
        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());
            }
        }
Ejemplo n.º 2
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().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);
        }
Ejemplo n.º 3
0
        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 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);
            }
        }
Ejemplo n.º 5
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);
        }