Skip to content

Commit

Permalink
fix: previousChanges should check instance is new record or not while…
Browse files Browse the repository at this point in the history
… specific attributes' values were undefined (#276)

* fix: previousChanges should check instance is new record or not while specific attributes' value was undefined

* fix: take attribute.cast null-like value to null

Co-authored-by: JimmyDaddy <[email protected]>
Co-authored-by: Chen Yangjian <[email protected]>
  • Loading branch information
3 people authored Feb 21, 2022
1 parent 75c2a50 commit 9b31605
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ class Bone {
}

if (args.length > 1) {
// execute validators
this.#raw[name] = value instanceof Raw ? value : attribute.cast(value);
this.#rawUnset.delete(name);
return this;
Expand Down
3 changes: 2 additions & 1 deletion src/drivers/abstract/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ class Attribute {
}

cast(value) {
return this.type.cast(value);
const castedValue = this.type.cast(value);
return castedValue == null? null : castedValue;
}

uncast(value, strict = true) {
Expand Down
6 changes: 5 additions & 1 deletion test/integration/suite/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,12 @@ describe('=> Basic', () => {
assert.deepEqual(post.previousChanges('title'), {});
await post.save();
assert.deepEqual(post.previousChanges('title'), {});
assert.deepEqual(post.previousChanges('authorId'), { });
post.title = 'MHW';
post.authorId = 100;
await post.save();
assert.deepEqual(post.previousChanges('title'), { title: [ 'Untitled', 'MHW' ] });
assert.deepEqual(post.previousChanges('authorId'), { authorId: [ null, 100 ] });
// should return {} if key does not exist
assert.deepEqual(post.previousChanges('notExisted'), {});
});
Expand All @@ -208,10 +211,11 @@ describe('=> Basic', () => {
await post.save();
assert.deepEqual(post.previousChanges(), {});
post.title = 'MHW';
post.authorId = 100;
const prevUpdatedAt = post.updatedAt;
await sleep(10);
await post.save();
assert.deepEqual(post.previousChanges(), { title: [ 'Untitled', 'MHW' ], updatedAt: [ prevUpdatedAt, post.updatedAt ] });
assert.deepEqual(post.previousChanges(), { authorId: [ null, 100 ], title: [ 'Untitled', 'MHW' ], updatedAt: [ prevUpdatedAt, post.updatedAt ] });
});

it('Bone.changes(key): raw VS rawSaved', async function () {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/suite/data_types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ describe('=> Data types - JSON', () => {
assert.deepEqual(note3.meta, [ 1, 2, 3 ]);
await note3.reload();
assert.deepEqual(note3.meta, [ 1, 2, 3 ]);
assert.deepEqual(note3.body, null);
assert.deepEqual(note3.isPrivate, null);
assert.notEqual(note3.id, null);

const note4 = await Note.findOne({ meta: { $like: '%1,2,3%' }});
assert.deepEqual(note3.toJSON(), note4.toJSON());
Expand Down
21 changes: 21 additions & 0 deletions test/unit/adapters/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,13 @@ describe('=> Sequelize adapter', () => {
updatedAt: post.updatedAt,
createdAt: post.createdAt,
wordCount: 0,
authorId: null,
content: null,
deletedAt: null,
extra: null,
settings: null,
summary: null,
thumb: null,
});
post.content = 'a';
assert.deepEqual(post.previous(), {
Expand All @@ -1052,6 +1059,13 @@ describe('=> Sequelize adapter', () => {
updatedAt: post.updatedAt,
createdAt: post.createdAt,
wordCount: 0,
authorId: null,
content: null,
deletedAt: null,
extra: null,
settings: null,
summary: null,
thumb: null,
});
const prevUpdatedAt = post.updatedAt;
await post.update();
Expand All @@ -1062,6 +1076,13 @@ describe('=> Sequelize adapter', () => {
updatedAt: prevUpdatedAt,
createdAt: post.createdAt,
wordCount: 0,
authorId: null,
content: null,
deletedAt: null,
extra: null,
settings: null,
summary: null,
thumb: null,
});
});

Expand Down

0 comments on commit 9b31605

Please sign in to comment.