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