Created
July 30, 2023 21:07
-
-
Save brunomlopes/e079c73ae5b4b18255885ae234a29d74 to your computer and use it in GitHub Desktop.
This test surfaces an issue on RavenDB 3.5 where strings containing datetimes are converted to datetimes and indexes might fail.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Raven.Client.Embedded; | |
using Raven.Client.Indexes; | |
using Raven.Tests.Helpers; | |
using Xunit; | |
namespace TestDateTimeOffsetsInRavenDb35 | |
{ | |
public class Entity | |
{ | |
public string Id { get; set; } | |
public string Group { get; set; } | |
public string Value { get; set; } | |
} | |
public class EntityIndex : AbstractIndexCreationTask<Entity, EntityIndex.Result> | |
{ | |
public class Result | |
{ | |
public string Group { get; set; } | |
public string[] Values { get; set; } = Array.Empty<string>(); | |
} | |
public EntityIndex() | |
{ | |
Map = entities => from entity in entities | |
select new | |
{ | |
entity.Group, | |
Values = entity.Value != null ? new[] { entity.Value } : new string [] { } | |
}; | |
Reduce = entities => from entity in entities | |
group entity by entity.Group | |
into g | |
select new Result | |
{ | |
Group = g.Key, | |
Values = g.SelectMany(e => e.Values).ToArray() | |
}; | |
} | |
} | |
public class Class1 : RavenTestBase | |
{ | |
[Fact] | |
public void WhenStringIsADateTimeOffsetAndIsHandledAsAString_IndexCrashes() | |
{ | |
var dtAsString = new DateTimeOffset(1997, 8, 29, 6, 14, 0, TimeSpan.Zero).ToString("O"); | |
var store = NewDocumentStore(); | |
store.ExecuteIndex(new EntityIndex()); | |
using (var session = store.OpenSession()) | |
{ | |
session.Store(new Entity() | |
{ | |
Group = "g1", | |
Value = dtAsString | |
}); | |
session.Store(new Entity() | |
{ | |
Group = "g1", | |
Value = "Some random text" | |
}); | |
session.SaveChanges(); | |
} | |
WaitForIndexing(store); | |
// This will fail with "Cannot implicitly convert type 'System.DateTimeOffset' to 'string'" | |
AssertNoIndexingErrors(store); | |
using (var session = store.OpenSession()) | |
{ | |
var result = session.Query<EntityIndex.Result, EntityIndex>().First(e => e.Group == "g1"); | |
Assert.Contains("Some random text", result.Values); | |
Assert.Contains(dtAsString, result.Values); | |
} | |
} | |
public static void AssertNoIndexingErrors(EmbeddableDocumentStore store) | |
{ | |
var message = string.Format("Errors found while indexing: \n\t{0}", | |
string.Join("\n\t", store.DocumentDatabase.Statistics.Errors.Select(e => e.ToString()))); | |
Assert.False(store.DocumentDatabase.Statistics.Errors.Any(), message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment