Skip to content

Commit 4b94602

Browse files
authored
fix: auto sync db on local env (#115)
1 parent caecbf9 commit 4b94602

File tree

6 files changed

+70
-10
lines changed

6 files changed

+70
-10
lines changed

todomvc/app.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const initDatabase = require('./config/database/init');
4+
5+
class AppBootHook {
6+
constructor(app) {
7+
this.app = app;
8+
}
9+
10+
async didLoad() {
11+
await initDatabase(this.app);
12+
}
13+
}
14+
15+
module.exports = AppBootHook;

todomvc/appveyor.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
environment:
2+
matrix:
3+
- nodejs_version: '10'
4+
- nodejs_version: '12'
5+
6+
install:
7+
- ps: Install-Product node $env:nodejs_version
8+
- npm i npminstall && node_modules\.bin\npminstall
9+
10+
test_script:
11+
- node --version
12+
- npm --version
13+
- npm run test
14+
15+
build: off

todomvc/config/config.unittest.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint valid-jsdoc: "off" */
2+
3+
'use strict';
4+
5+
const path = require('path');
6+
7+
module.exports = () => {
8+
/**
9+
* built-in config
10+
* @type {Egg.EggAppConfig}
11+
**/
12+
const config = {};
13+
14+
config.orm = {
15+
database: path.join(__dirname, '..', 'todos_unittest.sqlite3'),
16+
};
17+
18+
return {
19+
...config,
20+
};
21+
};

todomvc/config/database/init.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
// init database here
4+
module.exports = async app => {
5+
// sync database defines
6+
await app.model.sync();
7+
8+
if (app.config.env === 'unittest') {
9+
await app.model.Todo.remove({}, true);
10+
await app.model.Todo.bulkCreate([
11+
{ id: 1, title: 'Read history of Node.js', completed: true },
12+
{ id: 2, title: 'Learn Koa', completed: true },
13+
{ id: 3, title: 'Star Egg', completed: false },
14+
]);
15+
}
16+
};

todomvc/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
"type": "git",
4343
"url": ""
4444
},
45+
"ci": {
46+
"version": "10, 12"
47+
},
4548
"author": "TZ <atian25@qq.com>",
4649
"license": "MIT"
4750
}

todomvc/test/app/controller/home.test.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
const { app, assert } = require('egg-mock/bootstrap');
44

55
describe('test/app/controller/home.test.js', () => {
6-
before(async () => {
7-
await app.model.sync();
8-
await app.model.Todo.remove({}, true);
9-
await app.model.Todo.bulkCreate([
10-
{ id: 1, title: 'Read history of Node.js', completed: true },
11-
{ id: 2, title: 'Learn Koa', completed: true },
12-
{ id: 3, title: 'Star Egg', completed: false },
13-
]);
14-
});
15-
166
beforeEach(() => {
177
app.mockCsrf();
188
});

0 commit comments

Comments
 (0)