public async Task SelectConditionalValuesTestAsync() { var now = RoundForDialect(DateTime.Now); using (var spy = new SqlLogSpy()) using (var session = OpenSession()) using (session.BeginTransaction()) { var days = 33; var cat = new MyLovelyCat { GUID = Guid.NewGuid(), Birthdate = now.AddDays(-days), Color = "Black", Name = "Kitty", Price = 0 }; await(session.SaveAsync(cat)); await(session.FlushAsync()); var catInfo = await(session.Query <MyLovelyCat>() .Select(o => new { o.Color, AliveDays = (now - o.Birthdate).TotalDays, o.Name, o.Price, }) .SingleAsync()); //Console.WriteLine(spy.ToString()); // We need a tolerance: we are diffing dates as a timespan instead of counting days boundaries, // yielding a float. TimeSpan.Days yould always truncate a resulting partial day, so do not use it. Assert.That(catInfo.AliveDays, Is.EqualTo(days).Within(0.1)); var catInfo2 = await(session.Query <MyLovelyCat>() .Select(o => new { o.Color, AliveDays = o.Price > 0 ? (now - o.Birthdate).TotalDays : 0, o.Name, o.Price, }) .SingleAsync()); //Console.WriteLine(spy.ToString()); Assert.That(catInfo2.AliveDays, Is.EqualTo(0)); } }
public void SelectConditionalValuesTest() { using (var spy = new SqlLogSpy()) using (var session = OpenSession()) using (session.BeginTransaction()) { var days = 33; var cat = new MyLovelyCat { GUID = Guid.NewGuid(), Birthdate = DateTime.Now.AddDays(-days), Color = "Black", Name = "Kitty", Price = 0 }; session.Save(cat); session.Flush(); var catInfo = session.Query<MyLovelyCat>() .Select(o => new { o.Color, AliveDays = (int)(DateTime.Now - o.Birthdate).TotalDays, o.Name, o.Price, }) .Single(); //Console.WriteLine(spy.ToString()); Assert.That(catInfo.AliveDays == days); var catInfo2 = session.Query<MyLovelyCat>() .Select(o => new { o.Color, AliveDays = o.Price > 0 ? (DateTime.Now - o.Birthdate).TotalDays : 0, o.Name, o.Price, }) .Single(); //Console.WriteLine(spy.ToString()); Assert.That(catInfo2.AliveDays == 0); } }
public void SelectConditionalValuesTest() { using (var spy = new SqlLogSpy()) using (var session = OpenSession()) using (session.BeginTransaction()) { var days = 33; var cat = new MyLovelyCat { GUID = Guid.NewGuid(), Birthdate = DateTime.Now.AddDays(-days), Color = "Black", Name = "Kitty", Price = 0 }; session.Save(cat); session.Flush(); var catInfo = session.Query <MyLovelyCat>() .Select(o => new { o.Color, AliveDays = (int)(DateTime.Now - o.Birthdate).TotalDays, o.Name, o.Price, }) .Single(); //Console.WriteLine(spy.ToString()); Assert.That(catInfo.AliveDays == days); var catInfo2 = session.Query <MyLovelyCat>() .Select(o => new { o.Color, AliveDays = o.Price > 0 ? (DateTime.Now - o.Birthdate).TotalDays : 0, o.Name, o.Price, }) .Single(); //Console.WriteLine(spy.ToString()); Assert.That(catInfo2.AliveDays == 0); } }