76 lines
1.9 KiB
JavaScript
76 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
|
|
/**
|
|
* 构造 store 模型测试所需的最小 Sequelize 类型集合。
|
|
* @return {Object} Sequelize 类型集合。
|
|
*/
|
|
function createSequelizeTypes() {
|
|
const makeType = sql => ({
|
|
toSql() {
|
|
return sql;
|
|
},
|
|
});
|
|
|
|
return {
|
|
BIGINT: {
|
|
UNSIGNED: makeType('BIGINT UNSIGNED'),
|
|
},
|
|
STRING: length => makeType(`VARCHAR(${length})`),
|
|
DATE: makeType('DATETIME'),
|
|
TINYINT: length => makeType(`TINYINT(${length})`),
|
|
JSON: makeType('JSON'),
|
|
literal(value) {
|
|
return value;
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 构造加载 store 模型所需的最小应用对象。
|
|
* @return {Object} 应用对象。
|
|
*/
|
|
function createApp() {
|
|
return {
|
|
Sequelize: createSequelizeTypes(),
|
|
model: {
|
|
define(name, attributes, options) {
|
|
return {
|
|
name,
|
|
rawAttributes: attributes,
|
|
options,
|
|
prototype: {},
|
|
belongsTo() {},
|
|
hasMany() {},
|
|
};
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
describe('test/app/model/store.test.js', () => {
|
|
it('should keep auto_grouping_enabled disabled by default in normalized subject config', () => {
|
|
const Store = require('../../../app/model/store')(createApp());
|
|
|
|
const fromNull = Store.normalizeSubjectConfig(null);
|
|
const fromEmptyObject = Store.normalizeSubjectConfig({});
|
|
|
|
assert.equal(Store.DEFAULT_SUBJECT_CONFIG.auto_grouping_enabled, false);
|
|
assert.equal(fromNull.auto_grouping_enabled, false);
|
|
assert.equal(fromEmptyObject.auto_grouping_enabled, false);
|
|
});
|
|
|
|
it('should normalize auto_grouping_enabled to true when explicitly enabled', () => {
|
|
const Store = require('../../../app/model/store')(createApp());
|
|
const truthyInputs = [ true, 1, '1', 'true' ];
|
|
|
|
truthyInputs.forEach(input => {
|
|
const normalized = Store.normalizeSubjectConfig({
|
|
auto_grouping_enabled: input,
|
|
});
|
|
assert.equal(normalized.auto_grouping_enabled, true);
|
|
});
|
|
});
|
|
});
|