Skip to content

Commit

Permalink
chore: add sub model describe
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Aug 14, 2018
1 parent af53e5d commit a6325bf
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 5 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ Please put models under `app/model` dir.

## Conventions

| model file | class name |
| --------------- | --------------------- |
| `user.js` | `app.model.User` |
| `person.js` | `app.model.Person` |
| `user_group.js` | `app.model.UserGroup` |
| model file | class name |
| ---------------- | ----------------------- |
| `user.js` | `app.model.User` |
| `person.js` | `app.model.Person` |
| `user_group.js` | `app.model.UserGroup` |
| `user/profile.js`| `app.model.User.Profile`|

- Tables always has timestamp fields: `created_at datetime`, `updated_at datetime`.
- Use underscore style column name, for example: `user_id`, `comments_count`.
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/apps/sub-model/app/model/sub/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const assert = require('assert');

module.exports = app => {
const { INTEGER, STRING } = app.Sequelize;
const Post = app.model.define('post', {
user_id: INTEGER,
name: STRING(30),
});

Post.associate = function() {
assert.ok(app.model.User);
assert.ok(app.model.Sub.Post);
app.model.Sub.Post.belongsTo(app.model.User, { as: 'user', foreignKey: 'user_id' });
};

return Post;
};
19 changes: 19 additions & 0 deletions test/fixtures/apps/sub-model/app/model/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const assert = require('assert');

module.exports = app => {
const { STRING, INTEGER } = app.Sequelize;
const User = app.model.define('user', {
name: STRING(30),
age: INTEGER,
});

User.associate = function() {
assert.ok(app.model.User);
assert.ok(app.model.Sub.Post);
app.model.User.hasMany(app.model.Sub.Post, { as: 'posts', foreignKey: 'user_id' });
};

return User;
};
19 changes: 19 additions & 0 deletions test/fixtures/apps/sub-model/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

exports.sequelize = {
port: '3306',
host: '127.0.0.1',
username: 'root',
password: '',
database: 'test',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000,
},
storage: 'db/test-foo.sqlite',
timezone: '+08:01',
};

exports.keys = '0jN4Fw7ZBjo4xtrLklDg4g==';
3 changes: 3 additions & 0 deletions test/fixtures/apps/sub-model/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "sub-model"
}
45 changes: 45 additions & 0 deletions test/submodel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const assert = require('assert');
const mm = require('egg-mock');

describe('test/plugin.test.js', () => {
let app;

before(() => {
app = mm.app({
baseDir: 'apps/sub-model',
});
return app.ready();
});
before(() => app.model.sync({ force: true }));

after(mm.restore);

describe('Base', () => {
it('sequelize init success', () => {
assert.ok(app.model);
assert.ok(app.model.User);
assert.ok(app.model.Sub.Post);
});

it('ctx model property getter', () => {
const ctx = app.mockContext();
assert.ok(ctx.model);
assert.ok(ctx.model.User);
assert.ok(ctx.model.Sub.Post);
});
});

describe('Associate', () => {
it('ctx model associate init success', () => {
const ctx = app.mockContext();
assert.ok(ctx.model);
assert.ok(ctx.model.User);
assert.ok(ctx.model.User.prototype.hasPosts);
assert.ok(ctx.model.Sub.Post);
console.log(ctx.model.Sub.Post);
assert.ok(ctx.model.Sub.Post.prototype.getUser);
});
});
});

0 comments on commit a6325bf

Please sign in to comment.