1
exercism fetch fsharp bank-account

BankAccountTest.fs

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
module BankAccountTest

open Xunit
open FsUnit.Xunit
open BankAccount

[<Fact>]
let ``Returns empty balance after opening`` () =
    let account = mkBankAccount() |> openAccount

    getBalance account |> should equal <| Some 0.0

[<Fact(Skip = "Remove to run test")>]
let ``Check basic balance`` () =
    let account = mkBankAccount() |> openAccount
    let openingBalance = account |> getBalance 

    let updatedBalance = 
        account
        |> updateBalance 10.0 
        |> getBalance

    openingBalance |> should equal <| Some 0.0
    updatedBalance |> should equal <| Some 10.0

[<Fact(Skip = "Remove to run test")>]
let ``Balance can increment or decrement`` () =    
    let account = mkBankAccount() |> openAccount
    let openingBalance = account |> getBalance 

    let addedBalance = 
        account 
        |> updateBalance 10.0
        |> getBalance

    let subtractedBalance = 
        account 
        |> updateBalance -15.0
        |> getBalance

    openingBalance |> should equal <| Some 0.0
    addedBalance |> should equal <| Some 10.0
    subtractedBalance |> should equal <| Some -5.0

[<Fact(Skip = "Remove to run test")>]
let ``Account can be closed`` () =
    let account = 
        mkBankAccount()
        |> openAccount
        |> closeAccount

    getBalance account |> should equal None
    
[<Fact(Skip = "Remove to run test")>]
let ``Account can be updated from multiple threads`` () =
    let account = 
        mkBankAccount()
        |> openAccount

    let updateAccountAsync =        
        async {                             
            account 
            |> updateBalance 1.0
            |> ignore
        }

    updateAccountAsync
    |> List.replicate 1000
    |> Async.Parallel 
    |> Async.RunSynchronously
    |> ignore

    getBalance account |> should equal <| Some 1000.0