Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create mocks helper #420

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion base_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func API(request rest.Request) (*rest.Response, error) {

// MakeRequest attempts a Twilio SendGrid request synchronously.
func MakeRequest(request rest.Request) (*rest.Response, error) {
if mock.Get() != nil {

if mock.IsMocked() && mock.Get() != nil {
return mock.Request()
}

Expand Down
19 changes: 17 additions & 2 deletions examples/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func main() {
// Send Mail
sendMail()

// start mocks server
mock.StartTestServer()

// add mock value
mock.Add(&mock.Mock{
StatusCode: 400,
Expand All @@ -32,26 +35,38 @@ func main() {
mock.Add(&mock.Mock{
Err: errors.New("Has been an mock error"),
})

// stop mocks server
mock.StopTestServer()

sendMail() // Send Mail

// start mocks server
mock.StartTestServer()

sendMail() // Response with mock data

// clear mock
mock.Flush()
sendMail() // Send Mail

// stop mocks server
mock.StopTestServer()

}

func sendMail() {
m := mail.NewV3Mail()
content := mail.NewContent("text/html", "<h1>Hello world</h1>This is an example")

from := mail.NewEmail("envalidMail", "invalidMail@mail.com")
from := mail.NewEmail("Nahuel", "ncostamagna@hotmail.com.ar")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert to the example name and email.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @thinkingserious,
the example of how to get started mocks is in line 19 for start and line 42 for end

	mock.StartTestServer() // start mock server
	
	mock.StopTestServer() // end mock server

Do I write documentation in a markdown file?

Thanks you!
Merry christmas and happy new year!

m.SetFrom(from)

m.AddContent(content)

personalization := mail.NewPersonalization()

personalization.AddTos(mail.NewEmail("Nahuel", "nlcostamagna"))
personalization.AddTos(mail.NewEmail("Nahuel", "nlcostamagna@gmail.com"))
personalization.Subject = "Example"

m.AddPersonalizations(personalization)
Expand Down
20 changes: 19 additions & 1 deletion helpers/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ type Mock struct {
Err error
}

var mock *Mock
var (
mock *Mock
isMocked bool
)

// Add - add mock method
func Add(m *Mock) {
Expand All @@ -26,6 +29,21 @@ func Get() *Mock {
return mock
}

// StartTestServer - start mock server
func StartTestServer() {
isMocked = true
}

// StopTestServer - start mock server
func StopTestServer() {
isMocked = false
}

// IsMocked - return true if the mocks server was started
func IsMocked() bool {
return isMocked
}

// Request - return mock sengrid response and error
func Request() (*rest.Response, error) {

Expand Down
18 changes: 17 additions & 1 deletion sendgrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3773,6 +3773,8 @@ func sendMailExample() (code int, body string, err error) {
}

func TestMockStatusCode400(t *testing.T) {
mock.StartTestServer()

mock.Add(&mock.Mock{
StatusCode: 400,
Body: `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`,
Expand All @@ -3787,9 +3789,13 @@ func TestMockStatusCode400(t *testing.T) {
assert.EqualValues(t, m.Body, `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`)
assert.Nil(t, err)
assert.Nil(t, m.Err)

mock.StopTestServer()
}

func TestMockStatusCode202(t *testing.T) {
mock.StartTestServer()

mock.Add(&mock.Mock{
StatusCode: 202,
})
Expand All @@ -3803,9 +3809,13 @@ func TestMockStatusCode202(t *testing.T) {
assert.EqualValues(t, m.Body, "")
assert.Nil(t, err)
assert.Nil(t, m.Err)

mock.StopTestServer()
}

func TestMockErr(t *testing.T) {
mock.StartTestServer()

mock.Add(&mock.Mock{
Err: errors.New("Has been an error in TestMockErr"),
})
Expand All @@ -3819,9 +3829,13 @@ func TestMockErr(t *testing.T) {
assert.EqualValues(t, m.Body, "")
assert.EqualValues(t, code, 0)
assert.EqualValues(t, m.StatusCode, 0)

mock.StopTestServer()
}

func TestMockFlush(t *testing.T) {
mock.StartTestServer()

mock.Add(&mock.Mock{
StatusCode: 202,
})
Expand All @@ -3832,7 +3846,9 @@ func TestMockFlush(t *testing.T) {
code, _, err := sendMailExample()

assert.EqualValues(t, m1.StatusCode, 202)
assert.EqualValues(t, code, 400)
assert.EqualValues(t, code, 401)
assert.Nil(t, m2)
assert.Nil(t, err)

mock.StopTestServer()
}