-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
where we get the index page and check it contains a particular post in the mock database. To complete this, I had to mock the database by turning the previous declaration into an interface, and moving the concrete types into SqlDatabase. The mock is done in DatabaseMock.
- Loading branch information
1 parent
31a88c9
commit 2b8b006
Showing
11 changed files
with
130 additions
and
63 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
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
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/matheusgomes28/urchin/app" | ||
"github.com/matheusgomes28/urchin/common" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type DatabaseMock struct {} | ||
|
||
func (db DatabaseMock) GetPosts() ([]common.Post, error) { | ||
return []common.Post{ | ||
{ | ||
Title: "TestPost", | ||
Content: "TestContent", | ||
Excerpt: "TestExcerpt", | ||
Id: 0, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (db DatabaseMock) GetPost(post_id int) (common.Post, error) { | ||
return common.Post{}, fmt.Errorf("not implemented") | ||
} | ||
|
||
func (db DatabaseMock) AddPost(title string, excerpt string, content string) (int, error) { | ||
return 0, fmt.Errorf("not implemented") | ||
} | ||
|
||
func (db DatabaseMock) ChangePost(id int, title string, excerpt string, content string) error { | ||
return nil | ||
} | ||
|
||
func (db DatabaseMock) DeletePost(id int) error { | ||
return fmt.Errorf("not implemented") | ||
} | ||
|
||
func TestIndexPing(t *testing.T) { | ||
app_settings := common.AppSettings{ | ||
DatabaseAddress: "localhost", | ||
DatabasePort: 3006, | ||
DatabaseUser: "root", | ||
DatabasePassword: "root", | ||
DatabaseName: "urchin", | ||
WebserverPort: 8080, | ||
} | ||
|
||
database_mock := DatabaseMock{} | ||
|
||
r := app.SetupRoutes(app_settings, database_mock) | ||
w := httptest.NewRecorder() | ||
req, _ := http.NewRequest("GET", "/", nil) | ||
r.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, 200, w.Code) | ||
assert.Contains(t, w.Body.String(), "TestPost") | ||
assert.Contains(t, w.Body.String(), "TestExcerpt") | ||
} |
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
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
Oops, something went wrong.