TestLINQProvider.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » CmsServices » Test » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » CmsServices » Test » TestLINQProvider.cs
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Everest.CmsServices.LINQ;
using System.Reflection;
namespace Everest.CmsServices.Test{
    internal class MockContentQueryProvider : ContentQueryProvider
    {
        public override IQueryable<TElement> CreateQuery<TElement>(Expression expression)
        {
            return (IQueryable<TElement>)new MockContentQueryable(expression);
        }

        public override IQueryable CreateQuery(Expression expression)
        {
            Type elementType = TypeSystem.GetElementType(expression.Type);
            try
            {
                return (IQueryable)Activator.CreateInstance(typeof(MockContentQueryable), new object[] { this, expression });
            }
            catch (TargetInvocationException tie)
            {
                throw tie.InnerException;
            }
        }
        protected override object Count(StatementExpression exp)
        {
            if (exp.QueryStatement == "1=1")
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        protected override object First(StatementExpression exp)
        {
            return new ContentContainer(exp.Parameters ?? new Dictionary<string, object>());
        }

        protected override object FirstOrDefault(StatementExpression exp)
        {
            return null;
        }

        protected override object Last(StatementExpression exp)
        {
            return new ContentContainer(exp.Parameters ?? new Dictionary<string, object>());
        }

        protected override object LastOrDefault(StatementExpression exp)
        {
            return null;
        }

        protected override object ExecuteCore(StatementExpression exp)
        {
            List<ContentContainer> list = new List<ContentContainer>();

            for (int i = 0; i < exp.TakeCount; i++)
            {
                list.Add(new ContentContainer());
            }
            return list;
        }
    }
    public class MockContentQueryable : ContentQueryable<ContentContainer>
    {
        public MockContentQueryable()
            : base()
        { }
        public MockContentQueryable(Expression expression)
            : base(expression)
        {
        }
        public override IQueryProvider Provider
        {
            get
            {
                return new MockContentQueryProvider();
            }
        }
    }
    /// <summary>
    /// Summary description for LINQProviderTest
    /// </summary>
    [TestClass]
    public class TestLINQProvider
    {
        public TestLINQProvider()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestContentQueryTranslator1()
        {
            string field = "ContentId";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<int>(field) == 10;
            ContentQueryTranslator translator = new ContentQueryTranslator();

            var statementExpression = translator.Translate(expression);
            Assert.AreEqual("[ContentId] = {ContentParam0}", statementExpression.QueryStatement);
            Assert.AreEqual(10, statementExpression.Parameters["ContentParam0"]);
        }

        [TestMethod]
        public void TestContentQueryTranslator2()
        {
            string field = "ContentStatus";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<bool>(field) == true;
            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(expression);
            Assert.AreEqual("[ContentStatus] = {ContentParam0}", statementExpression.QueryStatement);
            Assert.AreEqual(1, statementExpression.Parameters["ContentParam0"]);
        }

        [TestMethod]
        public void TestContentQueryTranslator3()
        {
            string field = "ContentId";
            string field2 = "ContentStatus";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<int>(field) == 10 && cd.GetColumn<bool>(field2) == true;
            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(expression);

            Assert.AreEqual("([ContentId] = {ContentParam0} AND [ContentStatus] = {ContentParam1})", statementExpression.QueryStatement);
            Assert.AreEqual(10, statementExpression.Parameters["ContentParam0"]);
            Assert.AreEqual(1, statementExpression.Parameters["ContentParam1"]);
        }

        [TestMethod]
        public void TestContentQueryTranslator4()
        {
            string field = "ContentId";
            string field2 = "ContentStatus";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<int>(field) == 10 || cd.GetColumn<bool>(field2) != false;
            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(expression);

            Assert.AreEqual("([ContentId] = {ContentParam0} OR [ContentStatus] <> {ContentParam1})", statementExpression.QueryStatement);
            Assert.AreEqual(10, statementExpression.Parameters["ContentParam0"]);
            Assert.AreEqual(0, statementExpression.Parameters["ContentParam1"]);
        }

        [TestMethod]
        public void TestContentQueryTranslator5()
        {
            string field = "ContentId";
            string field2 = "ContentStatus";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<int>(field) == 10 || cd.GetColumn<string>("Title") == "title1" || cd.GetColumn<bool>(field2) != false;
            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(expression);

            Assert.AreEqual("(([ContentId] = {ContentParam0} OR [Title] = {ContentParam1}) OR [ContentStatus] <> {ContentParam2})", statementExpression.QueryStatement);
            Assert.AreEqual(10, statementExpression.Parameters["ContentParam0"]);
            Assert.AreEqual("title1", statementExpression.Parameters["ContentParam1"]);
            Assert.AreEqual(0, statementExpression.Parameters["ContentParam2"]);
        }

        [TestMethod]
        public void TestContentQueryTranslator6()
        {
            string field = "ContentId";
            string field2 = "ContentStatus";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<int>(field) == 10 || (cd.GetColumn<string>("Title") == "title1" && cd.GetColumn<bool>(field2) != false);
            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(expression);
            Assert.AreEqual("([ContentId] = {ContentParam0} OR ([Title] = {ContentParam1} AND [ContentStatus] <> {ContentParam2}))", statementExpression.QueryStatement);
            Assert.AreEqual(10, statementExpression.Parameters["ContentParam0"]);
            Assert.AreEqual("title1", statementExpression.Parameters["ContentParam1"]);
            Assert.AreEqual(0, statementExpression.Parameters["ContentParam2"]);
        }

        [TestMethod]
        public void TestContentQueryTranslator7()
        {
            string field = "ContentId";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<int>(field) == 10 && true;
            ContentQueryTranslator translator = new ContentQueryTranslator();

            var statementExpression = translator.Translate(expression);
            Assert.AreEqual("([ContentId] = {ContentParam0} AND 1=1)", statementExpression.QueryStatement);
            Assert.AreEqual(10, statementExpression.Parameters["ContentParam0"]);
        }

        [TestMethod]
        public void TestContentQueryTranslator8()
        {
            string field = "ContentId";
            Expression<Func<ContentContainer, bool>> expression = cd => 10 == cd.GetColumn<int>(field);
            ContentQueryTranslator translator = new ContentQueryTranslator();

            var statementExpression = translator.Translate(expression);
            Assert.AreEqual("[ContentId] = {ContentParam0}", statementExpression.QueryStatement);
            Assert.AreEqual(10, statementExpression.Parameters["ContentParam0"]);
        }

        [TestMethod]
        public void TestContentQueryTranslator9()
        {
            string field = "ContentId";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<int>(field) == cd.GetColumn<int>(field);
            ContentQueryTranslator translator = new ContentQueryTranslator();

            var statementExpression = translator.Translate(expression);
            Assert.AreEqual("[ContentId] = [ContentId]", statementExpression.QueryStatement);
        }

        [TestMethod]
        public void TestContentQueryTranslator10()
        {
            string field = "Title";
            Expression<Func<ContentContainer, bool>> expression = cd => string.IsNullOrEmpty(field);
            ContentQueryTranslator translator = new ContentQueryTranslator();

            var statementExpression = translator.Translate(expression);
            Assert.AreEqual("1<>1", statementExpression.QueryStatement);
        }

        [TestMethod]
        public void TestTranslateContains()
        {
            string field = "Title";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<string>(field).Contains("title1");
            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(expression);

            Assert.AreEqual("[Title] LIKE '%' + {ContentParam0} + '%'", statementExpression.QueryStatement);

            Assert.AreEqual("title1", statementExpression.Parameters["ContentParam0"]);
        }

        [TestMethod]
        public void TestTranslateEquals()
        {
            string field = "ContentStatus";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<int>(field).Equals(0);
            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(expression);

            Assert.AreEqual("[ContentStatus] = {ContentParam0}", statementExpression.QueryStatement);

            Assert.AreEqual(0, statementExpression.Parameters["ContentParam0"]);
        }

        [ExpectedException(typeof(NotSupportedException))]
        [TestMethod]
        public void TestContentQueryUnsupported1()
        {
            string field = "Title";
            Expression<Func<ContentContainer, bool>> expression = cd => cd.GetColumn<string>(field).IsNormalized();
            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(expression);
        }

        [TestMethod]
        public void TestTranslateWhere()
        {
            string field = "ContentId";
            string field2 = "ContentStatus";

            MockContentQueryable mockContainer = new MockContentQueryable();
            var MockContentQueryable = mockContainer.Where(cd => cd.GetColumn<int>(field) == 10);
            MockContentQueryable = MockContentQueryable.Where(cd => cd.GetColumn<bool>(field2) != false);

            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(MockContentQueryable.Expression);

            Assert.AreEqual("((1=1 AND [ContentId] = {ContentParam0}) AND [ContentStatus] <> {ContentParam1})", statementExpression.QueryStatement);
            Assert.AreEqual(10, statementExpression.Parameters["ContentParam0"]);
            Assert.AreEqual(0, statementExpression.Parameters["ContentParam1"]);
        }
        [TestMethod]
        public void TestTranslateSkip()
        {
            string field = "ContentId";
            string field2 = "ContentStatus";

            MockContentQueryable mockContainer = new MockContentQueryable();
            var MockContentQueryable = mockContainer.Where(cd => cd.GetColumn<int>(field) == 10);
            MockContentQueryable = MockContentQueryable.Where(cd => cd.GetColumn<bool>(field2) != false);
            MockContentQueryable = MockContentQueryable.Skip(10).Skip(10).Skip(10);

            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(MockContentQueryable.Expression);

            Assert.AreEqual("((1=1 AND [ContentId] = {ContentParam0}) AND [ContentStatus] <> {ContentParam1})", statementExpression.QueryStatement);
            Assert.AreEqual(30, statementExpression.SkipCount);

        }

        [TestMethod]
        public void TestTranslateOrderBy()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            var contentQueryable = mockContainer.Where(cd => true && string.IsNullOrEmpty(""));
            var ordered = contentQueryable.OrderBy(cd => cd.GetColumn<int>("ContentId"));

            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(ordered.Expression);

            Assert.AreEqual(" [ContentId] ASC", statementExpression.OrderBy);
        }

        [TestMethod]
        public void TestTranslateOrderByDescending()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            var contentQueryable = mockContainer.Where(cd => true && string.IsNullOrEmpty(""));
            contentQueryable = contentQueryable.OrderByDescending(cd => cd.GetColumn<int>("ContentId"));

            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(contentQueryable.Expression);

            Assert.AreEqual(" [ContentId] DESC", statementExpression.OrderBy);
        }

        [TestMethod]
        public void TestCount1()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            //var MockContentQueryable = mockContainer.Where(cd => true && string.IsNullOrEmpty(""));
            var count = mockContainer.Count();

            Assert.AreEqual(1, count);
        }

        [TestMethod]
        public void TestCount2()
        {
            string field = "ContentId";
            string field2 = "ContentStatus";

            MockContentQueryable mockContainer = new MockContentQueryable();
            var MockContentQueryable = mockContainer.Where(cd => cd.GetColumn<int>(field) == 10);
            MockContentQueryable = MockContentQueryable.Where(cd => cd.GetColumn<bool>(field2) != false);
            var count = MockContentQueryable.Count();

            Assert.AreEqual(0, count);
        }

        [TestMethod]
        public void TestTake()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            //var MockContentQueryable = mockContainer.Where(cd => true && string.IsNullOrEmpty(""));
            var takeCounts = mockContainer.Take(50);

            ContentQueryTranslator translator = new ContentQueryTranslator();
            var statementExpression = translator.Translate(takeCounts.Expression);

            Assert.AreEqual(50, statementExpression.TakeCount);
        }

        [TestMethod]
        public void TestFirst1()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            //var MockContentQueryable = mockContainer.Where(cd => true && string.IsNullOrEmpty(""));
            var content = mockContainer.First();
            Assert.IsNotNull(content);
            Assert.AreEqual(0, content.Count);
        }

        [TestMethod]
        public void TestFirst2()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            var queryable = mockContainer.Where(cd => cd.GetColumn<int>("ContentId") == 10);
            var content = queryable.First();
            Assert.AreEqual(10, content["ContentParam0"]);
        }

        [TestMethod]
        public void TestFirstOrDefault()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            //var MockContentQueryable = mockContainer.Where(cd => true && string.IsNullOrEmpty(""));
            var content = mockContainer.FirstOrDefault();
            Assert.IsNull(content);
        }

        [TestMethod]
        public void TestLast()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            var content = mockContainer.Last();
            Assert.IsNotNull(content);
        }

        [TestMethod]
        public void TestLastOrDefault()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            var content = mockContainer.LastOrDefault();
            Assert.IsNull(content);
        }


        [TestMethod]
        public void TestToArray1()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            var contents = mockContainer.Where(cd => cd.GetColumn<string>("Title").Contains("title")).ToArray();
            Assert.AreEqual(0, contents.Count());
        }

        [TestMethod]
        public void TestToArray2()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            var contents = mockContainer.Where(cd => cd.GetColumn<string>("Title").Contains("title")).Take(10).ToArray();
            Assert.AreEqual(10, contents.Count());
        }

        [TestMethod]
        public void TestToList()
        {
            MockContentQueryable mockContainer = new MockContentQueryable();
            var contents = mockContainer.Where(cd => cd.GetColumn<string>("Title").Contains("title")).Take(10).ToList();
            Assert.AreEqual(10, contents.Count());
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.