本文整理汇总了Golang中github.com/sendgrid/go-apid/clientfakes.NewFakeClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewFakeClient函数的具体用法?Golang NewFakeClient怎么用?Golang NewFakeClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewFakeClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestGetSignupPackageInfoValid
func TestGetSignupPackageInfoValid(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
// Apid return free package
fakeClient := clientfakes.NewFakeClient()
expectedResponse := client.PackageRecord{ID: 1, IsFree: 1, PackageGroupID: 1, Credits: 12000}
fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) {
return []client.PackageRecord{expectedResponse}, nil
})
adaptor := New(fakeClient)
freePackage := adaptor.GetSignupPackageInfo("abc")
assert.Equal(t, expectedResponse, freePackage)
// Apid return paid package, then our function will return 12k Free package
fakeClient = clientfakes.NewFakeClient()
fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) {
return []client.PackageRecord{client.PackageRecord{ID: 1, IsFree: 0, PackageGroupID: 1, Credits: 12000}}, nil
})
adaptor = New(fakeClient)
freePackage = adaptor.GetSignupPackageInfo("abc")
assert.Equal(t, client.PackageRecord{ID: FreePackageID, IsFree: 1, PackageGroupID: FreePackageGroupID, Credits: FreeAccountCreditsLimits}, freePackage)
}
示例2: TestSetUserPackageError
func TestSetUserPackageError(t *testing.T) {
userID := 180
packageID := 11
packageGroupID := 2
fakeClient := clientfakes.NewFakeClient()
expectedPackage := Package{ID: packageID, GroupID: packageGroupID}
fakeClient.RegisterFunction("getPackage", func(params url.Values) (interface{}, error) {
return expectedPackage, nil
})
fakeClient.RegisterFunction("delete", func(params url.Values) (interface{}, error) {
return 1, nil
})
fakeClient.RegisterFunction("add", func(params url.Values) (interface{}, error) {
return "", errors.New("some apid error with add to user package")
})
adaptor := New(fakeClient)
adaptorErr := adaptor.SetUserPackage(userID, packageID)
require.NotNil(t, adaptorErr, "expected error")
}
示例3: TestGetSubusers
func TestGetSubusers(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
expected := []client.Subuser{
{
ID: 1,
Username: "dustin",
Email: "[email protected]",
}, {
ID: 2,
Username: "jovel",
Email: "[email protected]",
},
}
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("getSubusers", func(params url.Values) (interface{}, error) {
return expected, nil
})
adaptor := New(fakeClient)
actual, adaptorError := adaptor.GetSubusers(&client.SubuserRequest{UserID: 180})
assert.Nil(t, adaptorError)
if assert.NotNil(t, actual) {
assert.Equal(t, len(expected), len(actual))
for i, _ := range actual {
assert.Equal(t, expected[i].ID, actual[i].ID)
assert.Equal(t, expected[i].Username, actual[i].Username)
assert.Equal(t, expected[i].Email, actual[i].Email)
}
}
}
示例4: TestAddUserPartnerErrWithSerializeString
func TestAddUserPartnerErrWithSerializeString(t *testing.T) {
expectedGetResult := make([]PartnerRecord, 1)
expectedGetResult[0] = PartnerRecord{ID: 1, Label: "partner name"}
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("add", func(param url.Values) (interface{}, error) {
return "success", nil
})
fakeClient.RegisterFunction("get", func(param url.Values) (interface{}, error) {
return expectedGetResult, nil
})
adaptor := New(fakeClient)
adaptorErr := adaptor.AddUserPartner(
client.Signup{
Username: "test_user",
Email: "[email protected]",
Password: "asdfasdf",
// bad serialized array
SendGridPartner: "czo0OiJhYWFhIjs=",
})
assert.NotNil(t, adaptorErr, "an error should have occured")
}
示例5: TestEditUser
func TestEditUser(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
userID := 23
var actualParams url.Values
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("editUser", func(params url.Values) (interface{}, error) {
actualParams = params
return 1, nil
})
adaptor := New(fakeClient)
request := &client.User{
ID: userID,
AccountOwnerID: 123,
Username: "jake_the_dog",
Email: "[email protected]",
}
actual, adaptorErr := adaptor.EditUser(request)
assert.NoError(t, adaptorErr)
assert.Equal(t, actualParams, url.Values{"userid": []string{strconv.Itoa(userID)}, "reseller_id": []string{"123"}, "username": []string{"jake_the_dog"}, "email": []string{"[email protected]"}})
assert.True(t, actual)
}
示例6: TestGetUserByUsernameSlaveFailMasterSuccess
func TestGetUserByUsernameSlaveFailMasterSuccess(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
username := "mrpickles"
bad := User{ID: 0}
expected := User{ID: 123, Username: username, AccountOwnerID: 4000, AccountID: "iamanaccountid"}
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("getUserInfo", func(params url.Values) (interface{}, error) {
return bad, nil
})
fakeClient.RegisterFunction("getUserInfoMaster", func(params url.Values) (interface{}, error) {
return expected, nil
})
adaptor := New(fakeClient)
actual, clientErr := adaptor.GetUserByUsername(username)
assert.Nil(t, clientErr)
require.NotNil(t, actual)
assert.Equal(t, expected.ID, actual.ID)
assert.Equal(t, expected.Username, actual.Username)
assert.Equal(t, expected.AccountOwnerID, actual.AccountOwnerID)
assert.Equal(t, expected.AccountID, actual.AccountID)
}
示例7: TestEditUserNoUpdate
func TestEditUserNoUpdate(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
// When a call is made to update a user, but there are no changes in the db
// (If we are editing to the same info the user already has in the db)
userID := 23
var actualParams url.Values
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("editUser", func(params url.Values) (interface{}, error) {
actualParams = params
return 0, nil
})
adaptor := New(fakeClient)
request := &client.User{
ID: userID,
AccountOwnerID: 123,
Username: "jake_the_dog",
Email: "[email protected]",
}
actual, adaptorErr := adaptor.EditUser(request)
assert.NoError(t, adaptorErr)
assert.Equal(t, actualParams, url.Values{"userid": []string{strconv.Itoa(userID)}, "reseller_id": []string{"123"}, "username": []string{"jake_the_dog"}, "email": []string{"[email protected]"}})
assert.True(t, actual)
}
示例8: TestAddUserPackageForSubuser
func TestAddUserPackageForSubuser(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
fakeClient := clientfakes.NewFakeClient()
addPackageSpy := fakeClient.RegisterFunction("add", func(param url.Values) (interface{}, error) {
return "success", nil
})
adaptor := New(fakeClient)
err := adaptor.AddUserPackage(
client.Signup{
UserID: 180,
ResellerID: 888,
UserPackageStatus: 444,
})
assert.Len(t, err, 0)
//verify subuser params are removed
params := addPackageSpy.CalledParams["values"][0]
exists := strings.Contains(params, "package_id")
assert.False(t, exists, "package id should be null")
exists = strings.Contains(params, "package_group_id")
assert.False(t, exists, "package group id should be null")
exists = strings.Contains(params, "package_status")
assert.False(t, exists, "package status should be null")
//verify subuser params is modified
re := regexp.MustCompile(`{"status":"(\d*)"}`)
matches := re.FindAllStringSubmatch(params, -1)
status := matches[0][1]
exists = strings.Contains(params, "status")
assert.True(t, exists, "status is passed through")
assert.Equal(t, "444", status, "status should be pass through") //default is 1 = SendGrid Free
}
示例9: TestGetUserHolds
func TestGetUserHolds(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
var userIDFromParams int
expected := UserHolds{
"10": "test",
"101": "test2",
}
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("getUserHolds", func(params url.Values) (interface{}, error) {
userIDFromParams, _ = strconv.Atoi(params.Get("userid"))
return expected, nil
})
adaptor := New(fakeClient)
actual, err := adaptor.GetUserHolds(23)
require.Nil(t, err)
require.NotNil(t, actual)
assert.Equal(t, actual, expected)
assert.Equal(t, userIDFromParams, 23)
}
示例10: TestAddFiltersInvalid
func TestAddFiltersInvalid(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
user := client.Signup{
Username: "username",
UserID: 1,
Password: "password",
Email: "[email protected]",
}
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("enableUserFilter", func(params url.Values) (interface{}, error) {
return 1, nil
})
expectedApidError := fmt.Errorf("apid internal server error")
fakeClient.RegisterFunction("addUserFilters", func(params url.Values) (interface{}, error) {
return 0, expectedApidError
})
adaptor := New(fakeClient)
errs := adaptor.AddFilters(user)
assert.Len(t, errs, 4)
assert.Equal(t, errs[0], expectedApidError)
assert.Equal(t, errs[1], expectedApidError)
assert.Equal(t, errs[2], expectedApidError)
assert.Equal(t, errs[3], expectedApidError)
}
示例11: TestEditUserProfileError
func TestEditUserProfileError(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
ln.SetOutput(ioutil.Discard, "test_logger")
someProfile := client.UserProfile{
UserID: 23,
Phone: "123-123-1234",
Website: "www.google.com",
FirstName: "Homer",
LastName: "Simpson",
Address1: "12345 Seasame St.",
City: "Springfield",
State: "IL",
Zip: "11252",
Country: "USA",
Company: "Simpsons",
}
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("editUserProfile", func(params url.Values) (interface{}, error) {
return 0, errors.New("some serious error happened")
})
adaptor := New(fakeClient)
success, err := adaptor.EditUserProfile(&someProfile)
require.NotNil(t, err)
assert.False(t, success)
}
示例12: TestUpdateURLMailDomainFailure
func TestUpdateURLMailDomainFailure(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
fakeClient := clientfakes.NewFakeClient()
var actualParams url.Values
err := errors.New("error")
fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) {
actualParams = params
return nil, err
})
adaptor := New(fakeClient)
testUserID := 1
testUrlDomain := fmt.Sprintf("u%d.ct.sendgrid.net", testUserID)
testMailDomain := "sendgrid.net"
updateErr := adaptor.UpdateURLMailDomain(testUserID)
assert.NotNil(t, updateErr)
assert.Equal(t, actualParams.Get("tableName"), "user")
assert.Equal(t, actualParams.Get("where"), fmt.Sprintf(`{"id" : "%d"}`, testUserID))
assert.Equal(t, actualParams.Get("values"), fmt.Sprintf(`[{"url_domain": "%s"},{"mail_domain": "%s"}]`, testUrlDomain, testMailDomain))
}
示例13: TestAddFiltersValid
func TestAddFiltersValid(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
user := client.Signup{
Username: "username",
UserID: 1,
Password: "password",
Email: "[email protected]",
}
// filter already enabled
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("enableUserFilter", func(params url.Values) (interface{}, error) {
return 1, nil
})
// no errors, but register one row updated
fakeClient.RegisterFunction("addUserFilters", func(params url.Values) (interface{}, error) {
return 1, nil
})
adaptor := New(fakeClient)
errs := adaptor.AddFilters(user)
assert.Len(t, errs, 0)
}
示例14: TestCreateUserError
func TestCreateUserError(t *testing.T) {
ln.SetOutput(ioutil.Discard, "test_logger")
expectedNewUserID := 1234
// value to be passed into apid
expectedUsername := "test_user"
// expected output from apidadaptor
expectedErr := adaptor.NewError("error when adding user")
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("addUser", func(params url.Values) (interface{}, error) {
username := params.Get("username")
if username == expectedUsername {
return 0, fmt.Errorf("some apid error")
}
return expectedNewUserID, nil
})
adaptor := New(fakeClient)
_, adaptorErr := adaptor.CreateUser(
client.Signup{
Username: expectedUsername,
Email: "[email protected]",
Password: "asdfasdf",
})
if assert.NotNil(t, adaptorErr, adaptorErr.Error()) {
assert.Equal(t, expectedErr.Error(), adaptorErr.Error())
assert.Equal(t, expectedErr.SuggestedStatusCode, adaptorErr.SuggestedStatusCode)
}
}
示例15: TestCreateUserPasswordValidationError
func TestCreateUserPasswordValidationError(t *testing.T) {
expectedNewUserID := 1234
// value to be passed into apid
expectedUsername := "test_user"
// expected output from apidadaptor (pass along password errors)
expectedApidErr := "your password sucks bro. because reasons."
expectedErr := adaptor.NewErrorWithStatus("password invalid - "+expectedApidErr, http.StatusBadRequest)
fakeClient := clientfakes.NewFakeClient()
fakeClient.RegisterFunction("addUser", func(params url.Values) (interface{}, error) {
username := params.Get("username")
if username == expectedUsername {
// apid returns a json hash with the key exists on duplicate entries
return 0, fmt.Errorf(expectedApidErr)
}
return expectedNewUserID, nil
})
adaptor := New(fakeClient)
_, adaptorErr := adaptor.CreateUser(
client.Signup{
Username: expectedUsername,
Email: "[email protected]",
Password: "asdfasdf",
})
if assert.NotNil(t, adaptorErr, adaptorErr.Error()) {
assert.Equal(t, expectedErr.Error(), adaptorErr.Error())
assert.Equal(t, expectedErr.SuggestedStatusCode, adaptorErr.SuggestedStatusCode)
}
}