fix(database): add test cases
This commit is contained in:
parent
4f420c670f
commit
449c307eb9
302
packages/database/src/__tests__/field-options/hidden.test.ts
Normal file
302
packages/database/src/__tests__/field-options/hidden.test.ts
Normal file
@ -0,0 +1,302 @@
|
||||
import { mockDatabase } from '../';
|
||||
import { Database } from '../../database';
|
||||
import { Model } from '../../model';
|
||||
|
||||
describe('hidden field options', () => {
|
||||
let db: Database;
|
||||
|
||||
beforeEach(async () => {
|
||||
db = mockDatabase();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('case 1', async () => {
|
||||
db.collection({
|
||||
name: 'tests',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
{
|
||||
type: 'string',
|
||||
name: 'password',
|
||||
hidden: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
await db.sync();
|
||||
const repo = db.getRepository('tests');
|
||||
let instance = await repo.create({
|
||||
values: {
|
||||
name: 'abc',
|
||||
password: '123',
|
||||
},
|
||||
});
|
||||
expect(instance.get('password')).toBe('123');
|
||||
expect(instance.toJSON().password).toBeUndefined();
|
||||
instance = await repo.findOne({
|
||||
filter: {
|
||||
name: 'abc',
|
||||
},
|
||||
});
|
||||
expect(instance.get('password')).toBe('123');
|
||||
expect(instance.toJSON().password).toBeUndefined();
|
||||
});
|
||||
|
||||
it('case 2', async () => {
|
||||
db.collection({
|
||||
name: 'posts',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'title',
|
||||
},
|
||||
{
|
||||
type: 'belongsTo',
|
||||
name: 'user',
|
||||
target: 'users',
|
||||
hidden: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
db.collection({
|
||||
name: 'users',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
{
|
||||
type: 'string',
|
||||
name: 'password',
|
||||
hidden: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
await db.sync();
|
||||
const repo = db.getRepository('posts');
|
||||
await repo.create({
|
||||
values: {
|
||||
title: 'post1',
|
||||
user: {
|
||||
name: 'abc',
|
||||
password: '123',
|
||||
},
|
||||
},
|
||||
});
|
||||
const instance = await repo.findOne({
|
||||
filter: {
|
||||
title: 'post1',
|
||||
},
|
||||
appends: ['user'],
|
||||
});
|
||||
expect(instance.get('user')).toBeInstanceOf(Model);
|
||||
expect(instance.toJSON().user).toBeUndefined();
|
||||
});
|
||||
|
||||
it('case 3', async () => {
|
||||
db.collection({
|
||||
name: 'posts',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'title',
|
||||
},
|
||||
{
|
||||
type: 'belongsTo',
|
||||
name: 'user',
|
||||
target: 'users',
|
||||
},
|
||||
],
|
||||
});
|
||||
db.collection({
|
||||
name: 'users',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
{
|
||||
type: 'string',
|
||||
name: 'password',
|
||||
hidden: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
await db.sync();
|
||||
const repo = db.getRepository('posts');
|
||||
await repo.create({
|
||||
values: {
|
||||
title: 'post1',
|
||||
user: {
|
||||
name: 'abc',
|
||||
password: '123',
|
||||
},
|
||||
},
|
||||
});
|
||||
const instance = await repo.findOne({
|
||||
filter: {
|
||||
title: 'post1',
|
||||
},
|
||||
appends: ['user'],
|
||||
});
|
||||
expect(instance.get('user')).toBeInstanceOf(Model);
|
||||
expect(instance.toJSON().user).toBeDefined();
|
||||
expect(instance.toJSON().user.password).toBeUndefined();
|
||||
});
|
||||
|
||||
it('case 6', async () => {
|
||||
db.collection({
|
||||
name: 'posts',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'title',
|
||||
},
|
||||
{
|
||||
type: 'hasOne',
|
||||
name: 'user',
|
||||
target: 'users',
|
||||
},
|
||||
],
|
||||
});
|
||||
db.collection({
|
||||
name: 'users',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
{
|
||||
type: 'string',
|
||||
name: 'password',
|
||||
hidden: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
await db.sync();
|
||||
const repo = db.getRepository('posts');
|
||||
await repo.create({
|
||||
values: {
|
||||
title: 'post1',
|
||||
user: {
|
||||
name: 'abc',
|
||||
password: '123',
|
||||
},
|
||||
},
|
||||
});
|
||||
const instance = await repo.findOne({
|
||||
filter: {
|
||||
title: 'post1',
|
||||
},
|
||||
appends: ['user'],
|
||||
});
|
||||
expect(instance.get('user')).toBeInstanceOf(Model);
|
||||
expect(instance.toJSON().user).toBeDefined();
|
||||
expect(instance.toJSON().user.password).toBeUndefined();
|
||||
});
|
||||
|
||||
it('case 4', async () => {
|
||||
db.collection({
|
||||
name: 'posts',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'title',
|
||||
},
|
||||
{
|
||||
type: 'belongsToMany',
|
||||
name: 'users',
|
||||
target: 'users',
|
||||
},
|
||||
],
|
||||
});
|
||||
db.collection({
|
||||
name: 'users',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
{
|
||||
type: 'string',
|
||||
name: 'password',
|
||||
hidden: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
await db.sync();
|
||||
const repo = db.getRepository('posts');
|
||||
await repo.create({
|
||||
values: {
|
||||
title: 'post1',
|
||||
users: [{
|
||||
name: 'abc',
|
||||
password: '123',
|
||||
}],
|
||||
},
|
||||
});
|
||||
const instance = await repo.findOne({
|
||||
filter: {
|
||||
title: 'post1',
|
||||
},
|
||||
appends: ['users'],
|
||||
});
|
||||
expect(instance.get('users')[0]).toBeInstanceOf(Model);
|
||||
expect(instance.toJSON().users[0].password).toBeUndefined();
|
||||
});
|
||||
|
||||
it('case 5', async () => {
|
||||
db.collection({
|
||||
name: 'posts',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'title',
|
||||
},
|
||||
{
|
||||
type: 'hasMany',
|
||||
name: 'users',
|
||||
target: 'users',
|
||||
},
|
||||
],
|
||||
});
|
||||
db.collection({
|
||||
name: 'users',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
{
|
||||
type: 'string',
|
||||
name: 'password',
|
||||
hidden: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
await db.sync();
|
||||
const repo = db.getRepository('posts');
|
||||
await repo.create({
|
||||
values: {
|
||||
title: 'post1',
|
||||
users: [{
|
||||
name: 'abc',
|
||||
password: '123',
|
||||
}],
|
||||
},
|
||||
});
|
||||
const instance = await repo.findOne({
|
||||
filter: {
|
||||
title: 'post1',
|
||||
},
|
||||
appends: ['users'],
|
||||
});
|
||||
expect(instance.get('users')[0]).toBeInstanceOf(Model);
|
||||
expect(instance.toJSON().users[0].password).toBeUndefined();
|
||||
});
|
||||
});
|
@ -1,7 +1,7 @@
|
||||
import { Collection } from '../collection';
|
||||
import { Database } from '../database';
|
||||
import _ from 'lodash';
|
||||
import { DataType, ModelAttributeColumnOptions, ModelIndexesOptions, SyncOptions } from 'sequelize';
|
||||
import { Collection } from '../collection';
|
||||
import { Database } from '../database';
|
||||
|
||||
export interface FieldContext {
|
||||
database: Database;
|
||||
@ -10,6 +10,7 @@ export interface FieldContext {
|
||||
|
||||
export interface BaseFieldOptions {
|
||||
name?: string;
|
||||
hidden?: boolean;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ export class MagicAttributeModel extends Model {
|
||||
if ((this.constructor as any).rawAttributes[column]) {
|
||||
return super.get(key, value);
|
||||
}
|
||||
const options = super.get(this.magicAttribute);
|
||||
const options = super.get(this.magicAttribute, value);
|
||||
return _.get(options, key);
|
||||
}
|
||||
const data = super.get(key, value);
|
||||
|
@ -18,6 +18,9 @@ export class Model<TModelAttributes extends {} = any, TCreationAttributes extend
|
||||
if (!data) {
|
||||
return data;
|
||||
}
|
||||
if (typeof data.toJSON === 'function') {
|
||||
data = data.toJSON();
|
||||
}
|
||||
const db = (this.constructor as any).database as Database;
|
||||
const hidden = [];
|
||||
collection.forEachField((field) => {
|
||||
|
@ -119,11 +119,10 @@ describe('collections repository', () => {
|
||||
appends: ['fields'],
|
||||
});
|
||||
|
||||
const json = data.toJSON();
|
||||
const json = data.get();
|
||||
json.fields = json.fields.sort((a, b) => a.sort - b.sort);
|
||||
expect(json.fields.length).toBe(7);
|
||||
|
||||
expect(json).toMatchObject({
|
||||
expect(JSON.parse(JSON.stringify(json, null, 2))).toMatchObject({
|
||||
name: 'tests',
|
||||
fields: [
|
||||
{
|
||||
|
@ -1,4 +1,4 @@
|
||||
import Database, { Collection as DBCollection, StringFieldOptions } from '@nocobase/database';
|
||||
import Database, { Collection as DBCollection } from '@nocobase/database';
|
||||
import Application from '@nocobase/server';
|
||||
import { createApp } from '..';
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user