Skip to content

Commit

Permalink
updated f# api with some select, take and skip combinators
Browse files Browse the repository at this point in the history
  • Loading branch information
colinbull committed Apr 10, 2012
1 parent 7d12f31 commit 02d20bd
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Raven.Client.Lightweight.FSharp/Raven.fs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open System.Linq.Expressions
open System.ComponentModel.Composition open System.ComponentModel.Composition


open Raven.Client open Raven.Client
open Raven.Client.Indexes
open Raven.Client.Linq open Raven.Client.Linq
open Raven.Client.Document open Raven.Client.Document
open Raven.Abstractions.Extensions open Raven.Abstractions.Extensions
Expand All @@ -21,6 +22,19 @@ open Newtonsoft.Json


let luceneQuery<'a, 'b> (f : (IDocumentQuery<'a> -> IDocumentQuery<'b>)) (a : IDocumentSession) = let luceneQuery<'a, 'b> (f : (IDocumentQuery<'a> -> IDocumentQuery<'b>)) (a : IDocumentSession) =
f(a.Advanced.LuceneQuery<'a>()).AsEnumerable() f(a.Advanced.LuceneQuery<'a>()).AsEnumerable()

let select (p : Expr<'a -> 'b>) (a : IRavenQueryable<'a>) =
let expr = Linq.toLinqExpression (fun b a -> Expression.Lambda<Func<'a,'b>>(a, b |> Array.ofSeq)) p
a.Select(expr)

let skip n (a : IRavenQueryable<'a>) =
a.Skip(n)

let take n (a : IRavenQueryable<'a>) =
a.Take(n)

let queryIndex<'a, 'b, 'index when 'index : (new : unit -> 'index) and 'index :> AbstractIndexCreationTask> (f : (IRavenQueryable<'a> -> IQueryable<'b>)) (a : IDocumentSession) =
f(a.Query<'a, 'index>()).AsEnumerable()


let where (p : Expr<'a -> bool>) (a : IRavenQueryable<'a>) = let where (p : Expr<'a -> bool>) (a : IRavenQueryable<'a>) =
let expr = Linq.toLinqExpression (fun b a -> Expression.Lambda<Func<'a,bool>>(a, b |> Array.ofSeq)) p let expr = Linq.toLinqExpression (fun b a -> Expression.Lambda<Func<'a,bool>>(a, b |> Array.ofSeq)) p
Expand Down
47 changes: 46 additions & 1 deletion Raven.Tests.FSharp/RavenTests.fs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -133,6 +133,51 @@ type ``Given a Initailised Document store execute using computation expression``
return (!results |> List.rev) return (!results |> List.rev)
} }


[<Fact>]
let ``Should be able to skip results with skip``() =
use ds = test.NewDocumentStore()
use session = ds.OpenSession()
let actual =
raven {
do! storeMany (createCustomers 15) >> ignore
let! actual = query (where <@ fun x -> x.Dob < new DateTime(2012,1,7) @> >> skip 3)
return actual
} |> run session |> Seq.toList

let expected = createCustomers ((new DateTime(2012,1,7)).Subtract(new DateTime(2012,1,1)).Days) |> Seq.skip 3 |> Seq.toList

Assert.True((expected = actual))

[<Fact>]
let ``Should be able to take n results with take``() =
use ds = test.NewDocumentStore()
use session = ds.OpenSession()
let actual =
raven {
do! storeMany (createCustomers 15) >> ignore
let! actual = query (where <@ fun x -> x.Dob < new DateTime(2012,1,7) @> >> take 3)
return actual
} |> run session |> Seq.toList

let expected = createCustomers ((new DateTime(2012,1,4)).Subtract(new DateTime(2012,1,1)).Days) |> Seq.toList

Assert.True((expected = actual))

[<Fact>]
let ``Should be able to project a property with select``() =
use ds = test.NewDocumentStore()
use session = ds.OpenSession()
let actual =
raven {
do! storeMany (createCustomers 7) >> ignore
let! actual = query (select <@ fun x -> x.Id @>)
return actual
} |> run session |> Seq.toList

let expected = createCustomers 7 |> Seq.map (fun x -> x.Id) |> Seq.toList

Assert.True((expected = actual))

[<Fact>] [<Fact>]
let ``Should implicitly call save changes if runs to completion``() = let ``Should implicitly call save changes if runs to completion``() =
use ds = test.NewDocumentStore() use ds = test.NewDocumentStore()
Expand All @@ -142,7 +187,7 @@ type ``Given a Initailised Document store execute using computation expression``


//Humm... Is this the only way I could get it too compile, Assert.Equals cant resolve the correct overload //Humm... Is this the only way I could get it too compile, Assert.Equals cant resolve the correct overload
Assert.True((expected = actual)) Assert.True((expected = actual))

[<Fact>] [<Fact>]
let ``Should be able to save and retrieve an entity``() = let ``Should be able to save and retrieve an entity``() =
use ds = test.NewDocumentStore() use ds = test.NewDocumentStore()
Expand Down
24 changes: 24 additions & 0 deletions RavenDB.sln
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{CFD25A
CommonAssemblyInfo.cs = CommonAssemblyInfo.cs CommonAssemblyInfo.cs = CommonAssemblyInfo.cs
EndProjectSection EndProjectSection
EndProject EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Raven.Client.Lightweight.FSharp", "Raven.Client.Lightweight.FSharp\Raven.Client.Lightweight.FSharp.fsproj", "{6996133D-EE21-4CC8-B29B-F6222EE476BB}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Raven.Tests.FSharp", "Raven.Tests.FSharp\Raven.Tests.FSharp.fsproj", "{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -241,6 +245,26 @@ Global
{8A1DB308-D56B-49BC-A421-A2AB32617215}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {8A1DB308-D56B-49BC-A421-A2AB32617215}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{8A1DB308-D56B-49BC-A421-A2AB32617215}.Release|Mixed Platforms.Build.0 = Release|Any CPU {8A1DB308-D56B-49BC-A421-A2AB32617215}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{8A1DB308-D56B-49BC-A421-A2AB32617215}.Release|x86.ActiveCfg = Release|Any CPU {8A1DB308-D56B-49BC-A421-A2AB32617215}.Release|x86.ActiveCfg = Release|Any CPU
{6996133D-EE21-4CC8-B29B-F6222EE476BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6996133D-EE21-4CC8-B29B-F6222EE476BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6996133D-EE21-4CC8-B29B-F6222EE476BB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{6996133D-EE21-4CC8-B29B-F6222EE476BB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{6996133D-EE21-4CC8-B29B-F6222EE476BB}.Debug|x86.ActiveCfg = Debug|Any CPU
{6996133D-EE21-4CC8-B29B-F6222EE476BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6996133D-EE21-4CC8-B29B-F6222EE476BB}.Release|Any CPU.Build.0 = Release|Any CPU
{6996133D-EE21-4CC8-B29B-F6222EE476BB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{6996133D-EE21-4CC8-B29B-F6222EE476BB}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{6996133D-EE21-4CC8-B29B-F6222EE476BB}.Release|x86.ActiveCfg = Release|Any CPU
{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}.Debug|Any CPU.Build.0 = Debug|Any CPU
{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}.Debug|x86.ActiveCfg = Debug|Any CPU
{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}.Release|Any CPU.ActiveCfg = Release|Any CPU
{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}.Release|Any CPU.Build.0 = Release|Any CPU
{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{295D9C7D-5F05-4B12-BB1E-8FB7A21E9071}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
Expand Down

0 comments on commit 02d20bd

Please sign in to comment.