-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests and chnage to upsert
- Loading branch information
1 parent
6387815
commit a6e5cf6
Showing
3 changed files
with
93 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,7 +173,7 @@ func main() { | |
} | ||
``` | ||
|
||
### Create a subscriber | ||
### Create/Upsert a subscriber | ||
|
||
```go | ||
package main | ||
|
@@ -192,14 +192,14 @@ func main() { | |
|
||
ctx := context.TODO() | ||
|
||
subscriber := &mailerlite.Subscriber{ | ||
subscriber := &mailerlite.UpsertSubscriber{ | ||
Email: "[email protected]", | ||
Fields: map[string]interface{}{ | ||
"city": "Vilnius", | ||
}, | ||
} | ||
|
||
newSubscriber, _, err := client.Subscriber.Create(ctx, subscriber) | ||
newSubscriber, _, err := client.Subscriber.Upsert(ctx, subscriber) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
@@ -227,14 +227,15 @@ func main() { | |
|
||
ctx := context.TODO() | ||
|
||
subscriber := &mailerlite.Subscriber{ | ||
subscriber := &mailerlite.UpdateSubscriber{ | ||
ID: "1", | ||
Email: "[email protected]", | ||
Fields: map[string]interface{}{ | ||
"company": "MailerLite", | ||
}, | ||
} | ||
|
||
newSubscriber, _, err := client.Subscriber.Create(ctx, subscriber) | ||
newSubscriber, _, err := client.Subscriber.Update(ctx, subscriber) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,7 +168,7 @@ func TestCanForgetSubscrber(t *testing.T) { | |
assert.Equal(t, res.StatusCode, http.StatusOK) | ||
} | ||
|
||
func TestCanCreateSubscrberWithGroup(t *testing.T) { | ||
func TestCanCreateSubscrberWithGroupDeprecated(t *testing.T) { | ||
client := mailerlite.NewClient(testKey) | ||
|
||
testClient := NewTestClient(func(req *http.Request) *http.Response { | ||
|
@@ -198,3 +198,66 @@ func TestCanCreateSubscrberWithGroup(t *testing.T) { | |
|
||
assert.Equal(t, res.StatusCode, http.StatusOK) | ||
} | ||
|
||
func TestCanUpsertSubscrberWithGroup(t *testing.T) { | ||
client := mailerlite.NewClient(testKey) | ||
|
||
testClient := NewTestClient(func(req *http.Request) *http.Response { | ||
assert.Equal(t, req.Method, http.MethodPost) | ||
assert.Equal(t, req.URL.String(), "https://connect.mailerlite.com/api/subscribers") | ||
b, _ := io.ReadAll(req.Body) | ||
assert.Equal(t, strings.TrimRight(string(b), "\r\n"), `{"email":"[email protected]","groups":["1234"]}`) | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: io.NopCloser(bytes.NewBufferString(`OK`)), | ||
} | ||
}) | ||
|
||
ctx := context.TODO() | ||
|
||
client.SetHttpClient(testClient) | ||
|
||
options := &mailerlite.UpsertSubscriber{ | ||
Email: "[email protected]", | ||
Groups: []string{"1234"}, | ||
} | ||
|
||
_, res, err := client.Subscriber.Upsert(ctx, options) | ||
if err != nil { | ||
return | ||
} | ||
|
||
assert.Equal(t, res.StatusCode, http.StatusOK) | ||
} | ||
|
||
func TestCanUpdateSubscrberWithGroup(t *testing.T) { | ||
client := mailerlite.NewClient(testKey) | ||
|
||
testClient := NewTestClient(func(req *http.Request) *http.Response { | ||
assert.Equal(t, req.Method, http.MethodPut) | ||
assert.Equal(t, req.URL.String(), "https://connect.mailerlite.com/api/subscribers/1") | ||
b, _ := io.ReadAll(req.Body) | ||
assert.Equal(t, strings.TrimRight(string(b), "\r\n"), `{"id":"1","email":"[email protected]","groups":["1234"]}`) | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: io.NopCloser(bytes.NewBufferString(`OK`)), | ||
} | ||
}) | ||
|
||
ctx := context.TODO() | ||
|
||
client.SetHttpClient(testClient) | ||
|
||
options := &mailerlite.UpdateSubscriber{ | ||
ID: "1", | ||
Email: "[email protected]", | ||
Groups: []string{"1234"}, | ||
} | ||
|
||
_, res, err := client.Subscriber.Update(ctx, options) | ||
if err != nil { | ||
return | ||
} | ||
|
||
assert.Equal(t, res.StatusCode, http.StatusOK) | ||
} |