Last active
June 20, 2019 13:14
-
-
Save atsapura/bdce02913ee58b14278d48539aae5965 to your computer and use it in GitHub Desktop.
Mocking interpreter for testing execution trees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TestingInterpreter = | |
open CardManagement | |
open System | |
open CardDomain | |
open CardManagement.Common.Errors | |
open CardManagement.Common.CommonTypes | |
open CardManagement.Common.CountryModule | |
open CardProgramBuilder | |
open CardManagement.Common | |
type SaveResult = Result<unit, DataRelatedError> | |
type TestInterpreterConfig = | |
{ GetCard: Card option | |
GetCardWithAccountInfo: (Card*AccountInfo) option | |
CreateCard: SaveResult | |
ReplaceCard: SaveResult | |
GetUser: User option | |
CreateUser: SaveResult | |
GetBalanceOperations: BalanceOperation list | |
SaveBalanceOperation: SaveResult } | |
let private valueOrThrow = | |
function | |
| Ok v -> v | |
| Error e -> sprintf "%A" e |> failwith | |
let letterString = LetterString.create "asd" "letters" |> valueOrThrow | |
let cardNumber = CardNumber.create "cardNumber" "1234123412341234" |> valueOrThrow | |
let postalCode = PostalCode.create "postalCode" "12345" |> valueOrThrow | |
let year = Year.create "year" 2023us |> valueOrThrow | |
let userId = Guid.NewGuid() | |
let accountInfo = | |
{ Holder = userId | |
Balance = Money 1000m | |
DailyLimit = DailyLimit.ofDecimal 0m } | |
let card = | |
{ Card.Name = letterString | |
Number = cardNumber | |
Expiration = (January, year) | |
HolderId = userId | |
AccountDetails = Active accountInfo } | |
let balanceOperations = | |
[ { CardNumber = cardNumber | |
Timestamp = DateTimeOffset.UtcNow | |
BalanceChange = MoneyTransaction.create 100m |> valueOrThrow |> Increase | |
NewBalance = Money 1100m}] | |
let user = | |
{ UserInfo = | |
{ UserInfo.Id = userId | |
Name = letterString | |
Address = | |
{ Country = Country.Russia | |
City = letterString | |
PostalCode = postalCode | |
AddressLine1 = "" | |
AddressLine2 = ""}} | |
Cards = Set.singleton card } | |
let defaultConfig = | |
{ GetCard = Some card | |
GetUser = Some user | |
GetCardWithAccountInfo = (card, accountInfo) |> Some | |
CreateCard = Ok() | |
GetBalanceOperations = balanceOperations | |
SaveBalanceOperation = Ok() | |
ReplaceCard = Ok() | |
CreateUser = Ok() } | |
let testInject a = fun _ -> a | |
let rec interpretCardProgram config (prog: Program<'a>) = | |
match prog with | |
| GetCard (cardNumber, next) -> | |
cardNumber |> testInject config.GetCard |> (next >> interpretCardProgram config) | |
| GetCardWithAccountInfo (number, next) -> | |
number |> testInject config.GetCardWithAccountInfo |> (next >> interpretCardProgram config) | |
| CreateCard ((card,acc), next) -> | |
(card, acc) |> testInject config.CreateCard |> (next >> interpretCardProgram config) | |
| ReplaceCard (card, next) -> | |
card |> testInject config.ReplaceCard |> (next >> interpretCardProgram config) | |
| GetUser (id, next) -> | |
id |> testInject config.GetUser |> (next >> interpretCardProgram config) | |
| CreateUser (user, next) -> | |
user |> testInject config.CreateUser |> (next >> interpretCardProgram config) | |
| GetBalanceOperations (request, next) -> | |
testInject config.GetBalanceOperations request |> (next >> interpretCardProgram config) | |
| SaveBalanceOperation (op, next) -> | |
testInject config.SaveBalanceOperation op |> (next >> interpretCardProgram config) | |
| Stop a -> a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment