New version alpha

This commit is contained in:
grzegorz.russek
2013-06-04 17:44:36 +00:00
parent 4e0652dd89
commit 02cd81aab5
35 changed files with 4363 additions and 1275 deletions

View File

@@ -29,6 +29,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DynamORM.Builders;
using DynamORM.Tests.Helpers;
using NUnit.Framework;
@@ -65,6 +66,13 @@ namespace DynamORM.Tests.Select
return Database.Table();
}
/// <summary>Create table using specified method.</summary>
/// <returns>Dynamic table.</returns>
public virtual IDynamicSelectQueryBuilder GetTestBuilder()
{
return Database.Table().Query() as IDynamicSelectQueryBuilder;
}
#region Select typed
/// <summary>Test load all rows into mapped list alternate way.</summary>
@@ -76,6 +84,19 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(200, list.Count);
}
/// <summary>Test load all rows into mapped list alternate way.</summary>
[Test]
public virtual void TestTypedGetAll2()
{
var list = GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Execute()
.MapEnumerable<T>()
.ToList();
Assert.AreEqual(200, list.Count);
}
/// <summary>Test unknown op.</summary>
[Test]
public virtual void TestTypedUnknownOperation()
@@ -90,7 +111,17 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(200, GetTestTable().Count(type: typeof(T), columns: "id"));
}
/// <summary>Test count with in steatement.</summary>
/// <summary>Test typed <c>Count</c> method.</summary>
[Test]
public virtual void TestTypedCount2()
{
Assert.AreEqual(200, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Select(x => x.Count(x.t.id))
.Scalar());
}
/// <summary>Test count with in statement.</summary>
[Test]
public virtual void TestTypedSelectInEnumerableCount()
{
@@ -101,7 +132,18 @@ namespace DynamORM.Tests.Select
}));
}
/// <summary>Test count with in steatement.</summary>
/// <summary>Test count with in statement.</summary>
[Test]
public virtual void TestTypedSelectInEnumerableCount2()
{
Assert.AreEqual(4, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.last.In(new object[] { "Hendricks", "Goodwin", "Freeman" }.Take(3)))
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test count with in statement.</summary>
[Test]
public virtual void TestTypedSelectInArrayCount()
{
@@ -112,6 +154,17 @@ namespace DynamORM.Tests.Select
}));
}
/// <summary>Test count with in statement.</summary>
[Test]
public virtual void TestTypedSelectInArrayCount2()
{
Assert.AreEqual(4, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.last.In(new object[] { "Hendricks", "Goodwin", "Freeman" }))
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test typed <c>First</c> method.</summary>
[Test]
public virtual void TestTypedFirst()
@@ -119,6 +172,17 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(1, GetTestTable().First(type: typeof(T), columns: "id").id);
}
/// <summary>Test typed <c>First</c> method.</summary>
[Test]
public virtual void TestTypedFirst2()
{
Assert.AreEqual(1, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Select(x => x.t.id)
.Execute()
.First().id);
}
/// <summary>Test typed <c>Last</c> method.</summary>
[Test]
public virtual void TestTypedLast()
@@ -126,6 +190,17 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(200, GetTestTable().Last(type: typeof(T), columns: "id").id);
}
/// <summary>Test typed <c>Last</c> method.</summary>
[Test]
public virtual void TestTypedLast2()
{
Assert.AreEqual(200, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Select(x => x.t.id)
.Execute()
.Last().id);
}
/// <summary>Test typed <c>Count</c> method.</summary>
[Test]
public virtual void TestTypedCountSpecificRecord()
@@ -140,6 +215,16 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(1, GetTestTable().Min(type: typeof(T), columns: "id"));
}
/// <summary>Test typed <c>Min</c> method.</summary>
[Test]
public virtual void TestTypedMin2()
{
Assert.AreEqual(1, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Select(x => x.Min(x.t.id))
.Scalar());
}
/// <summary>Test typed <c>Min</c> method.</summary>
[Test]
public virtual void TestTypedMax()
@@ -147,6 +232,16 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(200, GetTestTable().Max(type: typeof(T), columns: "id"));
}
/// <summary>Test typed <c>Min</c> method.</summary>
[Test]
public virtual void TestTypedMax2()
{
Assert.AreEqual(200, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Select(x => x.Max(x.t.id))
.Scalar());
}
/// <summary>Test typed <c>Min</c> method.</summary>
[Test]
public virtual void TestTypedtAvg()
@@ -154,6 +249,16 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(100.5, GetTestTable().Avg(type: typeof(T), columns: "id"));
}
/// <summary>Test typed <c>Min</c> method.</summary>
[Test]
public virtual void TestTypedtAvg2()
{
Assert.AreEqual(100.5, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Select(x => x.Avg(x.t.id))
.Scalar());
}
/// <summary>Test typed <c>Sum</c> method.</summary>
[Test]
public virtual void TestTypedSum()
@@ -161,6 +266,16 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(20100, GetTestTable().Sum(type: typeof(T), columns: "id"));
}
/// <summary>Test typed <c>Sum</c> method.</summary>
[Test]
public virtual void TestTypedSum2()
{
Assert.AreEqual(20100, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Select(x => x.Sum(x.t.id))
.Scalar());
}
/// <summary>Test typed <c>Scalar</c> method for invalid operation exception.</summary>
[Test]
public virtual void TestTypedScalarException()
@@ -175,6 +290,17 @@ namespace DynamORM.Tests.Select
Assert.AreEqual("Ori", GetTestTable().Scalar(type: typeof(T), columns: "first", id: 19));
}
/// <summary>Test typed <c>Scalar</c> method.</summary>
[Test]
public void TestTypedScalar2()
{
Assert.AreEqual("Ori", GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id == 19)
.Select(x => x.t.first)
.Scalar());
}
/// <summary>Test typed <c>Scalar</c> method with SQLite specific aggregate.</summary>
[Test]
public virtual void TestTypedScalarGroupConcat()
@@ -185,6 +311,20 @@ namespace DynamORM.Tests.Select
GetTestTable().Scalar(type: typeof(T), columns: "first:first:group_concat", id: new DynamicColumn { Operator = DynamicColumn.CompareOperator.Lt, Value = 20 }));
}
/// <summary>Test typed <c>Scalar</c> method with SQLite specific aggregate.</summary>
[Test]
public virtual void TestTypedScalarGroupConcat2()
{
// This test should produce something like this:
// select group_concat("first") AS first from "users" where "id" < 20;
Assert.AreEqual("Clarke,Marny,Dai,Forrest,Blossom,George,Ivory,Inez,Sigourney,Fulton,Logan,Anne,Alexandra,Adena,Lionel,Aimee,Selma,Lara,Ori",
GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id < 20)
.Select(x => x.group_concat(x.t.first).As(x.first))
.Scalar());
}
/// <summary>Test typed <c>Scalar</c> method with SQLite specific aggregate not using aggregate field.</summary>
[Test]
public virtual void TestTypedScalarGroupConcatNoAggregateField()
@@ -195,6 +335,20 @@ namespace DynamORM.Tests.Select
GetTestTable().Scalar(type: typeof(T), columns: "group_concat(first):first", id: new DynamicColumn { Operator = DynamicColumn.CompareOperator.Lt, Value = 20 }));
}
/// <summary>Test typed <c>Scalar</c> method with SQLite specific aggregate not using aggregate field.</summary>
[Test]
public virtual void TestTypedScalarGroupConcatNoAggregateField2()
{
// This test should produce something like this:
// select group_concat("first") AS first from "users" where "id" < 20;
Assert.AreEqual("Clarke,Marny,Dai,Forrest,Blossom,George,Ivory,Inez,Sigourney,Fulton,Logan,Anne,Alexandra,Adena,Lionel,Aimee,Selma,Lara,Ori",
GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id < 20)
.Select("group_concat(first):first")
.Scalar());
}
/// <summary>Test something fancy... like: <code>select "first", count("first") aggregatefield from "users" group by "first" order by 2 desc;</code>.</summary>
[Test]
public virtual void TestTypedFancyAggregateQuery()
@@ -210,6 +364,27 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(1, v.Last().aggregatefield);
}
/// <summary>Test something fancy... like: <code>select "first", count("first") aggregatefield from "users" group by "first" order by 2 desc;</code>.</summary>
[Test]
public virtual void TestTypedFancyAggregateQuery2()
{
var v = GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Select(x => x.t.first, x => x.Count(x.t.first).As(x.aggregatefield))
.GroupBy(x => x.t.first)
.OrderBy(x => x.Desc(2))
.Execute()
.ToList();
Assert.IsNotNull(v);
Assert.AreEqual(187, v.Count());
Assert.AreEqual(4, v.First().aggregatefield);
Assert.AreEqual("Logan", v.First().first);
Assert.AreEqual(2, v.Take(10).Last().aggregatefield);
Assert.AreEqual(1, v.Take(11).Last().aggregatefield);
Assert.AreEqual(1, v.Last().aggregatefield);
}
/// <summary>This time also something fancy... aggregate in aggregate <code>select AVG(LENGTH("login")) len from "users";</code>.</summary>
[Test]
public virtual void TestTypedAggregateInAggregate()
@@ -217,6 +392,16 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(12.77, GetTestTable().Scalar(type: typeof(T), columns: @"length(""login""):len:avg"));
}
/// <summary>This time also something fancy... aggregate in aggregate <code>select AVG(LENGTH("login")) len from "users";</code>.</summary>
[Test]
public virtual void TestTypedAggregateInAggregate2()
{
Assert.AreEqual(12.77, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Select(x => x.Avg(x.Length(x.t.login)).As(x.len))
.Scalar());
}
/// <summary>This time also something fancy... aggregate in aggregate <code>select AVG(LENGTH("email")) len from "users";</code>.</summary>
[Test]
public virtual void TestTypedAggregateInAggregateMark2()
@@ -224,6 +409,16 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(27.7, GetTestTable().Avg(type: typeof(T), columns: @"length(""email""):len"));
}
/// <summary>This time also something fancy... aggregate in aggregate <code>select AVG(LENGTH("email")) len from "users";</code>.</summary>
[Test]
public virtual void TestTypedAggregateInAggregateMark3()
{
Assert.AreEqual(27.7, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Select(x => "AVG(LENGTH(t.email)) AS LEN")
.Scalar());
}
/// <summary>Test emails longer than 27 chars. <code>select count(*) from "users" where length("email") > 27;</code>.</summary>
public virtual void TestTypedFunctionInWhere()
{
@@ -238,6 +433,16 @@ namespace DynamORM.Tests.Select
}));
}
/// <summary>Test emails longer than 27 chars. <code>select count(*) from "users" where length("email") > 27;</code>.</summary>
public virtual void TestTypedFunctionInWhere2()
{
Assert.AreEqual(97, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.Length(x.t.email) > 27)
.Select(x => x.Count(x.t.All()))
.Scalar());
}
/// <summary>Test typed <c>Single</c> multi.</summary>
[Test]
public virtual void TestTypedSingleObject()
@@ -250,6 +455,23 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(exp.last, o.last);
}
/// <summary>Test typed <c>Single</c> multi.</summary>
[Test]
public virtual void TestTypedSingleObject2()
{
var exp = new { id = 19, first = "Ori", last = "Ellis" };
var o = GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id == 19)
.Select(x => new { id = x.t.id, first = x.t.first, last = x.t.last })
.Execute()
.First();
Assert.AreEqual(exp.id, o.id);
Assert.AreEqual(exp.first, o.first);
Assert.AreEqual(exp.last, o.last);
}
#endregion Select typed
#region Where typed
@@ -261,6 +483,15 @@ namespace DynamORM.Tests.Select
Assert.AreEqual("hoyt.tran", GetTestTable().Single(type: typeof(T), where: new DynamicColumn("id").Eq(100)).login);
}
/// <summary>Test typed where expression equal.</summary>
[Test]
public virtual void TestTypedWhereEq2()
{
Assert.AreEqual("hoyt.tran", GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id == 100).Execute().First().login);
}
/// <summary>Test typed where expression not equal.</summary>
[Test]
public virtual void TestTypedWhereNot()
@@ -268,6 +499,17 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(199, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").Not(100)));
}
/// <summary>Test typed where expression not equal.</summary>
[Test]
public virtual void TestTypedWhereNot2()
{
Assert.AreEqual(199, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id != 100)
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test typed where expression like.</summary>
[Test]
public virtual void TestTypedWhereLike()
@@ -275,6 +517,15 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(100, GetTestTable().Single(type: typeof(T), where: new DynamicColumn("login").Like("Hoyt.%")).id);
}
/// <summary>Test typed where expression like.</summary>
[Test]
public virtual void TestTypedWhereLike2()
{
Assert.AreEqual(100, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.login.Like("Hoyt.%")).Execute().First().id);
}
/// <summary>Test typed where expression not like.</summary>
[Test]
public virtual void TestTypedWhereNotLike()
@@ -282,6 +533,28 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(199, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("login").NotLike("Hoyt.%")));
}
/// <summary>Test typed where expression not like.</summary>
[Test]
public virtual void TestTypedWhereNotLike2()
{
Assert.AreEqual(199, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.login.NotLike("Hoyt.%"))
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test typed where expression not like.</summary>
[Test]
public virtual void TestTypedWhereNotLike3()
{
Assert.AreEqual(199, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => !x.t.login.Like("Hoyt.%"))
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test typed where expression greater.</summary>
[Test]
public virtual void TestTypedWhereGt()
@@ -289,6 +562,17 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(100, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").Greater(100)));
}
/// <summary>Test typed where expression greater.</summary>
[Test]
public virtual void TestTypedWhereGt2()
{
Assert.AreEqual(100, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id > 100)
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test typed where expression greater or equal.</summary>
[Test]
public virtual void TestTypedWhereGte()
@@ -296,6 +580,17 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(101, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").GreaterOrEqual(100)));
}
/// <summary>Test typed where expression greater or equal.</summary>
[Test]
public virtual void TestTypedWhereGte2()
{
Assert.AreEqual(101, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id >= 100)
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test typed where expression less.</summary>
[Test]
public virtual void TestTypedWhereLt()
@@ -303,6 +598,17 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(99, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").Less(100)));
}
/// <summary>Test typed where expression less.</summary>
[Test]
public virtual void TestTypedWhereLt2()
{
Assert.AreEqual(99, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id < 100)
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test typed where expression less or equal.</summary>
[Test]
public virtual void TestTypedWhereLte()
@@ -310,6 +616,17 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(100, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").LessOrEqual(100)));
}
/// <summary>Test typed where expression less or equal.</summary>
[Test]
public virtual void TestTypedWhereLte2()
{
Assert.AreEqual(100, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id <= 100)
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test typed where expression between.</summary>
[Test]
public virtual void TestTypedWhereBetween()
@@ -317,7 +634,18 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(26, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").Between(75, 100)));
}
/// <summary>Test typed where expression in params.</summary>
/// <summary>Test typed where expression between.</summary>
[Test]
public virtual void TestTypedWhereBetween2()
{
Assert.AreEqual(26, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id.Between(75, 100))
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test typed where expression in parameters.</summary>
[Test]
public virtual void TestTypedWhereIn1()
{
@@ -331,6 +659,28 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(3, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").In(new[] { 75, 99, 100 })));
}
/// <summary>Test typed where expression in parameters.</summary>
[Test]
public virtual void TestTypedWhereIn3()
{
Assert.AreEqual(3, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id.In(75, 99, 100))
.Select(x => x.Count())
.Scalar());
}
/// <summary>Test typed where expression in array.</summary>
[Test]
public virtual void TestTypedWhereIn4()
{
Assert.AreEqual(3, GetTestBuilder()
.From(x => x(typeof(T)).As(x.t))
.Where(x => x.t.id.In(new[] { 75, 99, 100 }))
.Select(x => x.Count())
.Scalar());
}
#endregion Where typed
#region Select generic
@@ -358,7 +708,7 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(200, GetTestTable().Count<T>(columns: "id"));
}
/// <summary>Test count with in steatement.</summary>
/// <summary>Test count with in statement.</summary>
[Test]
public virtual void TestGenericSelectInEnumerableCount()
{
@@ -369,7 +719,7 @@ namespace DynamORM.Tests.Select
}));
}
/// <summary>Test count with in steatement.</summary>
/// <summary>Test count with in statement.</summary>
[Test]
public virtual void TestGenericSelectInArrayCount()
{
@@ -585,7 +935,7 @@ namespace DynamORM.Tests.Select
Assert.AreEqual(26, GetTestTable().Count<T>(where: new DynamicColumn("id").Between(75, 100)));
}
/// <summary>Test generic where expression in params.</summary>
/// <summary>Test generic where expression in parameters.</summary>
[Test]
public virtual void TestGenericWhereIn1()
{