Skip to content

Commit

Permalink
Cover resource_data_record with unit tests (#341)
Browse files Browse the repository at this point in the history
* adding unit tests for resource_data_record

* removing code

* chore: Fix parameter name in applyRelations function
  • Loading branch information
mawasile committed Jun 18, 2024
1 parent 1857414 commit 52e197e
Show file tree
Hide file tree
Showing 39 changed files with 18,222 additions and 22 deletions.
773 changes: 763 additions & 10 deletions internal/powerplatform/resource_data_record_test.go

Large diffs are not rendered by default.

19 changes: 7 additions & 12 deletions internal/powerplatform/services/data_record/api_data_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,18 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId, e
json.Unmarshal(response.BodyAsBytes, &result)
} else if response.Response.Header.Get(constants.HEADER_ODATA_ENTITY_ID) != "" {
re := regexp.MustCompile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")
match := re.FindStringSubmatch(response.Response.Header.Get(constants.HEADER_ODATA_ENTITY_ID))
match := re.FindAllStringSubmatch(response.Response.Header.Get(constants.HEADER_ODATA_ENTITY_ID), -1)
if len(match) > 0 {
result.Id = match[0]
result.Id = match[len(match)-1][0]
} else {
return nil, fmt.Errorf("no entity record id returned from the odata-entityid header")
}
} else {
return nil, fmt.Errorf("no entity record id returned from the API")
}

result.Id = parseLocationHeader(response, environmentUrl, entityDefinition)

err = applyRelations(ctx, client, relations, environmentId, result.Id, entityDefinition)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -501,13 +500,6 @@ func (client *DataRecordClient) DeleteDataRecord(ctx context.Context, recordId s
return nil
}

func parseLocationHeader(response *api.ApiHttpResponse, environmentUrl string, entityDefinition *EntityDefinitionsDto) string {
locationHeader := response.GetHeader(constants.HEADER_LOCATION)
locationHeader = strings.TrimPrefix(locationHeader, fmt.Sprintf("%s/api/data/%s/%s(", environmentUrl, constants.DATAVERSE_API_VERSION, entityDefinition.LogicalCollectionName))
locationHeader = strings.TrimSuffix(locationHeader, ")")
return locationHeader
}

func getTableLogicalNameAndDataRecordIdFromMap(nestedMap map[string]interface{}) (string, string, error) {
tableLogicalName, ok := nestedMap["table_logical_name"].(string)
if !ok {
Expand Down Expand Up @@ -540,7 +532,10 @@ func applyRelations(ctx context.Context, client *DataRecordClient, relations map

existingRelationsResponse := RelationApiResponse{}

apiResponse, _ := client.Api.Execute(ctx, "GET", apiUrl.String(), nil, nil, []int{http.StatusOK, http.StatusNoContent}, nil)
apiResponse, err := client.Api.Execute(ctx, "GET", apiUrl.String(), nil, nil, []int{http.StatusOK, http.StatusNoContent}, nil)
if err != nil {
return err
}

json.Unmarshal(apiResponse.BodyAsBytes, &existingRelationsResponse)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"@odata.context": "https://00000000-0000-0000-0000-000000000001.crm4.dynamics.com/api/data/v9.2/$metadata#accounts/$entity",
"@odata.etag": "W/\"2264380\"",
"address1_latitude": 47.63,
"merged": false,
"territorycode": 1,
"exchangerate": 1.000000000000,
"accountcategorycode": 1,
"name": "Sample Account",
"_owningbusinessunit_value": "d568ee97-961f-ef11-840b-000d3abf969a",
"_owninguser_value": "c76fee97-961f-ef11-840b-000d3abf969a",
"_primarycontactid_value": "00000000-0000-0000-0000-000000000010",
"donotpostalmail": false,
"accountratingcode": 1,
"marketingonly": false,
"revenue_base": 5000000.0000000000,
"preferredcontactmethodcode": 1,
"_ownerid_value": "c76fee97-961f-ef11-840b-000d3abf969a",
"accountclassificationcode": 1,
"description": "This is the description of the sample account",
"customersizecode": 1,
"businesstypecode": 1,
"donotemail": false,
"address2_shippingmethodcode": 1,
"address1_addressid": "92dc7147-a78a-4bd6-b2d8-a4ee1a5b9da2",
"address2_freighttermscode": 1,
"statuscode": 1,
"createdon": "2024-06-12T14:13:25Z",
"donotsendmm": false,
"donotfax": false,
"donotbulkpostalmail": false,
"versionnumber": 2264380,
"modifiedon": "2024-06-12T14:13:25Z",
"creditonhold": false,
"donotphone": false,
"_transactioncurrencyid_value": "3d2766c7-c41f-ef11-840b-000d3abf969a",
"accountid": "00000000-0000-0000-0000-000000000020",
"donotbulkemail": false,
"_modifiedby_value": "c76fee97-961f-ef11-840b-000d3abf969a",
"followemail": true,
"shippingmethodcode": 1,
"_createdby_value": "c76fee97-961f-ef11-840b-000d3abf969a",
"address2_addresstypecode": 1,
"revenue": 5000000.0000000000,
"participatesinworkflow": false,
"statecode": 0,
"address2_addressid": "0887c958-8401-4fa4-86ea-21dedc00eff2",
"telephone3": null,
"address1_shippingmethodcode": null,
"sharesoutstanding": null,
"ownershipcode": null,
"address1_freighttermscode": null,
"address1_upszone": null,
"websiteurl": null,
"address2_city": null,
"_slainvokedid_value": null,
"address1_postofficebox": null,
"importsequencenumber": null,
"preferredappointmentdaycode": null,
"customertypecode": null,
"utcconversiontimezonecode": null,
"overriddencreatedon": null,
"aging90": null,
"stageid": null,
"address1_utcoffset": null,
"adx_createdbyipaddress": null,
"_masterid_value": null,
"lastonholdtime": null,
"address2_fax": null,
"accountnumber": null,
"address2_line1": null,
"address1_telephone3": null,
"address1_telephone2": null,
"address1_telephone1": null,
"address2_postofficebox": null,
"emailaddress1": null,
"ftpsiteurl": null,
"emailaddress2": null,
"address2_latitude": null,
"processid": null,
"emailaddress3": null,
"address2_composite": null,
"traversedpath": null,
"address1_city": null,
"address2_line2": null,
"aging30_base": null,
"numberofemployees": null,
"address1_addresstypecode": null,
"address2_stateorprovince": null,
"address2_postalcode": null,
"_msa_managingpartnerid_value": null,
"entityimage_url": null,
"address1_composite": null,
"aging60": null,
"timezoneruleversionnumber": null,
"address2_telephone3": null,
"address2_telephone2": null,
"address2_telephone1": null,
"address1_postalcode": null,
"address2_upszone": null,
"_owningteam_value": null,
"primarysatoriid": null,
"address2_line3": null,
"timespentbymeonemailandmeetings": null,
"address1_country": null,
"address2_longitude": null,
"_modifiedonbehalfby_value": null,
"creditlimit": null,
"address1_line2": null,
"paymenttermscode": null,
"address1_county": null,
"marketcap": null,
"_preferredsystemuserid_value": null,
"preferredappointmenttimecode": null,
"address1_fax": null,
"_createdonbehalfby_value": null,
"address2_name": null,
"creditlimit_base": null,
"marketcap_base": null,
"_modifiedbyexternalparty_value": null,
"address2_utcoffset": null,
"adx_modifiedbyusername": null,
"sic": null,
"_slaid_value": null,
"fax": null,
"address1_line1": null,
"address2_county": null,
"aging30": null,
"address1_line3": null,
"industrycode": null,
"address1_stateorprovince": null,
"onholdtime": null,
"_createdbyexternalparty_value": null,
"entityimage_timestamp": null,
"entityimageid": null,
"_parentaccountid_value": null,
"yominame": null,
"lastusedincampaign": null,
"primarytwitterid": null,
"adx_createdbyusername": null,
"telephone2": null,
"stockexchange": null,
"aging90_base": null,
"tickersymbol": null,
"address1_name": null,
"adx_modifiedbyipaddress": null,
"telephone1": null,
"address1_primarycontactname": null,
"address1_longitude": null,
"address2_primarycontactname": null,
"entityimage": null,
"aging60_base": null,
"address2_country": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"@odata.context": "https://00000000-0000-0000-0000-000000000001.crm4.dynamics.com/api/data/v9.2/$metadata#contacts(createdon)",
"value": [
{
"@odata.etag": "W/\"2264386\"",
"createdon": "2024-06-12T14:13:17Z",
"contactid": "00000000-0000-0000-0000-000000000010"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"@odata.context": "https://00000000-0000-0000-0000-000000000001.crm4.dynamics.com/api/data/v9.2/$metadata#Collection($ref)",
"value": [
{
"@odata.id": "https://00000000-0000-0000-0000-000000000001.crm4.dynamics.com/api/data/v9.2/contacts(00000000-0000-0000-0000-000000000010)"
}
]
}
Loading

0 comments on commit 52e197e

Please sign in to comment.