Skip to content

Commit

Permalink
Merge pull request #15 from Uchencho/bugfix/balance-response-type
Browse files Browse the repository at this point in the history
using an interface to accomodate different balance types
  • Loading branch information
Uchencho authored Nov 22, 2021
2 parents eef1d96 + 2a1a4e2 commit 17daa9d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
10 changes: 5 additions & 5 deletions switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ type AutoGeneratedMessageRequest struct {

// AutoGeneratedMessageResponse is a representation of a send message response from an auto generated "source" number
type AutoGeneratedMessageResponse struct {
Code string `json:"code"`
MessageID string `json:"message_id"`
Message string `json:"message"`
Balance int `json:"balance"`
User string `json:"user"`
Code string `json:"code"`
MessageID string `json:"message_id"`
Message string `json:"message"`
Balance interface{} `json:"balance"`
User string `json:"user"`
}

// SendMesageRequest is a representation of a send message request
Expand Down
2 changes: 1 addition & 1 deletion termii.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *Client) makeRequest(method, rURL string, reqBody interface{}, resp inte
}

if err := json.NewDecoder(res.Body).Decode(&resp); err != nil {
return errors.Wrap(err, "unable to unmarshal request body")
return errors.Wrap(err, "unable to unmarshal response body")
}
return nil
}
56 changes: 44 additions & 12 deletions termii_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func fileToStruct(filepath string, s interface{}) io.Reader {
return bytes.NewReader(bb)
}

type row struct {
name string
in interface{}
out interface{}
termiiResponse string
}

func TestSendTokenSuccess(t *testing.T) {
os.Setenv("TERMII_API_KEY", termiiTestApiKey)
var (
Expand Down Expand Up @@ -344,26 +351,51 @@ func TestSendAutoGeneratedMessageSuccess(t *testing.T) {
})

var resp termii.AutoGeneratedMessageResponse
fileToStruct(filepath.Join("testdata", "send_auto_message_response.json"), &resp)
bb, _ := ioutil.ReadFile(os.Getenv("termiiResponse"))
if err := json.Unmarshal(bb, &resp); err != nil {
log.Printf("error in marshalling %+v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
bb, _ := json.Marshal(resp)
w.Write(bb)
bbb, _ := json.Marshal(resp)
w.Write(bbb)
}))
os.Setenv("TERMII_URL", termiiService.URL)
fileToStruct(filepath.Join("testdata", "send_auto_message_request.json"), &req)

c := termii.NewClient()

resp, err := c.SendAutoGeneratedMessage(req)
t.Run("No error is returned", func(t *testing.T) {
assert.NoError(t, err)
})
table := []row{
{
name: "Balance is integer and success is expected",
in: filepath.Join("testdata", "send_auto_message_request.json"),
out: filepath.Join("testdata", "send_auto_message_response.json"),
termiiResponse: filepath.Join("testdata", "send_auto_message_response.json"),
},
{
name: "Balance is float and success is expected",
in: filepath.Join("testdata", "send_auto_message_request.json"),
out: filepath.Join("testdata", "send_auto_message_response_float.json"),
termiiResponse: filepath.Join("testdata", "send_auto_message_response_float.json"),
},
}

for _, entry := range table {
req = termii.AutoGeneratedMessageRequest{}
os.Setenv("termiiResponse", entry.termiiResponse)
fileToStruct(entry.in.(string), &req)

resp, err := c.SendAutoGeneratedMessage(req)
t.Run(fmt.Sprintf("%s - No error is returned", entry.name), func(t *testing.T) {
assert.NoError(t, err)
})

t.Run("Response is as expected", func(t *testing.T) {
fileToStruct(filepath.Join("testdata", "send_auto_message_response.json"), &expectedResponse)
assert.Equal(t, expectedResponse, resp)
})
t.Run(fmt.Sprintf("%s - Response is as expected", entry.name), func(t *testing.T) {
fileToStruct(entry.out.(string), &expectedResponse)
assert.Equal(t, expectedResponse, resp)
})
}
}

func TestSetDeviceTemplateSuccess(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions testdata/send_auto_message_response_float.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"code": "ok",
"message_id": "174749423",
"message": "Successfully Sent",
"balance": 2745.6,
"user": "Seun Junior"
}

0 comments on commit 17daa9d

Please sign in to comment.