Skip to content

Commit 12edc83

Browse files
authored
add NRT test (#23)
Check NRT are working correctly. :warning: if value is missing, NRT can't be considered null as of now.
1 parent 26788ff commit 12edc83

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

Diff for: README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
## Goals of this project
66

7-
* Provide an idiomatic F# API for interacting with MongoDB.
7+
* Provide support for F# types for interacting with MongoDB.
88
* Have an implementation that is fully testable without connecting to a server.
99
* Isomorphic bson serialization for C# and F#.
1010

@@ -84,6 +84,13 @@ If you want to auto-generate `ObjectId` (as the Id of the collection), add `[<CL
8484
The case of the discriminated union is stored in `_t` key.
8585
Each value of the DU is serialized as an `object` using its corresponding value name.
8686

87+
## Nullable Reference Type
88+
NRT are serialized as:
89+
* `null` if `Null`
90+
* `object` if `NonNull` object
91+
92+
:warning: As of now, NRT can't be considered `null` when deserializing if value is missing.
93+
8794
# License
8895
The contents of this library are made available under the [Apache License, Version 2.0][license].
8996

Diff for: tests/FSharp.MongoDB.Bson.Tests/FSharp.MongoDB.Bson.Tests.fsproj

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<Compile Include="Serialization\FSharpListSerializationTests.fs" />
1313
<Compile Include="Serialization\FSharpMapSerializationTests.fs" />
1414
<Compile Include="Serialization\FSharpOptionSerializationTests.fs" />
15+
<Compile Include="Serialization\FSharpNRTSerializationTests.fs" />
1516
<Compile Include="Serialization\FSharpValueOptionSerializationTests.fs" />
1617
<Compile Include="Serialization\FSharpRecordSerializationTests.fs" />
1718
<Compile Include="Serialization\FSharpSetSerializationTests.fs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
(*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*)
14+
15+
namespace FSharp.MongoDB.Bson.Tests.Serialization
16+
17+
open MongoDB.Bson
18+
open FsUnit
19+
open NUnit.Framework
20+
21+
module FSharpNRTSerialization =
22+
23+
type Primitive =
24+
{ String : string | null }
25+
26+
[<Test>]
27+
let ``test serialize nullable reference (null) in a record type``() =
28+
let value = { String = null }
29+
30+
let result = serialize value
31+
let expected = BsonDocument([ BsonElement("String", BsonNull.Value) ])
32+
33+
result |> should equal expected
34+
35+
[<Test>]
36+
let ``test deserialize nullable reference (null) in a record type)``() =
37+
// FIXME: this shall support deserializing missing null value for NRT
38+
// as of now this means NRT can't be a missing value while deserializing
39+
// let doc = BsonDocument()
40+
let doc = BsonDocument([ BsonElement("String", BsonNull.Value) ])
41+
42+
let result = deserialize<Primitive> doc
43+
let expected = { String = null }
44+
45+
result |> should equal expected
46+
47+
[<Test>]
48+
let ``test serialize nullable reference (some) in a record type``() =
49+
let value = { String = "A String" }
50+
51+
let result = serialize value
52+
let expected = BsonDocument([ BsonElement("String", BsonString "A String") ])
53+
54+
result |> should equal expected
55+
56+
[<Test>]
57+
let ``test deserialize nullable reference (some) in a record type``() =
58+
let doc = BsonDocument([ BsonElement("String", BsonString "A String") ])
59+
60+
let result = deserialize<Primitive> doc
61+
let expected = { String = "A String" }
62+
63+
result |> should equal expected

Diff for: tests/FSharp.MongoDB.Bson.Tests/Serialization/FSharpValueOptionSerializationTests.fs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
(* Copyright (c) 2015 MongoDB, Inc.
2-
*
1+
(*
32
* Licensed under the Apache License, Version 2.0 (the "License");
43
* you may not use this file except in compliance with the License.
54
* You may obtain a copy of the License at

0 commit comments

Comments
 (0)