Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 24c5de506f
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 326 lines (265 sloc) 11.898 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
namespace Raven.Tests

open System
open System.Linq
open Xunit
open Raven.Client.Linq
open Raven.Client
open Raven.Tests
open Raven.Abstractions.Extensions

type ``With Raven Computation Expression Builder``() =
    inherit RavenTest()

    [<Fact>]
    member test.``Zero should allow empty else branch``() =
       use ds = test.NewDocumentStore()
       use session = ds.OpenSession()
       let called = ref false
       raven {
               if false then called := true
             } |> run session
       Assert.False(!called)


    [<Fact>]
    member test.``TryCatch should catch exception``() =
        use ds = test.NewDocumentStore()
        use session = ds.OpenSession()
        let called = ref false
        raven {
                try
                  failwith "Oh no!"
                with e ->
                  called := true
              } |> run session

        Assert.True(!called)

    [<Fact>]
    member test.``TryFinally with exception should execute finally``() =
        use ds = test.NewDocumentStore()
        use session = ds.OpenSession()
        let called = ref false
        let r = raven {
                      try
                        failwith "Oh No"
                      finally
                        called := true
                     }
        
        Assert.Throws<Exception>(fun () -> r |> run session |> ignore) |> ignore
        Assert.True(!called)

    [<Fact>]
    member test.``use should call Dispose``() =
        use ds = test.NewDocumentStore()
        use session = ds.OpenSession()
        let disposed = ref false
        let disp =
          { new IDisposable with
              member x.Dispose() = disposed := true }
    
        raven { use d = disp
                ()
              } |> run session
          
        Assert.True(!disposed)

    [<Fact>]
    member test.``use! should call Dispose``() =
        use ds = test.NewDocumentStore()
        use session = ds.OpenSession()
        let disposed = ref false
        let disp = (fun s ->
          { new IDisposable with
              member x.Dispose() = disposed := true })
    
        raven { use! d = disp
                ()
              } |> run session
          
        Assert.True(!disposed)

    [<Fact>]
    member test.``While should loop and increment count``() =
      use ds = test.NewDocumentStore()
      use session = ds.OpenSession()
      let count = ref 0
      raven {
        while !count < 1 do
          incr count } |> run session
    
      
      Assert.Equal(1, !count)

    [<Fact>]
    member test.``For should loop and increment count``() =
      use ds = test.NewDocumentStore()
      use session = ds.OpenSession()
      let count = ref 0
      raven {
        for i in [0;1] do incr count } |> run session

      Assert.Equal(2, !count)

    [<Fact>]
    member test.``Combine should combine if statement and return true branch``() =
        use ds = test.NewDocumentStore()
        use session = ds.OpenSession()
        let expected = "Foo"
        let actual =
            raven {
                if true then ()
                return expected } |> run session
       
        Assert.True((expected = actual))
      
       


type ``Given a Initailised Document store execute using computation expression``() as test =
    inherit RavenTest()
    
    let createCustomers n =
        Seq.init n (fun i -> Customer.Create("test_"+i.ToString(), (new DateTime(2012, 1, 1)).AddDays(i |> float))) |> List.ofSeq

    let storeMany (data : seq<'a>) =
        raven {
            let results = ref []
            for d in data do
                let! a = store d
                results := a :: !results
            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()
        ds.Conventions.DefaultQueryingConsistency <- Raven.Client.Document.ConsistencyOptions.QueryYourWrites
        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>]
    let ``Should implicitly call save changes if runs to completion``() =
           use ds = test.NewDocumentStore()
           use session = ds.OpenSession()
           let expected : Customer list= storeMany (createCustomers 15) |> run session
           let actual : Customer list = session.Query<Customer>().Customize(fun x->x.WaitForNonStaleResults() |> ignore).AsEnumerable() |> Seq.toList

           //Humm... Is this the only way I could get it too compile, Assert.Equals cant resolve the correct overload
           Assert.True((expected = actual))
    
    [<Fact>]
    let ``Should be able to save and retrieve an entity``() =
           use ds = test.NewDocumentStore()
           use session = ds.OpenSession()
           let exp, act =
                raven {
                         let! expected = store (Customer.Create("test", new DateTime(2012, 1, 1)))
                         do! saveChanges
                         let! actual = (load<Customer> ["customers/test"] >> Seq.head)
                         return expected, actual
                      } |> run session
           Assert.Equal(exp,act)
    
    [<Fact>]
    let ``Should be able to save and retrieve an entity with tryLoad``() =
           use ds = test.NewDocumentStore()
           use session = ds.OpenSession()
           let cust, act =
                raven {
                         let! customer = store (Customer.Create("test", new DateTime(2012, 1, 1)))
                         do! saveChanges
                         let! actual = (tryLoad<Customer> ["customers/test";"customers/doesntexist"])
                         return customer, actual
                      } |> run session
           let exp =
                [
                  ("customers/test", Option.Some(cust));
                  ("customers/doesntexist", None)
                ]
           Assert.True((exp = (act |> Seq.toList)))

    [<Fact>]
    let ``Should be able to query for filtered all entites``() =
        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) @>)
                            return actual
                         } |> run session |> Seq.toList

        let expected = createCustomers ((new DateTime(2012,1,7)).Subtract(new DateTime(2012,1,1)).Days)

        Assert.True((expected = actual))
    
    [<Fact>]
    let ``Should be able to query an entity including a reference``() =
        use ds = test.NewDocumentStore()
        use session = ds.OpenSession()
        let customer = Customer.Create("Test", (new DateTime(2012, 1, 1)))
        let products = [| Product.Create("1-23-4", "Cat Food", 2.30) |]
        let order = Order.Create(customer.Id, products)

        let retrieved =
            raven {
                do! store customer >> ignore
                do! store order >> ignore
                do! saveChanges
                return! (fun s ->
                              let order = including <@ fun s -> s.Customer @> (fun s -> s.Load("orders/1")) s
                              let customer : Customer = s.Load(order.Customer)
                              order, customer
                        )
            } |> run session

        Assert.Equal(order,fst(retrieved))
        Assert.Equal(customer,snd(retrieved))

    [<Fact>]
    let ``Should be able to query for filtered batches of entites``() =
        use ds = test.NewDocumentStore()
        use session = ds.OpenSession()
        let testData = Seq.init 60 (fun i -> Customer.Create("test_"+i.ToString(), (new DateTime(2012, 1, 1)).AddDays(i |> float))) |> List.ofSeq
        
        let actual =
            raven {
                let! a = storeMany testData
                return! query (where <@ fun x -> x.Dob < new DateTime(2012,2,1) @>)
            } |> run session |> Seq.toList

        let expected = Seq.init 31 (fun i -> Customer.Create("test_"+i.ToString(), (new DateTime(2012, 1, 1)).AddDays(i |> float))) |> Seq.toList

        Assert.True((expected = actual))


    [<Fact>]
    let ``Should be able to compose queries``() =
        use ds = test.NewDocumentStore()
        use session = ds.OpenSession()
        let customer = Customer.Create("Test", (new DateTime(2012, 1, 1)))
        let products = [| Product.Create("1-23-4", "Cat Food", 2.30) |]
        let order = Order.Create(customer.Id, products)

        let storeData =
             raven {
                    do! store customer >> ignore
                    do! store order >> ignore
                    do! saveChanges
                }

        let findOrderIncludeCustomer (id : string) =
            raven {
                 return! (fun s ->
                              let order = including <@ fun s -> s.Customer @> (fun s -> s.Load(id)) s
                              let customer : Customer = s.Load(order.Customer)
                              order, customer
                        )
            }

        let retrieved =
            raven {
                do! storeData
                return! findOrderIncludeCustomer "orders/1"
            } |> run session

        Assert.Equal(order,fst(retrieved))
        Assert.Equal(customer,snd(retrieved))

    [<Fact>]
    let ``Should be able to query via a lucene query``() =
        use ds = test.NewDocumentStore()
        use session = ds.OpenSession()
        let testData = Seq.init 60 (fun i -> Customer.Create("test_"+i.ToString(), (new DateTime(2012, 1, 1)).AddDays(i |> float))) |> List.ofSeq
        
        let actual =
            raven {
                    let! a = storeMany testData
                    return! luceneQuery (fun docQuery -> docQuery.WhereLessThan("Dob", new DateTime(2012,2,1)))
            } |> run session |> Seq.toList

        let expected = Seq.init 31 (fun i -> Customer.Create("test_"+i.ToString(), (new DateTime(2012, 1, 1)).AddDays(i |> float))) |> Seq.toList

        Assert.True((expected = actual))
Something went wrong with that request. Please try again.