Skip to content

Commit 9aa50a0

Browse files
unit-test
1 parent 14b7e29 commit 9aa50a0

File tree

13 files changed

+117
-115
lines changed

13 files changed

+117
-115
lines changed

17-nodejs/02-egg/.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"javascriptreact",
55
{ "language": "typescript", "autoFix": true },
66
{ "language": "typescriptreact", "autoFix": true },
7-
]
7+
],
8+
"mocha.subdirectory": "test"
89
}

17-nodejs/02-egg/LICENSE

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
MIT LICENSE
22

3-
Copyright (c) 2015-present Ant UED, https://xtech.antfin.com/
4-
53
Permission is hereby granted, free of charge, to any person obtaining
64
a copy of this software and associated documentation files (the
75
"Software"), to deal in the Software without restriction, including

17-nodejs/02-egg/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ $ redis-server
4444
4. husky
4545
5. lint-staged
4646
6. prettier
47+
7. password organization
4748

4849
## Docs
4950
**validate**
@@ -58,3 +59,6 @@ https://mochajs.cn/
5859

5960
https://sequelize.org/master/manual/query-interface.html
6061
https://sequelize.org/master/variable/index.html#static-variable-DataTypes
62+
63+
**factory-girl**
64+
https://www.npmjs.com/package/factory-girl

17-nodejs/02-egg/app/model/post.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Application } from 'egg';
2-
export default function(app: Application) {
1+
// import { Application } from 'egg';
2+
// export default function(app: Application) {
3+
export default function(app) {
34
const { STRING, INTEGER } = app.Sequelize;
45

56
const Post = app.model.define('posts', {

17-nodejs/02-egg/app/model/user.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Application } from 'egg';
2-
export default function(app: Application) {
1+
// import { Application } from 'egg';
2+
// export default function(app: Application) {
3+
export default function(app) {
34
const { STRING, BIGINT, INTEGER } = app.Sequelize;
45

56
const User = app.model.define('users', {

17-nodejs/02-egg/package-lock.json

Lines changed: 49 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

17-nodejs/02-egg/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"stop": "egg-scripts stop --title=egg-server-demo",
1313
"dev": "npx sequelize db:migrate && egg-bin dev --port 2048",
1414
"debug": "egg-bin debug",
15-
"test-local": "egg-bin test",
16-
"test": "NODE_ENV=test npm run sequelize -- db:migrate && npm run lint -- --fix && npm run test-local",
15+
"test": "NODE_ENV=test npm run sequelize -- db:migrate && npm run lint -- --fix && npm run test:local",
16+
"test:local": "egg-bin test",
1717
"cov": "egg-bin cov",
1818
"tsc": "ets && tsc -p tsconfig.json",
1919
"ci": "npm run lint && NODE_ENV=test npx sequelize db:migrate && npm run cov && npm run tsc",
@@ -32,6 +32,7 @@
3232
"seed:down:all": "npx sequelize db:seed:undo:all"
3333
},
3434
"dependencies": {
35+
"chai": "^4.2.0",
3536
"egg": "^2.6.1",
3637
"egg-redis": "^2.4.0",
3738
"egg-scripts": "^2.6.0",

17-nodejs/02-egg/test/app/controller/post.test.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,54 @@
11
import { app } from 'egg-mock/bootstrap';
22
import assert = require('assert');
3+
// import { expect } from 'chai';
34

45
describe('test/app/service/post.test.js', () => {
5-
describe('GET /posts', () => {
6+
describe('GET /api/posts', () => {
67
it('should work', async () => {
78
await app.factory.createMany('post', 3);
8-
const res = await app.httpRequest().get('/posts?limit=2');
9+
const res = await app.httpRequest().get('/api/posts?limit=2');
910
assert(res.status === 200);
10-
assert(res.body.count === 3);
11-
assert(res.body.rows.length === 2);
12-
assert(res.body.rows[0].title);
13-
assert(!res.body.rows[0].content);
11+
assert(res.body.data.count === 3);
12+
assert(res.body.data.rows.length === 2);
13+
assert(res.body.data.rows[0].title);
14+
assert(!res.body.data.rows[0].content);
1415
});
1516
});
1617

17-
describe('GET /posts/:id', () => {
18+
describe('GET /api/posts/:id', () => {
1819
it('should work', async () => {
1920
const post = await app.factory.create('post');
20-
const res = await app.httpRequest().get(`/posts/${post.id}`);
21+
const res = await app.httpRequest().get(`/api/posts/${post.id}`);
2122
assert(res.status === 200);
22-
assert(res.body.title === post.title);
23-
assert(res.body.content === post.content);
23+
assert(res.body.data.title === post.title);
24+
assert(res.body.data.content === post.content);
2425
});
2526
});
2627

27-
describe('POST /posts', () => {
28+
describe('POST /api/posts', () => {
2829
it('should work', async () => {
2930
app.mockCsrf();
30-
let res = await app.httpRequest().post('/posts')
31+
let res = await app.httpRequest().post('/api/posts')
3132
.send({
3233
title: 'title',
3334
content: 'content',
3435
user_id: 1,
3536
});
3637
assert(res.status === 201);
37-
assert(res.body.id);
38+
assert(res.body.data.id);
3839

39-
res = await app.httpRequest().get(`/posts/${res.body.id}`);
40+
res = await app.httpRequest().get(`/api/posts/${res.body.data.id}`);
4041
assert(res.status === 200);
41-
assert(res.body.title === 'title');
42+
assert(res.body.data.title === 'title');
4243
});
4344
});
4445

45-
describe('DELETE /posts/:id', () => {
46+
describe('DELETE /api/posts/:id', () => {
4647
it('should work', async () => {
4748
const post = await app.factory.create('post');
4849

4950
app.mockCsrf();
50-
const res = await app.httpRequest().delete(`/posts/${post.id}`)
51+
const res = await app.httpRequest().delete(`/api/posts/${post.id}`)
5152
.send({ user_id: post.user_id });
5253
assert(res.status === 200);
5354
});

17-nodejs/02-egg/test/app/controller/user.test.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,50 @@ import { app } from 'egg-mock/bootstrap';
22
import assert = require('assert');
33

44
describe('test/app/service/user.test.js', () => {
5-
describe('GET /users', () => {
5+
describe('GET /api/users', () => {
66
it('should work', async () => {
77
await app.factory.createMany('user', 3);
8-
const res = await app.httpRequest().get('/users?limit=2');
8+
const res = await app.httpRequest().get('/api/users?limit=2');
99
assert(res.status === 200);
10-
assert(res.body.count === 3);
11-
assert(res.body.rows.length === 2);
12-
assert(res.body.rows[0].name);
13-
assert(res.body.rows[0].age);
10+
assert(res.body.data.count === 3);
11+
assert(res.body.data.rows.length === 2);
12+
assert(res.body.data.rows[0].name);
13+
assert(res.body.data.rows[0].age);
1414
});
1515
});
1616

17-
describe('GET /users/:id', () => {
17+
describe('GET /api/users/:id', () => {
1818
it('should work', async () => {
1919
const user = await app.factory.create('user');
20-
const res = await app.httpRequest().get(`/users/${user.id}`);
20+
const res = await app.httpRequest().get(`/api/users/${user.id}`);
2121
assert(res.status === 200);
22-
assert(res.body.age === user.age);
22+
assert(res.body.data.age === user.age);
2323
});
2424
});
2525

26-
describe('POST /users', () => {
26+
describe('POST /api/users', () => {
2727
it('should work', async () => {
2828
app.mockCsrf();
29-
let res = await app.httpRequest().post('/users')
29+
let res = await app.httpRequest().post('/api/users')
3030
.send({
3131
age: 10,
3232
name: 'name',
33+
gender: 'male'
3334
});
3435
assert(res.status === 201);
35-
assert(res.body.id);
36+
assert(res.body.data.id);
3637

37-
res = await app.httpRequest().get(`/users/${res.body.id}`);
38+
res = await app.httpRequest().get(`/api/users/${res.body.data.id}`);
3839
assert(res.status === 200);
39-
assert(res.body.name === 'name');
40+
assert(res.body.data.name === 'name');
4041
});
4142
});
4243

43-
describe('DELETE /users/:id', () => {
44+
describe('DELETE /api/users/:id', () => {
4445
it('should work', async () => {
4546
const user = await app.factory.create('user');
46-
4747
app.mockCsrf();
48-
const res = await app.httpRequest().delete(`/users/${user.id}`);
48+
const res = await app.httpRequest().delete(`/api/users/${user.id}`);
4949
assert(res.status === 200);
5050
});
5151
});

17-nodejs/02-egg/test/app/service/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)