--- lib/db.js
+++ lib/db.js
... | ... | @@ -7,16 +7,20 @@ |
7 | 7 |
}) |
8 | 8 |
require('dotenv').config() |
9 | 9 |
const { Sequelize, Model, DataTypes } = require('sequelize') |
10 |
-const sequelize = new Sequelize( |
|
11 |
- process.env.DB_NAME || 'devuser', |
|
12 |
- process.env.DB_USER || 'devpass', |
|
13 |
- process.env.DB_PASS || 'devdb', |
|
14 |
- { |
|
15 |
- host: 'localhost', |
|
16 |
- dialect: 'mariadb', |
|
17 |
- timezone: 'Asia/Seoul', |
|
18 |
- logging: false, |
|
19 |
- } |
|
20 |
-) |
|
10 |
+let dbName = process.env.DB_NAME || 'devdb' |
|
11 |
+let dbUser = process.env.DB_USER || 'devuser' |
|
12 |
+let dbPass = process.env.DB_PASS || 'devpass' |
|
13 |
+const testFlag = process.env['JEST_WORKER_ID'] |
|
14 |
+if (testFlag) { |
|
15 |
+ dbName = 'devdbtest' |
|
16 |
+ dbUser = 'testuser' |
|
17 |
+ dbPass = 'testpass' |
|
18 |
+} |
|
19 |
+const sequelize = new Sequelize(dbName, dbUser, dbPass, { |
|
20 |
+ host: 'localhost', |
|
21 |
+ dialect: 'mariadb', |
|
22 |
+ timezone: 'Asia/Seoul', |
|
23 |
+ logging: false, |
|
24 |
+}) |
|
21 | 25 |
|
22 | 26 |
module.exports = { connection, sequelize, Model, DataTypes } |
--- lib/user.js
+++ lib/user.js
... | ... | @@ -28,16 +28,6 @@ |
28 | 28 |
return rows |
29 | 29 |
} |
30 | 30 |
|
31 |
-async function remove(id) { |
|
32 |
- await sequelize.sync() |
|
33 |
- const row = await User.destroy({ |
|
34 |
- where: { |
|
35 |
- id: id, |
|
36 |
- }, |
|
37 |
- }) |
|
38 |
- return row |
|
39 |
-} |
|
40 |
- |
|
41 | 31 |
async function update(data) { |
42 | 32 |
await sequelize.sync() |
43 | 33 |
const row = await User.update(data, { |
... | ... | @@ -48,10 +38,30 @@ |
48 | 38 |
return row |
49 | 39 |
} |
50 | 40 |
|
41 |
+async function remove(id) { |
|
42 |
+ await sequelize.sync() |
|
43 |
+ const row = await User.destroy({ |
|
44 |
+ where: { |
|
45 |
+ id: id, |
|
46 |
+ }, |
|
47 |
+ }) |
|
48 |
+ return row |
|
49 |
+} |
|
50 |
+ |
|
51 |
+async function removeAll() { |
|
52 |
+ await sequelize.sync() |
|
53 |
+ const row = await User.destroy({ |
|
54 |
+ where: {}, |
|
55 |
+ truncate: true, |
|
56 |
+ }) |
|
57 |
+ return row |
|
58 |
+} |
|
59 |
+ |
|
51 | 60 |
module.exports = { |
52 | 61 |
create, |
53 | 62 |
get, |
54 | 63 |
findAll, |
55 |
- remove, |
|
56 | 64 |
update, |
65 |
+ remove, |
|
66 |
+ removeAll, |
|
57 | 67 |
} |
--- package.json
+++ package.json
... | ... | @@ -42,6 +42,7 @@ |
42 | 42 |
"passport": "^0.6.0", |
43 | 43 |
"passport-facebook": "^3.0.0", |
44 | 44 |
"passport-github2": "^0.1.12", |
45 |
+ "random-email": "^1.0.3", |
|
45 | 46 |
"redis": "^3.1.2", |
46 | 47 |
"sequelize": "^6.29.0", |
47 | 48 |
"serve-favicon": "^2.5.0", |
--- tests/bookmark.test.js
+++ tests/bookmark.test.js
... | ... | @@ -16,5 +16,8 @@ |
16 | 16 |
// get bookmark |
17 | 17 |
const row = await bookmark.get(result.dataValues.id) |
18 | 18 |
expect(row.pathname).toBe(data.pathname) |
19 |
+ expect(row.userId).toBe(data.userId) |
|
20 |
+ await bookmark.remove(result.dataValues.id) |
|
21 |
+ user.remove(data.userId) |
|
19 | 22 |
}) |
20 | 23 |
}) |
--- tests/mariadb.test.js
... | ... | @@ -1,20 +0,0 @@ |
1 | -const bookmark = require('../lib/bookmark') | |
2 | -describe('Bookmark', () => { | |
3 | - it('should create a bookmark', async () => { | |
4 | - const ts = Date.now() | |
5 | - const result = await bookmark.create({ | |
6 | - pathname: '/mib/java/jsp' + ts, | |
7 | - }) | |
8 | - expect(result.pathname).toBe('/mib/java/jsp' + ts) | |
9 | - }) | |
10 | - | |
11 | - it('should get a bookmark', async () => { | |
12 | - const ts = Date.now() | |
13 | - const result = await bookmark.create({ | |
14 | - pathname: '/mib/java/jsp' + ts, | |
15 | - }) | |
16 | - console.log(result.id) | |
17 | - const row = await bookmark.get(result.id) | |
18 | - expect(row.pathname).toBe('/mib/java/jsp' + ts) | |
19 | - }) | |
20 | -}) |
--- tests/number.test.js
+++ tests/number.test.js
... | ... | @@ -1,28 +1,12 @@ |
1 |
+const {add, subtract, multiply, divide} = require('./numbers') |
|
2 |
+ |
|
1 | 3 |
test('add', () => { |
2 | 4 |
expect(add(1, 2)).toBe(3) |
3 | 5 |
}) |
4 |
- |
|
5 | 6 |
// test for subtract |
6 | 7 |
test('subtract', () => { |
7 | 8 |
expect(subtract(2, 1)).toBe(1) |
8 | 9 |
}) |
9 |
-// add |
|
10 |
-function add(a, b) { |
|
11 |
- return a + b |
|
12 |
-} |
|
13 |
-function subtract(a, b) { |
|
14 |
- return a - b |
|
15 |
-} |
|
16 |
- |
|
17 |
-// multiply |
|
18 |
-function multiply(a, b) { |
|
19 |
- return a * b |
|
20 |
-} |
|
21 |
- |
|
22 |
-// divide |
|
23 |
-function divide(a, b) { |
|
24 |
- return a / b |
|
25 |
-} |
|
26 | 10 |
|
27 | 11 |
// test for multiply |
28 | 12 |
test('multiply', () => { |
+++ tests/numbers.js
... | ... | @@ -0,0 +1,16 @@ |
1 | +function add(a, b) { | |
2 | + return a + b | |
3 | +} | |
4 | +function subtract(a, b) { | |
5 | + return a - b | |
6 | +} | |
7 | +function multiply(a, b) { | |
8 | + return a * b | |
9 | +} | |
10 | +function divide(a, b) { | |
11 | + return a / b | |
12 | +} | |
13 | + | |
14 | +module.exports = { | |
15 | + add, subtract, multiply, divide | |
16 | +}(No newline at end of file) |
--- tests/user.test.js
+++ tests/user.test.js
... | ... | @@ -1,6 +1,7 @@ |
1 | 1 |
const user = require('../lib/user') |
2 |
+const randomEmail = require('random-email')({ domain: 'okdevtv.com' }) |
|
2 | 3 |
const userData = { |
3 |
- email: 'kenu.heo@gmail.com', |
|
4 |
+ email: randomEmail, |
|
4 | 5 |
passwd: '', |
5 | 6 |
github: '', |
6 | 7 |
} |
... | ... | @@ -20,4 +21,4 @@ |
20 | 21 |
}) |
21 | 22 |
}) |
22 | 23 |
|
23 |
-module.exports = userData; |
|
24 |
+module.exports = userData |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?