|
| 1 | +using System.Linq; |
| 2 | +using NHibernate.Cfg.MappingSchema; |
| 3 | +using NHibernate.Linq; |
| 4 | +using NHibernate.Mapping.ByCode; |
| 5 | +using NUnit.Framework; |
| 6 | +using System; |
| 7 | + |
| 8 | +namespace NHibernate.Test.NHSpecificTest.NH3237 |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + public class ByCodeFixture : TestCaseMappingByCode |
| 12 | + { |
| 13 | + protected override HbmMapping GetMappings() |
| 14 | + { |
| 15 | + var mapper = new ModelMapper(); |
| 16 | + mapper.Class<Entity>(rc => |
| 17 | + { |
| 18 | + rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); |
| 19 | + rc.Property(x => x.DateTimeOffsetValue, m => m.Type(typeof(DateTimeOffsetUserType), new DateTimeOffsetUserType(TimeSpan.FromHours(10)))); |
| 20 | + rc.Property(x => x.EnumValue, m => m.Type(typeof(EnumUserType), null)); |
| 21 | + rc.Property(x => x.IntValue); |
| 22 | + rc.Property(x => x.LongValue); |
| 23 | + rc.Property(x => x.DecimalValue); |
| 24 | + rc.Property(x => x.DoubleValue); |
| 25 | + rc.Property(x => x.FloatValue); |
| 26 | + rc.Property(x => x.DateTimeValue); |
| 27 | + rc.Property(x => x.StringValue); |
| 28 | + }); |
| 29 | + |
| 30 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 31 | + } |
| 32 | + |
| 33 | + protected override void OnSetUp() |
| 34 | + { |
| 35 | + using (ISession session = OpenSession()) |
| 36 | + using (ITransaction transaction = session.BeginTransaction()) |
| 37 | + { |
| 38 | + var e1 = new Entity |
| 39 | + { |
| 40 | + DateTimeOffsetValue = new DateTimeOffset(2012, 08, 06, 11, 0, 0, TimeSpan.FromHours(10)), |
| 41 | + EnumValue = TestEnum.Zero, |
| 42 | + IntValue = 1, |
| 43 | + LongValue = 1L, |
| 44 | + DecimalValue = 1.2m, |
| 45 | + DoubleValue = 1.2d, |
| 46 | + FloatValue = 1.2f, |
| 47 | + DateTimeValue = new DateTime(2012, 08, 06, 11, 0, 0), |
| 48 | + StringValue = "a" |
| 49 | + }; |
| 50 | + session.Save(e1); |
| 51 | + |
| 52 | + var e2 = new Entity |
| 53 | + { |
| 54 | + DateTimeOffsetValue = new DateTimeOffset(2012, 08, 06, 12, 0, 0, TimeSpan.FromHours(10)), |
| 55 | + EnumValue = TestEnum.One, |
| 56 | + IntValue = 2, |
| 57 | + LongValue = 2L, |
| 58 | + DecimalValue = 2.2m, |
| 59 | + DoubleValue = 2.2d, |
| 60 | + FloatValue = 2.2f, |
| 61 | + DateTimeValue = new DateTime(2012, 08, 06, 12, 0, 0), |
| 62 | + StringValue = "b" |
| 63 | + }; |
| 64 | + session.Save(e2); |
| 65 | + |
| 66 | + var e3 = new Entity |
| 67 | + { |
| 68 | + DateTimeOffsetValue = new DateTimeOffset(2012, 08, 06, 13, 0, 0, TimeSpan.FromHours(10)), |
| 69 | + EnumValue = TestEnum.Two, |
| 70 | + IntValue = 3, |
| 71 | + LongValue = 3L, |
| 72 | + DecimalValue = 3.2m, |
| 73 | + DoubleValue = 3.2d, |
| 74 | + FloatValue = 3.2f, |
| 75 | + DateTimeValue = new DateTime(2012, 08, 06, 13, 0, 0), |
| 76 | + StringValue = "c" |
| 77 | + }; |
| 78 | + session.Save(e3); |
| 79 | + |
| 80 | + session.Flush(); |
| 81 | + transaction.Commit(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + protected override void OnTearDown() |
| 86 | + { |
| 87 | + using (ISession session = OpenSession()) |
| 88 | + using (ITransaction transaction = session.BeginTransaction()) |
| 89 | + { |
| 90 | + session.Delete("from System.Object"); |
| 91 | + |
| 92 | + session.Flush(); |
| 93 | + transaction.Commit(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public void Test_That_DateTimeOffset_UserType_Can_Be_Used_For_Max_And_Min_Aggregates() |
| 99 | + { |
| 100 | + using (ISession session = OpenSession()) |
| 101 | + using (session.BeginTransaction()) |
| 102 | + { |
| 103 | + var min = session.Query<Entity>().Min(e => e.DateTimeOffsetValue); |
| 104 | + Assert.AreEqual(new DateTimeOffset(2012, 08, 06, 11, 0, 0, TimeSpan.FromHours(10)), min); |
| 105 | + |
| 106 | + var max = session.Query<Entity>().Max(e => e.DateTimeOffsetValue); |
| 107 | + Assert.AreEqual(new DateTimeOffset(2012, 08, 06, 13, 0, 0, TimeSpan.FromHours(10)), max); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public void Test_That_Enum_Type_Can_Be_Used_For_Max_And_Min_Aggregates() |
| 113 | + { |
| 114 | + using (ISession session = OpenSession()) |
| 115 | + using (session.BeginTransaction()) |
| 116 | + { |
| 117 | + var min = session.Query<Entity>().Min(e => e.EnumValue); |
| 118 | + Assert.AreEqual(TestEnum.Zero, min); |
| 119 | + |
| 120 | + var max = session.Query<Entity>().Max(e => e.EnumValue); |
| 121 | + Assert.AreEqual(TestEnum.Two, max); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + [Test] |
| 126 | + public void Test_Max_And_Min_Aggregates_Work_For_Ints() |
| 127 | + { |
| 128 | + using (ISession session = OpenSession()) |
| 129 | + using (session.BeginTransaction()) |
| 130 | + { |
| 131 | + var min = session.Query<Entity>().Min(e => e.IntValue); |
| 132 | + Assert.AreEqual(1, min); |
| 133 | + |
| 134 | + var max = session.Query<Entity>().Max(e => e.IntValue); |
| 135 | + Assert.AreEqual(3, max); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + [Test] |
| 140 | + public void Test_Max_And_Min_Aggregates_Work_For_Longs() |
| 141 | + { |
| 142 | + using (ISession session = OpenSession()) |
| 143 | + using (session.BeginTransaction()) |
| 144 | + { |
| 145 | + var min = session.Query<Entity>().Min(e => e.LongValue); |
| 146 | + Assert.AreEqual(1L, min); |
| 147 | + |
| 148 | + var max = session.Query<Entity>().Max(e => e.LongValue); |
| 149 | + Assert.AreEqual(3L, max); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + [Test] |
| 154 | + public void Test_Max_And_Min_Aggregates_Work_For_Decimals() |
| 155 | + { |
| 156 | + using (ISession session = OpenSession()) |
| 157 | + using (session.BeginTransaction()) |
| 158 | + { |
| 159 | + var min = session.Query<Entity>().Min(e => e.DecimalValue); |
| 160 | + Assert.AreEqual(1.2m, min); |
| 161 | + |
| 162 | + var max = session.Query<Entity>().Max(e => e.DecimalValue); |
| 163 | + Assert.AreEqual(3.2m, max); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + [Test] |
| 168 | + public void Test_Max_And_Min_Aggregates_Work_For_Doubles() |
| 169 | + { |
| 170 | + using (ISession session = OpenSession()) |
| 171 | + using (session.BeginTransaction()) |
| 172 | + { |
| 173 | + var min = session.Query<Entity>().Min(e => e.DoubleValue); |
| 174 | + Assert.AreEqual(1.2d, min); |
| 175 | + |
| 176 | + var max = session.Query<Entity>().Max(e => e.DoubleValue); |
| 177 | + Assert.AreEqual(3.2d, max); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + [Test] |
| 182 | + public void Test_Max_And_Min_Aggregates_Work_For_Floats() |
| 183 | + { |
| 184 | + using (ISession session = OpenSession()) |
| 185 | + using (session.BeginTransaction()) |
| 186 | + { |
| 187 | + var min = session.Query<Entity>().Min(e => e.FloatValue); |
| 188 | + Assert.AreEqual(1.2f, min); |
| 189 | + |
| 190 | + var max = session.Query<Entity>().Max(e => e.FloatValue); |
| 191 | + Assert.AreEqual(3.2f, max); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + [Test] |
| 196 | + public void Test_Max_And_Min_Aggregates_Work_For_DateTimes() |
| 197 | + { |
| 198 | + using (ISession session = OpenSession()) |
| 199 | + using (session.BeginTransaction()) |
| 200 | + { |
| 201 | + var min = session.Query<Entity>().Min(e => e.DateTimeValue); |
| 202 | + Assert.AreEqual(new DateTime(2012, 08, 06, 11, 0, 0), min); |
| 203 | + |
| 204 | + var max = session.Query<Entity>().Max(e => e.DateTimeValue); |
| 205 | + Assert.AreEqual(new DateTime(2012, 08, 06, 13, 0, 0), max); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + [Test] |
| 210 | + public void Test_Max_And_Min_Aggregates_Work_For_Strings() |
| 211 | + { |
| 212 | + using (ISession session = OpenSession()) |
| 213 | + using (session.BeginTransaction()) |
| 214 | + { |
| 215 | + var min = session.Query<Entity>().Min(e => e.StringValue); |
| 216 | + Assert.AreEqual("a", min); |
| 217 | + |
| 218 | + var max = session.Query<Entity>().Max(e => e.StringValue); |
| 219 | + Assert.AreEqual("c", max); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments