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