Repository: VictorAvelar/mollie-api-go (go)

Issue:
improve test coverage for orders.go file
**Is your feature request related to a problem? Please describe.**
Extend the tests for the `orders` resource, this is the only file in which we don't have more than 90% coverage.

**Describe the solution you'd like**
Force errors and test that the client is handling those errors gracefully without breaking the existing tests.

You can follow [customers.go](https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/customers_test.go) test suite as an example.

**Additional context**
If you have any concern or question, please feel free to address it in the comments.


Pull request description:
Fix the orders.go test scenario
<!--- Provide a general summary of your changes in the Title above -->

## Description

Adds 3 test scenario: NewAPIRequestErrors, JsonDecodingErrors and HTTPRequestErrors.

## Motivation and context

Fixes #40 

## How has this been tested?

Tested using the built-in Golang test tools.
Golang version go1.12.6 windows/amd64

## Screenshots (if appropriate)
![image](https://user-images.githubusercontent.com/26384044/67661876-d412f900-f994-11e9-9366-09c71ef82f75.png)


## Types of changes

What types of changes does your code introduce? Put an `x` in all the boxes that apply:
- [x] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

Go over all the following points, and put an `x` in all the boxes that apply.

Please, please, please, don't send your pull request until all of the boxes are ticked. Once your pull request is created, it will trigger a build on our continuous integration server to make sure your [tests and code style pass](https://help.github.com/articles/about-required-status-checks/).

- [x] I have read the **[CONTRIBUTING](CONTRIBUTING.md)** document.
- [x] My pull request addresses exactly one patch/feature.
- [x] I have created a branch for this patch/feature.
- [x] Each individual commit in the pull request is meaningful.
- [x] I have added tests to cover my changes.
- [x] If my change requires a change to the documentation, I have updated it accordingly.

If you're unsure about any of these, don't hesitate to ask. We're here to help!


Patch:
diff --git a/mollie/orders.go b/mollie/orders.go
index 80ac722..b29c7f5 100644
--- a/mollie/orders.go
+++ b/mollie/orders.go
@@ -377,10 +377,13 @@ func (ors *OrdersService) CancelOrderLine(orderID string, orderlines *Orders) (e
 
 	res, err := ors.client.Do(req)
 	if err != nil {
-		err = json.Unmarshal(res.content, &errorResponse)
-		if err != nil {
-			return
+		if res.ContentLength > 0 {
+			err = json.Unmarshal(res.content, &errorResponse)
+			if err != nil {
+				return
+			}
 		}
+
 		return
 	}
 
@@ -404,10 +407,13 @@ func (ors *OrdersService) CreateOrderPayment(orderID string, ordPay *OrderPaymen
 
 	res, err := ors.client.Do(req)
 	if err != nil {
-		err = json.Unmarshal(res.content, &errorResponse)
-		if err != nil {
-			return
+		if res.ContentLength > 0 {
+			err = json.Unmarshal(res.content, &errorResponse)
+			if err != nil {
+				return
+			}
 		}
+
 		return
 	}
 
@@ -430,10 +436,13 @@ func (ors *OrdersService) CreateOrderRefund(orderID string, order *Orders) (refu
 
 	res, err := ors.client.Do(req)
 	if err != nil {
-		err = json.Unmarshal(res.content, &errorResponse)
-		if err != nil {
-			return
+		if res.ContentLength > 0 {
+			err = json.Unmarshal(res.content, &errorResponse)
+			if err != nil {
+				return
+			}
 		}
+
 		return
 	}
 


Test patch:
diff --git a/mollie/orders_test.go b/mollie/orders_test.go
index b9d5b3c..279fe54 100644
--- a/mollie/orders_test.go
+++ b/mollie/orders_test.go
@@ -3,6 +3,8 @@ package mollie
 import (
 	"encoding/json"
 	"net/http"
+	"net/url"
+	"strings"
 	"testing"
 
 	"github.com/VictorAvelar/mollie-api-go/testdata"
@@ -438,3 +440,77 @@ func TestOrdersService_ListOrderRefund(t *testing.T) {
 		t.Errorf("mismatching info. want %v got %v", 1, res.Count)
 	}
 }
+
+func TestOrdersService_NewAPIRequestErrors(t *testing.T) {
+	setup()
+	defer teardown()
+
+	u, _ := url.Parse(tServer.URL)
+	tClient.BaseURL = u
+
+	tMux.HandleFunc("/v2/orders/", errorHandler)
+
+	tests := forceOrdersErrors(true)
+
+	for _, tt := range tests {
+		if tt != errBadBaseURL {
+			t.Error(tt)
+		}
+	}
+}
+
+func TestOrdersService_JsonDecodingErrors(t *testing.T) {
+	setup()
+	defer teardown()
+
+	tMux.HandleFunc("/v2/orders/", encodingHandler)
+
+	tests := forceOrdersErrors(false)
+
+	for _, tt := range tests {
+		if tt == nil {
+			t.Error(tt)
+		} else if !strings.Contains(tt.Error(), "invalid character") {
+			t.Errorf("unexpected error %v", tt)
+		}
+	}
+}
+
+func TestOrdersService_HTTPRequestErrors(t *testing.T) {
+	setup()
+	defer teardown()
+
+	tMux.HandleFunc("/v2/orders/", errorHandler)
+
+	tests := forceOrdersErrors(true)
+	for _, tt := range tests {
+		if !strings.Contains(tt.Error(), "Internal Server Error") {
+			t.Error(tt)
+		}
+	}
+}
+
+func forceOrdersErrors(del bool) []error {
+	id := "ord_8wmqcHMN4U"
+
+	_, cerr := tClient.Orders.Create(Orders{}, nil)
+	_, _, coperr := tClient.Orders.CreateOrderPayment(id, nil)
+	_, _, corerr := tClient.Orders.CreateOrderRefund(id, nil)
+	_, rerr := tClient.Orders.Get(id, nil)
+	_, lerr := tClient.Orders.List(nil)
+	_, lorerr := tClient.Orders.ListOrderRefunds(id, nil)
+	_, uerr := tClient.Orders.Update(id, Orders{})
+	_, uolerr := tClient.Orders.UpdateOrderline(id, "", OrderLines{})
+
+	errs := []error{cerr, coperr, corerr, rerr, lerr, lorerr, uerr, uolerr}
+
+	if del {
+		_, cnlerr := tClient.Orders.Cancel(id)
+		errs = append(errs, cnlerr)
+
+		_, colerr := tClient.Orders.CancelOrderLine(id, nil)
+		errs = append(errs, colerr)
+	}
+
+	return errs
+}