Skip to content

Commit 6cead99

Browse files
author
maxime.dharboulle
committed
edit typescript and add rates to cucumber tests
1 parent 2b04596 commit 6cead99

File tree

7 files changed

+112
-33
lines changed

7 files changed

+112
-33
lines changed

features/stepDefinitions/walletValueStepDefs.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,29 @@ export class WalletValueSteps {
99

1010
wallet: Wallet = new Wallet()
1111
answers: number[] = []
12+
dates: string[] = []
13+
referenceRatesAtDate: Map<string,Map<string,Map<string,number>>> = new Map()
14+
referenceRateWalletValue: number = 0
1215

1316
@given('I have a wallet')
1417
public setNewWallet() {
1518
this.wallet = new Wallet()
1619
}
1720

21+
@given('the rates at the time of {string} are')
22+
public setRatesToMap(date: string, setRatesForDate: {rawTable: Array<Array<any>>}) {
23+
let referenceRates = new Map<string,Map<string,number>>()
24+
setRatesForDate.rawTable.forEach((rate: Array<string>) => {
25+
if(referenceRates.has(rate[0])){
26+
referenceRates.get(rate[0])?.set(rate[1], parseFloat(rate[2]))
27+
}else{
28+
referenceRates.set(rate[0], new Map())
29+
referenceRates.get(rate[0])?.set(rate[1], parseFloat(rate[2]))
30+
}
31+
});
32+
this.referenceRatesAtDate.set(date, referenceRates)
33+
}
34+
1835
@given('the following stocks are in the wallet')
1936
public setStocks(stocks: any) {
2037
for(let row of stocks.rawTable) {
@@ -29,32 +46,52 @@ export class WalletValueSteps {
2946

3047
@when('I ask to compute it\'s value in {string} from the rates of {string}')
3148
public async computeRatesOneDate(currency: Currencies, date: string) {
49+
this.dates = []
50+
this.dates.push(date)
51+
this.answers = []
3252
this.answers.push(await Wallet.computeValue(this.wallet, currency, date))
53+
this.referenceRateWalletValue = this.computeWalletValueLocal(currency, date)
3354
}
3455

3556
@when('I ask to compute it\'s value in {string} from the rates of {string} and then from the rates of {string}')
3657
async computeRatesTwoDates(currency: Currencies, date1: string, date2: string) {
58+
this.dates = []
59+
this.dates.push(date1)
60+
this.dates.push(date2)
3761
this.answers = []
3862
this.answers.push(await Wallet.computeValue(this.wallet, currency, date1))
3963
this.answers.push(await Wallet.computeValue(this.wallet, currency, date2))
4064
}
4165

4266
@then('the wallet value in the given currency should be {string}')
4367
public compareAnswer(expectedAnswer: string) {
44-
assert.strictEqual(this.answers.shift(), parseFloat(expectedAnswer));
45-
this.answers = []
68+
const actual = this.answers.shift() || 0
69+
const expected = parseFloat(expectedAnswer)
70+
71+
//test actual with expected answer
72+
assert.strictEqual(actual, expected);
73+
//test actual with computesd value with rates defiend locally in scébario
74+
assert.strictEqual(actual, this.referenceRateWalletValue);
4675
}
4776

4877
@then('the two values calculated should be different')
4978
compareTwoDifferentAnswers() {
5079
assert.notStrictEqual(this.answers[0],this.answers[1])
51-
this.answers = []
5280
}
5381

5482
@then('the two values calculated should be identical')
5583
public compareTwoSameAnswers() {
5684
assert.strictEqual(this.answers.shift(),this.answers.shift())
57-
this.answers = []
85+
}
86+
87+
public computeWalletValueLocal(baseCurrency: Currencies, date: string): number {
88+
let result = 0
89+
for(let stock of this.wallet.stocks) {
90+
let multiplier = this.referenceRatesAtDate?.get(date)?.get(stock.currency)?.get(baseCurrency) ? this.referenceRatesAtDate?.get(date)?.get(stock.currency)?.get(baseCurrency) : 0
91+
if(multiplier)
92+
result += parseFloat((multiplier * stock.count).toFixed(2))
93+
}
94+
return parseFloat(result.toFixed(2))
5895
}
5996

6097
}
Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
Feature: You want to compute the value of your wallet at set dates
22

33
Background: Wallet
4-
Given I have a wallet
4+
Given I have a wallet
5+
And the rates at the time of "2021-01-15" are
6+
| GBP | USD | 1.37 |
7+
| GBP | EUR | 1.12 |
8+
| GBP | GBP | 1 |
9+
| EUR | USD | 1.22 |
10+
| EUR | GBP | 0.89 |
11+
| EUR | EUR | 1 |
12+
| USD | EUR | 0.83 |
13+
| USD | GBP | 0.74 |
14+
| USD | USD | 1 |
15+
And the rates at the time of "2018-03-25" are
16+
| GBP | USD | 1.36 |
17+
| GBP | EUR | 1.12 |
18+
| GBP | GBP | 1.12 |
19+
| EUR | USD | 1.21 |
20+
| EUR | GBP | 0.89 |
21+
| EUR | EUR | 0.89 |
22+
| USD | EUR | 0.82 |
23+
| USD | GBP | 0.73 |
24+
| USD | USD | 0.73 |
525

626
Scenario: Two identical wallet have their values computed at the same date:
727
Given the following stocks are in the wallet
8-
| RUB | 3456 |
9-
| USD | 34.01 |
10-
| EUR | 4.32 |
11-
| GBP | 1.32 |
28+
| GBP | 10 |
29+
| USD | 10 |
30+
| EUR | 5 |
1231
When I ask to compute it's value in "EUR" from the rates of "2021-01-15" and then from the rates of "2021-01-15"
1332
Then the two values calculated should be identical
1433

1534
Scenario: The wallet's value is computed at two different dates:
1635
Given the following stocks are in the wallet
17-
| RUB | 3456 |
18-
| USD | 34.01 |
19-
| EUR | 4.32 |
20-
| GBP | 1.32 |
36+
| RUB | 10 |
37+
| USD | 10 |
38+
| EUR | 5 |
2139
When I ask to compute it's value in "EUR" from the rates of "2021-01-15" and then from the rates of "2018-03-25"
2240
Then the two values calculated should be different

features/wallet-value-emtpy.feature

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ Feature: You want to compute the value of your empty wallet
33

44
Background: Wallet
55
Given I have a wallet
6+
And the rates at the time of "2021-01-15" are
7+
| GBP | USD | 1.37 |
8+
| GBP | EUR | 1.12 |
9+
| GBP | GBP | 1 |
10+
| EUR | USD | 1.22 |
11+
| EUR | GBP | 0.89 |
12+
| EUR | EUR | 1 |
13+
| USD | EUR | 0.83 |
14+
| USD | GBP | 0.74 |
15+
| USD | USD | 1 |
616

717
Scenario: The wallet has no stocks
818
Given no stocks are in the wallet
@@ -12,8 +22,7 @@ Feature: You want to compute the value of your empty wallet
1222
Scenario: The wallet stocks are all at 0
1323
Given the following stocks are in the wallet
1424
| USD | 0 |
15-
| RUB | 0 |
16-
| EUR | 0 |
1725
| GBP | 0 |
26+
| EUR | 0 |
1827
When I ask to compute it's value in "EUR" from the rates of "2021-01-15"
1928
Then the wallet value in the given currency should be "0"

features/wallet-value-normal.feature

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,33 @@ Feature: You want to compute the value of your normal wallet
33

44
Background: Wallet
55
Given I have a wallet
6+
And the rates at the time of "2021-01-15" are
7+
| GBP | USD | 1.37 |
8+
| GBP | EUR | 1.12 |
9+
| GBP | GBP | 1 |
10+
| EUR | USD | 1.22 |
11+
| EUR | GBP | 0.89 |
12+
| EUR | EUR | 1 |
13+
| USD | EUR | 0.83 |
14+
| USD | GBP | 0.74 |
15+
| USD | USD | 1 |
616

717
Scenario: The wallet contains different currencies:
818
Given the following stocks are in the wallet
9-
| RUB | 1234 |
10-
| USD | 5.4 |
11-
| EUR | 4.32 |
12-
| GBP | 2.32 |
19+
| GBP | 10 |
20+
| USD | 10 |
21+
| EUR | 5 |
1322
When I ask to compute it's value in "EUR" from the rates of "2021-01-15"
14-
Then the wallet value in the given currency should be "25.24"
23+
Then the wallet value in the given currency should be "24.5"
1524

1625
Scenario: The wallet contains only the same currency it wants its total value to be in
1726
Given the following stocks are in the wallet
18-
| EUR | 4.32 |
27+
| EUR | 1 |
1928
When I ask to compute it's value in "EUR" from the rates of "2021-01-15"
20-
Then the wallet value in the given currency should be "4.32"
29+
Then the wallet value in the given currency should be "1"
2130

22-
Scenario: The wallet only has the same currency it wants its total value
31+
Scenario: The wallet only has the same currency it wants its total value in
2332
Given the following stocks are in the wallet
24-
| USD | 4.32 |
25-
When I ask to compute it's value in "GBP" from the rates of "2021-01-15"
26-
Then the wallet value in the given currency should be "3.56"
33+
| USD | 1 |
34+
When I ask to compute it's value in "EUR" from the rates of "2021-01-15"
35+
Then the wallet value in the given currency should be "0.83"

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Behavior Driven Developement school project to use cucumber as a testing plateform",
55
"main": "./build/index.js",
66
"scripts": {
7-
"build": "rimraf ./build && tsc",
7+
"build": "rimraf ./build && tsc && npm run clean-build",
8+
"clean-build": "rimraf ./build/features && mv ./build/src/* ./build && rmdir ./build/src",
89
"test:cucumber": "cucumber-js -p default",
910
"test:unit": "rimraf ./build && tsc && mocha ./test/test.js"
1011
},

src/wallet.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from "axios";
2+
import { parse } from "url";
23
import { Currencies } from "./enums/currencies.enum";
34
import { Stock } from "./stock"
45

@@ -16,14 +17,18 @@ export class Wallet {
1617
let total: number = 0
1718
const searchCurrencies: string = wallet.stocks.filter(stock => stock.currency !== returnValueCurrency).map(stock => stock.currency).join(',')
1819
try {
19-
const response = await axios.get(`${exchangeRateApiUrl}?symbols=${searchCurrencies}`)
20-
const exchangeRatesRatios = response.data.rates
20+
const response = await axios.get(`${exchangeRateApiUrl}?base=${returnValueCurrency}&symbols=${searchCurrencies}`)
21+
const rawExchangeRatesRatios = response.data.rates
22+
const exchangeRatesRatios = new Map<string, number>(Object.entries(rawExchangeRatesRatios));
23+
for (let item of exchangeRatesRatios) {
24+
exchangeRatesRatios.set(item[0], parseFloat(item[1].toFixed(2)));
25+
}
2126
for(let stock of wallet.stocks){
22-
total += stock.count / (stock.currency === returnValueCurrency ? 1 : exchangeRatesRatios[stock.currency])
27+
total += parseFloat((stock.count / (stock.currency === returnValueCurrency ? 1 : exchangeRatesRatios.get(stock.currency) || 0)).toFixed(2))
2328
}
2429
return parseFloat(total.toFixed(2))
2530
} catch (error) {
26-
if(error.response.data.error)
31+
if(error.response && error.response.data && error.response.data.error)
2732
throw new Error(error.response.data.error);
2833
else
2934
throw error

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// "sourceMap": true, /* Generates corresponding '.map' file. */
1414
// "outFile": "./", /* Concatenate and emit output to single file. */
1515
"outDir": "build", /* Redirect output structure to the directory. */
16-
"rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
16+
"rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
1717
// "composite": true, /* Enable project compilation */
1818
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
1919
// "removeComments": true, /* Do not emit comments to output. */
@@ -66,6 +66,6 @@
6666
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
6767
},
6868
"include": [
69-
"src/**/*"
69+
"**/*.ts"
7070
]
7171
}

0 commit comments

Comments
 (0)