Skip to content

Commit

Permalink
tests(Relationships): Prove relationship setters use the cache
Browse files Browse the repository at this point in the history
Add a test proving that setting a relationship via the magic
set relationship methods also sets the relationship in the cache
so additional queries are not executed to retrieve that relationship.
  • Loading branch information
elpete authored May 14, 2019
1 parent c5a6eea commit 953d653
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/specs/integration/BaseEntity/Relationships/BelongsToSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
expect( user.posts().count() ).toBe( 3 );
} );

it( "sets an entity in the relationship cache when calling a relationship setter", function() {
var newPost = getInstance( "Post" );
newPost.setBody( "A new post by me!" );
var user = getInstance( "User" ).find( 1 );
newPost.setAuthor( user );
writeDump( var = newPost, top = 2, abort = true );
newPost.getAuthor();
newPost.getAuthor();
newPost.getAuthor();
newPost.getAuthor();
expect( newPost.retrieveAttribute( "user_id" ) ).toBe( user.getId() );
writeDump( var = variables.queries, top = 2 );
expect( variables.queries ).toHaveLength( 1, "Only one query should have been executed." );
} );

it( "can set the associated relationship by calling a relationship setter with an id", function() {
var newPost = getInstance( "Post" );
newPost.setBody( "A new post by me!" );
Expand Down

0 comments on commit 953d653

Please sign in to comment.