Skip to content

Commit 1163d67

Browse files
authored
Improve Test coverage (#4)
* Improve Test Coverage Write more tests to improve overall Test Coverage Include New Usage Directory for authentication module * Tidy Go mod file
1 parent 4e618e6 commit 1163d67

File tree

11 files changed

+214
-4
lines changed

11 files changed

+214
-4
lines changed

airtime/countries_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package airtime_test
22

33
import (
4+
"encoding/json"
5+
reloadly "github.com/reloadly/reloadly-sdk-golang/airtime"
46
"net/http"
57
"testing"
68
)
@@ -24,3 +26,32 @@ func TestClient_GetCountries(t *testing.T) {
2426
t.Errorf("Expected body to be nil but got %q", body)
2527
}
2628
}
29+
30+
func TestClient_GetCountriesByIso(t *testing.T) {
31+
teardown := setup()
32+
33+
defer teardown()
34+
35+
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
36+
rw.WriteHeader(http.StatusOK)
37+
38+
data := reloadly.Country{
39+
IsoName: "NG",
40+
CurrencyCode: "NGN",
41+
CurrencyName: "Naira",
42+
}
43+
44+
json.NewEncoder(rw).Encode(data)
45+
})
46+
47+
48+
body, err := client.GetCountriesByIso("NG")
49+
50+
if err != nil {
51+
t.Errorf("Expected error to be %q but got nil", err)
52+
}
53+
54+
if body.IsoName != "NG" {
55+
t.Errorf("Expected body to be NG but got %q", body.IsoName)
56+
}
57+
}

airtime/discounts_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@ func TestClient_GetDiscounts(t *testing.T) {
3131
t.Errorf("Expected TotalPages to be 5 but got %v", body.TotalPages)
3232
}
3333

34+
if body.TotalElements != 20 {
35+
t.Errorf("Expected TotalElements to be 20 but got %v", body.TotalElements)
36+
}
37+
}
38+
39+
func TestClient_GetDiscountsByOperatorID(t *testing.T) {
40+
teardown := setup()
41+
42+
defer teardown()
43+
44+
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
45+
rw.WriteHeader(http.StatusOK)
46+
47+
data := reloadly.Discounts{
48+
TotalElements: 20,
49+
TotalPages: 5,
50+
}
51+
json.NewEncoder(rw).Encode(data)
52+
53+
})
54+
55+
body, err := client.GetDiscountsByOperatorID("")
56+
if err != nil {
57+
t.Errorf("Expected error to be nil but got %q", err)
58+
}
59+
60+
if body.TotalPages != 5 {
61+
t.Errorf("Expected TotalPages to be 5 but got %v", body.TotalPages)
62+
}
63+
3464
if body.TotalElements != 20 {
3565
t.Errorf("Expected TotalElements to be 20 but got %v", body.TotalElements)
3666
}

airtime/operators_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,46 @@ func TestClient_GetOperatorsByISO(t *testing.T) {
8181
if body.Size != 20 {
8282
t.Errorf("Expected Size to be 20 but got %v", body.Size)
8383
}
84+
}
85+
86+
func TestClient_GetOperators(t *testing.T) {
87+
teardown := setup()
88+
89+
defer teardown()
90+
91+
mux.HandleFunc("/operators", func(rw http.ResponseWriter, req *http.Request) {
92+
rw.WriteHeader(http.StatusOK)
93+
data := reloadly.Operators{
94+
TotalPages: 5,
95+
Size: 20,
96+
}
97+
98+
json.NewEncoder(rw).Encode(data)
99+
100+
})
101+
102+
body, err := client.GetOperators()
103+
if err != nil {
104+
t.Errorf("Expected error to be nil but got %q", err)
105+
}
106+
107+
if body.Size != 20 {
108+
t.Errorf("Expected Size to be 20 but got %v", body.Size)
109+
}
110+
}
111+
112+
func TestClient_GetOperatorsById(t *testing.T) {
113+
teardown := setup()
114+
115+
defer teardown()
116+
117+
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
118+
rw.WriteHeader(http.StatusInternalServerError)
119+
120+
})
121+
122+
_, err := client.GetOperators()
123+
if err == nil {
124+
t.Errorf("Expected error but got nil")
125+
}
84126
}

airtime/promotions_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,33 @@ func TestClient_GetPromotionsByCode(t *testing.T) {
4343
}
4444
}
4545

46+
func TestClient_GetPromotionsById(t *testing.T) {
47+
teardown := setup()
48+
49+
defer teardown()
50+
51+
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
52+
rw.WriteHeader(http.StatusInternalServerError)
53+
})
54+
55+
_, err := client.GetPromotionsById(0123)
56+
if err == nil {
57+
t.Errorf("Expected error but got nil")
58+
}
59+
}
60+
61+
func TestClient_GetPromotionsByOperatorId(t *testing.T) {
62+
teardown := setup()
63+
64+
defer teardown()
65+
66+
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
67+
rw.WriteHeader(http.StatusInternalServerError)
68+
})
69+
70+
_, err := client.GetPromotionsByOperatorId(0123)
71+
if err == nil {
72+
t.Errorf("Expected error but got nil")
73+
}
74+
}
75+

airtime/reloadly_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package airtime
2+
3+

airtime/reports.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type Transactions struct {
6464
//Apart from doing just that we also provide a neat way to integrate transactions into your own systems.
6565
//You can get a complete list of your past top-up transactions by calling the GetTransactions function. Internally, it makes a GET request to the /topups/reports/transactions Endpoint.
6666
//This will provide you with a paginated result with all your recent transactions and the ability to paginate to further previous transactions.
67+
6768
func (c *Client)GetTransactions(filter ...Filters)(*Transactions, error){
6869
o := &FilterOptions{}
6970
for _, opt := range filter {

airtime/topup_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package airtime_test
22

33
import (
44
"encoding/json"
5+
"errors"
56
reloadly "github.com/reloadly/reloadly-sdk-golang/airtime"
67
Err "github.com/reloadly/reloadly-sdk-golang/error"
78
"net/http"
89
"net/http/httptest"
10+
"reflect"
911
"strings"
1012
"testing"
1113
)
@@ -77,3 +79,48 @@ func TestClient_NautaCubaTopup(t *testing.T) {
7779
}
7880
}
7981

82+
func TestGetPhone(t *testing.T) {
83+
cases := [] struct{
84+
Number string
85+
CountryCode string
86+
ExpectedNumber string
87+
ExpectedCountryCode string
88+
ExpectedErr error
89+
}{
90+
{
91+
Number: "",
92+
CountryCode: "",
93+
ExpectedNumber: "",
94+
ExpectedCountryCode: "",
95+
ExpectedErr: errors.New("Invalid_Credentials"),
96+
},
97+
{
98+
Number: "5555",
99+
CountryCode: "4444",
100+
ExpectedNumber: "5555",
101+
ExpectedCountryCode: "4444",
102+
ExpectedErr: nil,
103+
},
104+
}
105+
106+
107+
for _, c := range cases {
108+
res, err := reloadly.GetPhone(c.Number, c.CountryCode)
109+
if !reflect.DeepEqual(err, c.ExpectedErr) {
110+
t.Fatalf("Expected err to be %q but it was %q", c.ExpectedErr, err)
111+
}
112+
113+
if res != nil{
114+
//t.Skip("Skipped the rest of the tests")
115+
if c.ExpectedNumber != res.Number {
116+
t.Fatalf("Expected Number to be %s but got %s", c.ExpectedNumber, res.Number)
117+
}
118+
119+
if c.CountryCode != res.CountryCode {
120+
t.Fatalf("Expected Country Code to be %s but got %s", c.ExpectedCountryCode, res.CountryCode)
121+
}
122+
}
123+
124+
125+
}
126+
}

airtime/usage/USAGE.MD

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Set the client id, client secret & environment; The most straight-forward or sim
2626
package main
2727

2828
import (
29-
"github.com/Ghvstcode/reloadly"
29+
"github.com/reloadly/reloadly-sdk-golang/authentication"
3030
"fmt"
3131
)
3232

@@ -43,14 +43,14 @@ Set the client id, client secret & environment; The most straight-forward or sim
4343
### Option 2
4444

4545
You may alternatively acquire an access token from the
46-
[AuthenticationAPI](https://github.com/reloadly/reloadly-sdk-java/blob/master/reloadly-java-sdk-authentication/USAGE.md)
46+
[AuthenticationAPI](https://developers.reloadly.com/#authentication_auth_anc)
4747
and then set it.
4848

4949
```golang
5050
package main
5151

5252
import (
53-
"github.com/Ghvstcode/reloadly/authentication"
53+
"github.com/reloadly/reloadly-sdk-golang/authentication"
5454
"fmt"
5555
)
5656

authentication/Usage/Usage.MD

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
You can use the authentication module to alternatively acquire authentication details(access token) from the
2+
[AuthenticationAPI](https://developers.reloadly.com/#authentication_auth_anc)
3+
and then set it.
4+
5+
```golang
6+
package main
7+
8+
import (
9+
"github.com/reloadly/reloadly-sdk-golang/authentication"
10+
"fmt"
11+
)
12+
13+
func main(){
14+
authClient, err := authentication.NewAuthClient(xx1234, xx23, false)
15+
if err != nil {
16+
fmt.Println(err)
17+
}
18+
19+
token, err := authClient.GetAccessToken()
20+
if err != nil {
21+
fmt.Println(err)
22+
}
23+
24+
fmt.Println(token.AccessToken)
25+
}
26+
```

authentication/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestNewAuthClient(t *testing.T) {
3939
}
4040

4141
if res != nil{
42-
t.Skip("Skipped the rest of the tests")
42+
//t.Skip("Skipped the rest of the tests")
4343
if c.ExpectedSandbox != res.SandBox{
4444
t.Fatalf("Expected Sandbox to be %t but got %s", c.ExpectedSandbox, res.URL)
4545
}

0 commit comments

Comments
 (0)