Skip to content

Test cases for regression when retrieving child classes through parent #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/resources/app/models/Comment.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ component
property name="sentimentAnalysis" casts="JsonCast@quick";

variables._discriminators = [
"InternalComment"
"InternalComment",
"PictureComment"
];


Expand Down
9 changes: 9 additions & 0 deletions tests/resources/app/models/PictureComment.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
component
accessors="true"
extends="Comment"
table="pictureComments"
joincolumn="FK_comment"
discriminatorValue="picture"
{
property name="filename";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
component {

function up( schema, qb ) {
schema.create( "pictureComments", function( t ) {
t.unsignedInteger( "FK_comment" )
.references( "id" )
.onTable( "comments" )
.onUpdate( "CASCADE" )
.onDelete( "CASCADE" );
t.text( "filename" );
t.primaryKey( "FK_comment" );
} );

qb.newQuery().table( "comments" ).insert( [
{
"id": 5,
"body": "This is an picture comment. It is very, very picturesque.",
"commentable_id": 1246,
"commentable_type": "Post",
"designation": "picture",
"user_id": 1,
"created_date": "2024-06-13 13:14:22",
"modified_date": "2024-06-13 13:14:22",
"sentimentAnalysis" : '{ "analyzed": true, "magnitude": 0.8, "score": 0.6 }'
}
] );

qb.newQuery().table( "pictureComments" ).insert( [
{
"FK_comment": 5,
"filename": "bliss.jpg"
}
] );
}

function down( schema, qb ) {
schema.drop( "pictureComments" );
qb.table( "comments" ).where( "id", 5 ).delete();
}

}
57 changes: 57 additions & 0 deletions tests/specs/integration/BaseEntity/ChildClassSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,63 @@ component extends="tests.resources.ModuleIntegrationSpec" {
} );
} );


it( "Will return an mix of child classes when retrieving all discriminated comments", function() {
var comments = getInstance( "Comment" )
.where('designation', '!=', 'public')
.orderBy('id')
.get();

expect( comments[1] ).toBeInstanceOf( "InternalComment" );
expect( comments[2] ).toBeInstanceOf( "PictureComment" );
} );

it( "Will load foreign key when retrieving different child classes through parent", function() {
// comment id 4 = internal comment
var internalMemento = getInstance( "Comment" )
.findOrFail(4)
.getMemento();

expect( internalMemento ).toHaveKey( "FK_comment" );
expect( internalMemento['FK_comment'] ).toBe( 4 )

// comment id 5 = picture comment
var pictureMemento = getInstance( "Comment" )
.findOrFail(5)
.getMemento();

expect( pictureMemento ).toHaveKey( "FK_comment" );
expect( pictureMemento['FK_comment'] ).toBe( 5 )
} );

it( "Can update each child class when fetching from parent class", function() {
// comment id 4 = internal comment
var internalComment = getInstance( "Comment" )
.findOrFail(4)
.update({
reason: 'Super private, ya know?'
});

var uInternalComment = getInstance( "Comment" )
.findOrFail(4);

expect( uInternalComment).toBeInstanceOf( "internalComment" );
expect( uInternalComment.getReason() ).toBe('Super private, ya know?');

// comment id 5 = picture comment
var pictureComment = getInstance( "Comment" )
.findOrFail(5)
.update({
filename: 'Lenna.jpeg'
});

var uPictureComment = getInstance( "Comment" )
.findOrFail(5);

expect( uPictureComment).toBeInstanceOf( "pictureComment" );
expect( uPictureComment.getFileName() ).toBe('Lenna.jpeg');
} );

it( "returns an array of discriminated entities when loading through a relationship", () => {
var post = getInstance( "Post" ).findOrFail( 1245 );
var comments = post.getComments();
Expand Down
Loading