From 1f39a4d3a91e0c3892985f2396b2d3c8b85bca3b Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Fri, 8 Apr 2016 14:04:49 -0700 Subject: [PATCH 1/3] issue #1948, added testing code to test go api --- samples/client/petstore/go/petStore_test.go | 47 +++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 samples/client/petstore/go/petStore_test.go diff --git a/samples/client/petstore/go/petStore_test.go b/samples/client/petstore/go/petStore_test.go new file mode 100644 index 00000000000..63afc57c7e9 --- /dev/null +++ b/samples/client/petstore/go/petStore_test.go @@ -0,0 +1,47 @@ +package main + +import ( + "testing" + + sw "./swagger" +) + +func TestAddPet(t *testing.T) { + t.Log("Testing TestAddPet...") + s := sw.NewPetApi() + newPet := (sw.Pet{Id: 12830, Name: "gopher", + PhotoUrls: []string{"http://1.com", "http://2.com"}, Status: "pending"}) + + err := s.AddPet(newPet) + + if err != nil { + t.Errorf("Error while adding pet") + t.Log(err) + } +} + +func TestGetPetById(t *testing.T) { + t.Log("Testing TestGetPetById...") + + s := sw.NewPetApi() + resp, err := s.GetPetById(12830) + if err != nil { + t.Errorf("Error while getting pet by id") + t.Log(err) + } else { + t.Log(resp) + } +} + +func TestUpdatePetWithForm(t *testing.T) { + t.Log("Testing UpdatePetWithForm...") + + s := sw.NewPetApi() + + err := s.UpdatePetWithForm("12830", "golang", "available") + + if err != nil { + t.Errorf("Error while updating pet by id") + t.Log(err) + } +} From 33491a7a8f1091ae5408e03453fdbec7d9cdbd13 Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Sat, 9 Apr 2016 21:40:47 -0700 Subject: [PATCH 2/3] added testify assert --- samples/client/petstore/go/petStore_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/samples/client/petstore/go/petStore_test.go b/samples/client/petstore/go/petStore_test.go index 63afc57c7e9..0a63fd12c10 100644 --- a/samples/client/petstore/go/petStore_test.go +++ b/samples/client/petstore/go/petStore_test.go @@ -2,7 +2,7 @@ package main import ( "testing" - + "github.com/stretchr/testify/assert" sw "./swagger" ) @@ -21,6 +21,7 @@ func TestAddPet(t *testing.T) { } func TestGetPetById(t *testing.T) { + assert := assert.New(t) t.Log("Testing TestGetPetById...") s := sw.NewPetApi() @@ -29,7 +30,11 @@ func TestGetPetById(t *testing.T) { t.Errorf("Error while getting pet by id") t.Log(err) } else { - t.Log(resp) + assert.Equal(resp.Id, 12830, "Pet id should be equal") + assert.Equal(resp.Name, "gopher", "Pet name should be gopher") + assert.Equal(resp.Status, "pending", "Pet status should be pending") + + t.Log(resp) } } @@ -37,7 +42,6 @@ func TestUpdatePetWithForm(t *testing.T) { t.Log("Testing UpdatePetWithForm...") s := sw.NewPetApi() - err := s.UpdatePetWithForm("12830", "golang", "available") if err != nil { From b16439e17b966bf59db551abe5e18538a72033a0 Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Sat, 9 Apr 2016 21:43:46 -0700 Subject: [PATCH 3/3] formatted code --- samples/client/petstore/go/petStore_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/samples/client/petstore/go/petStore_test.go b/samples/client/petstore/go/petStore_test.go index 0a63fd12c10..e3df602c3b8 100644 --- a/samples/client/petstore/go/petStore_test.go +++ b/samples/client/petstore/go/petStore_test.go @@ -2,8 +2,9 @@ package main import ( "testing" - "github.com/stretchr/testify/assert" + sw "./swagger" + "github.com/stretchr/testify/assert" ) func TestAddPet(t *testing.T) { @@ -21,7 +22,7 @@ func TestAddPet(t *testing.T) { } func TestGetPetById(t *testing.T) { - assert := assert.New(t) + assert := assert.New(t) t.Log("Testing TestGetPetById...") s := sw.NewPetApi() @@ -33,8 +34,8 @@ func TestGetPetById(t *testing.T) { assert.Equal(resp.Id, 12830, "Pet id should be equal") assert.Equal(resp.Name, "gopher", "Pet name should be gopher") assert.Equal(resp.Status, "pending", "Pet status should be pending") - - t.Log(resp) + + t.Log(resp) } } @@ -47,5 +48,5 @@ func TestUpdatePetWithForm(t *testing.T) { if err != nil { t.Errorf("Error while updating pet by id") t.Log(err) - } + } }