|
const assert = require("node:assert"); |
|
|
|
(async () => { |
|
{ |
|
const createdAt = new Date("2023-08-13 11:00:00"); |
|
const updatedAt = new Date("2023-09-10 10:00:00"); |
|
|
|
let noArgs; |
|
let withArgs; |
|
|
|
noArgs = Test.build({}); |
|
assert(noArgs.createdAt); |
|
assert(!(noArgs.createdAt instanceof Date)); // unexpected |
|
// returns `literal {"val":"CURRENT_TIMESTAMP"}` |
|
assert(noArgs.updatedAt); |
|
assert(!(noArgs.updatedAt instanceof Date)); // unexpected |
|
// returns `literal {"val":"CURRENT_TIMESTAMP"}` |
|
|
|
noArgs = await noArgs.save(); |
|
assert(noArgs.createdAt); |
|
assert(!(noArgs.createdAt instanceof Date)); // unexpected |
|
assert(noArgs.updatedAt); |
|
assert(!(noArgs.updatedAt instanceof Date)); // unexpected |
|
|
|
noArgs = await Test.findByPk(noArgs.id); |
|
assert(noArgs.createdAt instanceof Date); |
|
assert(noArgs.updatedAt instanceof Date); |
|
|
|
withArgs = Test.build({ createdAt, updatedAt }); |
|
assert.equal(withArgs.createdAt, createdAt); |
|
assert.equal(withArgs.updatedAt, updatedAt); |
|
|
|
withArgs = await withArgs.save(); |
|
assert.equal(withArgs.createdAt, createdAt); |
|
assert(withArgs.updatedAt); |
|
assert(!(withArgs.updatedAt instanceof Date)); // unexpected |
|
|
|
withArgs = await Test.findByPk(withArgs.id); |
|
assert.equal(unix(withArgs.createdAt), unix(createdAt)); |
|
assert.notEqual(unix(withArgs.updatedAt), unix(updatedAt)); |
|
|
|
noArgs = Test.build({ id: noArgs.id }, { isNewRecord: false }); |
|
assert.equal(noArgs.createdAt, undefined); |
|
assert.equal(noArgs.updatedAt, undefined); |
|
|
|
noArgs = await noArgs.save(); |
|
assert.equal(noArgs.createdAt, undefined); |
|
assert(noArgs.updatedAt); |
|
assert(!(noArgs.updatedAt instanceof Date)); // unexpected |
|
|
|
noArgs = await Test.findByPk(noArgs.id); |
|
assert(unix(noArgs.createdAt) > unix(createdAt)); |
|
assert(unix(noArgs.updatedAt) > unix(updatedAt)); |
|
|
|
withArgs = Test.build( |
|
{ id: withArgs.id, createdAt, updatedAt }, |
|
{ isNewRecord: false } |
|
); |
|
assert.equal(withArgs.createdAt, undefined); // unexpected |
|
assert.equal(withArgs.updatedAt, undefined); // unexpected |
|
|
|
withArgs = await withArgs.save(); |
|
assert.equal(withArgs.createdAt, undefined); |
|
assert(withArgs.updatedAt); |
|
assert(!(withArgs.updatedAt instanceof Date)); // unexpected |
|
|
|
withArgs = await Test.findByPk(withArgs.id); |
|
assert.equal(unix(withArgs.createdAt), unix(createdAt)); |
|
assert(unix(withArgs.updatedAt) > unix(updatedAt)); |
|
} |
|
})(); |