Formula field is calculated on the fly and the values will be available after record is committed to database.
Before saving the Opportunity ie. in before insert and update, you are checking Opportunity values, which are not available.
So, in this case, related_Partner_Supplier__c
is always null
.
2nd thing, your trigger is not bulkified. It is always taking first account's id.
3rd thing, you have unnecessarily used DML statements in test method:
Opportunity g = new Opportunity (Name = 'testOpp',
AccountId = a.Id,
Amount = 250,
StageName = 'new',
CloseDate = date.today(),
related_Partner_Supplier__c = a.Id);
insert g;
update a; //why it is needed?
update g; //why it is needed?
Explanations of this error in the following lines of code:
g = [SELECT Account.Id, Source_Details__c FROM Opportunity WHERE Id = :g.Id];
System.AssertEquals(a.Id, g.AccountID);
Account ag = [SELECT Id FROM Account WHERE Name = :g.Source_Details__c];
Source_Details__c
of Opportunity is null due to your inappropriate logic in trigger, which is getting passed to retrive Account record.
By default, Account Name is mandatory so it cannot be null, But here due to query condition no records are returning.
That's why, in this below line Account ag = [SELECT Id FROM Account WHERE Name = :g.Source_Details__c];
, you are getting that error
System.QueryException: List has no rows for assignment to SObject.
Workaround, makeperform the formula field value checking in after insert and after update of Opportunity trigger.
Hope it makes sense.