@@ -43,17 +43,21 @@ public void Attribute_should_set_correct_units()
43
43
Microseconds = timeOnly ,
44
44
Ticks = timeOnly ,
45
45
Nanoseconds = timeOnly ,
46
+ Document = timeOnly
46
47
} ;
47
48
48
49
var json = testObj . ToJson ( ) ;
49
50
50
- var expected = "{ \" Hours\" : 13, "
51
- + "\" Minutes\" : 804, "
52
- + "\" Seconds\" : 48293, "
53
- + "\" Milliseconds\" : 48293000, "
54
- + "\" Microseconds\" : 48293000000, "
55
- + "\" Ticks\" : 482930000000, "
56
- + "\" Nanoseconds\" : 48293000000000 }" ;
51
+ var baseString = """
52
+ { "Hours" : 13, "Minutes" : 804, "Seconds" : 48293, "Milliseconds" : 48293000, "Microseconds" : 48293000000, "Ticks" : 482930000000, "Nanoseconds" : 48293000000000
53
+ """ ;
54
+
55
+ var documentString = """
56
+ { "Hour" : 13, "Minute" : 24, "Second" : 53, "Millisecond" : 0, "Microsecond" : 0, "Nanosecond" : 0, "Ticks" : 482930000000 }
57
+ """ ;
58
+
59
+
60
+ var expected = baseString + """, "Document" : """ + documentString + " }" ;
57
61
Assert . Equal ( expected , json ) ;
58
62
}
59
63
@@ -69,7 +73,7 @@ public void Constructor_with_no_arguments_should_return_expected_result()
69
73
[ Theory ]
70
74
[ ParameterAttributeData ]
71
75
public void Constructor_with_representation_should_return_expected_result (
72
- [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double ) ]
76
+ [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double , BsonType . Document ) ]
73
77
BsonType representation ,
74
78
[ Values ( TimeOnlyUnits . Ticks , TimeOnlyUnits . Hours , TimeOnlyUnits . Minutes , TimeOnlyUnits . Seconds ,
75
79
TimeOnlyUnits . Milliseconds , TimeOnlyUnits . Microseconds , TimeOnlyUnits . Nanoseconds ) ]
@@ -81,6 +85,60 @@ public void Constructor_with_representation_should_return_expected_result(
81
85
subject . Units . Should ( ) . Be ( units ) ;
82
86
}
83
87
88
+ [ Theory ]
89
+ [ InlineData ( """{ "x" : { "Hour" : { "$numberInt" : "8" }, "Minute" : { "$numberInt" : "32" }, "Second" : { "$numberInt" : "5" }, "Millisecond" : { "$numberInt" : "594" }, "Microsecond" : { "$numberInt" : "658" }, "Nanosecond" : { "$numberInt" : "300" }, "Ticks" : { "$numberLong" : "307255946583" } } }""" , "08:32:05.5946583" ) ]
90
+ [ InlineData ( """{ "x" : { "Hour" : { "$numberInt" : "0" }, "Minute" : { "$numberInt" : "0" }, "Second" : { "$numberInt" : "0" }, "Millisecond" : { "$numberInt" : "0" }, "Microsecond" : { "$numberInt" : "0" }, "Nanosecond" : { "$numberInt" : "0" }, "Ticks" : { "$numberLong" : "0" } } }""" , "00:00:00.0000000" ) ]
91
+ [ InlineData ( """{ "x" : { "Hour" : { "$numberInt" : "23" }, "Minute" : { "$numberInt" : "59" }, "Second" : { "$numberInt" : "59" }, "Millisecond" : { "$numberInt" : "999" }, "Microsecond" : { "$numberInt" : "999" }, "Nanosecond" : { "$numberInt" : "900" }, "Ticks" : { "$numberLong" : "863999999999" } } }""" , "23:59:59.9999999" ) ]
92
+ public void Deserialize_with_document_should_have_expected_result ( string json , string expectedResult )
93
+ {
94
+ var subject = new TimeOnlySerializer ( ) ;
95
+ TestDeserialize ( subject , json , expectedResult ) ;
96
+ }
97
+
98
+ [ Theory ]
99
+ [ InlineData ( """{ "x" : { "Hour" : { "$numberInt" : "8" }, "Minute" : { "$numberInt" : "32" }, "Second" : { "$numberInt" : "5" }, "Millisecond" : { "$numberInt" : "594" }, "Microsecond" : { "$numberInt" : "658" }, "Ticks" : { "$numberLong" : "307255946583" } } }""" , "08:32:05.5946583" ) ]
100
+ [ InlineData ( """{ "x" : { "Hour" : { "$numberInt" : "8" }, "Minute" : { "$numberInt" : "32" }, "Second" : { "$numberInt" : "5" }, "Millisecond" : { "$numberInt" : "594" }, "Nanosecond" : { "$numberInt" : "300" }, "Ticks" : { "$numberLong" : "307255946583" } } }""" , "08:32:05.5946583" ) ]
101
+ [ InlineData ( """{ "x" : { "Hour" : { "$numberInt" : "8" }, "Minute" : { "$numberInt" : "32" }, "Second" : { "$numberInt" : "5" }, "Millisecond" : { "$numberInt" : "594" }, "Ticks" : { "$numberLong" : "307255946583" } } }""" , "08:32:05.5946583" ) ]
102
+ public void Deserialize_with_document_should_work_with_missing_microsecond_or_nanosecond ( string json , string expectedResult )
103
+ {
104
+ var subject = new TimeOnlySerializer ( ) ;
105
+ TestDeserialize ( subject , json , expectedResult ) ;
106
+ }
107
+
108
+ [ Theory ]
109
+ [ InlineData ( """{ "x" : { "Hour" : { "$numberInt" : "7" }, "Minute" : { "$numberInt" : "32" }, "Second" : { "$numberInt" : "5" }, "Millisecond" : { "$numberInt" : "594" }, "Microsecond" : { "$numberInt" : "658" }, "Nanosecond" : { "$numberInt" : "300" }, "Ticks" : { "$numberLong" : "307255946583" } } }""" ) ]
110
+ [ InlineData ( """{ "x" : { "Hour" : { "$numberInt" : "8" }, "Minute" : { "$numberInt" : "33" }, "Second" : { "$numberInt" : "5" }, "Millisecond" : { "$numberInt" : "594" }, "Microsecond" : { "$numberInt" : "658" }, "Nanosecond" : { "$numberInt" : "300" }, "Ticks" : { "$numberLong" : "307255946583" } } }""" ) ]
111
+ [ InlineData ( """{ "x" : { "Hour" : { "$numberInt" : "8" }, "Minute" : { "$numberInt" : "32" }, "Second" : { "$numberInt" : "6" }, "Millisecond" : { "$numberInt" : "594" }, "Microsecond" : { "$numberInt" : "658" }, "Nanosecond" : { "$numberInt" : "300" }, "Ticks" : { "$numberLong" : "307255946583" } } }""" ) ]
112
+ public void Deserialize_with_document_should_throw_when_component_is_not_correct ( string json )
113
+ {
114
+ var subject = new TimeOnlySerializer ( ) ;
115
+
116
+ using var reader = new JsonReader ( json ) ;
117
+ reader . ReadStartDocument ( ) ;
118
+ reader . ReadName ( "x" ) ;
119
+ var context = BsonDeserializationContext . CreateRoot ( reader ) ;
120
+
121
+ var exception = Record . Exception ( ( ) => subject . Deserialize ( context ) ) ;
122
+ exception . Should ( ) . BeOfType < BsonSerializationException > ( ) ;
123
+ exception . Message . Should ( ) . Be ( "Deserialized TimeOnly components do not match the ticks value." ) ;
124
+ }
125
+
126
+ [ Fact ]
127
+ public void Deserialize_with_document_should_throw_when_field_is_unknown ( )
128
+ {
129
+ const string json = """{ "x" : { "Unknown": "test", Ticks: { "$numberDouble" : "307255946583" } } }""" ;
130
+ var subject = new TimeOnlySerializer ( ) ;
131
+
132
+ using var reader = new JsonReader ( json ) ;
133
+ reader . ReadStartDocument ( ) ;
134
+ reader . ReadName ( "x" ) ;
135
+ var context = BsonDeserializationContext . CreateRoot ( reader ) ;
136
+
137
+ var exception = Record . Exception ( ( ) => subject . Deserialize ( context ) ) ;
138
+ exception . Should ( ) . BeOfType < BsonSerializationException > ( ) ;
139
+ exception . Message . Should ( ) . Be ( "Invalid element: 'Unknown'." ) ;
140
+ }
141
+
84
142
[ Theory ]
85
143
[ InlineData ( """{ "x" : "08:32:05.5946583" }""" , "08:32:05.5946583" ) ]
86
144
[ InlineData ( """{ "x" : "00:00:00.0000000" }""" , "00:00:00.0000000" ) ]
@@ -273,6 +331,17 @@ public void GetHashCode_should_return_zero()
273
331
result . Should ( ) . Be ( 0 ) ;
274
332
}
275
333
334
+ [ Theory ]
335
+ [ InlineData ( "08:32:05.5946583" , """{ "x" : { "Hour" : { "$numberInt" : "8" }, "Minute" : { "$numberInt" : "32" }, "Second" : { "$numberInt" : "5" }, "Millisecond" : { "$numberInt" : "594" }, "Microsecond" : { "$numberInt" : "658" }, "Nanosecond" : { "$numberInt" : "300" }, "Ticks" : { "$numberLong" : "307255946583" } } }""" ) ]
336
+ [ InlineData ( "00:00:00.0000000" , """{ "x" : { "Hour" : { "$numberInt" : "0" }, "Minute" : { "$numberInt" : "0" }, "Second" : { "$numberInt" : "0" }, "Millisecond" : { "$numberInt" : "0" }, "Microsecond" : { "$numberInt" : "0" }, "Nanosecond" : { "$numberInt" : "0" }, "Ticks" : { "$numberLong" : "0" } } }""" ) ]
337
+ [ InlineData ( "23:59:59.9999999" , """{ "x" : { "Hour" : { "$numberInt" : "23" }, "Minute" : { "$numberInt" : "59" }, "Second" : { "$numberInt" : "59" }, "Millisecond" : { "$numberInt" : "999" }, "Microsecond" : { "$numberInt" : "999" }, "Nanosecond" : { "$numberInt" : "900" }, "Ticks" : { "$numberLong" : "863999999999" } } }""" ) ]
338
+ public void Serialize_with_document_representation_should_have_expected_result ( string valueString , string expectedResult )
339
+ {
340
+ var subject = new TimeOnlySerializer ( BsonType . Document ) ;
341
+
342
+ TestSerialize ( subject , valueString , expectedResult ) ;
343
+ }
344
+
276
345
[ Theory ]
277
346
[ InlineData ( BsonType . String , "08:32:05.5946583" , """{ "x" : "08:32:05.5946583" }""" ) ]
278
347
[ InlineData ( BsonType . String , "00:00:00.0000000" , """{ "x" : "00:00:00.0000000" }""" ) ]
@@ -407,8 +476,8 @@ public void Serializer_should_be_registered()
407
476
[ Theory ]
408
477
[ ParameterAttributeData ]
409
478
public void WithRepresentation_should_return_expected_result (
410
- [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double ) ] BsonType oldRepresentation ,
411
- [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double ) ] BsonType newRepresentation )
479
+ [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double , BsonType . Document ) ] BsonType oldRepresentation ,
480
+ [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double , BsonType . Document ) ] BsonType newRepresentation )
412
481
{
413
482
var subject = new TimeOnlySerializer ( oldRepresentation ) ;
414
483
@@ -473,6 +542,9 @@ private class TestClass
473
542
474
543
[ BsonTimeOnlyOptions ( BsonType . Int64 , TimeOnlyUnits . Nanoseconds ) ]
475
544
public TimeOnly Nanoseconds { get ; set ; }
545
+
546
+ [ BsonTimeOnlyOptions ( BsonType . Document ) ]
547
+ public TimeOnly Document { get ; set ; }
476
548
}
477
549
}
478
550
#endif
0 commit comments