-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix account test case to delete ledger after all
- Loading branch information
Showing
9 changed files
with
272 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import theRouter from './accountRouter'; | ||
import * as supertest from 'supertest'; | ||
import * as express from "express"; | ||
import { getOrmManager } from '../../db/ormManager'; | ||
import { User } from '../../entity/User'; | ||
import { signAccessToken } from '../auth/provider'; | ||
import { Ledger } from '../../entity/Ledger'; | ||
|
||
const app = express(); | ||
app.use('/', theRouter); | ||
|
||
const entityData = { | ||
name: 'test-account', | ||
description: 'haha optional', | ||
amount: 100, | ||
ledgerID: null, | ||
}; | ||
|
||
|
||
const ledger2Data = { | ||
name: 'ledger2haha2', | ||
userId: null, | ||
}; | ||
|
||
describe("routes", () => { | ||
let token; | ||
let user; | ||
let ledger; | ||
let entity; | ||
let updatedEntityData; | ||
|
||
beforeAll(async () => { | ||
const UserRepo = User.getRepo(); | ||
const users = await UserRepo.find(); | ||
user = users[0]; | ||
token = signAccessToken(users[0].id, user.email); | ||
|
||
const ledgerRepo = Ledger.getRepo(); | ||
ledger = await ledgerRepo.create(ledger2Data); | ||
ledger.user=user; | ||
await ledgerRepo.save(ledger); | ||
}); | ||
|
||
afterAll(async () => { | ||
await getOrmManager().query( | ||
` | ||
DELETE FROM LEDGER where name = '${ledger.name}'; | ||
DELETE FROM ACCOUNT; | ||
` | ||
); | ||
}); | ||
|
||
it('get all before create, should be empty', async () => { | ||
const resp = await supertest(app).get('/'+ledger.id) | ||
.set({'Authorization': token} | ||
); | ||
expect(resp.status).toEqual(204); | ||
expect(!resp.body?.length); | ||
}); | ||
|
||
// it('create one', async () => { | ||
// const resp = await supertest(app).post('/'+ledger.id) | ||
// .send(entityData) | ||
// .set({'Authorization': token} | ||
// ); | ||
// expect(resp.status).toEqual(200); | ||
// expect(resp.body.name).toEqual(entityData.name); | ||
// expect(resp.body.ledgerId).toEqual(+ledger.id); | ||
// entity = resp.body; | ||
// }); | ||
// | ||
// it('get one', async () => { | ||
// const resp = await supertest(app).get(`/${ledger.id}/${entity.id}`) | ||
// .set({'Authorization': token} | ||
// ); | ||
// expect(resp.status).toEqual(200); | ||
// expect(resp.body.name).toEqual(entityData.name); | ||
// }); | ||
// | ||
// it('get all should be one', async () => { | ||
// const resp = await supertest(app).get(`/${ledger.id}`) | ||
// .set({'Authorization': token} | ||
// ); | ||
// expect(resp.status).toEqual(200); | ||
// expect(resp.body.length).toEqual(1); | ||
// }); | ||
// | ||
// it('shout not get not existed one', async () => { | ||
// const resp = await supertest(app).get(`/${ledger.id}/${entity.id+1}`) | ||
// .set({'Authorization': token} | ||
// ); | ||
// expect(resp.status).toEqual(204); | ||
// }); | ||
// | ||
// it('update one', async () => { | ||
// const tmpEntity = {...entityData}; | ||
// updatedEntityData = { | ||
// ...tmpEntity, | ||
// name: 'test-entity-1', | ||
// }; | ||
// const resp = await supertest(app).put(`/${ledger.id}/${entity.id}`) | ||
// .send(updatedEntityData) | ||
// .set({'Authorization': token} | ||
// ); | ||
// expect(resp.status).toEqual(200); | ||
// }); | ||
// | ||
// it('get updated one', async () => { | ||
// const resp = await supertest(app).get(`/${ledger.id}/${entity.id}`) | ||
// .set({'Authorization': token} | ||
// ); | ||
// expect(resp.status).toEqual(200); | ||
// expect(resp.body.name).toEqual(updatedEntityData.name); | ||
// }); | ||
// | ||
// it('should not update not existed one', async () => { | ||
// updatedEntityData = { | ||
// ...entity, | ||
// name: 'test-entity-1', | ||
// }; | ||
// const resp = await supertest(app).put(`/${ledger.id}/${entity.id+1}`) | ||
// .send(updatedEntityData) | ||
// .set({'Authorization': token} | ||
// ); | ||
// expect(resp.status).toEqual(400); | ||
// }); | ||
// | ||
// it('delete the one', async () => { | ||
// const resp = await supertest(app).delete(`/${ledger.id}/${entity.id}`) | ||
// .set({'Authorization': token} | ||
// ); | ||
// expect(resp.status).toEqual(200); | ||
// }); | ||
// | ||
// it('should not delete un-exited one', async () => { | ||
// const resp = await supertest(app).delete(`/${ledger.id}/${entity.id+1}`) | ||
// .set({'Authorization': token} | ||
// ); | ||
// expect(resp.status).toEqual(400); | ||
// }); | ||
// | ||
// it('should not get after delete', async () => { | ||
// const resp = await supertest(app).get(`/${ledger.id}/${entity.id}`) | ||
// .set({'Authorization': token} | ||
// ); | ||
// expect(resp.status).toEqual(204); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as express from 'express'; | ||
import * as services from './accountService'; | ||
import { handleBodyRequestParsing } from '../../middleware/common'; | ||
import { authenticateJWT } from '../../middleware/jwtAuthMiddleWare'; | ||
import { getLedger } from '../../middleware/getLedgerMiddleware'; | ||
|
||
const router = express.Router(); | ||
|
||
handleBodyRequestParsing(router); | ||
router.use(authenticateJWT); | ||
|
||
|
||
router.post('/:ledgerID', getLedger, services.createOne); | ||
router.get('/:ledgerID', getLedger, services.getAll); | ||
router.put('/:ledgerID/:entityID', getLedger, services.updateOne); | ||
router.get('/:ledgerID/:entityID', getLedger, services.getOne); | ||
router.delete('/:ledgerID/:entityID', getLedger, services.deleteOne); | ||
|
||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Response } from 'express'; | ||
import { Account as Entity } from '../../entity/Account'; | ||
|
||
|
||
const getFindOption = (ledgerId) => ( | ||
{ | ||
where: { | ||
ledgerId: ledgerId | ||
} | ||
} | ||
); | ||
|
||
export const getAll = async (req: any, res: Response) => { | ||
const entityRepo = Entity.getRepo(); | ||
try { | ||
const items = await entityRepo.find( | ||
getFindOption(req.ledgerID) | ||
); | ||
if (items?.length) { | ||
res.status(200).send(items); | ||
} else { | ||
res.status(204).send([]); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).end(); | ||
} | ||
}; | ||
|
||
|
||
export const getOne = async (req: any, res: Response) => { | ||
const entityRepo = Entity.getRepo(); | ||
const ledgerID = req.ledgerID; | ||
try { | ||
const item = await entityRepo.findOne(ledgerID, getFindOption(ledgerID)); | ||
if (item) { | ||
res.status(200).send(item); | ||
} else { | ||
res.status(204).send({}); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).end(); | ||
} | ||
}; | ||
|
||
export const createOne = async (req, res: Response) => { | ||
const entityRepo = Entity.getRepo(); | ||
try { | ||
const item = await entityRepo.create(req.body) as unknown as Entity; | ||
item.ledgerId = req.ledgerID; | ||
await entityRepo.save(item); | ||
res.status(200).send(item); | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).end(); | ||
} | ||
}; | ||
|
||
export const updateOne = async (req, res: Response) => { | ||
const ledgerID = req.params?.ledgerID; | ||
const entityRepo = Entity.getRepo(); | ||
try { | ||
const item = await entityRepo.findOne(ledgerID, getFindOption(ledgerID)); | ||
if (item) { | ||
const r = await entityRepo.update(item.id, req.body); | ||
res.status(200).send(r); | ||
} | ||
res.status(400).end(); | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).end(); | ||
} | ||
}; | ||
|
||
export const deleteOne = async (req, res: Response) => { | ||
const ledgerID = req.params?.ledgerID; | ||
const entityRepo = Entity.getRepo(); | ||
try { | ||
const item = await entityRepo.findOne(ledgerID, getFindOption(req.ledgerID)); | ||
if (item) { | ||
await entityRepo.softDelete(item.id); | ||
res.status(200).json({}); | ||
} else { | ||
res.status(400).end(); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).end(); | ||
} | ||
}; |