add test cases for coin selection

This commit is contained in:
Florian Dold 2016-11-14 03:37:47 +01:00
parent af901cb892
commit ce33432036
3 changed files with 201 additions and 0 deletions

View File

@ -559,3 +559,10 @@ export interface CheckRepurchaseResult {
export interface Notifier {
notify(): void;
}
/**
* For terseness.
*/
export function mkAmount(value: number, fraction: number, currency: string): AmountJson {
return {value, fraction, currency};
}

193
src/wallet-test.ts Normal file
View File

@ -0,0 +1,193 @@
import {test, TestLib} from "testlib/talertest";
import {mkAmount} from "./types";
import * as wallet from "./wallet";
test("coin selection 1", (t: TestLib) => {
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"),
},
});
let res = wallet.selectCoins(cds, mkAmount(2,0,"EUR"), mkAmount(0,5,"EUR"));
if (!res) {
t.fail();
return;
}
t.assert(res.length == 2);
t.pass();
});
test("coin selection 2", (t: TestLib) => {
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"));
if (!res) {
t.fail();
return;
}
t.assert(res.length == 2);
t.pass();
});
test("coin selection 2", (t: TestLib) => {
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.assert(res.length == 2);
t.pass();
});
test("coin selection 3", (t: TestLib) => {
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"));
if (!res) {
t.fail();
return;
}
t.assert(res.length == 3);
t.pass();
});
test("coin selection 3", (t: TestLib) => {
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"));
t.assert(!res);
t.pass();
});

View File

@ -28,6 +28,7 @@
"src/types-test.ts",
"decl/chrome/chrome.d.ts",
"src/cryptoWorker.ts",
"src/wallet-test.ts",
"src/db.ts",
"src/emscriptif.ts",
"src/helpers.ts",