fix(database): fix test cases (#6811)

* fix(database): fix test cases

* fix(database): fix unique and null
This commit is contained in:
Junyi 2025-05-01 11:47:17 +08:00 committed by GitHub
parent fd8606e69e
commit 15bb8ac97b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 22 deletions

View File

@ -115,9 +115,7 @@ describe('string field', () => {
const model = await collection.model.create({
name: ' n1\n ',
});
expect(model.toJSON()).toMatchObject({
name: 'n1',
});
expect(model.get('name')).toBe('n1');
});
it('trim when value is null should be null', async () => {
@ -129,9 +127,7 @@ describe('string field', () => {
const model = await collection.model.create({
name: null,
});
expect(model.toJSON()).toMatchObject({
name: null,
});
expect(model.get('name')).toBeFalsy();
});
it('when value is number should be convert to string', async () => {
@ -143,8 +139,6 @@ describe('string field', () => {
const model = await collection.model.create({
name: 123,
});
expect(model.toJSON()).toMatchObject({
name: '123',
});
expect(model.get('name')).toBe('123');
});
});

View File

@ -62,9 +62,7 @@ describe('text field', () => {
const model = await collection.model.create({
name: ' n1\n ',
});
expect(model.toJSON()).toMatchObject({
name: 'n1',
});
expect(model.get('name')).toBe('n1');
});
it('trim when value is null should be null', async () => {
@ -76,9 +74,7 @@ describe('text field', () => {
const model = await collection.model.create({
name: null,
});
expect(model.toJSON()).toMatchObject({
name: null,
});
expect(model.get('name')).toBeFalsy();
});
it('when value is number should be convert to string', async () => {
@ -90,8 +86,6 @@ describe('text field', () => {
const model = await collection.model.create({
name: 123,
});
expect(model.toJSON()).toMatchObject({
name: '123',
});
expect(model.get('name')).toBe('123');
});
});

View File

@ -20,12 +20,16 @@ export class StringField extends Field {
}
additionalSequelizeOptions() {
const { name, trim } = this.options;
const { name, trim, unique } = this.options;
return {
set(value) {
if (unique && value === '') {
value = null;
}
if (value == null) {
return value;
this.setDataValue(name, null);
return;
}
if (typeof value !== 'string') {
value = value.toString();

View File

@ -25,12 +25,16 @@ export class TextField extends Field {
}
additionalSequelizeOptions() {
const { name, trim } = this.options;
const { name, trim, unique } = this.options;
return {
set(value) {
if (unique && value === '') {
value = null;
}
if (value == null) {
return value;
this.setDataValue(name, null);
return;
}
if (typeof value !== 'string') {
value = value.toString();