Skip to content

Commit

Permalink
test: improvements and change coverage threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
dexfs committed Apr 14, 2024
1 parent 91ee689 commit f1a05e2
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .testcoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ local-prefix: "github.com/org/project"
threshold:
# (optional; default 0)
# The minimum coverage that each file should have
file: 70
file: 60

# (optional; default 0)
# The minimum coverage that each package should have
package: 80
package: 70

# (optional; default 0)
# The minimum total coverage project should have
total: 95
total: 70

# Holds regexp rules which will override thresholds for matched files or packages
# using their paths.
Expand Down
199 changes: 198 additions & 1 deletion cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/dexfs/go-twitter-clone/internal/infra/repository/inmemory"
"github.com/dexfs/go-twitter-clone/tests/mocks"
"github.com/google/uuid"
"io"
"log"
"net/http"
"net/http/httptest"
Expand All @@ -19,6 +20,35 @@ import (
)

// users
func TestUserInfoResource_WithNoFoundUser_ReturnsErrorMessage(t *testing.T) {
server := http.NewServeMux()

dbMocks := mocks.GetTestMocks()
userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB)

getUserInfoUseCase, err := app.NewGetUserInfoUseCase(userRepo)
if err != nil {
log.Fatal(err)
}
server.HandleFunc("/users/{username}/info", handlers.NewGetUserInfoHandler(getUserInfoUseCase).Handle)

request, _ := http.NewRequest("GET", "/users/not_found/info", nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)

var got struct {
Erro string `json:"error"`
}
if err := helperDecodeJSON(response.Body, &got); err != nil {
log.Fatal(err)
}

want := "user not found"

if got.Erro != want {
t.Errorf("got %q, want %q", got.Erro, want)
}
}
func TestUserInfoResource(t *testing.T) {
server := http.NewServeMux()

Expand Down Expand Up @@ -46,7 +76,63 @@ func TestUserInfoResource(t *testing.T) {
t.Errorf("got %q, want %q", got.Username, want)
}
}
func TestUserFeedResource(t *testing.T) {}
func TestUserFeedResource(t *testing.T) {
server := http.NewServeMux()

dbMocks := mocks.GetTestMocks()
userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB)
postRepo := inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB)

getUserFeedUseCase, err := app.NewGetUserFeedUseCase(userRepo, postRepo)
if err != nil {
log.Fatal(err)
}
server.HandleFunc("/users/{username}/feed", handlers.NewGetFeedHandler(getUserFeedUseCase).Handle)

request, _ := http.NewRequest("GET", "/users/user0/feed", nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)

var got app.GetUserFeedUseCaseOutput

if err := json.NewDecoder(response.Body).Decode(&got); err != nil {
fmt.Errorf("could not decode JSON: %v", err)
}

if len(got.Items) != 2 {
t.Errorf("got %q, want %q", len(got.Items), 2)
}
}
func TestUserFeedResource_WithNoFoundUser_ReturnsErrorMessage(t *testing.T) {
server := http.NewServeMux()

dbMocks := mocks.GetTestMocks()
userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB)
postRepo := inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB)

getUserFeedUseCase, err := app.NewGetUserFeedUseCase(userRepo, postRepo)
if err != nil {
log.Fatal(err)
}
server.HandleFunc("/users/{username}/feed", handlers.NewGetFeedHandler(getUserFeedUseCase).Handle)

request, _ := http.NewRequest("GET", "/users/not_found/feed", nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)

var got struct {
Error string `json:"error"`
}

if err := helperDecodeJSON(response.Body, &got); err != nil {
log.Fatal(err)
}

want := "user not found"
if got.Error != want {
t.Errorf("got %q, want %q", got.Error, want)
}
}

// posts
func TestCreatePostResource(t *testing.T) {
Expand Down Expand Up @@ -76,6 +162,79 @@ func TestCreatePostResource(t *testing.T) {
t.Errorf("got %q, want valid UUID", got.PostID)
}
}
func TestCreatePostResource_WithoutLimit_ReturnsError(t *testing.T) {
server := http.NewServeMux()

dbMocks := mocks.GetTestMocks()

for i := 0; i < 6; i++ {
aInput := domain.NewPostInput{
User: dbMocks.MockUserSeed[0],
Content: "Content post" + strconv.Itoa(i),
}
aPost, _ := domain.NewPost(aInput)
dbMocks.MockPostDB.Insert(aPost)
}

userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB)
postRepo := inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB)
createPostUseCase := app.NewCreatePostUseCase(userRepo, postRepo)
createPostHandler := handlers.NewCreatePostHandler(createPostUseCase)
server.HandleFunc(createPostHandler.Path, createPostHandler.Handle)

userID := strconv.Quote(dbMocks.MockUserSeed[0].ID)
jsonStr := `{"user_id": ` + userID + `, "content": "test content"}`

request, _ := http.NewRequest("POST", "/posts", bytes.NewBufferString(jsonStr))
response := httptest.NewRecorder()
server.ServeHTTP(response, request)

var got struct {
Error string `json:"error"`
}

if err := helperDecodeJSON(response.Body, &got); err != nil {
log.Fatal(err)
}
want := "you reached your posts day limit"

if got.Error != want {
t.Errorf("got %s want %s", got.Error, want)
}
}

func TestCreatePostResource_WithNotFoundUser_ReturnsError(t *testing.T) {
server := http.NewServeMux()

dbMocks := mocks.GetTestMocks()

userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB)
postRepo := inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB)
createPostUseCase := app.NewCreatePostUseCase(userRepo, postRepo)
createPostHandler := handlers.NewCreatePostHandler(createPostUseCase)
server.HandleFunc(createPostHandler.Path, createPostHandler.Handle)

userID := strconv.Quote(uuid.NewString())
jsonStr := `{"user_id": ` + userID + `, "content": "test content"}`

request, _ := http.NewRequest("POST", "/posts", bytes.NewBufferString(jsonStr))
response := httptest.NewRecorder()
server.ServeHTTP(response, request)

var got struct {
Error string `json:"error"`
}

if err := helperDecodeJSON(response.Body, &got); err != nil {
log.Fatal(err)
}

want := "user not found"
if got.Error != want {
t.Errorf("got %s want %s", got.Error, want)
}
}

func TestCreateQuotePostResource(t *testing.T) {
server := http.NewServeMux()

Expand Down Expand Up @@ -111,7 +270,38 @@ func TestCreateQuotePostResource(t *testing.T) {
if err := uuid.Validate(got.PostID); err != nil {
t.Errorf("got %q, want valid UUID", got.PostID)
}
}

func TestCreateQuotePostResource_WithTheOriginalUser_ReturnsError(t *testing.T) {
server := http.NewServeMux()

dbMocks := mocks.GetTestMocks()
userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB)
postRepo := inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB)
createQuotePostUseCase := app.NewCreateQuotePostUseCase(userRepo, postRepo)

createQuotePostHandler := handlers.NewCreateQuoteHandler(createQuotePostUseCase)
server.HandleFunc(createQuotePostHandler.Path, createQuotePostHandler.Handle)

userID := strconv.Quote(dbMocks.MockUserSeed[0].ID)
postID := strconv.Quote(dbMocks.MockPostsSeed[0].ID)
jsonStr := `{"user_id": ` + userID + `, "post_id":` + postID + `, "quote": "quote post content"}`
request, _ := http.NewRequest("POST", "/posts/quote", bytes.NewBufferString(jsonStr))
response := httptest.NewRecorder()
server.ServeHTTP(response, request)

var got struct {
Error string `json:"error"`
}

if err := helperDecodeJSON(response.Body, &got); err != nil {
t.Fatal(err)
}

want := "it is not possible repost your own post"
if got.Error != want {
t.Errorf("got %s, want %s", got.Error, want)
}
}
func TestCreateRepostResource(t *testing.T) {
server := http.NewServeMux()
Expand Down Expand Up @@ -149,3 +339,10 @@ func TestCreateRepostResource(t *testing.T) {
t.Errorf("got %q, want valid UUID", got.PostID)
}
}

func helperDecodeJSON(body io.Reader, v interface{}) error {
if err := json.NewDecoder(body).Decode(v); err != nil {
return fmt.Errorf("could not decode JSON: %v", err)
}
return nil
}

0 comments on commit f1a05e2

Please sign in to comment.