|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using NHibernate.Hql.Ast.ANTLR; |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +namespace NHibernate.Test.Hql.Ast |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + public class LimitClauseFixture : BaseFixture |
| 12 | + { |
| 13 | + protected override void OnSetUp() |
| 14 | + { |
| 15 | + ISession session = OpenSession(); |
| 16 | + ITransaction txn = session.BeginTransaction(); |
| 17 | + |
| 18 | + var mother = new Human { BodyWeight = 10, Description = "mother" }; |
| 19 | + var father = new Human { BodyWeight = 15, Description = "father" }; |
| 20 | + var child1 = new Human { BodyWeight = 5, Description = "child1" }; |
| 21 | + var child2 = new Human { BodyWeight = 6, Description = "child2" }; |
| 22 | + var friend = new Human { BodyWeight = 20, Description = "friend" }; |
| 23 | + |
| 24 | + session.Save(mother); |
| 25 | + session.Save(father); |
| 26 | + session.Save(child1); |
| 27 | + session.Save(child2); |
| 28 | + session.Save(friend); |
| 29 | + |
| 30 | + txn.Commit(); |
| 31 | + session.Close(); |
| 32 | + } |
| 33 | + |
| 34 | + protected override void OnTearDown() |
| 35 | + { |
| 36 | + ISession session = OpenSession(); |
| 37 | + ITransaction txn = session.BeginTransaction(); |
| 38 | + session.CreateQuery("delete Human").ExecuteUpdate(); |
| 39 | + txn.Commit(); |
| 40 | + session.Close(); |
| 41 | + } |
| 42 | + |
| 43 | + [Test] |
| 44 | + public void None() |
| 45 | + { |
| 46 | + ISession s = OpenSession(); |
| 47 | + ITransaction txn = s.BeginTransaction(); |
| 48 | + |
| 49 | + var actual = s.CreateQuery("from Human h order by h.bodyWeight").List<Human>().Select(h => h.BodyWeight).ToArray(); |
| 50 | + var expected = new[] { 5, 6, 10, 15, 20 }; |
| 51 | + CollectionAssert.AreEqual(expected, actual); |
| 52 | + |
| 53 | + txn.Commit(); |
| 54 | + s.Close(); |
| 55 | + } |
| 56 | + |
| 57 | + [Test] |
| 58 | + public void Skip() |
| 59 | + { |
| 60 | + ISession s = OpenSession(); |
| 61 | + ITransaction txn = s.BeginTransaction(); |
| 62 | + |
| 63 | + var actual = s.CreateQuery("from Human h order by h.bodyWeight skip 2").List<Human>().Select(h => h.BodyWeight).ToArray(); |
| 64 | + var expected = new[] { 10, 15, 20 }; |
| 65 | + CollectionAssert.AreEqual(expected, actual); |
| 66 | + |
| 67 | + txn.Commit(); |
| 68 | + s.Close(); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public void Take() |
| 73 | + { |
| 74 | + ISession s = OpenSession(); |
| 75 | + ITransaction txn = s.BeginTransaction(); |
| 76 | + |
| 77 | + var actual = s.CreateQuery("from Human h order by h.bodyWeight take 2").List<Human>().Select(h => h.BodyWeight).ToArray(); |
| 78 | + var expected = new[] { 5, 6 }; |
| 79 | + CollectionAssert.AreEqual(expected, actual); |
| 80 | + |
| 81 | + txn.Commit(); |
| 82 | + s.Close(); |
| 83 | + } |
| 84 | + |
| 85 | + [Test] |
| 86 | + public void SkipTake() |
| 87 | + { |
| 88 | + ISession s = OpenSession(); |
| 89 | + ITransaction txn = s.BeginTransaction(); |
| 90 | + |
| 91 | + var actual = s.CreateQuery("from Human h order by h.bodyWeight skip 1 take 3").List<Human>().Select(h => h.BodyWeight).ToArray(); |
| 92 | + var expected = new[] { 6, 10, 15 }; |
| 93 | + CollectionAssert.AreEqual(expected, actual); |
| 94 | + |
| 95 | + txn.Commit(); |
| 96 | + s.Close(); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public void TakeSkip() |
| 101 | + { |
| 102 | + ISession s = OpenSession(); |
| 103 | + ITransaction txn = s.BeginTransaction(); |
| 104 | + |
| 105 | + Assert.Throws<QuerySyntaxException>(() => s.CreateQuery("from Human h order by h.bodyWeight take 1 skip 2").List<Human>(), "take should not be allowed before skip"); |
| 106 | + |
| 107 | + txn.Commit(); |
| 108 | + s.Close(); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments