Skip to content

Commit

Permalink
Better testing support and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jcobhams authored and jcobhams committed Jul 30, 2020
1 parent cfbd241 commit 6719254
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
12 changes: 12 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import (
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)

//Base or shared methods

func newBase(config Config) *base {

if tm, ok := os.LookupEnv("GOMONNIFY_TESTMODE"); ok && strings.ToUpper(tm) == "ON" {
config.Environment = EnvTest
}

b := &base{
Config: &config,
HTTPClient: &http.Client{Timeout: config.RequestTimeout},
Expand All @@ -25,6 +31,12 @@ func newBase(config Config) *base {
b.APIBaseUrl = APIBaseUrlSandbox
case EnvLive:
b.APIBaseUrl = APIBaseUrlLive
case EnvTest:
if testUrl, ok := os.LookupEnv("GOMONNIFY_TESTURL"); ok {
b.APIBaseUrl = testUrl
} else {
panic("gomonnify is running in test mode but no test url provided. Please Set GOMONNIFY_TESTURL env var")
}
}

return b
Expand Down
6 changes: 3 additions & 3 deletions monify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
var client *Monnify

func TestMain(m *testing.M) {
t := new(testing.T)
mockAPIServer := testhelpers.MockAPIServer(t)
mockAPIServer := testhelpers.MockAPIServer()

os.Setenv("GOMONNIFY_TESTMODE", "ON")
os.Setenv("GOMONNIFY_TESTURL", mockAPIServer.URL)
client, _ = New(DefaultConfig)
client.ReservedAccounts.APIBaseUrl = mockAPIServer.URL

os.Exit(m.Run())
}
Expand Down
5 changes: 3 additions & 2 deletions monnify.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

const (
EnvSandbox Environment = "sandbox"
EnvLive Environment = "live"
EnvSandbox Environment = "sandbox" //Sandbox environment for development
EnvLive Environment = "live" //Live environmrnt
EnvTest Environment = "test" //Test environment used during unit/integration testing

SandBoxAPIKey string = "MK_TEST_SAF7HR5F3F"
SandBoxSecretKey string = "4SY6TNL8CK3VPRSBTHTRG2N8XXEGC6NL"
Expand Down
41 changes: 40 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TeamAPT's Monnify Go API Wrapper
# GoMonnify - TeamAPT's Monnify API Wrapper
[![Build Status](https://travis-ci.org/jcobhams/gomonnify.svg?branch=master)](https://travis-ci.org/jcobhams/gomonnify)
[![codecov](https://codecov.io/gh/jcobhams/gomonnify/branch/master/graph/badge.svg)](https://codecov.io/gh/jcobhams/gomonnify)
[![GoDoc](https://godoc.org/github.com/jcobhams/gomonnify?status.svg)](https://godoc.org/github.com/jcobhams/gomonnify)
Expand Down Expand Up @@ -48,6 +48,45 @@ func main() {

4. General - Coming soon or open a PR :)

### Test Helpers
GoMonnify ships with nifty test helpers to ease unit and integration testing your code that import or relies on gomonnify.
Set the following environment variables:

`GOMONNIFY_TESTMODE` this tells the module to assume it's running in a test context.

`GOMONNIFY_TESTURL` a full integration server is bundled with the module, you can use it or write one if you need.

Example Test File `my_controller_test.go`
```go
package gomonnify

import (
"github.com/jcobhams/gomonnify/testhelpers"
"os"
"testing"
)

func TestMain(m *testing.M) {
mockAPIServer := testhelpers.MockAPIServer()

os.Setenv("GOMONNIFY_TESTMODE", "ON")
os.Setenv("GOMONNIFY_TESTURL", mockAPIServer.URL)

os.Exit(m.Run())
}

func TestMyContollerMethodThatUsesGoMonnify(t *testing.T) {
...
// test body
...

}



```


### Run Tests
`$ go test -race -v -coverprofile cover.out`

Expand Down
3 changes: 1 addition & 2 deletions testhelpers/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"net/http"
"net/http/httptest"
"testing"
)

//Testhelpers contains utility methods to ease third party unit/integration testing
Expand Down Expand Up @@ -305,7 +304,7 @@ func mockResendOTPResponseData() string {
}

//MockAPIServer initializes a test HTTP server useful for request mocking, Integration tests and Client configuration
func MockAPIServer(t *testing.T) *httptest.Server {
func MockAPIServer() *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")

Expand Down

0 comments on commit 6719254

Please sign in to comment.