think-model
Version:
An adapter-based ORM for ThinkJS 3.x
125 lines (112 loc) • 2.6 kB
JavaScript
const {test} = require('ava');
const Model = require('../../lib/model');
const Relation = require('../../lib/relation/has_one');
test('has one get relation no relation where', async t => {
const relation = new Relation([{
id: 3,
title: 'hello1',
content: 'world1'
}, {
id: '',
title: 'hello2',
content: 'world2'
}], {
key: 'id',
fKey: 'post_id',
name: 'user'
});
relation.parseRelationWhere = () => false;
t.deepEqual(
await relation.getRelationData(),
[{
id: 3,
title: 'hello1',
content: 'world1'
}, {
id: '',
title: 'hello2',
content: 'world2'
}]
);
});
test('has one get relation', async t => {
t.plan(2);
const relation = new Relation([{
id: 3,
title: 'hello1',
content: 'world1'
}, {
id: 10,
title: 'hello2',
content: 'world2'
}], {
key: 'id',
fKey: 'post_id',
name: 'user'
});
relation.parseRelationWhere = () => ({post_id: ['IN', [3, 10]]});
relation.options.model = new Model('user', {handle: new Function()});
relation.options.model.select = function() {
t.deepEqual(this.options.where, {post_id: ['IN', [3, 10]]});
return [
{name: 'lizheming', post_id: 10},
{name: 'lizheming1', post_id: 10},
{name: 'lizheming', post_id: 3}
];
};
t.deepEqual(
await relation.getRelationData(),
[
{
id: 3,
title: 'hello1',
content: 'world1',
user: {name: 'lizheming', post_id: 3}
},
{
id: 10,
title: 'hello2',
content: 'world2',
user: {name: 'lizheming1', post_id: 10}
}
]
);
});
test('has one data with relation', async t => {
t.plan(4);
const relation = new Relation({
id: 3,
profile: {
age: 30,
sex: 'female'
}
}, {
key: 'id',
fKey: 'user_id',
name: 'profile'
});
relation.model = new Model('user', {handle: new Function()});
relation.options.model = new Model('profile', {handle: new Function()});
const {model} = relation.options;
model.db = function() {
return {
where() {
},
delete() {
}
};
};
model.where = function(where) {
t.deepEqual(where, {user_id: 3});
return model;
};
model.add = function(data) {
t.deepEqual(data, {user_id: 3, age: 30, sex: 'female'});
};
model.update = function(data) {
t.deepEqual(data, {age: 30, sex: 'female'});
};
await relation.setRelationData('UPDATE');
await relation.setRelationData('ADD');
await relation.setRelationData('DELETE');
});