From fd9cf68710400e15a5340321fda931749a8162e7 Mon Sep 17 00:00:00 2001 From: Marco Troisi Date: Fri, 31 Mar 2017 12:15:28 +0100 Subject: [PATCH 1/2] USM-33 Updated request and response; Added tests for endpoint --- endpoint_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ request.go | 4 ---- 2 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 endpoint_test.go diff --git a/endpoint_test.go b/endpoint_test.go new file mode 100644 index 0000000..bfdcb7f --- /dev/null +++ b/endpoint_test.go @@ -0,0 +1,57 @@ +package main + +import ( + "testing" + "github.com/microlib/usermanager/lib" + "errors" + "context" + "fmt" + "encoding/json" + "reflect" +) + +type FkUserManagerService struct{} + +func (u *FkUserManagerService) FindUser(id string) (usermanager.UserInterface, error) { + if id == "1" { + return usermanager.NewUser("1", "Karl", "karl@email.com", "mypassword"), nil + } + return &usermanager.User{}, errors.New("User not found") +} + +func TestMakeFindUserEndpoint(t *testing.T) { + testCases := []struct { + findUserReq findUserRequest + expected findUserResponse + }{ + { + findUserRequest{Id:"1"}, + findUserResponse{ + Id:"1", + Name: "Karl", + Email: "karl@email.com", + Err: nil, + }, + }, + { + findUserRequest{Id:"2"}, + findUserResponse{ + Id:"", + Name: "", + Email: "", + Err: errors.New("User not found"), + }, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("User with id '%s'", tc.findUserReq.Id), func(t *testing.T) { + res, _ := makeFindUserEndpoint(&FkUserManagerService{})(context.Background(), tc.findUserReq) + res, _ = json.Marshal(res) + exp, _ := json.Marshal(tc.expected) + if !reflect.DeepEqual(res, exp) { + t.Errorf("got %s; want %s", res, exp) + } + }) + } +} diff --git a/request.go b/request.go index d6584bc..40e4da7 100644 --- a/request.go +++ b/request.go @@ -2,16 +2,12 @@ package main type findUserRequest struct { Id string `json:"id"` - Name string `json:"name"` - Email string `json:"email"` - Password string `json:"password"` } type findUserResponse struct { Id string `json:"id"` Name string `json:"name"` Email string `json:"email"` - Password string `json:"password"` Err error `json:"err,omitempty"` } From 8428596562fa8a3c09e01f68d182b2b9e2130931 Mon Sep 17 00:00:00 2001 From: Marco Troisi Date: Fri, 31 Mar 2017 12:26:12 +0100 Subject: [PATCH 2/2] USM-33 Improved tests for endpoint --- endpoint_test.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/endpoint_test.go b/endpoint_test.go index bfdcb7f..24f7568 100644 --- a/endpoint_test.go +++ b/endpoint_test.go @@ -6,7 +6,6 @@ import ( "errors" "context" "fmt" - "encoding/json" "reflect" ) @@ -47,10 +46,8 @@ func TestMakeFindUserEndpoint(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("User with id '%s'", tc.findUserReq.Id), func(t *testing.T) { res, _ := makeFindUserEndpoint(&FkUserManagerService{})(context.Background(), tc.findUserReq) - res, _ = json.Marshal(res) - exp, _ := json.Marshal(tc.expected) - if !reflect.DeepEqual(res, exp) { - t.Errorf("got %s; want %s", res, exp) + if !reflect.DeepEqual(res, tc.expected) { + t.Errorf("got %s; want %s", res, tc.expected) } }) }