correct and simplify test cases
This commit is contained in:
parent
bd17ead09a
commit
489b8cb45e
@ -51,7 +51,6 @@ test("contract validation", t => {
|
|||||||
products: [],
|
products: [],
|
||||||
refund_deadline: "Date(12345)",
|
refund_deadline: "Date(12345)",
|
||||||
timestamp: "Date(12345)",
|
timestamp: "Date(12345)",
|
||||||
transaction_id: 1234,
|
|
||||||
fulfillment_url: "foo",
|
fulfillment_url: "foo",
|
||||||
wire_method: "test",
|
wire_method: "test",
|
||||||
order_id: "test_order",
|
order_id: "test_order",
|
||||||
|
15
src/types.ts
15
src/types.ts
@ -741,6 +741,21 @@ export namespace Amounts {
|
|||||||
export function isNonZero(a: AmountJson) {
|
export function isNonZero(a: AmountJson) {
|
||||||
return a.value > 0 || a.fraction > 0;
|
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")),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,30 +1,57 @@
|
|||||||
import {test} from "ava";
|
import {test} from "ava";
|
||||||
import {mkAmount} from "./types";
|
import * as types from "./types";
|
||||||
import * as wallet from "./wallet";
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
function fakeCwd(current: string, value: string, feeDeposit: string): wallet.CoinWithDenom {
|
||||||
|
return {
|
||||||
|
coin: {
|
||||||
|
currentAmount: a(current),
|
||||||
|
coinPub: "(mock)",
|
||||||
|
coinPriv: "(mock)",
|
||||||
|
denomPub: "(mock)",
|
||||||
|
denomSig: "(mock)",
|
||||||
|
exchangeBaseUrl: "(mock)",
|
||||||
|
blindingKey: "(mock)",
|
||||||
|
reservePub: "(mock)",
|
||||||
|
status: types.CoinStatus.Fresh,
|
||||||
|
},
|
||||||
|
denom: {
|
||||||
|
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)",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
test("coin selection 1", t => {
|
test("coin selection 1", t => {
|
||||||
let cds: any = [];
|
let cds: wallet.CoinWithDenom[] = [
|
||||||
cds.push({
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.1"),
|
||||||
coin: {
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.0"),
|
||||||
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"),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
let res = wallet.selectCoins(cds, mkAmount(2,0,"EUR"), mkAmount(0,5,"EUR"));
|
let res = wallet.selectCoins(cds, a("EUR:2.0"), a("EUR:0.1"));
|
||||||
if (!res) {
|
if (!res) {
|
||||||
t.fail();
|
t.fail();
|
||||||
return;
|
return;
|
||||||
@ -35,37 +62,13 @@ test("coin selection 1", t => {
|
|||||||
|
|
||||||
|
|
||||||
test("coin selection 2", t => {
|
test("coin selection 2", t => {
|
||||||
let cds: any = [];
|
let cds: wallet.CoinWithDenom[] = [
|
||||||
cds.push({
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"),
|
||||||
coin: {
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.0"),
|
||||||
currentAmount: mkAmount(1, 0, "EUR"),
|
// Merchant covers the fee, this one shouldn't be used
|
||||||
},
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.0"),
|
||||||
denom: {
|
];
|
||||||
value: mkAmount(1, 0, "EUR"),
|
let res = wallet.selectCoins(cds, a("EUR:2.0"), a("EUR:0.5"));
|
||||||
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"));
|
|
||||||
if (!res) {
|
if (!res) {
|
||||||
t.fail();
|
t.fail();
|
||||||
return;
|
return;
|
||||||
@ -75,78 +78,31 @@ 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"));
|
|
||||||
if (!res) {
|
|
||||||
t.fail();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
t.true(res.length == 2);
|
|
||||||
t.pass();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
test("coin selection 3", t => {
|
test("coin selection 3", t => {
|
||||||
let cds: any = [];
|
let cds: wallet.CoinWithDenom[] = [
|
||||||
cds.push({
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"),
|
||||||
coin: {
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"),
|
||||||
currentAmount: mkAmount(1, 0, "EUR"),
|
// this coin should be selected instead of previous one with fee
|
||||||
},
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.0"),
|
||||||
denom: {
|
];
|
||||||
value: mkAmount(1, 0, "EUR"),
|
let res = wallet.selectCoins(cds, a("EUR:2.0"), a("EUR:0.5"));
|
||||||
fee_deposit: mkAmount(0, 5, "EUR"),
|
if (!res) {
|
||||||
},
|
t.fail();
|
||||||
});
|
return;
|
||||||
cds.push({
|
}
|
||||||
coin: {
|
t.true(res.length == 2);
|
||||||
currentAmount: mkAmount(1, 0, "EUR"),
|
t.pass();
|
||||||
},
|
});
|
||||||
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) {
|
if (!res) {
|
||||||
t.fail();
|
t.fail();
|
||||||
return;
|
return;
|
||||||
@ -156,38 +112,24 @@ test("coin selection 3", t => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
test("coin selection 3", t => {
|
test("coin selection 5", t => {
|
||||||
let cds: any = [];
|
let cds: wallet.CoinWithDenom[] = [
|
||||||
cds.push({
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"),
|
||||||
coin: {
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"),
|
||||||
currentAmount: mkAmount(1, 0, "EUR"),
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"),
|
||||||
},
|
];
|
||||||
denom: {
|
let res = wallet.selectCoins(cds, a("EUR:4.0"), a("EUR:0.2"));
|
||||||
value: mkAmount(1, 0, "EUR"),
|
t.true(!res);
|
||||||
fee_deposit: mkAmount(0, 5, "EUR"),
|
t.pass();
|
||||||
},
|
});
|
||||||
});
|
|
||||||
cds.push({
|
|
||||||
coin: {
|
test("coin selection 6", t => {
|
||||||
currentAmount: mkAmount(1, 0, "EUR"),
|
let cds: wallet.CoinWithDenom[] = [
|
||||||
},
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"),
|
||||||
denom: {
|
fakeCwd("EUR:1.0", "EUR:1.0", "EUR:0.5"),
|
||||||
value: mkAmount(1, 0, "EUR"),
|
];
|
||||||
fee_deposit: mkAmount(0, 0, "EUR"),
|
let res = wallet.selectCoins(cds, a("EUR:2.0"), a("EUR:0.2"));
|
||||||
},
|
|
||||||
});
|
|
||||||
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"));
|
|
||||||
t.true(!res);
|
t.true(!res);
|
||||||
t.pass();
|
t.pass();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user