0

你好!我无法测试书面顶点课程。你能帮我完成这项任务吗?

通过该组件,我们将发送到服务器的 CSV 文件的 ID 传递给 apex 类,之后我们的 apex 类如果之前没有站点,则创建站点,并根据 CSV 文件的记录更新传感器列表(插入/更新)。

控制器类

public inherited sharing class lwnReadCsvFileController {

    @AuraEnabled
    public static list<Sensor__c> readCSVFile(Id idContentDocument){
        list<Sensor__c> lstSensToInsert = new list<Sensor__c>();
        if(idContentDocument != null) {
            
            // getting File Data based on document id 
            ContentVersion objVersion = [SELECT Id, VersionData FROM ContentVersion WHERE ContentDocumentId =:idContentDocument];
            // split the file data
            list<String> lstCSVLines = objVersion.VersionData.toString().split('\n');

            //creating Basic Stations records
            List<Base_Station__c> createdBSRec = [SELECT Name, Id  From Base_Station__c];
            List<Base_Station__c> newBSRec = new List<Base_Station__c>();
            Set<String> uniqueSet = new Set<String>();
            for ( Integer i = 1; i < lstCSVLines.size(); i++ ) {
                integer coincidences = 0; 
                list<String> csvRowData = lstCSVLines[i].split(',');
                for ( Base_Station__c rec : createdBSRec ) {
                    if (csvRowData[0] == rec.Name) {
                        coincidences++;
                    }
                }
                if ( coincidences == 0 ) {
                    uniqueSet.add(csvRowData[0]);
                }
            }
            List<String> uniquelist = new List<String>(uniqueSet);
            for (integer i = 0; i < uniquelist.size() ; i++) {
                Base_Station__c newBS = new Base_Station__c(Name = uniquelist[i]);
                NewBSRec.add(newBS);
            }
            upsert newBSRec;
            

            //creating Sensor records
            for(Integer i = 1; i < lstCSVLines.size(); i++){
                Sensor__c objRec = new Sensor__c();
                list<String> csvRowData = lstCSVLines[i].split(',');
                string tempBase = csvRowData[0];
                objRec.Name = csvRowData[1];
                objRec.Base_Station__c = [SELECT Id From Base_Station__c Where Name = :tempBase ].id;
                objRec.Sensor_ID__c = integer.valueof(csvRowData[1]);
                objRec.Status__c = csvRowData[2];
                objRec.Sensor_Model__c = csvRowData[3];
                lstSensToInsert.add(objRec);
            }
                if(!lstSensToInsert.isEmpty()) {
                    List<Sensor__c> createdSenRec = [SELECT Name, Sensor_ID__c  From Sensor__c];
                    for (Sensor__c sens : lstSensToInsert ) {
                        integer coincidences = 0;
                        for (Sensor__c rec : createdSenRec ) {
                            if (sens.Sensor_ID__c == rec.Sensor_ID__c) {
                                sens.id = rec.id;
                                update sens;
                                coincidences++;
                            }
                        }
                        if ( coincidences == 0 ) {
                            insert sens;
                        }
                    }
                }
        }
        return lstSensToInsert;    
    }
}

测试类100% 覆盖率

@isTest
public class IwnReadCsvFileControllerTest {    
    public static String str = 'BASE STATION,SENSOR ID,STATUS,SENSOR MODEL \n' +
        'Leeds,1,Enabled ,R8 \n' +
        'Glasgow Central,2,Enabled,R8';
@isTest
    public static void testReadCSVFile(){
    Base_Station__c newBS = new Base_Station__c(Name = 'Leeds');
    upsert newBS;
    Sensor__c newSensor = new Sensor__c (Name = '1', Sensor_Id__c = 1, Status__c = 'Enabled', Base_Station__c = newBs.id);
    insert newSensor;
    ContentVersion contentVersionInsert = new ContentVersion(
            Title = 'Test',
            PathOnClient = 'Test.csv',
            VersionData = Blob.valueOf(str),
            IsMajorVersion = true
    );
    insert contentVersionInsert;
    Id getId = [Select ContentDocumentId From ContentVersion Where Id =:contentVersionInsert.id and isLatest=true].ContentDocumentId;
    List<Sensor__c> result = lwnReadCsvFileController.readCSVFile(getId);
}   
}
4

1 回答 1

0

您的单元测试类正在传递一个ContentVersionID:

List<Sensor__c> result = lwnReadCsvFileController.readCSVFile(contentVersionInsert.Id);

但是您的班级将此 ID 视为ContentDocumentID:

public static list<Sensor__c> readCSVFile(Id idContentDocument){
    if(idContentDocument != null) {
        ContentVersion objVersion = [
            SELECT Id, VersionData 
            FROM ContentVersion 
            WHERE ContentDocumentId =:idContentDocument
        ];

您的测试类需要查询ContentDocumentId新插入的ContentVersion并将其传递Id给您的测试类。

于 2020-08-02T22:27:53.550 回答