@@ -589,6 +589,97 @@ private with sharing class fflib_SObjectUnitOfWorkTest
589589 System .assertEquals (1 , [SELECT COUNT () FROM Opportunity WHERE StageName = ' Closed' ]);
590590 }
591591
592+
593+ @isTest
594+ private static void testRegisterUpsertByExternalIdParentWillResolve () {
595+
596+ Opportunity existingOpp = new Opportunity (Name = ' Existing Opportunity' , StageName = ' Open' , CloseDate = System .today ());
597+ insert existingOpp ;
598+
599+ existingOpp .StageName = ' Closed' ;
600+
601+ System .assertEquals (1 , [SELECT COUNT () FROM Opportunity ]);
602+ System .assertEquals (0 , [SELECT COUNT () FROM Opportunity WHERE StageName = ' Closed' ]);
603+
604+ List <Schema .SObjectType > orderWithAccounts = new List <Schema .SObjectType >();
605+ orderWithAccounts .add (Account .SObjectType );
606+ orderWithAccounts .addAll (MY_SOBJECTS );
607+
608+ String accName = ' new account' ;
609+ Account newAccount = new Account (Name = accName );
610+
611+ Test .startTest ();
612+ fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork (orderWithAccounts );
613+ uow .registerNew (newAccount );
614+ uow .registerUpsert (existingOpp , Opportunity .Id , Opportunity .AccountId , newAccount );
615+ uow .commitWork ();
616+ Test .stopTest ();
617+
618+ System .assertEquals (1 , [SELECT COUNT () FROM Opportunity ]);
619+
620+ Opportunity updatedOpp = [SELECT StageName , Account.Name FROM Opportunity ];
621+ System .assertEquals (' Closed' , updatedOpp .StageName );
622+ System .assertEquals (accName , updatedOpp .Account .Name );
623+ }
624+
625+
626+ @isTest
627+ private static void testRegisterUpsertByExternalIdChildWillResolve () {
628+
629+ Opportunity newOpportunity = new Opportunity (Name = ' Existing Opportunity' , StageName = ' Closed' , CloseDate = System .today ());
630+
631+ Account existingAccount = new Account (Name = ' old account name' );
632+ insert existingAccount ;
633+
634+ List <Schema .SObjectType > orderWithAccounts = new List <Schema .SObjectType >();
635+ orderWithAccounts .add (Account .SObjectType );
636+ orderWithAccounts .addAll (MY_SOBJECTS );
637+
638+ String accName = ' new account' ;
639+
640+ existingAccount .Name = accName ;
641+
642+ Test .startTest ();
643+ fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork (orderWithAccounts );
644+ uow .registerDirty (existingAccount );
645+ uow .registerUpsert (newOpportunity , Opportunity .Id , Opportunity .AccountId , existingAccount );
646+ uow .commitWork ();
647+ Test .stopTest ();
648+
649+ System .assertEquals (1 , [SELECT COUNT () FROM Opportunity ]);
650+
651+ Opportunity updatedOpp = [SELECT StageName , Account.Name FROM Opportunity ];
652+ System .assertEquals (' Closed' , updatedOpp .StageName );
653+ System .assertEquals (accName , updatedOpp .Account .Name );
654+ }
655+
656+
657+ @isTest
658+ private static void testRegisterUpsertByExternalIdBothWillResolve () {
659+
660+ Opportunity newOpportunity = new Opportunity (Name = ' Existing Opportunity' , StageName = ' Closed' , CloseDate = System .today ());
661+
662+ String accName = ' new account' ;
663+ Account newAccount = new Account (Name = accName );
664+
665+ List <Schema .SObjectType > orderWithAccounts = new List <Schema .SObjectType >();
666+ orderWithAccounts .add (Account .SObjectType );
667+ orderWithAccounts .addAll (MY_SOBJECTS );
668+
669+ Test .startTest ();
670+ fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork (orderWithAccounts );
671+ uow .registerNew (newAccount );
672+ uow .registerUpsert (newOpportunity , Opportunity .Id , Opportunity .AccountId , newAccount );
673+ uow .commitWork ();
674+ Test .stopTest ();
675+
676+ System .assertEquals (1 , [SELECT COUNT () FROM Opportunity ]);
677+
678+ Opportunity updatedOpp = [SELECT StageName , Account.Name FROM Opportunity ];
679+ System .assertEquals (' Closed' , updatedOpp .StageName );
680+ System .assertEquals (accName , updatedOpp .Account .Name );
681+ }
682+
592683 /**
593684 * Assert that actual events exactly match expected events (size, order and name)
594685 * and types match expected types
0 commit comments