I'm testing a post installation script, but I'm having a problem. depending on the previous version, in my script I do some inserts and upserts of different sObjects. For instance, a PushTopic is upserted, but when I run my test class an upsert error appears saying that a pushtopic with this name already exists. Even with this in my scripting code:
pushTopic.Name = 'Question_Answered' + (Test.isRunningTest() ? '_test' : '');
So, even with that validation, it says that the PushTopic "QuestionAnswered_test" already exists when the test class is running. How do I delete it, or make it so it erases itself after the test is over?
This is my test method:
static testMethod void myUnitTest() {
PostInstallationScripting postInstallation = new PostInstallationScripting();
Test.testInstall(postInstallation, null);
Test.testInstall(postInstallation, new Version(1, 142));
Package_Resources__c pResources = [Select Name__c, Prefix__c From Package_Resources__c limit 1];
System.AssertEquals('ML_App_Resources', pResources.Name__c);
}
This is the entire method that fails in my post installation:
global void initStreamingApi(){
//chekcea q no exista
List<PushTopic> pushTopics = [Select ID From PushTopic Where Name = 'Question_Answered' limit 1];
//26-02-2015 cambiado por un upsert para cambiar la query
PushTopic pushTopic;
if(pushTopics.size() == 0){
pushTopic = new PushTopic();
pushTopic.Name = 'Question_Answered' + (Test.isRunningTest() ? '_test' : '');
pushTopic.ApiVersion = 31.0;
pushTopic.NotifyForOperationCreate = true;
pushTopic.NotifyForOperationUpdate = true;
pushTopic.NotifyForOperationUndelete = false;
pushTopic.NotifyForOperationDelete = false;
pushTopic.NotifyForFields = 'Referenced';
}else{
pushTopic = pushTopics.get(0);
}
pushTopic.Query = 'SELECT Id, mlsales__Answer_Date__c, mlsales__Seller_Nickname__c FROM mlsales__Question__c';
upsert pushTopic;
}