Skip to content

Commit 07b21e4

Browse files
committed
adds tests
1 parent d3614c8 commit 07b21e4

File tree

5 files changed

+180
-22
lines changed

5 files changed

+180
-22
lines changed

.github/workflows/classroom.yml

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,46 @@ jobs:
1313
steps:
1414
- name: Checkout code
1515
uses: actions/checkout@v4
16-
- name: Install packages
16+
- name: Install pnpm
1717
run: npm install -g pnpm
18-
- name: Test - 2 points
18+
- name: Install dependencies
19+
run: pnpm i
20+
- name: Run Test - 2 points
1921
id: test-2-points
2022
uses: classroom-resources/autograding-command-grader@v1
2123
with:
2224
test-name: Test - 2 points
23-
setup-command: pnpm i
24-
command: pnpm test-run-first
25+
command: pnpm test-run-basic
2526
timeout: 1
2627
max-score: 2
27-
- name: Test - 7 points
28+
- name: Run Test - 7 points
2829
id: test-7-points
2930
uses: classroom-resources/autograding-command-grader@v1
3031
with:
3132
test-name: Test - 7 points
32-
setup-command: pnpm i
33-
command: pnpm test-run-second
33+
command: pnpm test-run-real
3434
timeout: 1
3535
max-score: 7
36+
- name: Check TESTS_FOLDER
37+
env:
38+
TESTS_FOLDER: ${{ secrets.TESTS_FOLDER }}
39+
if: ${{ env.TESTS_FOLDER != '' }}
40+
run: echo "TESTS_FOLDER is set ${{ env.TESTS_FOLDER }}"
41+
- name: Run Test - Final 24 points
42+
id: test-24-points
43+
uses: classroom-resources/autograding-command-grader@v1
44+
env:
45+
TESTS_FOLDER: "${{ secrets.TESTS_FOLDER }}"
46+
with:
47+
test-name: Test - 24 points
48+
command: TESTS_FOLDER=${{ secrets.TESTS_FOLDER }} pnpm test-run-final
49+
timeout: 1
50+
max-score: 24
3651
- name: Autograding Reporter
3752
uses: classroom-resources/autograding-grading-reporter@v1
3853
env:
39-
TEST-2-POINTS_RESULTS: "${{steps.test-2-points.outputs.result}}"
40-
TEST-7-POINTS_RESULTS: "${{steps.test-7-points.outputs.result}}"
54+
TEST-2-POINTS_RESULTS: "${{ steps.test-2-points.outputs.result }}"
55+
TEST-7-POINTS_RESULTS: "${{ steps.test-7-points.outputs.result }}"
56+
TEST-24-POINTS_RESULTS: "${{ steps.test-24-points.outputs.result }}"
4157
with:
42-
runners: test-2-points,test-7-points
58+
runners: test-2-points,test-7-points,test-24-points

.github/workflows/use-pnpm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ jobs:
1616
- name: Install dependencies
1717
run: pnpm install --frozen-lockfile
1818
- name: Run tests
19-
run: pnpm test-run-first
19+
run: pnpm test-run-basic
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import User from '@/models/user';
2+
import Bank from '@/models/bank';
3+
import { BankAccountId, UserId } from '@/types/Common';
4+
import GlobalRegistry from '@/services/GlobalRegistry';
5+
6+
export interface TestFixtures {
7+
alice: User;
8+
aliceUserId: UserId;
9+
bob: User;
10+
bobUserId: UserId;
11+
bank: Bank;
12+
bankAllowsNegative: Bank;
13+
aliceAccountId: BankAccountId;
14+
aliceAccountAllowsNegativeId: BankAccountId;
15+
bobAccountId: BankAccountId;
16+
}
17+
18+
export class TestFactory {
19+
static createFixtures(): TestFixtures {
20+
GlobalRegistry.clear();
21+
22+
const bank = Bank.create();
23+
const bankAllowsNegative = Bank.create({ isNegativeAllowed: true });
24+
25+
const aliceAccount = bank.createAccount(1000);
26+
const aliceAccountAllowsNegative = bankAllowsNegative.createAccount(200);
27+
const bobAccount = bank.createAccount(500);
28+
29+
const alice = User.create('Alice', [aliceAccount.getId(), aliceAccountAllowsNegative.getId()]);
30+
const bob = User.create('Bob', [bobAccount.getId()]);
31+
32+
return {
33+
alice,
34+
aliceUserId: alice.getId(),
35+
bob,
36+
bobUserId: bob.getId(),
37+
bank,
38+
bankAllowsNegative,
39+
aliceAccountId: aliceAccount.getId(),
40+
aliceAccountAllowsNegativeId: aliceAccountAllowsNegative.getId(),
41+
bobAccountId: bobAccount.getId()
42+
};
43+
}
44+
45+
static createBank(options?: { isNegativeAllowed?: boolean }): Bank {
46+
return Bank.create(options);
47+
}
48+
49+
static createUser(name: string, accountIds: BankAccountId[]): User {
50+
return User.create(name, accountIds);
51+
}
52+
}

src/__tests__/real.test.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,53 @@
1-
import { describe, it, expect } from 'vitest';
2-
import isEqualChecker from '@/index';
1+
import { describe, beforeEach, it, expect } from 'vitest';
2+
import { TestFactory } from './helpers/TestFactory';
3+
import type { TestFixtures } from './helpers/TestFactory';
34

4-
describe('verify answers', () => {
5-
it('should equate same objects', () => {
6-
expect(isEqualChecker({ b: 2, a: 1 }, { a: 1, b: 2 })).toBe(true);
5+
describe('Bank Transfer Tests', () => {
6+
let fixtures: TestFixtures;
7+
8+
beforeEach(() => {
9+
fixtures = TestFactory.createFixtures();
10+
});
11+
12+
it('should allow transfer between accounts', () => {
13+
const { bank, aliceUserId, bobUserId, aliceAccountId, bobAccountId } = fixtures;
14+
15+
// Initial balances
16+
const aliceAccount = bank.getAccount(aliceAccountId);
17+
const bobAccount = bank.getAccount(bobAccountId);
18+
expect(aliceAccount.getBalance()).toBe(1000);
19+
expect(bobAccount.getBalance()).toBe(500);
20+
21+
// Perform transfer
22+
bank.send(aliceUserId, bobUserId, 300);
23+
24+
// Check final balances
25+
expect(aliceAccount.getBalance()).toBe(700);
26+
expect(bobAccount.getBalance()).toBe(800);
27+
});
28+
29+
30+
it('should not allow transfer with insufficient funds', () => {
31+
const { bank, aliceUserId, bobUserId } = fixtures;
32+
33+
expect(() => {
34+
bank.send(aliceUserId, bobUserId, 2000);
35+
}).toThrow('Insufficient funds');
736
});
837

9-
it('should not equate different objects', () => {
10-
expect(isEqualChecker({ a: 1, c: 2 }, { a: 1, b: 2 })).toBe(false);
38+
it('should allow transfer with negative balance', () => {
39+
const { bank, bankAllowsNegative, aliceUserId, bobUserId, aliceAccountAllowsNegativeId, bobAccountId } = fixtures;
40+
41+
const aliceAccountAllowsNegative = bankAllowsNegative.getAccount(aliceAccountAllowsNegativeId);
42+
const bobBankId = bank.getId();
43+
const bobAccount = bank.getAccount(bobAccountId);
44+
45+
expect(aliceAccountAllowsNegative.getBalance()).toBe(200);
46+
expect(bobAccount.getBalance()).toBe(500);
47+
48+
bankAllowsNegative.send(aliceUserId, bobUserId, 500, bobBankId);
49+
50+
expect(aliceAccountAllowsNegative.getBalance()).toBe(-300);
51+
expect(bobAccount.getBalance()).toBe(1000);
1152
});
1253
});

src/__tests__/simple.test.ts

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,57 @@
11
import { describe, it, expect } from 'vitest';
2-
import isEqual from '@/is-equal';
2+
import User from '@/models/user';
3+
import Bank from '@/models/bank';
4+
import BankAccount from '@/models/bank-account';
5+
import TransactionService from '@/services/TransactionService';
36

4-
describe('existence of function', () => {
5-
it('should exist', () => {
6-
expect(isEqual).toBeDefined();
7+
8+
describe('Banks', () => {
9+
it('has a bank class', () => {
10+
expect(Bank).toBeDefined();
11+
});
12+
13+
it('can create a bank', () => {
14+
const bank = Bank.create();
15+
expect(bank.getId()).toBeDefined();
16+
});
17+
18+
it('can create a bank account', () => {
19+
const bank = Bank.create();
20+
const bankAccount = bank.createAccount(100);
21+
expect(bankAccount.getId()).toBeDefined();
22+
});
23+
});
24+
25+
26+
describe('Users', () => {
27+
it('has a user class', () => {
28+
expect(User).toBeDefined();
29+
});
30+
31+
it('can create a user', () => {
32+
const user = User.create('Firstname Lastname', []);
33+
expect(user).toBeDefined();
34+
});
35+
36+
it('can create a user with accounts', () => {
37+
const bank = Bank.create();
38+
const bankAccount1Id = bank.createAccount(100).getId();
39+
const bankAccount2Id = bank.createAccount(200).getId();
40+
const bankAccount3Id = bank.createAccount(300).getId();
41+
42+
const user = User.create('Firstname Lastname', [bankAccount1Id, bankAccount2Id, bankAccount3Id]);
43+
expect(user).toBeDefined();
44+
});
45+
});
46+
47+
describe('Accounts', () => {
48+
it('has an account class', () => {
49+
expect(BankAccount).toBeDefined();
50+
});
51+
});
52+
53+
describe('Transactions', () => {
54+
it('has a transaction service', () => {
55+
expect(TransactionService).toBeDefined();
756
});
857
});

0 commit comments

Comments
 (0)