-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
180 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,16 @@ package msdsales | |
import ( | ||
"context" | ||
"errors" | ||
"github.com/amp-labs/connectors/common" | ||
"github.com/amp-labs/connectors/common/interpreter" | ||
"github.com/amp-labs/connectors/common/reqrepeater" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/amp-labs/connectors/common" | ||
"github.com/amp-labs/connectors/common/interpreter" | ||
"github.com/amp-labs/connectors/common/reqrepeater" | ||
"github.com/go-test/deep" | ||
) | ||
|
||
func Test_makeQueryValues(t *testing.T) { | ||
|
@@ -59,6 +61,34 @@ func Test_makeQueryValues(t *testing.T) { | |
} | ||
} | ||
|
||
var contactsFirstPageResponse = `{ | ||
"@odata.context": "https://org5bd08fdd.api.crm.dynamics.com/api/data/v9.2/$metadata#contacts(fullname,emailaddress1,fax,familystatuscode)", | ||
"@Microsoft.Dynamics.CRM.totalrecordcount": -1, | ||
"@Microsoft.Dynamics.CRM.totalrecordcountlimitexceeded": false, | ||
"@Microsoft.Dynamics.CRM.globalmetadataversion": "6012567", | ||
"value": [ | ||
{ | ||
"@odata.etag": "W/\"4372108\"", | ||
"fullname": "Heriberto Nathan", | ||
"emailaddress1": "[email protected]", | ||
"fax": "614-555-0122", | ||
"[email protected]": "Single", | ||
"familystatuscode": 1, | ||
"contactid": "cdcfa450-cb0c-ea11-a813-000d3a1b1223" | ||
}, | ||
{ | ||
"@odata.etag": "W/\"4372115\"", | ||
"fullname": "Dwayne Elijah", | ||
"emailaddress1": "[email protected]", | ||
"fax": "281-555-0158", | ||
"[email protected]": "Single", | ||
"familystatuscode": 1, | ||
"contactid": "9fd4a450-cb0c-ea11-a813-000d3a1b1223" | ||
} | ||
], | ||
"@odata.nextLink": "https://org5bd08fdd.api.crm.dynamics.com/api/data/v9.2/contacts?$select=fullname,emailaddress1,fax,familystatuscode&$skiptoken=%3Ccookie%20pagenumber=%222%22%20pagingcookie=%22%253ccookie%2520page%253d%25221%2522%253e%253ccontactid%2520last%253d%2522%257b9FD4A450-CB0C-EA11-A813-000D3A1B1223%257d%2522%2520first%253d%2522%257bCDCFA450-CB0C-EA11-A813-000D3A1B1223%257d%2522%2520%252f%253e%253c%252fcookie%253e%22%20istracking=%22False%22%20/%3E" | ||
}` | ||
|
||
func Test_Read(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
@@ -84,12 +114,12 @@ func Test_Read(t *testing.T) { | |
w.WriteHeader(http.StatusBadRequest) | ||
writeBody(w, `{ | ||
"error": { | ||
"code": "some-code", | ||
"message": "your fault" | ||
"code": "0x80060888", | ||
"message":"Resource not found for the segment 'conacs'." | ||
} | ||
}`) | ||
})), | ||
expectedErrs: []error{common.ErrBadRequest, errors.New("your fault")}, | ||
expectedErrs: []error{common.ErrBadRequest, errors.New("Resource not found for the segment 'conacs'")}, | ||
}, | ||
{ | ||
name: "Incorrect key in payload", | ||
|
@@ -113,18 +143,107 @@ func Test_Read(t *testing.T) { | |
})), | ||
expectedErrs: []error{common.ErrNotArray}, | ||
}, | ||
// TODO there are more test to write for pagination | ||
//{ | ||
// name: "@odata.nextLink must be in payload", | ||
// server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// w.Header().Set("Content-Type", "application/json") | ||
// w.WriteHeader(http.StatusOK) | ||
// writeBody(w, `{ | ||
// "value": [] | ||
// }`) | ||
// })), | ||
// expectedErrs: []error{common.ErrNotArray}, | ||
//}, | ||
{ | ||
name: "Next page cursor may be missing in payload", | ||
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
writeBody(w, `{ | ||
"value": [] | ||
}`) | ||
})), | ||
expected: &common.ReadResult{ | ||
Data: []common.ReadResultRow{}, | ||
Done: true, | ||
}, | ||
expectedErrs: nil, | ||
}, | ||
{ | ||
name: "Successful read with 2 entries", | ||
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
writeBody(w, contactsFirstPageResponse) | ||
})), | ||
expected: &common.ReadResult{ | ||
Rows: 2, | ||
Data: []common.ReadResultRow{{ | ||
Fields: map[string]any{}, | ||
Raw: map[string]any{ | ||
"@odata.etag": "W/\"4372108\"", | ||
"fullname": "Heriberto Nathan", | ||
"emailaddress1": "[email protected]", | ||
"fax": "614-555-0122", | ||
"[email protected]": "Single", | ||
"familystatuscode": float64(1), | ||
"contactid": "cdcfa450-cb0c-ea11-a813-000d3a1b1223", | ||
}, | ||
}, { | ||
Fields: map[string]any{}, | ||
Raw: map[string]any{ | ||
"@odata.etag": "W/\"4372115\"", | ||
"fullname": "Dwayne Elijah", | ||
"emailaddress1": "[email protected]", | ||
"fax": "281-555-0158", | ||
"[email protected]": "Single", | ||
"familystatuscode": float64(1), | ||
"contactid": "9fd4a450-cb0c-ea11-a813-000d3a1b1223", | ||
}, | ||
}}, | ||
NextPage: "https://org5bd08fdd.api.crm.dynamics.com/api/data/v9.2/contacts?$select=fullname,emailaddress1,fax,familystatuscode&$skiptoken=%3Ccookie%20pagenumber=%222%22%20pagingcookie=%22%253ccookie%2520page%253d%25221%2522%253e%253ccontactid%2520last%253d%2522%257b9FD4A450-CB0C-EA11-A813-000D3A1B1223%257d%2522%2520first%253d%2522%257bCDCFA450-CB0C-EA11-A813-000D3A1B1223%257d%2522%2520%252f%253e%253c%252fcookie%253e%22%20istracking=%22False%22%20/%3E", | ||
Done: false, | ||
}, | ||
expectedErrs: nil, | ||
}, | ||
{ | ||
// NOTE: all keys in ReadResultRow will be all in lower caps | ||
name: "Successful read with chosen fields plus prepended with display values", | ||
input: common.ReadParams{ | ||
Fields: []string{"fullname", "familystatuscode"}, | ||
}, | ||
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
writeBody(w, contactsFirstPageResponse) | ||
})), | ||
expected: &common.ReadResult{ | ||
Rows: 2, | ||
Data: []common.ReadResultRow{{ | ||
Fields: map[string]any{ | ||
"fullname": "Heriberto Nathan", | ||
"familystatuscode": float64(1), | ||
"[email protected]": "Single", | ||
}, | ||
Raw: map[string]any{ | ||
"@odata.etag": "W/\"4372108\"", | ||
"fullname": "Heriberto Nathan", | ||
"emailaddress1": "[email protected]", | ||
"fax": "614-555-0122", | ||
"[email protected]": "Single", | ||
"familystatuscode": float64(1), | ||
"contactid": "cdcfa450-cb0c-ea11-a813-000d3a1b1223", | ||
}, | ||
}, { | ||
Fields: map[string]any{ | ||
"fullname": "Dwayne Elijah", | ||
"familystatuscode": float64(1), | ||
"[email protected]": "Single", | ||
}, | ||
Raw: map[string]any{ | ||
"@odata.etag": "W/\"4372115\"", | ||
"fullname": "Dwayne Elijah", | ||
"emailaddress1": "[email protected]", | ||
"fax": "281-555-0158", | ||
"[email protected]": "Single", | ||
"familystatuscode": float64(1), | ||
"contactid": "9fd4a450-cb0c-ea11-a813-000d3a1b1223", | ||
}, | ||
}}, | ||
NextPage: "https://org5bd08fdd.api.crm.dynamics.com/api/data/v9.2/contacts?$select=fullname,emailaddress1,fax,familystatuscode&$skiptoken=%3Ccookie%20pagenumber=%222%22%20pagingcookie=%22%253ccookie%2520page%253d%25221%2522%253e%253ccontactid%2520last%253d%2522%257b9FD4A450-CB0C-EA11-A813-000D3A1B1223%257d%2522%2520first%253d%2522%257bCDCFA450-CB0C-EA11-A813-000D3A1B1223%257d%2522%2520%252f%253e%253c%252fcookie%253e%22%20istracking=%22False%22%20/%3E", | ||
Done: false, | ||
}, | ||
expectedErrs: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
|
@@ -153,14 +272,19 @@ func Test_Read(t *testing.T) { | |
t.Fatalf("%s: expected no errors, got: (%v)", tt.name, err) | ||
} | ||
|
||
if len(tt.expectedErrs) != 0 && err == nil { | ||
t.Fatalf("%s: expected errors (%v), but got nothing", tt.name, tt.expectedErrs) | ||
} | ||
|
||
for _, expectedErr := range tt.expectedErrs { | ||
if !errors.Is(err, expectedErr) && !strings.Contains(err.Error(), expectedErr.Error()) { | ||
t.Fatalf("%s: expected Error: (%v), got: (%v)", tt.name, expectedErr, err) | ||
} | ||
} | ||
|
||
if !reflect.DeepEqual(output, tt.expected) { | ||
t.Fatalf("%s: expected: (%v), got: (%v)", tt.name, tt.expected, output) | ||
diff := deep.Equal(output, tt.expected) | ||
t.Fatalf("%s:, \nexpected: (%v), \ngot: (%v), \ndiff: (%v)", tt.name, tt.expected, output, diff) | ||
} | ||
}) | ||
} | ||
|