Skip to content

Commit

Permalink
chore(tests): add test case for firstOrNew with aliases and columns
Browse files Browse the repository at this point in the history
See #107
  • Loading branch information
elpete committed Dec 6, 2023
1 parent 8ac6ba7 commit 5dc81bf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/resources/app/models/PostAlt.cfc
@@ -0,0 +1,60 @@
component
table ="my_posts"
extends ="quick.models.BaseEntity"
accessors="true"
{

property name="id" column="post_pk";
property name="userId" column="user_id";
property name="body";
property name="createdDate" column="created_date";
property name="modifiedDate" column="modified_date";
property name="publishedDate" column="published_date";

function author() {
return belongsTo( "User", "user_id" );
}

function authorWithEmptyDefault() {
return belongsTo( "User", "user_id" ).withDefault();
}

function authorWithDefaultAttributes() {
return belongsTo( "User", "user_id" ).withDefault( {
"firstName" : "Guest",
"lastName" : "User"
} );
}

function authorWithCalllbackConfiguredDefault() {
return belongsTo( "User", "user_id" ).withDefault( function( user, post ) {
user.setUsername( post.getBody() );
} );
}

function tags() {
return belongsToMany(
"Tag",
"my_posts_tags",
"custom_post_pk",
"tag_id"
);
}

function comments() {
return polymorphicHasMany( "Comment", "commentable" );
}

function country() {
return belongsToThrough( [ "author", "country" ] );
}

function commentingUsers() {
return hasManyThrough( [ "comments", "author" ] );
}

function scopeLatest( qb ) {
return qb.orderBy( "created_date", "desc" );
}

}
11 changes: 11 additions & 0 deletions tests/specs/integration/BaseEntity/GetSpec.cfc
Expand Up @@ -262,6 +262,17 @@ component extends="tests.resources.ModuleIntegrationSpec" {
expect( existingUser.getFirstName() ).notToBe( "Doesnt" );
expect( existingUser.getLastName() ).notToBe( "Exist" );
} );

// https://github.com/coldbox-modules/quick/issues/107
it( "ensures that you can retrieve posts by aliases or columns", function() {
var existingPostByAlias = getInstance( "PostAlt" ).firstOrNew( { "id" : 1245 } );
expect( existingPostByAlias.isLoaded() ).toBeTrue( "Post 1245 should have been loaded from the database" );
expect( existingPostByAlias.getId() ).toBe( 1245 );

var existingPostByColumn = getInstance( "PostAlt" ).firstOrNew( { "post_pk" : 1245 } );
expect( existingPostByColumn.isLoaded() ).toBeTrue( "Post 1245 should have been loaded from the database" );
expect( existingPostByColumn.getId() ).toBe( 1245 );
} );
} );

describe( "firstOrCreate", function() {
Expand Down

0 comments on commit 5dc81bf

Please sign in to comment.