Skip to content

Updated MongoDB Driver and FSharp Runtime #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
201 changes: 114 additions & 87 deletions MongoDB.FSharp.Tests/AcceptanceTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ open MongoDB.Bson
open MongoDB.Driver
open MongoDB.Driver.Linq

open MongoDB.FSharp
open MongoDB.Driver.FSharp
open System.Linq
open Microsoft.FSharp.Linq

open TestUtils

type ObjectWithList() =
member val Id : BsonObjectId = BsonObjectId.GenerateNewId() with get, set
member val Id : BsonObjectId = BsonObjectId(ObjectId.GenerateNewId()) with get, set
member val List : string list = [] with get, set

type RecordType = {
Expand All @@ -41,91 +41,98 @@ type DimmerSwitch =
| On

type ObjectWithOptions() =
member val Id : BsonObjectId = BsonObjectId.GenerateNewId() with get, set
member val Id : BsonObjectId = BsonObjectId(ObjectId.GenerateNewId()) with get, set
member val Age : int option = None with get, set

type ObjectWithDimmer() =
member val Id : BsonObjectId = BsonObjectId.GenerateNewId() with get, set
member val Id : BsonObjectId = BsonObjectId(ObjectId.GenerateNewId()) with get, set
member val Switch : DimmerSwitch = Off with get, set

type ObjectWithDimmers() =
member val Id : BsonObjectId = BsonObjectId.GenerateNewId() with get, set
member val Id : BsonObjectId = BsonObjectId(ObjectId.GenerateNewId()) with get, set
member val Kitchen : DimmerSwitch = Off with get, set
member val Bedroom1 : DimmerSwitch = Off with get, set
member val Bedroom2 : DimmerSwitch = Off with get, set

type ``When serializing lists``() =
let db = MongoDatabase.Create "mongodb://localhost/test"
let conn = new MongoClient("mongodb://localhost")
let db = conn.GetDatabase("test")
do
Serializers.Register()

interface System.IDisposable with
member this.Dispose() =
db.DropCollection "objects" |> ignore
db.DropCollection "persons" |> ignore
()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the unit here?

db.DropCollectionAsync "objects" |> AwaitVoidTask |> ignore
db.DropCollectionAsync "persons" |> AwaitVoidTask |> ignore

/// Seems to be fixed in version 1.5 of the C# driver
[<Fact>]
member this.``It can serialize an object with a list``() =
let collection = db.GetCollection<ObjectWithList> "objects"
let obj = ObjectWithList()
obj.List <- [ "hello"; "world" ]
collection.Save obj |> ignore

let genCollection = db.GetCollection "objects"
let fromDb = genCollection.FindOne(new QueryDocument("_id", obj.Id))
let array = fromDb.["List"].AsBsonArray
Assert.Equal(2, array.Count)
let collection = db.GetCollection<ObjectWithList> "objects"
let obj = ObjectWithList()
obj.List <- [ "hello"; "world" ]
collection.InsertOneAsync obj |> AwaitVoidTask |> Async.RunSynchronously

let genCollection = db.GetCollection<ObjectWithList> "objects"
let fromDb = genCollection.Find(fun x -> x.Id = obj.Id).FirstAsync()
|> Async.AwaitTask |> Async.RunSynchronously
let array = fromDb.List
Assert.Equal(2, array.Length)

[<Fact>]
member this.``It can deserialze lists``() =
let list = BsonArray([ "hello"; "world" ])
let id = BsonObjectId.GenerateNewId()
let document = BsonDocument([ BsonElement("_id", id); BsonElement("List", list) ])
let collection = db.GetCollection "objects"
collection.Save document |> ignore

let collection = db.GetCollection<ObjectWithList> "objects"
let fromDb = collection.FindOne(new QueryDocument("_id", id))
let array = fromDb.List
Assert.Equal(2, array.Length)
member this.``It can deserialze lists``() =
let list = BsonArray([ "hello"; "world" ])
let id = BsonObjectId(ObjectId.GenerateNewId())
let document = BsonDocument([ BsonElement("_id", id); BsonElement("List", list) ])
let collection = db.GetCollection "objects"
collection.InsertOneAsync document |> AwaitVoidTask |> Async.RunSynchronously

let collection = db.GetCollection<ObjectWithList> "objects"
let fromDb = collection.Find(fun x -> x.Id = id).FirstAsync()
|> Async.AwaitTask |> Async.RunSynchronously
let array = fromDb.List
Assert.Equal(2, array.Length)

[<Fact>]
member this.``It can serialize records``() =
let collection = db.GetCollection<RecordType> "objects"
let obj = { Id = BsonObjectId.GenerateNewId(); Name = "test" }
collection.Save obj |> ignore
let collection = db.GetCollection<RecordType> "objects"
let obj = { Id = BsonObjectId(ObjectId.GenerateNewId()); Name = "test" }
collection.InsertOneAsync obj |> AwaitVoidTask |> ignore

let genCollection = db.GetCollection "objects"
let fromDb = genCollection.FindOne(new QueryDocument("_id", obj.Id))
let test = fromDb.["Name"].AsString
Assert.Equal<string>("test", test)
let fromDb = collection.Find(fun x -> x.Id = obj.Id).FirstAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
Assert.Equal<string>("test", fromDb.Name)

[<Fact>]
member this.``It can deserialize records``() =
let id = BsonObjectId.GenerateNewId()
let id = BsonObjectId(ObjectId.GenerateNewId())
let document = BsonDocument([BsonElement("_id", id); BsonElement("Name", BsonString("value"))])
let collection = db.GetCollection "objects"
collection.Save(document) |> ignore
collection.InsertOneAsync(document) |> AwaitVoidTask |> Async.RunSynchronously

let collection = db.GetCollection<RecordType>("objects")
let fromDb = collection.FindOneById(id)
let fromDb = collection.Find(fun x -> x.Id = id).FirstAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
Assert.NotNull(fromDb)
Assert.Equal<string>("value", fromDb.Name)

[<Fact>]
member this.``It can serialize and deserialize nested records``() =
let collection = db.GetCollection<Person> "persons"
let obj = { Id = BsonObjectId.GenerateNewId(); PersonName = "test"; Age = 33; Childs = [{ChildName = "Adrian"; Age = 3}] }
collection.Save obj |> ignore
let obj = { Id = BsonObjectId(ObjectId.GenerateNewId());
PersonName = "test";
Age = 33;
Childs = [{ChildName = "Adrian";
Age = 3}] }
collection.InsertOneAsync obj |> AwaitVoidTask |> Async.RunSynchronously

let genCollection = db.GetCollection<Person> "persons"
let person = query {
for p in genCollection.AsQueryable() do
where (p.Id = obj.Id)
select p
headOrDefault
}
let person = genCollection.Find(fun x -> x.Id = obj.Id).FirstAsync()
|> Async.AwaitTask
|> Async.RunSynchronously

Assert.NotNull person
Assert.Equal<string>("test",person.PersonName)
Expand All @@ -135,21 +142,27 @@ type ``When serializing lists``() =
let child = person.Childs |> Seq.head

Assert.Equal<string>("Adrian", child.ChildName)
Assert.Equal<int>(3, child.Age)
Assert.Equal<int>(3, child.Age)


[<Fact>]
member this.``It can serialize option types``() =
let collection = db.GetCollection<ObjectWithOptions> "objects"
let obj = ObjectWithOptions()
obj.Age <- Some 42
collection.Save obj |> ignore
collection.InsertOneAsync obj
|> AwaitVoidTask
|> Async.RunSynchronously

let collection = db.GetCollection "objects"
let fromDb = collection.FindOneById(obj.Id)
let filter = new BsonDocumentFilterDefinition<_>(new BsonDocument()
|> (fun d -> d.Add("_id",obj.Id)))
let fromDb = collection.Find<BsonDocument>(filter).FirstAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
let age = fromDb.GetElement("Age")
Assert.NotNull(age);
Assert.Equal<string>("Some", age.Value.AsBsonDocument.GetElement("_t").Value.AsString)
Assert.Equal<string>("Some", age.Value.ToBsonDocument().GetElement("_t").Value.AsString)
let value = age.Value.AsBsonDocument.GetElement("_v").Value
Assert.True(value.IsBsonArray)
let array = value.AsBsonArray
Expand All @@ -158,13 +171,19 @@ type ``When serializing lists``() =

[<Fact>]
member this.``It can serialize DimmerSwitch types``() =
let collection = db.GetCollection<ObjectWithOptions> "objects"
let obj = ObjectWithDimmer()
obj.Switch <- DimMarquee(42, "loser")
collection.Save obj |> ignore

let collection = db.GetCollection "objects"
let fromDb = collection.FindOneById(obj.Id)
obj.Switch <- DimMarquee(42, "loser")
db.GetCollection<ObjectWithDimmer>("objects").InsertOneAsync (obj)
|> AwaitVoidTask
|> Async.RunSynchronously

let collection = db.GetCollection<BsonDocument> "objects"

let filter = new BsonDocumentFilterDefinition<_>(new BsonDocument()
|> (fun d -> d.Add("_id",obj.Id)))
let fromDb = collection.Find(filter).FirstAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
let switch = fromDb.GetElement("Switch")
Assert.NotNull(switch);
Assert.Equal<string>("DimMarquee", switch.Value.AsBsonDocument.GetElement("_t").Value.AsString)
Expand All @@ -177,37 +196,45 @@ type ``When serializing lists``() =

[<Fact>]
member this.``It can deserialize option types``() =
let id = BsonObjectId.GenerateNewId()
let arrayPart = BsonArray([ BsonInt32(42) ])
let structure = BsonDocument(BsonElement("_t", BsonString("Some")), BsonElement("_v", arrayPart))
let document = BsonDocument(BsonElement("_id", id), BsonElement("Age", structure))
let collection = db.GetCollection "objects"
collection.Save(document) |> ignore

let collection = db.GetCollection<ObjectWithOptions> "objects"
let fromDb = collection.FindOneById id
match fromDb.Age with
| Some 42 -> ()
| _ -> fail "expected Some 42 but got something else"
let id = BsonObjectId(ObjectId.GenerateNewId())
let arrayPart = BsonArray([ BsonInt32(42) ])
let structure = BsonDocument([| BsonElement("_t", BsonString("Some")); BsonElement("_v", arrayPart) |].AsEnumerable())
let document = BsonDocument([|BsonElement("_id", id); BsonElement("Age", structure)|].AsEnumerable())
let collection = db.GetCollection "objects"
collection.InsertOneAsync(document)
|> AwaitVoidTask
|> ignore

let collection = db.GetCollection<ObjectWithOptions> "objects"
let fromDb = collection.Find(fun x -> x.Id = id).FirstAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
match fromDb.Age with
| Some 42 -> ()
| _ -> fail "expected Some 42 but got something else"

[<Fact>]
member this.``We can integrate serialize & deserialize on DimmerSwitches``() =
let collection = db.GetCollection<ObjectWithDimmers> "objects"
let obj = ObjectWithDimmers()
obj.Kitchen <- Off
obj.Bedroom1 <- Dim 42
obj.Bedroom2 <- DimMarquee(12, "when I was little...")
collection.Save obj |> ignore

let fromDb = collection.FindOneById obj.Id
match fromDb.Kitchen with
| Off -> ()
| _ -> fail "Kitchen light wasn't off"

match fromDb.Bedroom1 with
| Dim 42 -> ()
| _ -> fail "Bedroom1 light wasn't dim enough"

match fromDb.Bedroom2 with
| DimMarquee(12, "when I was little...") -> ()
| _ -> fail "Bedroom2 doesn't have the party we thought"
let collection = db.GetCollection<ObjectWithDimmers> "objects"
let obj = ObjectWithDimmers()
obj.Kitchen <- Off
obj.Bedroom1 <- Dim 42
obj.Bedroom2 <- DimMarquee(12, "when I was little...")
collection.InsertOneAsync obj
|> AwaitVoidTask
|> ignore

let fromDb = collection.Find(fun x -> x.Id = obj.Id).FirstAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
match fromDb.Kitchen with
| Off -> ()
| _ -> fail "Kitchen light wasn't off"

match fromDb.Bedroom1 with
| Dim 42 -> ()
| _ -> fail "Bedroom1 light wasn't dim enough"

match fromDb.Bedroom2 with
| DimMarquee(12, "when I was little...") -> ()
| _ -> fail "Bedroom2 doesn't have the party we thought"
51 changes: 35 additions & 16 deletions MongoDB.FSharp.Tests/MongoDB.FSharp.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<TargetFrameworkProfile />
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFSharpCoreVersion>4.3.1.0</TargetFSharpCoreVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -34,16 +35,45 @@
<WarningLevel>3</WarningLevel>
<DocumentationFile>bin\Release\MongoDB.FSharp.Tests.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
</PropertyGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '11.0'">
<PropertyGroup>
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</Otherwise>
</Choose>
<Import Project="$(FSharpTargetsPath)" Condition="Exists('$(FSharpTargetsPath)')" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<ItemGroup>
<Reference Include="FSharp.Core, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Compile Include="TestUtils.fs" />
<Compile Include="AcceptanceTests.fs" />
<Compile Include="SerializersTests.fs" />
<None Include="packages.config" />
<Content Include="app.config" />
</ItemGroup>
<ItemGroup>
<Reference Include="FSharp.Core">
<HintPath>..\packages\FSharp.Core.3.1.2.1\lib\net40\FSharp.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MongoDB.Bson">
<HintPath>..\packages\mongocsharpdriver.1.8.1\lib\net35\MongoDB.Bson.dll</HintPath>
<HintPath>..\packages\MongoDB.Bson.2.0.1\lib\net45\MongoDB.Bson.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MongoDB.Driver">
<HintPath>..\packages\mongocsharpdriver.1.8.1\lib\net35\MongoDB.Driver.dll</HintPath>
<HintPath>..\packages\MongoDB.Driver.2.0.1\lib\net45\MongoDB.Driver.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MongoDB.Driver.Core">
<HintPath>..\packages\MongoDB.Driver.Core.2.0.1\lib\net45\MongoDB.Driver.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="mscorlib" />
Expand All @@ -59,23 +89,12 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="TestUtils.fs" />
<Compile Include="AcceptanceTests.fs" />
<Compile Include="SerializersTests.fs" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MongoDB.FSharp\MongoDB.FSharp.fsproj">
<Name>MongoDB.FSharp</Name>
<ProjectReference Include="..\MongoDB.FSharp\MongoDB.Driver.FSharp.fsproj">
<Name>MongoDB.Driver.FSharp</Name>
<Project>{7cbeb93a-1590-42db-9e40-61630e79304a}</Project>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets" Condition=" Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
2 changes: 1 addition & 1 deletion MongoDB.FSharp.Tests/SerializersTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

open Xunit

open MongoDB.FSharp.Serializers
open MongoDB.Driver.FSharp.Serializers
open TestUtils


Expand Down
Loading