Created
September 10, 2014 21:41
-
-
Save crunchie84/4df1bcdb3071a0b575b7 to your computer and use it in GitHub Desktop.
Testcase for NEST 1.0.2 enum as string serialization failing
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.Text; | |
using Nest; | |
using NUnit.Framework; | |
namespace ClassLibrary1 | |
{ | |
public class MyObject | |
{ | |
public MyEnum MyEnum { get; set; } | |
} | |
public enum MyEnum | |
{ | |
Foo = 0, Bar = 1 | |
} | |
[TestFixture] | |
public class Tests | |
{ | |
[Test] | |
public void ShouldSerialize_Enum_AsString() | |
{ | |
var query = new FilterDescriptor<MyObject>().Term(t => t.MyEnum, MyEnum.Foo); | |
var serialized = TestElasticClient.Serialize(query); | |
Assert.That(serialized.Contains("Foo"), "enum should be serialized as Foo instead of 0. Json= "+serialized); | |
} | |
} | |
/// <summary> | |
/// elasticClient which exposes function to serialize a query object | |
/// </summary> | |
/// <remarks> | |
/// based on https://github.com/elasticsearch/elasticsearch-net/blob/master/src/Tests/Nest.Tests.Unit/TestElasticClient.cs | |
/// </remarks> | |
public static class TestElasticClient | |
{ | |
public static ElasticClient Client; | |
public static ConnectionSettings Settings; | |
static TestElasticClient() | |
{ | |
Settings = new ConnectionSettings(new Uri("http://localhost:9200")) | |
.SetDefaultIndex("nest_test_data") | |
.AddContractJsonConverters( | |
t => typeof(Enum).IsAssignableFrom(t) ? new Newtonsoft.Json.Converters.StringEnumConverter() : null | |
); | |
Client = new ElasticClient(Settings); | |
} | |
public static string Serialize<T>(T obj) where T : class | |
{ | |
return Encoding.UTF8.GetString(Client.Serializer.Serialize(obj)); | |
} | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="Elasticsearch.Net" version="1.0.2" targetFramework="net45" /> | |
<package id="NEST" version="1.0.2" targetFramework="net45" /> | |
<package id="Newtonsoft.Json" version="6.0.1" targetFramework="net45" /> | |
<package id="NUnit" version="2.6.3" targetFramework="net45" /> | |
</packages> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment