PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Raven.Tests.Issues/Ravendb718/DateTime_QueryTransformTests.cs

https://github.com/barryhagan/ravendb
C# | 110 lines | 91 code | 16 blank | 3 comment | 1 complexity | fdcc410b34a5695ab04793662c40fb37 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
  1. using System;
  2. using System.Linq;
  3. using Raven.Client;
  4. using Raven.Client.Indexes;
  5. using Raven.Tests.Common;
  6. using Xunit;
  7. namespace Raven.Tests.Issues.Ravendb718
  8. {
  9. public class DateTime_QueryTransformTests : RavenTest
  10. {
  11. [Fact]
  12. public void DateTime_SavedAs_Local_OnTransformIndex_QueriesAs_Unspecified()
  13. {
  14. // There is no representation of Local once the DateTime is serialized,
  15. // so this is the one case where the date does not come back exactly as it was saved.
  16. // This is a known and expected behavior of DateTime.
  17. DoTest(DateTime.Now, DateTimeKind.Local, DateTimeKind.Unspecified);
  18. }
  19. [Fact]
  20. public void DateTime_SavedAs_Unspecified_OnTransformIndex_QueriesAs_Unspecified()
  21. {
  22. DoTest(new DateTime(2012, 1, 1), DateTimeKind.Unspecified, DateTimeKind.Unspecified);
  23. }
  24. [Fact]
  25. public void DateTime_SavedAs_UTC_OnTransformIndex_QueriesAs_UTC()
  26. {
  27. DoTest(DateTime.UtcNow, DateTimeKind.Utc, DateTimeKind.Utc);
  28. }
  29. private void DoTest(DateTime dt, DateTimeKind inKind, DateTimeKind outKind)
  30. {
  31. Assert.Equal(inKind, dt.Kind);
  32. using (var documentStore = NewDocumentStore())
  33. {
  34. new FoosAndBars().Execute(documentStore);
  35. using (var session = documentStore.OpenSession())
  36. {
  37. session.Store(new Foo { Id = "foos/1", DateTime = dt, BarId = "bars/1" });
  38. session.Store(new Bar { Id = "bars/1", DateTime = dt });
  39. session.SaveChanges();
  40. }
  41. using (var session = documentStore.OpenSession())
  42. {
  43. var results = session.Query<Foo, FoosAndBars>()
  44. .Customize(x => x.WaitForNonStaleResults())
  45. .Where(x => x.DateTime == dt)
  46. .As<FooAndBar>()
  47. .First();
  48. Assert.Equal(dt, results.FooDateTime);
  49. Assert.Equal(dt, results.BarDateTime);
  50. Assert.Equal(outKind, results.FooDateTime.Kind);
  51. Assert.Equal(outKind, results.BarDateTime.Kind);
  52. }
  53. }
  54. }
  55. public class Foo
  56. {
  57. public string Id { get; set; }
  58. public string BarId { get; set; }
  59. public DateTime DateTime { get; set; }
  60. }
  61. public class Bar
  62. {
  63. public string Id { get; set; }
  64. public DateTime DateTime { get; set; }
  65. }
  66. public class FooAndBar
  67. {
  68. public string FooId { get; set; }
  69. public string BarId { get; set; }
  70. public DateTime FooDateTime { get; set; }
  71. public DateTime BarDateTime { get; set; }
  72. }
  73. public class FoosAndBars : AbstractIndexCreationTask<Foo>
  74. {
  75. public FoosAndBars()
  76. {
  77. Map = foos => from foo in foos
  78. select new
  79. {
  80. foo.DateTime,
  81. };
  82. TransformResults = (database, foos) => from foo in foos
  83. let bar = database.Load<Bar>(foo.BarId)
  84. select new
  85. {
  86. FooId = foo.Id,
  87. BarId = bar.Id,
  88. FooDateTime = foo.DateTime,
  89. BarDateTime = bar.DateTime
  90. };
  91. }
  92. }
  93. }
  94. }