Skip to content

Commit

Permalink
🤖 TEST: Improve OOE-12 tests
Browse files Browse the repository at this point in the history
We need separate tests that confirm:

* preInsert can execute even when a `notnull` field is `null`
* `notnull` properties with `null` values are still caught before insertion
* preUpdate can execute even when a `notnull` field is `null`
* `notnull` properties with `null` values are still caught before update
  • Loading branch information
michaelborn committed Sep 26, 2023
1 parent 8fa4c5b commit 633f6bd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
6 changes: 6 additions & 0 deletions tests/models/User.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ component persistent="true" cacheUse="read-write" {

function preInsert(){
setDateCreated( now() );
if ( isNull( getPassword() ) ){
setPassword( createUUID() );
}
}
function preUpdate(){
setDateUpdated( now() );
if ( isNull( getPassword() ) ){
setPassword( createUUID() );
}
}
}
62 changes: 57 additions & 5 deletions tests/specs/entityEventsTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,37 @@ component extends="testbox.system.BaseSpec" {
expect( theUser.getDateCreated() ).notToBeNull( "persisted value should be todays date" );
});

it( "OOE-12 - still does nullability validation", () => {
it( "OOE-12 - can auto-update notnull password from preInsert", () => {
var theUser = entityNew( "User", {
id : createUUID(),
name : "Julian",
// omit username field
// and pass an explicit null
password: nullValue()
username: "jwell"
} );
expect( () => {
entitySave( theUser );
/**
* here's where the preInsert() should execute,
* detect a null password,
* update the password,
* and NOT throw a null constraint violation.
*/
ormFlush();
}).notToThrow(); // Sadly, we can't catch "org.hibernate.exception.ConstraintViolationException"
ormEvictEntity( "User" );
ormClearSession();
});
it( "OOE-12 - still throws on null username", () => {
var theUser = entityNew( "User", {
id : createUUID(),
name : "Julian"
} );
expect( () => {
entitySave( theUser );
/**
* here's where the preInsert() should execute,
* complete normally without touching the username,
* and throw a null constraint violation because a notnull field is null.
*/
ormFlush();
}).toThrow(); // Sadly, we can't catch "org.hibernate.exception.ConstraintViolationException"
ormEvictEntity( "User" );
Expand Down Expand Up @@ -108,7 +129,33 @@ component extends="testbox.system.BaseSpec" {
expect( theUser.getDateUpdated() ).notToBeNull( "persisted value should be todays date" );
});

it( "OOE-12 - still does nullability validation", () => {
it( "OOE-12 - can auto-update notnull password from preUpdate", () => {
var theUser = entityNew( "User", {
id : createUUID(),
name : "Julian",
username: "jwell",
password: "CF4Life"
} );
entitySave( theUser );
ormFlush();
entityReload( theUser );
expect( theUser.getDateUpdated() ).toBeNull();
theUser.setPassword( nullValue() );

expect( () => {
entitySave( theUser );
/**
* here's where the preUpdate() should execute,
* detect a null password,
* update the password,
* and NOT throw a null constraint violation.
*/
ormFlush();
}).notToThrow(); // Sadly, we can't catch "org.hibernate.exception.ConstraintViolationException"
ormEvictEntity( "User" );
ormClearSession();
});
it( "OOE-12 - still throws on null username", () => {
var theUser = entityNew( "User", {
id : createUUID(),
name : "Julian",
Expand All @@ -122,6 +169,11 @@ component extends="testbox.system.BaseSpec" {
theUser.setUsername( nullValue() );

expect( () => {
/**
* here's where the preUpdate() should execute,
* complete normally without touching the username,
* and throw a null constraint violation because a notnull field is null.
*/
entitySave( theUser );
ormFlush();
}).toThrow(); // Sadly, we can't catch "org.hibernate.exception.ConstraintViolationException"
Expand Down

0 comments on commit 633f6bd

Please sign in to comment.