Skip to content

Commit

Permalink
fix: auto sync db on local env (eggjs#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Feb 25, 2020
1 parent caecbf9 commit 4b94602
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 10 deletions.
15 changes: 15 additions & 0 deletions todomvc/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const initDatabase = require('./config/database/init');

class AppBootHook {
constructor(app) {
this.app = app;
}

async didLoad() {
await initDatabase(this.app);
}
}

module.exports = AppBootHook;
15 changes: 15 additions & 0 deletions todomvc/appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
environment:
matrix:
- nodejs_version: '10'
- nodejs_version: '12'

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
- npm --version
- npm run test

build: off
21 changes: 21 additions & 0 deletions todomvc/config/config.unittest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint valid-jsdoc: "off" */

'use strict';

const path = require('path');

module.exports = () => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = {};

config.orm = {
database: path.join(__dirname, '..', 'todos_unittest.sqlite3'),
};

return {
...config,
};
};
16 changes: 16 additions & 0 deletions todomvc/config/database/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

// init database here
module.exports = async app => {
// sync database defines
await app.model.sync();

if (app.config.env === 'unittest') {
await app.model.Todo.remove({}, true);
await app.model.Todo.bulkCreate([
{ id: 1, title: 'Read history of Node.js', completed: true },
{ id: 2, title: 'Learn Koa', completed: true },
{ id: 3, title: 'Star Egg', completed: false },
]);
}
};
3 changes: 3 additions & 0 deletions todomvc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"type": "git",
"url": ""
},
"ci": {
"version": "10, 12"
},
"author": "TZ <[email protected]>",
"license": "MIT"
}
10 changes: 0 additions & 10 deletions todomvc/test/app/controller/home.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@
const { app, assert } = require('egg-mock/bootstrap');

describe('test/app/controller/home.test.js', () => {
before(async () => {
await app.model.sync();
await app.model.Todo.remove({}, true);
await app.model.Todo.bulkCreate([
{ id: 1, title: 'Read history of Node.js', completed: true },
{ id: 2, title: 'Learn Koa', completed: true },
{ id: 3, title: 'Star Egg', completed: false },
]);
});

beforeEach(() => {
app.mockCsrf();
});
Expand Down

0 comments on commit 4b94602

Please sign in to comment.