Skip to content

Commit 8a5ab50

Browse files
author
Genert Org
committed
fix: tests
1 parent 54c6f9b commit 8a5ab50

14 files changed

+115
-78
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ _testmain.go
2525
# Editors
2626
.idea
2727
.vscode
28+
!.vscode/launch.json
2829

2930
# Local API tests
3031
tests.sh

pipedrive/activity_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (s *ActivityTypesService) List(ctx context.Context) (*ActivityTypesResponse
6767
// ActivityTypesAddOptions specifices the optional parameters to the
6868
// ActivityTypesService.Create method.
6969
type ActivityTypesAddOptions struct {
70-
Name string `url:"name,omitempty"`
71-
IconKey string `url:"icon_key,omitempty"`
70+
Name string `url:"name"`
71+
IconKey string `url:"icon_key"`
7272
Color string `url:"color,omitempty"`
7373
}
7474

pipedrive/errors.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package pipedrive
22

3-
import "net/http"
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
47

58
// RateLimitError occurs when Pipedrive returns 403 Forbidden response with a rate limit
69
// remaining value of 0.
@@ -11,14 +14,19 @@ type RateLimitError struct {
1114
}
1215

1316
func (e *RateLimitError) Error() string {
14-
return "Something went wrong with rate"
17+
return fmt.Sprintf("%v %v: %d %v",
18+
e.Response.Request.Method, e.Response.Request.URL,
19+
e.Response.StatusCode, e.Message)
1520
}
1621

1722
// ErrorResponse reports one or more errors caused by an API request.
1823
type ErrorResponse struct {
1924
Response *http.Response
25+
Message string
2026
}
2127

2228
func (e *ErrorResponse) Error() string {
23-
return "Something went wrong with response"
29+
return fmt.Sprintf("%v %v: %d %v",
30+
e.Response.Request.Method, e.Response.Request.URL,
31+
e.Response.StatusCode, e.Message)
2432
}

pipedrive/pipedrive.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,22 @@ func (c *Client) checkResponse(r *http.Response) error {
188188
return nil
189189
}
190190

191-
return &ErrorResponse{
192-
Response: r,
191+
data, err := ioutil.ReadAll(r.Body)
192+
errorResponse := &ErrorResponse{Response: r}
193+
194+
if err == nil && data != nil {
195+
json.Unmarshal(data, errorResponse)
196+
}
197+
198+
switch {
199+
case r.StatusCode == http.StatusForbidden && r.Header.Get(headerRateRemaining) == "0":
200+
return &RateLimitError{
201+
Rate: parseRateFromResponse(r),
202+
Response: errorResponse.Response,
203+
}
204+
205+
default:
206+
return errorResponse
193207
}
194208
}
195209

test/integration/activity_fields_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package integration
22

3-
/*
4-
import "testing"
3+
import (
4+
"context"
5+
"testing"
6+
)
57

68
func TestActivityFieldsService_List(t *testing.T) {
7-
searchResults, _, err := client.ActivityFields.List()
9+
result, _, err := client.ActivityFields.List(context.Background())
810

911
if err != nil {
10-
t.Errorf("Could not get search results: %v", err)
12+
t.Errorf("Could not get results: %v", err)
1113
}
1214

13-
if searchResults.Success != true {
14-
t.Error("Got invalid activity fields")
15+
if result.Success != true {
16+
t.Error("Got invalid result")
1517
}
1618
}
17-
*/
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package integration
22

3-
/*
43
import (
5-
"github.com/genert/pipedrive-api/pipedrive"
4+
"context"
65
"testing"
6+
7+
"github.com/genert/pipedrive-api/pipedrive"
78
)
89

910
func TestActivityTypesService_Create(t *testing.T) {
10-
result, _, err := client.ActivityTypes.Create(&pipedrive.ActivityTypesAddOptions{
11-
Name: "test",
11+
result, _, err := client.ActivityTypes.Create(context.Background(), &pipedrive.ActivityTypesAddOptions{
12+
Name: RandomString(13),
1213
IconKey: "email",
1314
})
1415

@@ -20,20 +21,3 @@ func TestActivityTypesService_Create(t *testing.T) {
2021
t.Error("Could not create activity type successfully")
2122
}
2223
}
23-
24-
func TestActivityTypesService_Delete(t *testing.T) {
25-
26-
}
27-
28-
func TestActivityTypesService_DeleteMultiple(t *testing.T) {
29-
30-
}
31-
32-
func TestActivityTypesService_Edit(t *testing.T) {
33-
34-
}
35-
36-
func TestActivityTypesService_List(t *testing.T) {
37-
38-
}
39-
*/

test/integration/currencies_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package integration
22

3-
/*
43
import (
4+
"context"
5+
"testing"
6+
57
"github.com/genert/pipedrive-api/pipedrive"
68
"github.com/go-test/deep"
7-
"testing"
89
)
910

1011
func TestCurrenciesService_List(t *testing.T) {
11-
currencies, _, err := client.Currencies.List(nil)
12+
result, _, err := client.Currencies.List(context.Background(), nil)
1213

1314
if err != nil {
1415
t.Errorf("Could not get currencies: %v", err)
1516
}
1617

17-
if currencies.Success != true {
18+
if result.Success != true {
1819
t.Error("Unsuccessful currencies request")
1920
}
2021

@@ -28,21 +29,21 @@ func TestCurrenciesService_List(t *testing.T) {
2829
IsCustomFlag: false,
2930
}
3031

31-
if diff := deep.Equal(expectedCurrency, currencies.Data[0]); diff != nil {
32+
if diff := deep.Equal(expectedCurrency, result.Data[0]); diff != nil {
3233
t.Error(diff)
3334
}
3435
}
3536

3637
func TestCurrenciesService_List2(t *testing.T) {
37-
currencies, _, err := client.Currencies.List(&pipedrive.CurrenciesListOptions{
38+
result, _, err := client.Currencies.List(context.Background(), &pipedrive.CurrenciesListOptions{
3839
Term: "estonia",
3940
})
4041

4142
if err != nil {
4243
t.Errorf("Could not get currencies: %v", err)
4344
}
4445

45-
if currencies.Success != true {
46+
if result.Success != true {
4647
t.Error("Unsuccessful currencies request")
4748
}
4849

@@ -56,8 +57,7 @@ func TestCurrenciesService_List2(t *testing.T) {
5657
IsCustomFlag: false,
5758
}
5859

59-
if diff := deep.Equal(expectedCurrency, currencies.Data[0]); diff != nil {
60+
if diff := deep.Equal(expectedCurrency, result.Data[0]); diff != nil {
6061
t.Error(diff)
6162
}
6263
}
63-
*/

test/integration/files_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package integration
22

3-
/*
4-
import "testing"
3+
import (
4+
"testing"
5+
)
56

6-
func TestFilesService_Download(t *testing.T) {
7-
result, _, err := client.Files.GetDownloadLinkById(1)
7+
func TestFilesService_GetDownloadLinkByID(t *testing.T) {
8+
result, _, err := client.Files.GetDownloadLinkByID(1)
89

910
if err != nil {
1011
t.Errorf("Could not get result: %v", err)
@@ -14,4 +15,3 @@ func TestFilesService_Download(t *testing.T) {
1415
t.Error("Got invalid download link")
1516
}
1617
}
17-
*/

test/integration/pipedrive_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
11
package integration
22

33
import (
4+
"math/rand"
45
"os"
6+
"time"
57

68
"github.com/genert/pipedrive-api/pipedrive"
79
)
810

911
var (
1012
client *pipedrive.Client
13+
14+
// Random string generator
15+
src = rand.NewSource(time.Now().UnixNano())
16+
)
17+
18+
// Random string generator
19+
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
20+
const (
21+
letterIdxBits = 6 // 6 bits to represent a letter index
22+
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
23+
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
1124
)
1225

26+
func RandomString(n int) string {
27+
b := make([]byte, n)
28+
29+
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
30+
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
31+
if remain == 0 {
32+
cache, remain = src.Int63(), letterIdxMax
33+
}
34+
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
35+
b[i] = letterBytes[idx]
36+
i--
37+
}
38+
cache >>= letterIdxBits
39+
remain--
40+
}
41+
42+
return string(b)
43+
}
44+
1345
func init() {
1446
token := os.Getenv("PIPEDRIVE_API_TOKEN")
1547

test/integration/recents_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
package integration
22

3-
/*
43
import (
5-
"github.com/genert/pipedrive-api/pipedrive"
4+
"context"
65
"testing"
76
"time"
7+
8+
"github.com/genert/pipedrive-api/pipedrive"
89
)
910

1011
func TestRecents_List(t *testing.T) {
1112
sinceTime := time.Date(2017, time.September, 10, 10, 0, 0, 0, time.UTC).Format("2006-01-02 15:04:05")
1213

13-
opt := &pipedrive.RecentsListOptions{
14+
result, _, err := client.Recents.List(context.Background(), &pipedrive.RecentsListOptions{
1415
SinceTimestamp: sinceTime,
1516
Start: 0,
16-
}
17-
18-
recents, _, err := client.Recents.List(opt)
17+
})
1918

2019
if err != nil {
2120
t.Errorf("Could not get recents: %v", err)
2221
}
2322

24-
if recents.Success != true {
23+
if result.Success != true {
2524
t.Error("Could not get successful recents response")
2625
}
2726
}
28-
*/

0 commit comments

Comments
 (0)