Search code examples
salesforceapex

Please tell me how to write an apex test


I have a map where the key is the day of the month, the value is the name of the month, and there are two methods as well as a wrapper class:

In the getMonths method, I get the values the input parameters, after which I fill in the map using these values I use my monthNumbers

In getExpensesByFilters method i need input values by options

public static Map<Integer, String> monthNumbers = new Map<Integer, String>{
        1 => 'January',
        2 => 'February',
        3 => 'March',
        4 => 'April',
        5 => 'May',
        6 => 'June',
        7 => 'July',
        8 => 'August',
        9 => 'September',
        10 => 'October',
        11 => 'November',
        12 => 'December'
    };

@AuraEnabled
    // передаем Id keeper чтоб достать значения 
    public static String getMonths(Integer currentYear, Id keeper) {
        Integer year = currentYear;
        // AND calendar_month(MonthDate__c) =: month
        // Integer month = 10;
        List<AggregateResult> aggregateResultList = [
            SELECT calendar_month(MonthDate__c) month, SUM(SpentAmount__c) amount, SUM(Balance__c) balance, SUM(Income__c) income
            FROM Month_Expense_Application__c
            WHERE calendar_year(MonthDate__c) =: year AND Keeper__c =: keeper
            GROUP BY calendar_month(MonthDate__c)
            ORDER BY calendar_month(MonthDate__c)
        ];
        
        Map<Integer, MonthRow> months = new Map<Integer, MonthRow>(); // key = number of month 

        for (AggregateResult result : aggregateResultList) {
            MonthRow row = new MonthRow();
            row.monthName = monthNumbers.get((Integer)result.get('month'));
            row.amount = (Decimal)result.get('amount');
            row.monthNumber = (Integer)result.get('month');
            row.income = (Decimal)result.get('income');
            row.balance = (Decimal)result.get('income') - (Decimal)result.get('amount');

            months.put((Integer)result.get('month'), row);
        }
        
        List<MonthRow> result = new List<MonthRow>();
        for (Integer i = 1; i < 13; i++) {
            if (!months.containsKey(i)) {
                MonthRow row = new MonthRow();
                row.monthName = monthNumbers.get(i);
                row.amount = 0;
                row.monthNumber = i;
                row.income = 0;
                //months.put(i, row);
                result.add(row);
            } else {
                result.add(months.get(i));
            }
        }
        return JSON.serialize(result);
        //return JSON.serialize(months.values());
    }

 @AuraEnabled
    public static Map<Date, List<ExpenseCard__c>> getExpensesByFilters(Integer currentYear, String currentMonth, Id keeper) {
        
        String monthString = currentMonth;
        Integer month;
        Integer year = currentYear;
        for (Integer key : monthNumbers.keySet()) {
            if (monthNumbers.get(key) == monthString) {
                month = key;
            }
        }
        
        List<ExpenseCard__c> expenseCardList = [
            SELECT Amount__c, Description__c, CardDate__c
            FROM ExpenseCard__c
            WHERE calendar_year(CardDate__c) =: year AND calendar_month(CardDate__c) =: month AND CardKeeper__c =: keeper
        ];
 
        Map<Date, List<ExpenseCard__c>> expensesMap = new Map<Date, List<ExpenseCard__c>>();

        for (ExpenseCard__c a : expenseCardList) {    
            Date fullDate = a.CardDate__c;
            System.debug(fullDate);
            if (expensesMap.containsKey(fullDate)) {
                expensesMap.get(fullDate).add(a);
            } else {
                List<ExpenseCard__c> expenses = new List<ExpenseCard__c>();
                expenses.add(a);
                expensesMap.put(fullDate, expenses);
            }
        }
        System.debug(expensesMap);
        return expensesMap;
        
    }

public class MonthRow {
        public Integer monthNumber{get; set;}
        public String monthName{get; set;}
        public Decimal amount{get; set;}
        public Decimal income{get; set;}
        public Decimal balance{get; set;}
    }

I have no idea how to write tests for these methods, can you tell me how to do it?


Solution

  • Hope you are doing well. What about something like this for getMonth method?

    @TestSetup
    private static void setup() {
        MAKEKEEPER
    }
    
    @IsTest
    private static void testGetMonths() {
        Integer yearToCheck = 2022;
        String keeperId = [SELECT Id FROM Keeper__c].Id;
    
        List<Month_Expense_Application__c> expenses = new List<Month_Expense_Application__c>();
        for(Integer key: YOURCLASS.monthNumbers.keySet()) {
            for (Integer day = 3; day < 6; day++) {
                expenses.add(new Month_Expense_Application__c(
                    MonthDate__c = Date.newinstance(yearToCheck, key, 17),
                    SpentAmount__c = key,
                    Balance__c = key, 
                    Income__c = key,
                    Keeper__c = keeperId
                ));
            }
        }
    
        String jsonResult;
        Test.startTest();
        jsonResult = YOURCLASS.getMonths(yearToCheck, keeperId);
        Test.stopTest();
        
        List<YOURCLASS.MonthRow> result = (List<YOURCLASS.MonthRow>)JSON.deserialize(jsonResult, List<YOURCLASS.MonthRow>);
        for(YOURCLASS.MonthRow resultRow: result) {
            System.assertEquals(YOURCLASS.monthNumbers.get(resultRow.monthNumber), resultRow.monthName);
            System.assertEquals(resultRow.monthNumber * 3, resultRow.balance);
            System.assertEquals(resultRow.monthNumber * 3, resultRow.income);
            System.assertEquals(resultRow.monthNumber * 3, resultRow.amount);
        }
    }