Skip to content
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

Use Diffract as assertion lib #5

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<!--<LangVersion>6.0</LangVersion>-->
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
<WarningLevel>5</WarningLevel>
Expand Down
2 changes: 1 addition & 1 deletion EsBankAccount.Tests/Domain/BankAccountTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let ``when withdrawing, the threshold limit should not be exceeded`` () =
When
( Withdraw (1m, DateTime.MinValue, Some thresholdLimit) )
ThenError
( ThresholdExceeded (-501m, thresholdLimit) |> WithdrawingError )
( ThresholdExceeded (-501m, thresholdLimit) |> WithdrawalError )
}

[<Fact>]
Expand Down
25 changes: 13 additions & 12 deletions EsBankAccount.Tests/Dsl/DeciderSpecs.fs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace EsBankAccount.Tests.Domain

open Swensen.Unquote
open Diffract

type DeciderSpecState<'State, 'Outcome> =
{ State: 'State
Outcome: 'Outcome }

type DeciderSpecList<'State, 'Command, 'Event>
type DeciderSpecList<'State, 'Command, 'Event when 'Event: equality>
akhansari marked this conversation as resolved.
Show resolved Hide resolved
(initialState: 'State
, evolve: 'State -> 'Event -> 'State
, decide: 'Command -> 'State -> 'Event list)
Expand All @@ -28,12 +28,13 @@ type DeciderSpecList<'State, 'Command, 'Event>
Outcome = events }

[<CustomOperation "Then">]
member _.Then (spec, expected) =
let events = spec.Outcome
test <@ events = expected @>
member _.Then (spec, expected: 'Event list) =
if not (expected = spec.Outcome) then
Diffract.Assert (expected, spec.Outcome)
spec

type DeciderSpecResult<'State, 'Command, 'Event, 'Error>
type DeciderSpecResult<'State, 'Command, 'Event, 'Error
when 'Event: equality and 'Error: equality>
(initialState: 'State
, evolve: 'State -> 'Event -> 'State
, decide: 'Command -> 'State -> Result<'Event list, 'Error>)
Expand All @@ -58,13 +59,13 @@ type DeciderSpecResult<'State, 'Command, 'Event, 'Error>
Outcome = result }

[<CustomOperation "Then">]
member _.ThenOk (spec, expected) =
let result = spec.Outcome
test <@ result = Ok expected @>
member _.ThenOk (spec, expected: 'Event list) =
if not (Ok expected = spec.Outcome) then
Diffract.Assert (Ok expected, spec.Outcome)
spec

[<CustomOperation "ThenError">]
member _.ThenError (spec, expected) =
let result = spec.Outcome
test <@ result = Error expected @>
member _.ThenError (spec, expected: 'Error) =
if not (Error expected = spec.Outcome) then
Diffract.Assert (Error expected, spec.Outcome)
spec
18 changes: 10 additions & 8 deletions EsBankAccount.Tests/Dsl/DeciderStateSpecs.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace EsBankAccount.Tests.Domain

open Swensen.Unquote
open Diffract

type DeciderStateSpecList<'State, 'Command, 'Event>
type DeciderStateSpecList<'State, 'Command, 'Event when 'State: equality>
(initialState: 'State
, evolve: 'State -> 'Event -> 'State
, decide: 'Command -> 'State -> 'Event list)
Expand All @@ -21,11 +21,12 @@ type DeciderStateSpecList<'State, 'Command, 'Event>
|> List.fold evolve state

[<CustomOperation "Then">]
member _.Then (state, expected) =
test <@ state = expected @>
member _.Then (state, expected: 'State) =
if not (expected = state) then
Diffract.Assert (expected, state)
state

type DeciderStateSpecResult<'State, 'Command, 'Event, 'Error>
type DeciderStateSpecResult<'State, 'Command, 'Event, 'Error when 'State: equality>
(initialState: 'State
, evolve: 'State -> 'Event -> 'State
, decide: 'Command -> 'State -> Result<'Event list, 'Error>)
Expand All @@ -35,7 +36,7 @@ type DeciderStateSpecResult<'State, 'Command, 'Event, 'Error>
initialState

[<CustomOperation "Given">]
member _.Given (_, newState) =
member _.Given (_, newState: 'State) =
newState

[<CustomOperation "GivenEvents">]
Expand All @@ -49,6 +50,7 @@ type DeciderStateSpecResult<'State, 'Command, 'Event, 'Error>
| Error _ -> state

[<CustomOperation "Then">]
member _.Then (state, expected) =
test <@ state = expected @>
member _.Then (state, expected: 'State) =
if not (expected = state) then
Diffract.Assert (expected, state)
state
4 changes: 2 additions & 2 deletions EsBankAccount.Tests/EsBankAccount.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

<Compile Include="Startup\BankAccountClientTests.fs" />

<Compile Include="Program.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FsCheck.Xunit" Version="2.16.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Unquote" Version="6.1.0" />
<PackageReference Include="Diffract" Version="0.1.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions EsBankAccount.Tests/Startup/BankAccountClientTests.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module EsBankAccount.Tests.Startup.BankAccountClientTests

open System
open Swensen.Unquote
open Xunit

open EsBankAccount.Startup
Expand All @@ -18,5 +17,5 @@ let ``Should deposit and then withdraw`` () =
let! _depositResult = deposit accountId 10m
let! _withdrawalResult = withdraw accountId 5m
let! account = ReadModelClient.readAccount accountId
account.Transactions.Length =! 2
Assert.Equal (2, account.Transactions.Length)
}
6 changes: 3 additions & 3 deletions EsBankAccount/Domain/BankAccount.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ let private close date state =

type Error =
| AlreadyClosed
| WithdrawingError of WithdrawingError
| ClosingError of ClosingError
| WithdrawalError of WithdrawingError
| ClosingError of ClosingError
and WithdrawingError =
| ThresholdExceeded of upcomingBalance: Amount * thresholdLimit: Amount
and ClosingError =
Expand All @@ -78,7 +78,7 @@ module private Check =
let thresholdLimit thresholdLimit amount state =
match thresholdLimit with
| Some thresholdLimit when state.Balance - amount < thresholdLimit ->
ThresholdExceeded (state.Balance - amount, thresholdLimit) |> WithdrawingError |> Error
ThresholdExceeded (state.Balance - amount, thresholdLimit) |> WithdrawalError |> Error
| _ ->
Ok ()

Expand Down
2 changes: 1 addition & 1 deletion EsBankAccount/Startup/BankAccountClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module private Client =
let errorMessage = function
| BankAccount.AlreadyClosed ->
"Account already closed"
| BankAccount.WithdrawingError (BankAccount.ThresholdExceeded _) ->
| BankAccount.WithdrawalError (BankAccount.ThresholdExceeded _) ->
"Threshold exceeded, withdrawal denied"
| BankAccount.ClosingError (BankAccount.BalanceIsNegative _) ->
"Balance is negative, closing denied"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ There are two kinds of them:
- We aren't too concerned about the outcome.
- But, we need to build the state in a particular way.

It should be noted that the BDD DSL style brings more readability and neat helpers but it isn't mandatory.\
In your test files you can have different kind of unit tests. For instance a test could be as simple as [this](https://github.com/thinkbeforecoding/UnoCore/blob/solution/Uno.Tests/Tests.fs).

### Decider Structure

It's possible to organize the Decider into five sections:
Expand Down