0

I have one azure function which is deleting records from mongo database with certain condition. I need to write test case for that. Please guide me how that can be done.

Function Code:

public class DeleteRef
{
    private readonly IDelete _iDelete;
    public DeleteRef(IDelete Delete)
    {
        _iDelete = Delete;
    }
    
    [FunctionName("DeletionOperation")]
    public async Task Run([TimerTrigger("0 */10 * * * *")] TimerInfo myTimer, ILogger log)
    {
        _iDelete.CleardData();
    }
}

Mongo Code:

public  class CleardAllData : IDelete
{

 private readonly IMongoClient _mongoClient;
 
public CleardAllData(IMongoClient mongoClient)
{
   _mongoClient = mongoClient;
}

public void CleardData()
{
    var db = _mongoClient.GetDatabase("ABC");
    var collection = db.GetCollection<BsonDocument>("XYZ");
    var filter = new FilterDefination<BsonDocument>(); // I have some filter condition
    var deleteResult = await collection.DeleteManyAsync(filter);
    var numrowsdeleted = deleteResult.DeletedCount;
} 
}
Abhishek
  • 85
  • 1
  • 9
  • What have you tried so far? – glenatron Mar 01 '22 at 10:31
  • You can refer to https://stackoverflow.com/questions/48580332/unit-testing-remove-one-item-in-my-mock-database , https://stackoverflow.com/questions/64302966/unit-tests-for-timer-triggered-azure-function-provide-test-data and https://stackoverflow.com/questions/53816215/how-to-mock-mongodb-when-it-is-called-from-another-function – Madhuraj Vadde Mar 01 '22 at 12:33

1 Answers1

0

Please check if given references help to work around:

  • Here is the article that will guide you the complete unit testing of Azure Functions with mocking database access.

  • To implement Unit Test Cases in Azure Functions, you have to use some methods like NullLoggerfactory, ILogger Class, ListLogger and as we know that the Unit Test comprises of 3 parts basically which are Arrange the Code, Act Part and Assert Part.

For complete demo information regarding Unit Testing on Azure Functions in above sections format, Please visit this article.

Please check these practical references regarding to Unit Tests in Azure Functions Timer Trigger and other triggers as well:

  1. Unit Tests for timer triggered Azure Function
  2. Unit testing app using node.js and azure functions with SQL
  3. Article 1, Article 2