protected static void TestQuery(EntityProvider pro, Expression query, string testName, bool expectedToFail)
        {
            if (query.NodeType == ExpressionType.Convert && query.Type == typeof(object))
            {
                query = ((UnaryExpression)query).Operand; // remove box
            }

            if (pro.Log != null)
            {
                DbExpressionWriter.Write(pro.Log, query);
                pro.Log.WriteLine();
                pro.Log.WriteLine("==>");
            }

            string queryText = pro.GetQueryText(query);
            SetNewBaseLine(testName, queryText);

            if (_executeQueries)
            {
                object result = pro.Execute(query);
                IEnumerable seq = result as IEnumerable;
                if (seq != null)
                {
                    // iterate results
                    foreach (var item in seq)
                    {
                    }
                }
                else
                {
                    IDisposable disposable = result as IDisposable;
                    if (disposable != null)
                        disposable.Dispose();
                }
            }
            else if (pro.Log != null)
            {
                var text = pro.GetQueryText(query);
                pro.Log.WriteLine(text);
                pro.Log.WriteLine();
            }

            string expected = GetBaseLine(testName);
            if (expected != null)
            {
                string trimActual = TrimExtraWhiteSpace(queryText).Trim();
                string trimExpected = TrimExtraWhiteSpace(expected).Trim();
                Assert.AreEqual(trimExpected, trimActual);
            }
        }
Exemple #2
0
        protected void TestQuery(EntityProvider pro, Expression query, bool expectedToFail)
        {
            if (query.NodeType == ExpressionType.Convert && query.Type == typeof(object))
            {
                query = ((UnaryExpression)query).Operand; // remove box
            }

            this.queryText = null;
            this.queryText = pro.GetQueryText(query);
            WriteBaseline(baselineKey, queryText);

            if (this.executeQueries)
            {
                Exception caught = null;
                try
                {
                    object result = pro.Execute(query);
                    IEnumerable seq = result as IEnumerable;
                    if (seq != null)
                    {
                        // iterate results
                        foreach (var item in seq)
                        {
                        }
                    }
                    else
                    {
                        IDisposable disposable = result as IDisposable;
                        if (disposable != null)
                            disposable.Dispose();
                    }
                }
                catch (Exception e)
                {
                    caught = e;

                    if (!expectedToFail)
                    {
                        throw new TestFailureException(e.Message + "\r\n\r\n" + queryText);
                    }
                }

                if (caught == null && expectedToFail)
                {
                    throw new InvalidOperationException("Query succeeded when expected to fail");
                }
            }

            string baseline = null;
            if (this.baselines != null && this.baselines.TryGetValue(this.baselineKey, out baseline))
            {
                string trimAct = TrimExtraWhiteSpace(queryText).Trim();
                string trimBase = TrimExtraWhiteSpace(baseline).Trim();
                if (trimAct != trimBase)
                {
                    throw new InvalidOperationException(string.Format("Query translation does not match baseline:\r\n    Expected: {0}\r\n    Actual  : {1}", trimBase, trimAct));
                }
            }

            if (baseline == null && this.baselines != null)
            {
                throw new InvalidOperationException("No baseline");
            }
        }
        protected void TestQuery(EntityProvider pro, Expression query, string baselineKey, bool expectedToFail)
        {
            ConsoleColor originalColor = Console.ForegroundColor;
            try
            {
                if (query.NodeType == ExpressionType.Convert && query.Type == typeof(object))
                {
                    query = ((UnaryExpression)query).Operand; // remove box
                }

                if (pro.Log != null)
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    DbExpressionWriter.Write(pro.Log, pro.Language, query);
                    pro.Log.WriteLine();
                    pro.Log.WriteLine("==>");
                }

                string queryText = null;
                try
                {
                    queryText = pro.GetQueryText(query);
                    WriteBaseline(baselineKey, queryText);
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(string.Format("Query translation failed for {0}", baselineKey));
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine(query.ToString());
                    throw new TestFailureException(e.Message);
                }

                if (this.executeQueries)
                {
                    Exception caught = null;
                    try
                    {
                        object result = pro.Execute(query);
                        IEnumerable seq = result as IEnumerable;
                        if (seq != null)
                        {
                            // iterate results
                            foreach (var item in seq)
                            {
                            }
                        }
                        else
                        {
                            IDisposable disposable = result as IDisposable;
                            if (disposable != null)
                                disposable.Dispose();
                        }
                    }
                    catch (Exception e)
                    {
                        caught = e;
                        if (!expectedToFail)
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("Query failed to execute:");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(queryText);
                            throw new TestFailureException(e.Message);
                        }
                    }
                    if (caught == null && expectedToFail)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Query succeeded when expected to fail");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(queryText);
                        throw new TestFailureException(null);
                    }
                }
                else if (pro.Log != null)
                {
                    var text = pro.GetQueryText(query);
                    pro.Log.WriteLine(text);
                    pro.Log.WriteLine();
                }

                string baseline = null;
                if (this.baselines != null && this.baselines.TryGetValue(baselineKey, out baseline))
                {
                    string trimAct = TrimExtraWhiteSpace(queryText).Trim();
                    string trimBase = TrimExtraWhiteSpace(baseline).Trim();
                    if (trimAct != trimBase)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Query translation does not match baseline:");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(queryText);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("---- current ----");
                        WriteDifferences(trimAct, trimBase);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("---- baseline ----");
                        WriteDifferences(trimBase, trimAct);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        throw new TestFailureException("Translation differed from baseline.");
                    }
                }

                if (baseline == null && this.baselines != null)
                {
                    throw new TestFailureException("No baseline");
                }
            }
            finally
            {
                Console.ForegroundColor = originalColor;
            }
        }
        protected void TestQuery(EntityProvider pro, Expression query, bool expectedToFail)
        {
            if (query.NodeType == ExpressionType.Convert && query.Type == typeof(object))
            {
                query = ((UnaryExpression)query).Operand; // remove box
            }

            this.queryText = null;
            this.queryText = pro.GetQueryText(query);
            WriteBaseline(baselineKey, queryText);

            if (this.executeQueries)
            {
                Exception caught = null;
                try
                {
                    object      result = pro.Execute(query);
                    IEnumerable seq    = result as IEnumerable;
                    if (seq != null)
                    {
                        // iterate results
                        foreach (var item in seq)
                        {
                        }
                    }
                    else
                    {
                        IDisposable disposable = result as IDisposable;
                        if (disposable != null)
                        {
                            disposable.Dispose();
                        }
                    }
                }
                catch (Exception e)
                {
                    caught = e;

                    if (!expectedToFail)
                    {
                        throw new TestFailureException(e.Message + "\r\n\r\n" + queryText);
                    }
                }

                if (caught == null && expectedToFail)
                {
                    throw new InvalidOperationException("Query succeeded when expected to fail");
                }
            }

            string baseline = null;

            if (this.baselines != null && this.baselines.TryGetValue(this.baselineKey, out baseline))
            {
                string trimAct  = TrimExtraWhiteSpace(queryText).Trim();
                string trimBase = TrimExtraWhiteSpace(baseline).Trim();
                if (trimAct != trimBase)
                {
                    throw new InvalidOperationException(string.Format("Query translation does not match baseline:\r\n    Expected: {0}\r\n    Actual  : {1}", trimBase, trimAct));
                }
            }

            if (baseline == null && this.baselines != null)
            {
                throw new InvalidOperationException("No baseline");
            }
        }
Exemple #5
0
        protected void TestQuery(EntityProvider pro, Expression query, string baselineKey, bool expectedToFail)
        {
            ConsoleColor originalColor = Console.ForegroundColor;

            try
            {
                if (query.NodeType == ExpressionType.Convert && query.Type == typeof(object))
                {
                    query = ((UnaryExpression)query).Operand; // remove box
                }

                if (pro.Log != null)
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    DbExpressionWriter.Write(pro.Log, pro.Language, query);
                    pro.Log.WriteLine();
                    pro.Log.WriteLine("==>");
                }

                string queryText = null;
                try
                {
                    queryText = pro.GetQueryText(query);
                    WriteBaseline(baselineKey, queryText);
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(string.Format("Query translation failed for {0}", baselineKey));
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine(query.ToString());
                    throw new TestFailureException(e.Message);
                }

                if (this.executeQueries)
                {
                    Exception caught = null;
                    try
                    {
                        object      result = pro.Execute(query);
                        IEnumerable seq    = result as IEnumerable;
                        if (seq != null)
                        {
                            // iterate results
                            foreach (var item in seq)
                            {
                            }
                        }
                        else
                        {
                            IDisposable disposable = result as IDisposable;
                            if (disposable != null)
                            {
                                disposable.Dispose();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        caught = e;
                        if (!expectedToFail)
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("Query failed to execute:");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(queryText);
                            throw new TestFailureException(e.Message);
                        }
                    }
                    if (caught == null && expectedToFail)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Query succeeded when expected to fail");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(queryText);
                        throw new TestFailureException(null);
                    }
                }
                else if (pro.Log != null)
                {
                    var text = pro.GetQueryText(query);
                    pro.Log.WriteLine(text);
                    pro.Log.WriteLine();
                }

                string baseline = null;
                if (this.baselines != null && this.baselines.TryGetValue(baselineKey, out baseline))
                {
                    string trimAct  = TrimExtraWhiteSpace(queryText).Trim();
                    string trimBase = TrimExtraWhiteSpace(baseline).Trim();
                    if (trimAct != trimBase)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Query translation does not match baseline:");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(queryText);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("---- current ----");
                        WriteDifferences(trimAct, trimBase);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("---- baseline ----");
                        WriteDifferences(trimBase, trimAct);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        throw new TestFailureException("Translation differed from baseline.");
                    }
                }

                if (baseline == null && this.baselines != null)
                {
                    throw new TestFailureException("No baseline");
                }
            }
            finally
            {
                Console.ForegroundColor = originalColor;
            }
        }