Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 2e561c6

Browse files
committed
Merge pull request #406 from BruceCowan-AI/DemonstrateEnumProblem
This test demonstrates the problem with selecting using a custom seriali...
2 parents c796338 + cd08f72 commit 2e561c6

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using NUnit.Framework;
5+
using ServiceStack.DataAnnotations;
6+
using ServiceStack.Text;
7+
8+
namespace ServiceStack.OrmLite.Tests.Expression
9+
{
10+
public enum ActivityType
11+
{
12+
Unknown = 0,
13+
Unspecified,
14+
HavingFun,
15+
Working
16+
}
17+
18+
public class Activity
19+
{
20+
[AutoIncrement]
21+
public int Id { get; set; }
22+
public ActivityType ActivityType { get; set; }
23+
public string Comment { get; set; }
24+
}
25+
26+
public class ExpressionUsingCustomSerializedEnumTests : ExpressionsTestBase
27+
{
28+
[Test]
29+
public void Can_select_on_custom_serialized_enum()
30+
{
31+
using (var db = OpenDbConnection())
32+
{
33+
var expected = Init(db);
34+
35+
var unknownActivities = db.Select<Activity>(
36+
s => s.ActivityType == expected.ActivityType
37+
&& s.Comment == expected.Comment);
38+
39+
Assert.That(unknownActivities.Count, Is.EqualTo(1));
40+
}
41+
}
42+
43+
private static Activity Init(IDbConnection db)
44+
{
45+
EnumSerializer.Configure();
46+
47+
db.DropAndCreateTable<Activity>();
48+
49+
var activities = new []
50+
{
51+
new Activity {ActivityType = ActivityType.Unknown, Comment = "know nothing about this"},
52+
new Activity {ActivityType = ActivityType.Unspecified, Comment = "know we don't know about this"},
53+
new Activity {ActivityType = ActivityType.HavingFun, Comment = "want to be doing this"},
54+
};
55+
db.InsertAll(activities);
56+
return activities[0];
57+
}
58+
}
59+
60+
public class EnumSerializer
61+
{
62+
public static void Configure()
63+
{
64+
var type = typeof(ActivityType);
65+
InvokeStaticGenericMethod(type, "ConfigureEnumSerialization");
66+
}
67+
68+
private static object InvokeStaticGenericMethod(Type genericType, string methodName)
69+
{
70+
return InvokeGenericMethod(genericType, methodName, null);
71+
}
72+
73+
private static object InvokeGenericMethod(Type genericType, string methodName, object obj)
74+
{
75+
return typeof(EnumSerializer).GetMethod(methodName).MakeGenericMethod(genericType).Invoke(obj, null);
76+
}
77+
78+
public static void ConfigureEnumSerialization<TEnum>()
79+
{
80+
DefaultEnumValues.Add(typeof(TEnum), GetDefault<TEnum>());
81+
JsConfig<TEnum>.SerializeFn = NonDefaultSerializer;
82+
JsConfig<TEnum>.DeSerializeFn = NonDefaultDeSerializer<TEnum>;
83+
}
84+
85+
private static readonly Dictionary<Type, object> DefaultEnumValues = new Dictionary<Type, object>();
86+
87+
private static string NonDefaultSerializer<TEnum>(TEnum value)
88+
{
89+
return value.Equals(DefaultEnumValues[typeof(TEnum)]) ? null : value.ToString();
90+
}
91+
92+
private static TEnum NonDefaultDeSerializer<TEnum>(string value)
93+
{
94+
return (String.IsNullOrEmpty(value) ? (TEnum)DefaultEnumValues[typeof(TEnum)] : (TEnum)Enum.Parse(typeof(TEnum), value, true));
95+
}
96+
97+
private static T GetDefault<T>()
98+
{
99+
return default(T);
100+
}
101+
}
102+
}

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
<Compile Include="CaptureSqlFilterTests.cs" />
120120
<Compile Include="CustomSqlTests.cs" />
121121
<Compile Include="DateTimeOffsetTests.cs" />
122+
<Compile Include="Expression\ExpressionUsingCustomSerializedEnumTests.cs" />
122123
<Compile Include="Expression\FromExpressionTests.cs" />
123124
<Compile Include="Expression\MethodExpressionTests.cs" />
124125
<Compile Include="Expression\SqlExpressionTests.cs" />

0 commit comments

Comments
 (0)