openapi-generator/samples/client/petstore/go/user_api_test.go

162 lines
4.1 KiB
Go
Raw Normal View History

2016-04-29 22:59:21 +00:00
package main
import (
2016-04-29 23:02:41 +00:00
sw "./go-petstore"
"github.com/stretchr/testify/assert"
"testing"
2016-04-29 22:59:21 +00:00
)
func TestCreateUser(t *testing.T) {
2016-04-29 23:02:41 +00:00
s := sw.NewUserApi()
newUser := sw.User{
Id: 1000,
FirstName: "gopher",
LastName : "lang",
Username : "gopher",
Password : "lang",
Email : "lang@test.com",
Phone : "5101112222",
UserStatus: 1}
apiResponse, err := s.CreateUser(newUser)
if err != nil {
t.Errorf("Error while adding user")
t.Log(err)
}
if apiResponse.Response.StatusCode != 200 {
t.Log(apiResponse.Response)
}
2016-04-29 22:59:21 +00:00
}
2016-04-29 23:17:18 +00:00
//adding x to skip the test, currently it is failing
func TestCreateUsersWithArrayInput(t *testing.T) {
2016-04-29 23:17:18 +00:00
s := sw.NewUserApi()
newUsers := []sw.User{
sw.User {
Id: int64(1001),
FirstName: "gopher1",
LastName : "lang1",
Username : "gopher1",
Password : "lang1",
Email : "lang1@test.com",
Phone : "5101112222",
UserStatus: int32(1),
},
sw.User {
Id: int64(1002),
FirstName: "gopher2",
LastName : "lang2",
Username : "gopher2",
Password : "lang2",
Email : "lang2@test.com",
Phone : "5101112222",
UserStatus: int32(1),
},
}
2016-04-29 23:02:41 +00:00
2016-04-29 23:17:18 +00:00
apiResponse, err := s.CreateUsersWithArrayInput(newUsers)
2016-04-29 23:02:41 +00:00
2016-04-29 23:17:18 +00:00
if err != nil {
t.Errorf("Error while adding users")
t.Log(err)
}
if apiResponse.Response.StatusCode != 200 {
t.Log(apiResponse.Response)
}
2016-04-29 23:02:41 +00:00
2016-04-29 23:17:18 +00:00
//tear down
_, err1 := s.DeleteUser("gopher1")
if(err1 != nil){
t.Errorf("Error while deleting user")
t.Log(err1)
}
2016-04-29 23:02:41 +00:00
2016-04-29 23:17:18 +00:00
_, err2 := s.DeleteUser("gopher2")
if(err2 != nil){
t.Errorf("Error while deleting user")
t.Log(err2)
}
}
2016-04-29 22:59:21 +00:00
func TestGetUserByName(t *testing.T) {
2016-04-29 23:02:41 +00:00
assert := assert.New(t)
s := sw.NewUserApi()
resp, apiResponse, err := s.GetUserByName("gopher")
if err != nil {
2016-04-29 23:10:10 +00:00
t.Errorf("Error while getting user by id")
2016-04-29 23:02:41 +00:00
t.Log(err)
} else {
assert.Equal(resp.Id, int64(1000), "User id should be equal")
assert.Equal(resp.Username, "gopher", "User name should be gopher")
assert.Equal(resp.LastName, "lang", "Last name should be lang")
//t.Log(resp)
}
if apiResponse.Response.StatusCode != 200 {
t.Log(apiResponse.Response)
}
2016-04-29 22:59:21 +00:00
}
func TestGetUserByNameWithInvalidID(t *testing.T) {
2016-04-29 23:02:41 +00:00
s := sw.NewUserApi()
resp, apiResponse, err := s.GetUserByName("999999999")
if err != nil {
2016-04-29 23:10:10 +00:00
t.Errorf("Error while getting user by invalid id")
2016-04-29 23:02:41 +00:00
t.Log(err)
t.Log(apiResponse)
} else {
t.Log(resp)
}
if apiResponse.Response.StatusCode != 200 {
t.Log(apiResponse.Response)
}
2016-04-29 22:59:21 +00:00
}
func TestUpdateUser(t *testing.T) {
2016-04-29 23:02:41 +00:00
assert := assert.New(t)
s := sw.NewUserApi()
newUser := sw.User{
Id: 1000,
FirstName: "gopher20",
LastName : "lang20",
Username : "gopher",
Password : "lang",
Email : "lang@test.com",
Phone : "5101112222",
UserStatus: 1}
apiResponse, err := s.UpdateUser("gopher", newUser)
if err != nil {
2016-04-29 23:10:10 +00:00
t.Errorf("Error while deleting user by id")
2016-04-29 23:02:41 +00:00
t.Log(err)
}
if apiResponse.Response.StatusCode != 200 {
t.Log(apiResponse.Response)
}
//verify changings are correct
resp, apiResponse, err := s.GetUserByName("gopher")
if err != nil {
2016-04-29 23:10:10 +00:00
t.Errorf("Error while getting user by id")
2016-04-29 23:02:41 +00:00
t.Log(err)
} else {
assert.Equal(resp.Id, int64(1000), "User id should be equal")
assert.Equal(resp.FirstName, "gopher20", "User name should be gopher")
assert.Equal(resp.Password, "lang", "User name should be the same")
}
2016-04-29 22:59:21 +00:00
}
func TestDeleteUser(t *testing.T) {
2016-04-29 23:02:41 +00:00
s := sw.NewUserApi()
apiResponse, err := s.DeleteUser("gopher")
if err != nil {
t.Errorf("Error while deleting user")
t.Log(err)
}
if apiResponse.Response.StatusCode != 200 {
t.Log(apiResponse.Response)
}
2016-04-29 22:59:21 +00:00
}