1+ using System ;
2+ using System . IO ;
3+ using System . Text ;
4+ using Elasticsearch . Net ;
5+ using Newtonsoft . Json ;
6+ using Newtonsoft . Json . Serialization ;
7+ using NUnit . Framework ;
8+
9+ namespace Nest . Tests . Unit . Internals . Serialize . DateTimes
10+ {
11+ public abstract class DateTimeHandlingTestsBase
12+ {
13+ protected Flight Flight ;
14+ protected string LocalOffsetString ;
15+ protected TimeSpan LocalOffset ;
16+
17+ [ SetUp ]
18+ public void SetUp ( )
19+ {
20+ var departureDateLocal = new DateTime ( 2013 , 1 , 21 , 0 , 0 , 0 , DateTimeKind . Local ) ;
21+ LocalOffset = TimeZoneInfo . Local . GetUtcOffset ( departureDateLocal ) ;
22+
23+ var departureDateUtc = new DateTime ( 2013 , 1 , 21 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
24+
25+ Flight = new Flight
26+ {
27+ DepartureDate = new DateTime ( 2013 , 1 , 21 , 0 , 0 , 0 , DateTimeKind . Unspecified ) ,
28+ DepartureDateUtc = departureDateUtc ,
29+ DepartureDateLocal = departureDateLocal ,
30+ DepartureDateUtcWithTicks = departureDateUtc . AddTicks ( 3456789 ) ,
31+ DepartureDateOffset = new DateTimeOffset ( 2013 , 1 , 21 , 0 , 0 , 0 , LocalOffset ) ,
32+ DepartureDateOffsetZero = new DateTimeOffset ( 2013 , 1 , 21 , 0 , 0 , 0 , TimeSpan . Zero ) ,
33+ DepartureDateOffsetNonLocal = new DateTimeOffset ( 2013 , 1 , 21 , 0 , 0 , 0 , TimeSpan . FromHours ( - 6.25 ) ) ,
34+ } ;
35+
36+ LocalOffsetString = string . Format ( "{0}:{1}" ,
37+ LocalOffset . Hours . ToString ( "+00;-00;" ) ,
38+ LocalOffset . Minutes . ToString ( "00" ) ) ;
39+ }
40+
41+ protected string SerializeUsing (
42+ DateTimeZoneHandling ? handling = null ,
43+ Func < IConnectionSettingsValues , IContractResolver > contractResolver = null )
44+ {
45+ var settings = new ConnectionSettings ( ) ;
46+
47+ settings
48+ . SetDefaultPropertyNameInferrer ( p => p )
49+ . SetJsonSerializerSettingsModifier ( s =>
50+ {
51+ s . Formatting = Formatting . Indented ;
52+
53+ if ( handling . HasValue )
54+ s . DateTimeZoneHandling = handling . Value ;
55+
56+ if ( contractResolver != null )
57+ s . ContractResolver = contractResolver ( settings ) ;
58+ } ) ;
59+
60+ var client = new ElasticClient ( settings ) ;
61+ return client . Serializer . Serialize ( Flight ) . Utf8String ( ) ;
62+ }
63+
64+ protected Flight DeserializeUsing (
65+ string json ,
66+ DateTimeZoneHandling ? handling = null ,
67+ Func < IConnectionSettingsValues , IContractResolver > contractResolver = null )
68+ {
69+ var settings = new ConnectionSettings ( ) ;
70+
71+ settings
72+ . SetDefaultPropertyNameInferrer ( p => p )
73+ . SetJsonSerializerSettingsModifier ( s =>
74+ {
75+ if ( handling . HasValue )
76+ s . DateTimeZoneHandling = handling . Value ;
77+
78+ if ( contractResolver != null )
79+ s . ContractResolver = contractResolver ( settings ) ;
80+ } ) ;
81+
82+ var client = new ElasticClient ( settings ) ;
83+ using ( var stream = new MemoryStream ( Encoding . UTF8 . GetBytes ( json ) ) )
84+ {
85+ return client . Serializer . Deserialize < Flight > ( stream ) ;
86+ }
87+ }
88+ }
89+ }
0 commit comments