Skip to content

Commit 7c27d49

Browse files
authored
Update tests bank-account (#2745)
* Update tests bank-account * Run format.mjs
1 parent 99b947c commit 7c27d49

File tree

3 files changed

+127
-14
lines changed

3 files changed

+127
-14
lines changed

exercises/practice/bank-account/.meta/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"TomPradat"
44
],
55
"contributors": [
6+
"jagdish-15",
67
"SleeplessByte"
78
],
89
"files": {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[983a1528-4ceb-45e5-8257-8ce01aceb5ed]
13+
description = "Newly opened account has zero balance"
14+
15+
[e88d4ec3-c6bf-4752-8e59-5046c44e3ba7]
16+
description = "Single deposit"
17+
18+
[3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d]
19+
description = "Multiple deposits"
20+
21+
[08f1af07-27ae-4b38-aa19-770bde558064]
22+
description = "Withdraw once"
23+
24+
[6f6d242f-8c31-4ac6-8995-a90d42cad59f]
25+
description = "Withdraw twice"
26+
27+
[45161c94-a094-4c77-9cec-998b70429bda]
28+
description = "Can do multiple operations sequentially"
29+
30+
[f9facfaa-d824-486e-8381-48832c4bbffd]
31+
description = "Cannot check balance of closed account"
32+
33+
[7a65ba52-e35c-4fd2-8159-bda2bde6e59c]
34+
description = "Cannot deposit into closed account"
35+
36+
[a0a1835d-faae-4ad4-a6f3-1fcc2121380b]
37+
description = "Cannot deposit into unopened account"
38+
39+
[570dfaa5-0532-4c1f-a7d3-0f65c3265608]
40+
description = "Cannot withdraw from closed account"
41+
42+
[c396d233-1c49-4272-98dc-7f502dbb9470]
43+
description = "Cannot close an account that was not opened"
44+
45+
[c06f534f-bdc2-4a02-a388-1063400684de]
46+
description = "Cannot open an already opened account"
47+
48+
[0722d404-6116-4f92-ba3b-da7f88f1669c]
49+
description = "Reopened account does not retain balance"
50+
51+
[ec42245f-9361-4341-8231-a22e8d19c52f]
52+
description = "Cannot withdraw more than deposited"
53+
54+
[4f381ef8-10ef-4507-8e1d-0631ecc8ee72]
55+
description = "Cannot withdraw negative"
56+
57+
[d45df9ea-1db0-47f3-b18c-d365db49d938]
58+
description = "Cannot deposit negative"
59+
60+
[ba0c1e0b-0f00-416f-8097-a7dfc97871ff]
61+
description = "Can handle concurrent transactions"

exercises/practice/bank-account/bank-account.spec.js

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ describe('Bank Account', () => {
88
expect(account.balance).toEqual(0);
99
});
1010

11-
xtest('can deposit money', () => {
11+
xtest('Single deposit', () => {
1212
const account = new BankAccount();
1313
account.open();
1414
account.deposit(100);
1515
expect(account.balance).toEqual(100);
1616
});
1717

18-
xtest('can deposit money sequentially', () => {
18+
xtest('Multiple deposits"', () => {
1919
const account = new BankAccount();
2020
account.open();
2121
account.deposit(100);
2222
account.deposit(50);
2323
expect(account.balance).toEqual(150);
2424
});
2525

26-
xtest('can withdraw money', () => {
26+
xtest('Withdraw once', () => {
2727
const account = new BankAccount();
2828
account.open();
2929
account.deposit(100);
3030
account.withdraw(50);
3131
expect(account.balance).toEqual(50);
3232
});
3333

34-
xtest('can withdraw money sequentially', () => {
34+
xtest('Withdraw twice', () => {
3535
const account = new BankAccount();
3636
account.open();
3737
account.deposit(100);
@@ -40,14 +40,25 @@ describe('Bank Account', () => {
4040
expect(account.balance).toEqual(0);
4141
});
4242

43-
xtest('checking balance of closed account throws error', () => {
43+
xtest('Can do multiple operations sequentially', () => {
44+
const account = new BankAccount();
45+
account.open();
46+
account.deposit(100);
47+
account.deposit(110);
48+
account.withdraw(200);
49+
account.deposit(60);
50+
account.withdraw(50);
51+
expect(account.balance).toEqual(20);
52+
});
53+
54+
xtest('Cannot check balance of closed account', () => {
4455
const account = new BankAccount();
4556
account.open();
4657
account.close();
4758
expect(() => account.balance).toThrow(ValueError);
4859
});
4960

50-
xtest('deposit into closed account throws error', () => {
61+
xtest('Cannot deposit into closed account', () => {
5162
const account = new BankAccount();
5263
account.open();
5364
account.close();
@@ -56,7 +67,14 @@ describe('Bank Account', () => {
5667
}).toThrow(ValueError);
5768
});
5869

59-
xtest('withdraw from closed account throws error', () => {
70+
xtest('Cannot deposit into closed account', () => {
71+
const account = new BankAccount();
72+
expect(() => {
73+
account.deposit(50);
74+
}).toThrow(ValueError);
75+
});
76+
77+
xtest('Cannot withdraw from closed account', () => {
6078
const account = new BankAccount();
6179
account.open();
6280
account.close();
@@ -65,22 +83,22 @@ describe('Bank Account', () => {
6583
}).toThrow(ValueError);
6684
});
6785

68-
xtest('close already closed account throws error', () => {
86+
xtest('Cannot close an account that was not opened', () => {
6987
const account = new BankAccount();
7088
expect(() => {
7189
account.close();
7290
}).toThrow(ValueError);
7391
});
7492

75-
xtest('open already opened account throws error', () => {
93+
xtest('Cannot open an already opened account', () => {
7694
const account = new BankAccount();
7795
account.open();
7896
expect(() => {
7997
account.open();
8098
}).toThrow(ValueError);
8199
});
82100

83-
xtest('reopened account does not retain balance', () => {
101+
xtest('Reopened account does not retain balance', () => {
84102
const account = new BankAccount();
85103
account.open();
86104
account.deposit(50);
@@ -89,7 +107,7 @@ describe('Bank Account', () => {
89107
expect(account.balance).toEqual(0);
90108
});
91109

92-
xtest('cannot withdraw more than deposited', () => {
110+
xtest('Cannot withdraw more than deposited', () => {
93111
const account = new BankAccount();
94112
account.open();
95113
account.deposit(25);
@@ -98,7 +116,7 @@ describe('Bank Account', () => {
98116
}).toThrow(ValueError);
99117
});
100118

101-
xtest('cannot withdraw negative amount', () => {
119+
xtest('Cannot withdraw negative', () => {
102120
const account = new BankAccount();
103121
account.open();
104122
account.deposit(100);
@@ -107,15 +125,48 @@ describe('Bank Account', () => {
107125
}).toThrow(ValueError);
108126
});
109127

110-
xtest('cannot deposit negative amount', () => {
128+
xtest('Cannot deposit negative', () => {
111129
const account = new BankAccount();
112130
account.open();
113131
expect(() => {
114132
account.deposit(-50);
115133
}).toThrow(ValueError);
116134
});
117135

118-
xtest('changing balance directly throws error', () => {
136+
xtest('Can handle concurrent transactions', async () => {
137+
const account = new BankAccount();
138+
account.open();
139+
account.deposit(1000);
140+
141+
for (let i = 0; i < 10; i++) {
142+
await adjustBalanceConcurrently(account);
143+
expect(account.balance).toEqual(1000);
144+
}
145+
});
146+
147+
function adjustBalanceConcurrently(account) {
148+
const random = () => Math.floor(Math.random() * 10);
149+
150+
const tasks = Array.from(
151+
{ length: 1000 },
152+
() =>
153+
new Promise((resolve) => {
154+
try {
155+
account.deposit(5);
156+
setTimeout(() => {
157+
account.withdraw(5);
158+
resolve();
159+
}, random());
160+
} catch (e) {
161+
throw new Error(`Exception should not be thrown: ${e.message}`);
162+
}
163+
}),
164+
);
165+
166+
return Promise.all(tasks);
167+
}
168+
169+
xtest('Changing balance directly throws error', () => {
119170
const account = new BankAccount();
120171
account.open();
121172
expect(() => {

0 commit comments

Comments
 (0)