From 489b8cb45e4a13ab85d008946acf91ac438851fa Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sun, 28 May 2017 00:00:26 +0200 Subject: correct and simplify test cases --- src/types-test.ts | 1 - src/types.ts | 15 ++++ src/wallet-test.ts | 226 ++++++++++++++++++++--------------------------------- 3 files changed, 99 insertions(+), 143 deletions(-) diff --git a/src/types-test.ts b/src/types-test.ts index 780d49e45..88f9f2723 100644 --- a/src/types-test.ts +++ b/src/types-test.ts @@ -51,7 +51,6 @@ test("contract validation", t => { products: [], refund_deadline: "Date(12345)", timestamp: "Date(12345)", - transaction_id: 1234, fulfillment_url: "foo", wire_method: "test", order_id: "test_order", diff --git a/src/types.ts b/src/types.ts index 240275975..53f98948e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -741,6 +741,21 @@ export namespace Amounts { export function isNonZero(a: AmountJson) { return a.value > 0 || a.fraction > 0; } + + /** + * Parse an amount like 'EUR:20.5' for 20 Euros and 50 ct. + */ + export function parse(s: string): AmountJson|undefined { + let res = s.match(/([a-zA-Z0-9_*-]+):([0-9])+([.][0-9]+)?/); + if (!res) { + return undefined; + } + return { + currency: res[1], + value: Number.parseInt(res[2]), + fraction: Math.round(fractionalBase * Number.parseFloat(res[3] || "0")), + } + } } diff --git a/src/wallet-test.ts b/src/wallet-test.ts index 56b5e6027..2c3ed52d0 100644 --- a/src/wallet-test.ts +++ b/src/wallet-test.ts @@ -1,30 +1,57 @@ import {test} from "ava"; -import {mkAmount} from "./types"; +import * as types from "./types"; import * as wallet from "./wallet"; +function a(x: string): types.AmountJson { + let amt = types.Amounts.parse(x); + if (!amt) { + throw Error("invalid amount"); + } + return amt; +} -test("coin selection 1", t => { - let cds: any = []; - cds.push({ +function fakeCwd(current: string, value: string, feeDeposit: string): wallet.CoinWithDenom { + return { coin: { - currentAmount: mkAmount(1, 0, "EUR"), + currentAmount: a(current), + coinPub: "(mock)", + coinPriv: "(mock)", + denomPub: "(mock)", + denomSig: "(mock)", + exchangeBaseUrl: "(mock)", + blindingKey: "(mock)", + reservePub: "(mock)", + status: types.CoinStatus.Fresh, }, denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 5, "EUR"), + value: a(value), + feeDeposit: a(feeDeposit), + denomPub: "(mock)", + denomPubHash: "(mock)", + feeWithdraw: a("EUR:0.0"), + feeRefresh: a("EUR:0.0"), + feeRefund: a("EUR:0.0"), + stampStart: "(mock)", + stampExpireWithdraw: "(mock)", + stampExpireLegal: "(mock)", + stampExpireDeposit: "(mock)", + masterSig: "(mock)", + status: types.DenominationStatus.VerifiedGood, + isOffered: true, + exchangeBaseUrl: "(mock)", }, - }); - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 0, "EUR"), - }, - }); + } +} + + - let res = wallet.selectCoins(cds, mkAmount(2,0,"EUR"), mkAmount(0,5,"EUR")); +test("coin selection 1", t => { + let cds: wallet.CoinWithDenom[] = [ + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.1"), + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.0"), + ]; + + let res = wallet.selectCoins(cds, a("EUR:2.0"), a("EUR:0.1")); if (!res) { t.fail(); return; @@ -35,37 +62,13 @@ test("coin selection 1", t => { test("coin selection 2", t => { - let cds: any = []; - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 5, "EUR"), - }, - }); - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 0, "EUR"), - }, - }); - // Merchant covers the fee, this one shouldn't be used - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 0, "EUR"), - }, - }); - - let res = wallet.selectCoins(cds, mkAmount(2,0,"EUR"), mkAmount(0,5,"EUR")); + let cds: wallet.CoinWithDenom[] = [ + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.0"), + // Merchant covers the fee, this one shouldn't be used + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.0"), + ]; + let res = wallet.selectCoins(cds, a("EUR:2.0"), a("EUR:0.5")); if (!res) { t.fail(); return; @@ -75,37 +78,14 @@ test("coin selection 2", t => { }); -test("coin selection 2", t => { - let cds: any = []; - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 5, "EUR"), - }, - }); - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 0, "EUR"), - }, - }); - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 0, "EUR"), - }, - }); - - let res = wallet.selectCoins(cds, mkAmount(2,0,"EUR"), mkAmount(0,2,"EUR")); +test("coin selection 3", t => { + let cds: wallet.CoinWithDenom[] = [ + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + // this coin should be selected instead of previous one with fee + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.0"), + ]; + let res = wallet.selectCoins(cds, a("EUR:2.0"), a("EUR:0.5")); if (!res) { t.fail(); return; @@ -116,37 +96,13 @@ test("coin selection 2", t => { -test("coin selection 3", t => { - let cds: any = []; - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 5, "EUR"), - }, - }); - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 0, "EUR"), - }, - }); - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 5, "EUR"), - }, - }); - - let res = wallet.selectCoins(cds, mkAmount(2,0,"EUR"), mkAmount(0,2,"EUR")); +test("coin selection 4", t => { + let cds: wallet.CoinWithDenom[] = [ + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + ]; + let res = wallet.selectCoins(cds, a("EUR:2.0"), a("EUR:0.2")); if (!res) { t.fail(); return; @@ -156,38 +112,24 @@ test("coin selection 3", t => { }); -test("coin selection 3", t => { - let cds: any = []; - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 5, "EUR"), - }, - }); - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 0, "EUR"), - }, - }); - cds.push({ - coin: { - currentAmount: mkAmount(1, 0, "EUR"), - }, - denom: { - value: mkAmount(1, 0, "EUR"), - fee_deposit: mkAmount(0, 5, "EUR"), - }, - }); - - let res = wallet.selectCoins(cds, mkAmount(4,0,"EUR"), mkAmount(0,2,"EUR")); +test("coin selection 5", t => { + let cds: wallet.CoinWithDenom[] = [ + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + ]; + let res = wallet.selectCoins(cds, a("EUR:4.0"), a("EUR:0.2")); t.true(!res); t.pass(); +}); + +test("coin selection 6", t => { + let cds: wallet.CoinWithDenom[] = [ + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"), + ]; + let res = wallet.selectCoins(cds, a("EUR:2.0"), a("EUR:0.2")); + t.true(!res); + t.pass(); }); -- cgit v1.2.3