/* * DynamORM - Dynamic Object-Relational Mapping library. * Copyright (c) 2012-2015, Grzegorz Russek (grzegorz.russek@gmail.com) * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ using System; using System.Collections.Generic; using System.Linq; using DynamORM.Builders; using DynamORM.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace DynamORM.Tests.Select { /// Test typed ORM. /// Type to test. [TestClass] public class TypedAccessTests : TestsBase where T : class { /// Setup test parameters. [TestInitialize] public virtual void SetUp() { CreateTestDatabase(); CreateDynamicDatabase(); } /// Tear down test objects. [TestCleanup] public virtual void TearDown() { DestroyDynamicDatabase(); DestroyTestDatabase(); } /// Create table using specified method. /// Dynamic table. public virtual dynamic GetTestTable() { return Database.Table(); } /// Create table using specified method. /// Dynamic table. public virtual IDynamicSelectQueryBuilder GetTestBuilder() { return Database.Table().Query() as IDynamicSelectQueryBuilder; } #region Select typed /// Test load all rows into mapped list alternate way. [TestMethod] public virtual void TestTypedGetAll() { var list = (GetTestTable().Query(type: typeof(T)) as IEnumerable).Cast().ToList(); Assert.AreEqual(200, list.Count); } /// Test load all rows into mapped list alternate way. [TestMethod] public virtual void TestTypedGetAll2() { var list = GetTestBuilder() .From(x => x(typeof(T)).As(x.t)) .Execute() .MapEnumerable() .ToList(); Assert.AreEqual(200, list.Count); } /// Test load all rows into mapped list alternate way. [TestMethod] public virtual void TestTypedGetAll3() { var list = GetTestBuilder() .From(x => x(typeof(T)).As(x.t)) .Execute() .ToList(); Assert.AreEqual(200, list.Count); } /// Test unknown op. [TestMethod] public virtual void TestTypedUnknownOperation() { Assert.ThrowsException(() => GetTestTable().MakeMeASandwitch(type: typeof(T), with: "cheese")); } /// Test typed Count method. [TestMethod] public virtual void TestTypedCount() { Assert.AreEqual(200, GetTestTable().Count(type: typeof(T), columns: "id")); } /// Test typed Count method. [TestMethod] public virtual void TestTypedCount2() { Assert.AreEqual(200, GetTestBuilder() .From(x => x(typeof(T)).As(x.t)) .Select(x => x.Count(x.t.id)) .ScalarAs()); } /// Test count with in statement. [TestMethod] public virtual void TestTypedSelectInEnumerableCount() { Assert.AreEqual(4, GetTestTable().Count(type: typeof(T), last: new DynamicColumn { Operator = DynamicColumn.CompareOperator.In, Value = new object[] { "Hendricks", "Goodwin", "Freeman" }.Take(3) })); } /// Test count with in statement. [TestMethod] 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()) .ScalarAs()); } /// Test count with in statement. [TestMethod] public virtual void TestTypedSelectInArrayCount() { Assert.AreEqual(4, GetTestTable().Count(type: typeof(T), last: new DynamicColumn { Operator = DynamicColumn.CompareOperator.In, Value = new object[] { "Hendricks", "Goodwin", "Freeman" } })); } /// Test count with in statement. [TestMethod] 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()) .ScalarAs()); } /// Test typed First method. [TestMethod] public virtual void TestTypedFirst() { Assert.AreEqual(1, GetTestTable().First(type: typeof(T), columns: "id").id); } /// Test typed First method. [TestMethod] public virtual void TestTypedFirst2() { Assert.AreEqual(1, GetTestBuilder() .From(x => x(typeof(T)).As(x.t)) .Select(x => x.t.id) .Execute() .First().id); } /// Test typed Last method. [TestMethod] public virtual void TestTypedLast() { Assert.AreEqual(200, GetTestTable().Last(type: typeof(T), columns: "id").id); } /// Test typed Last method. [TestMethod] public virtual void TestTypedLast2() { Assert.AreEqual(200, GetTestBuilder() .From(x => x(typeof(T)).As(x.t)) .Select(x => x.t.id) .Execute() .Last().id); } /// Test typed Count method. [TestMethod] public virtual void TestTypedCountSpecificRecord() { Assert.AreEqual(1, GetTestTable().Count(type: typeof(T), first: "Ori")); } /// Test typed Min method. [TestMethod] public virtual void TestTypedMin() { Assert.AreEqual(1, GetTestTable().Min(type: typeof(T), columns: "id")); } /// Test typed Min method. [TestMethod] public virtual void TestTypedMin2() { Assert.AreEqual(1, GetTestBuilder() .From(x => x(typeof(T)).As(x.t)) .Select(x => x.Min(x.t.id)) .ScalarAs()); } /// Test typed Min method. [TestMethod] public virtual void TestTypedMax() { Assert.AreEqual(200, GetTestTable().Max(type: typeof(T), columns: "id")); } /// Test typed Min method. [TestMethod] public virtual void TestTypedMax2() { Assert.AreEqual(200, GetTestBuilder() .From(x => x(typeof(T)).As(x.t)) .Select(x => x.Max(x.t.id)) .ScalarAs()); } /// Test typed Min method. [TestMethod] public virtual void TestTypedtAvg() { Assert.AreEqual(100.5, GetTestTable().Avg(type: typeof(T), columns: "id")); } /// Test typed Min method. [TestMethod] 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()); } /// Test typed Sum method. [TestMethod] public virtual void TestTypedSum() { Assert.AreEqual(20100, GetTestTable().Sum(type: typeof(T), columns: "id")); } /// Test typed Sum method. [TestMethod] public virtual void TestTypedSum2() { Assert.AreEqual(20100, GetTestBuilder() .From(x => x(typeof(T)).As(x.t)) .Select(x => x.Sum(x.t.id)) .ScalarAs()); } /// Test typed Scalar method for invalid operation exception. [TestMethod] public virtual void TestTypedScalarException() { Assert.ThrowsException(() => GetTestTable().Scalar(type: typeof(T), id: 19)); } /// Test typed Scalar method. [TestMethod] public virtual void TestTypedScalar() { Assert.AreEqual("Ori", GetTestTable().Scalar(type: typeof(T), columns: "first", id: 19)); } /// Test typed Scalar method. [TestMethod] 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()); } /// Test typed Scalar method with SQLite specific aggregate. [TestMethod] public virtual void TestTypedScalarGroupConcat() { // 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", GetTestTable().Scalar(type: typeof(T), columns: "first:first:group_concat", id: new DynamicColumn { Operator = DynamicColumn.CompareOperator.Lt, Value = 20 })); } /// Test typed Scalar method with SQLite specific aggregate. [TestMethod] 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()); } /// Test typed Scalar method with SQLite specific aggregate not using aggregate field. [TestMethod] public virtual void TestTypedScalarGroupConcatNoAggregateField() { // 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", GetTestTable().Scalar(type: typeof(T), columns: "group_concat(first):first", id: new DynamicColumn { Operator = DynamicColumn.CompareOperator.Lt, Value = 20 })); } /// Test typed Scalar method with SQLite specific aggregate not using aggregate field. [TestMethod] 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) .SelectColumn("group_concat(first):first") .Scalar()); } /// Test something fancy... like: select "first", count("first") aggregatefield from "users" group by "first" order by 2 desc;. [TestMethod] public virtual void TestTypedFancyAggregateQuery() { var v = (GetTestTable().Query(type: typeof(T), columns: "first,first:aggregatefield:count", group: "first", order: ":desc:2") as IEnumerable).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); } /// Test something fancy... like: select "first", count("first") aggregatefield from "users" group by "first" order by 2 desc;. [TestMethod] 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); } /// This time also something fancy... aggregate in aggregate select AVG(LENGTH("login")) len from "users";. [TestMethod] public virtual void TestTypedAggregateInAggregate() { Assert.AreEqual(12.77, GetTestTable().Scalar(type: typeof(T), columns: @"length(""login""):len:avg")); } /// This time also something fancy... aggregate in aggregate select AVG(LENGTH("login")) len from "users";. [TestMethod] 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()); } /// This time also something fancy... aggregate in aggregate select AVG(LENGTH("email")) len from "users";. [TestMethod] public virtual void TestTypedAggregateInAggregateMark2() { Assert.AreEqual(27.7, GetTestTable().Avg(type: typeof(T), columns: @"length(""email""):len")); } /// This time also something fancy... aggregate in aggregate select AVG(LENGTH("email")) len from "users";. [TestMethod] 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()); } /// Test emails longer than 27 chars. select count(*) from "users" where length("email") > 27;. public virtual void TestTypedFunctionInWhere() { Assert.AreEqual(97, GetTestTable().Count(type: typeof(T), condition1: new DynamicColumn() { ColumnName = "email", Aggregate = "length", Operator = DynamicColumn.CompareOperator.Gt, Value = 27 })); } /// Test emails longer than 27 chars. select count(*) from "users" where length("email") > 27;. 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()); } /// Test typed Single multi. [TestMethod] public virtual void TestTypedSingleObject() { var exp = new { id = 19, first = "Ori", last = "Ellis" }; var o = GetTestTable().Single(type: typeof(T), columns: "id,first,last", id: 19); Assert.AreEqual(exp.id, o.id); Assert.AreEqual(exp.first, o.first); Assert.AreEqual(exp.last, o.last); } /// Test typed Single multi. [TestMethod] 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 /// Test typed where expression equal. [TestMethod] public virtual void TestTypedWhereEq() { Assert.AreEqual("hoyt.tran", GetTestTable().Single(type: typeof(T), where: new DynamicColumn("id").Eq(100)).login); } /// Test typed where expression equal. [TestMethod] 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); } /// Test typed where expression not equal. [TestMethod] public virtual void TestTypedWhereNot() { Assert.AreEqual(199, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").Not(100))); } /// Test typed where expression not equal. [TestMethod] 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()) .ScalarAs()); } /// Test typed where expression like. [TestMethod] public virtual void TestTypedWhereLike() { Assert.AreEqual(100, GetTestTable().Single(type: typeof(T), where: new DynamicColumn("login").Like("Hoyt.%")).id); } /// Test typed where expression like. [TestMethod] 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); } /// Test typed where expression not like. [TestMethod] public virtual void TestTypedWhereNotLike() { Assert.AreEqual(199, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("login").NotLike("Hoyt.%"))); } /// Test typed where expression not like. [TestMethod] 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()) .ScalarAs()); } /// Test typed where expression not like. [TestMethod] 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()) .ScalarAs()); } /// Test typed where expression greater. [TestMethod] public virtual void TestTypedWhereGt() { Assert.AreEqual(100, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").Greater(100))); } /// Test typed where expression greater. [TestMethod] 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()) .ScalarAs()); } /// Test typed where expression greater or equal. [TestMethod] public virtual void TestTypedWhereGte() { Assert.AreEqual(101, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").GreaterOrEqual(100))); } /// Test typed where expression greater or equal. [TestMethod] 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()) .ScalarAs()); } /// Test typed where expression less. [TestMethod] public virtual void TestTypedWhereLt() { Assert.AreEqual(99, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").Less(100))); } /// Test typed where expression less. [TestMethod] 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()) .ScalarAs()); } /// Test typed where expression less or equal. [TestMethod] public virtual void TestTypedWhereLte() { Assert.AreEqual(100, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").LessOrEqual(100))); } /// Test typed where expression less or equal. [TestMethod] 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()) .ScalarAs()); } /// Test typed where expression between. [TestMethod] public virtual void TestTypedWhereBetween() { Assert.AreEqual(26, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").Between(75, 100))); } /// Test typed where expression between. [TestMethod] 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()) .ScalarAs()); } /// Test typed where expression in parameters. [TestMethod] public virtual void TestTypedWhereIn1() { Assert.AreEqual(3, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").In(75, 99, 100))); } /// Test typed where expression in array. [TestMethod] public virtual void TestTypedWhereIn2() { Assert.AreEqual(3, GetTestTable().Count(type: typeof(T), where: new DynamicColumn("id").In(new[] { 75, 99, 100 }))); } /// Test typed where expression in parameters. [TestMethod] 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()) .ScalarAs()); } /// Test typed where expression in array. [TestMethod] 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()) .ScalarAs()); } #endregion Where typed #region Select generic /// Test load all rows into mapped list alternate way. [TestMethod] public virtual void TestGenericGetAll() { var list = (GetTestTable().Query() as IEnumerable).Cast().ToList(); Assert.AreEqual(200, list.Count); } /// Test unknown op. [TestMethod] public virtual void TestGenericUnknownOperation() { Assert.ThrowsException(() => GetTestTable().MakeMeASandwitch(with: "cheese")); } /// Test generic Count method. [TestMethod] public virtual void TestGenericCount() { Assert.AreEqual(200, GetTestTable().Count(columns: "id")); } /// Test count with in statement. [TestMethod] public virtual void TestGenericSelectInEnumerableCount() { Assert.AreEqual(4, GetTestTable().Count(last: new DynamicColumn { Operator = DynamicColumn.CompareOperator.In, Value = new object[] { "Hendricks", "Goodwin", "Freeman" }.Take(3) })); } /// Test count with in statement. [TestMethod] public virtual void TestGenericSelectInArrayCount() { Assert.AreEqual(4, GetTestTable().Count(last: new DynamicColumn { Operator = DynamicColumn.CompareOperator.In, Value = new object[] { "Hendricks", "Goodwin", "Freeman" } })); } /// Test generic First method. [TestMethod] public virtual void TestGenericFirst() { Assert.AreEqual(1, GetTestTable().First(columns: "id").id); } /// Test generic Last method. [TestMethod] public virtual void TestGenericLast() { Assert.AreEqual(200, GetTestTable().Last(columns: "id").id); } /// Test generic Count method. [TestMethod] public virtual void TestGenericCountSpecificRecord() { Assert.AreEqual(1, GetTestTable().Count(first: "Ori")); } /// Test generic Min method. [TestMethod] public virtual void TestGenericMin() { Assert.AreEqual(1, GetTestTable().Min(columns: "id")); } /// Test generic Min method. [TestMethod] public virtual void TestGenericMax() { Assert.AreEqual(200, GetTestTable().Max(columns: "id")); } /// Test generic Min method. [TestMethod] public virtual void TestGenerictAvg() { Assert.AreEqual(100.5, GetTestTable().Avg(columns: "id")); } /// Test generic Sum method. [TestMethod] public virtual void TestGenericSum() { Assert.AreEqual(20100, GetTestTable().Sum(columns: "id")); } /// Test generic Scalar method for invalid operation exception. [TestMethod] public virtual void TestGenericScalarException() { Assert.ThrowsException(() => GetTestTable().Scalar(id: 19)); } /// Test generic Scalar method. [TestMethod] public virtual void TestGenericScalar() { Assert.AreEqual("Ori", GetTestTable().Scalar(columns: "first", id: 19)); } /// Test generic Scalar method with SQLite specific aggregate. [TestMethod] public virtual void TestGenericScalarGroupConcat() { // 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", GetTestTable().Scalar(columns: "first:first:group_concat", id: new DynamicColumn { Operator = DynamicColumn.CompareOperator.Lt, Value = 20 })); } /// Test generic Scalar method with SQLite specific aggregate not using aggregate field. [TestMethod] public virtual void TestGenericScalarGroupConcatNoAggregateField() { // 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", GetTestTable().Scalar(columns: "group_concat(first):first", id: new DynamicColumn { Operator = DynamicColumn.CompareOperator.Lt, Value = 20 })); } /// Test something fancy... like: select "first", count("first") aggregatefield from "users" group by "first" order by 2 desc;. [TestMethod] public virtual void TestGenericFancyAggregateQuery() { var v = (GetTestTable().Query(columns: "first,first:aggregatefield:count", group: "first", order: ":desc:2") as IEnumerable).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); } /// This time also something fancy... aggregate in aggregate select AVG(LENGTH("login")) len from "users";. [TestMethod] public virtual void TestGenericAggregateInAggregate() { Assert.AreEqual(12.77, GetTestTable().Scalar(columns: @"length(""login""):len:avg")); } /// This time also something fancy... aggregate in aggregate select AVG(LENGTH("email")) len from "users";. [TestMethod] public virtual void TestGenericAggregateInAggregateMark2() { Assert.AreEqual(27.7, GetTestTable().Avg(columns: @"length(""email""):len")); } /// Test emails longer than 27 chars. select count(*) from "users" where length("email") > 27;. public virtual void TestGenericFunctionInWhere() { Assert.AreEqual(97, GetTestTable().Count(condition1: new DynamicColumn() { ColumnName = "email", Aggregate = "length", Operator = DynamicColumn.CompareOperator.Gt, Value = 27 })); } /// Test generic Single multi. [TestMethod] public virtual void TestGenericSingleObject() { var exp = new { id = 19, first = "Ori", last = "Ellis" }; var o = GetTestTable().Single(columns: "id,first,last", id: 19); Assert.AreEqual(exp.id, o.id); Assert.AreEqual(exp.first, o.first); Assert.AreEqual(exp.last, o.last); } #endregion Select generic #region Where generic /// Test generic where expression equal. [TestMethod] public virtual void TestGenericWhereEq() { Assert.AreEqual("hoyt.tran", GetTestTable().Single(where: new DynamicColumn("id").Eq(100)).login); } /// Test generic where expression not equal. [TestMethod] public virtual void TestGenericWhereNot() { Assert.AreEqual(199, GetTestTable().Count(where: new DynamicColumn("id").Not(100))); } /// Test generic where expression like. [TestMethod] public virtual void TestGenericWhereLike() { Assert.AreEqual(100, GetTestTable().Single(where: new DynamicColumn("login").Like("Hoyt.%")).id); } /// Test generic where expression not like. [TestMethod] public virtual void TestGenericWhereNotLike() { Assert.AreEqual(199, GetTestTable().Count(where: new DynamicColumn("login").NotLike("Hoyt.%"))); } /// Test generic where expression greater. [TestMethod] public virtual void TestGenericWhereGt() { Assert.AreEqual(100, GetTestTable().Count(where: new DynamicColumn("id").Greater(100))); } /// Test generic where expression greater or equal. [TestMethod] public virtual void TestGenericWhereGte() { Assert.AreEqual(101, GetTestTable().Count(where: new DynamicColumn("id").GreaterOrEqual(100))); } /// Test generic where expression less. [TestMethod] public virtual void TestGenericWhereLt() { Assert.AreEqual(99, GetTestTable().Count(where: new DynamicColumn("id").Less(100))); } /// Test generic where expression less or equal. [TestMethod] public virtual void TestGenericWhereLte() { Assert.AreEqual(100, GetTestTable().Count(where: new DynamicColumn("id").LessOrEqual(100))); } /// Test generic where expression between. [TestMethod] public virtual void TestGenericWhereBetween() { Assert.AreEqual(26, GetTestTable().Count(where: new DynamicColumn("id").Between(75, 100))); } /// Test generic where expression in parameters. [TestMethod] public virtual void TestGenericWhereIn1() { Assert.AreEqual(3, GetTestTable().Count(where: new DynamicColumn("id").In(75, 99, 100))); } /// Test generic where expression in array. [TestMethod] public virtual void TestGenericWhereIn2() { Assert.AreEqual(3, GetTestTable().Count(where: new DynamicColumn("id").In(new[] { 75, 99, 100 }))); } #endregion Where generic } }