From 011f98f442cc4ac8e32bc10af99a5ea02ecc5100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Santos?= Date: Fri, 19 Apr 2024 01:51:37 -0300 Subject: [PATCH] chore: update imports --- cmd/api/api.go | 46 +++++++++--------- cmd/api/api_test.go | 114 +++++++++++++++++++++++--------------------- 2 files changed, 84 insertions(+), 76 deletions(-) diff --git a/cmd/api/api.go b/cmd/api/api.go index 61a3e6b..552ab2a 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -2,12 +2,14 @@ package main import ( "fmt" - "github.com/dexfs/go-twitter-clone/internal/application/handlers" - app "github.com/dexfs/go-twitter-clone/internal/application/usecases" - "github.com/dexfs/go-twitter-clone/internal/domain" - "github.com/dexfs/go-twitter-clone/internal/domain/interfaces" "github.com/dexfs/go-twitter-clone/internal/infra/repository/inmemory" - "github.com/dexfs/go-twitter-clone/tests/mocks" + "github.com/dexfs/go-twitter-clone/internal/post" + post_handler "github.com/dexfs/go-twitter-clone/internal/post/handler" + post_usecase "github.com/dexfs/go-twitter-clone/internal/post/usecase" + "github.com/dexfs/go-twitter-clone/internal/user" + user_handler "github.com/dexfs/go-twitter-clone/internal/user/handler" + user_usecase "github.com/dexfs/go-twitter-clone/internal/user/usecase" + "github.com/dexfs/go-twitter-clone/mocks" "log" "net/http" "time" @@ -34,8 +36,8 @@ type APIServer struct { } type Gateway struct { - userRepo interfaces.UserRepository - postRepo interfaces.PostRepository + userRepo user.UserRepository + postRepo post.PostRepository } func NewAPIServer(addr string) *APIServer { @@ -65,26 +67,26 @@ func (s *APIServer) Run() error { func (s *APIServer) initGateways() *Gateway { dbMocks := mocks.GetTestMocks() - dbMocks.MockUserDB.Insert(&domain.User{ + dbMocks.MockUserDB.Insert(&user.User{ ID: "4cfe67a9-defc-42b9-8410-cb5086bec2f5", Username: "alucard", CreatedAt: time.Now(), UpdatedAt: time.Now(), }) - dbMocks.MockUserDB.Insert(&domain.User{ + dbMocks.MockUserDB.Insert(&user.User{ ID: "b8903f77-5d16-4176-890f-f597594ff952", Username: "alexander", CreatedAt: time.Now(), UpdatedAt: time.Now(), }) - dbMocks.MockUserDB.Insert(&domain.User{ + dbMocks.MockUserDB.Insert(&user.User{ ID: "75135a97-46be-405f-8948-0821290ca83e", Username: "seras_victoria", CreatedAt: time.Now(), UpdatedAt: time.Now(), }) - userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) - postRepo := inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + postRepo := repo_inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) return &Gateway{ userRepo: userRepo, @@ -94,31 +96,31 @@ func (s *APIServer) initGateways() *Gateway { func (s *APIServer) initUserRoutes(router *http.ServeMux, gateways *Gateway) { - getUserFeed, err := app.NewGetUserFeedUseCase(gateways.userRepo, gateways.postRepo) + getUserFeed, err := user_usecase.NewGetUserFeedUseCase(gateways.userRepo, gateways.postRepo) if err != nil { log.Fatal(err) } - getUserInfo, err := app.NewGetUserInfoUseCase(gateways.userRepo) + getUserInfo, err := user_usecase.NewGetUserInfoUseCase(gateways.userRepo) if err != nil { log.Fatal(err) } - router.HandleFunc("GET /users/{username}/info", handlers.NewGetUserInfoHandler(getUserInfo).Handle) - router.HandleFunc("GET /users/{username}/feed", handlers.NewGetFeedHandler(getUserFeed).Handle) + router.HandleFunc("GET /users/{username}/info", user_handler.NewGetUserInfoHandler(getUserInfo).Handle) + router.HandleFunc("GET /users/{username}/feed", user_handler.NewGetFeedHandler(getUserFeed).Handle) } func (s *APIServer) initPostRoutes(router *http.ServeMux, gateways *Gateway) { - createPostUseCase := app.NewCreatePostUseCase(gateways.userRepo, gateways.postRepo) - createPostHandler := handlers.NewCreatePostHandler(createPostUseCase) + createPostUseCase := post_usecase.NewCreatePostUseCase(gateways.userRepo, gateways.postRepo) + createPostHandler := post_handler.NewCreatePostHandler(createPostUseCase) router.HandleFunc(createPostHandler.Path, createPostHandler.Handle) - createQuotePostUseCase := app.NewCreateQuotePostUseCase(gateways.userRepo, gateways.postRepo) - crateQuotePostHandler := handlers.NewCreateQuoteHandler(createQuotePostUseCase) + createQuotePostUseCase := post_usecase.NewCreateQuotePostUseCase(gateways.userRepo, gateways.postRepo) + crateQuotePostHandler := post_handler.NewCreateQuoteHandler(createQuotePostUseCase) router.HandleFunc(crateQuotePostHandler.Path, crateQuotePostHandler.Handle) - createRepostUseCase := app.NewCreateRepostUseCase(gateways.userRepo, gateways.postRepo) - createRepostHandler := handlers.NewRepostHandler(createRepostUseCase) + createRepostUseCase := post_usecase.NewCreateRepostUseCase(gateways.userRepo, gateways.postRepo) + createRepostHandler := post_handler.NewRepostHandler(createRepostUseCase) router.HandleFunc(createRepostHandler.Path, createRepostHandler.Handle) } diff --git a/cmd/api/api_test.go b/cmd/api/api_test.go index eb46628..d30cdd0 100644 --- a/cmd/api/api_test.go +++ b/cmd/api/api_test.go @@ -4,11 +4,14 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/dexfs/go-twitter-clone/internal/application/handlers" - app "github.com/dexfs/go-twitter-clone/internal/application/usecases" - "github.com/dexfs/go-twitter-clone/internal/domain" "github.com/dexfs/go-twitter-clone/internal/infra/repository/inmemory" - "github.com/dexfs/go-twitter-clone/tests/mocks" + "github.com/dexfs/go-twitter-clone/internal/post" + post_handler "github.com/dexfs/go-twitter-clone/internal/post/handler" + post_usecase "github.com/dexfs/go-twitter-clone/internal/post/usecase" + "github.com/dexfs/go-twitter-clone/internal/user" + "github.com/dexfs/go-twitter-clone/internal/user/handler" + user_usecase "github.com/dexfs/go-twitter-clone/internal/user/usecase" + "github.com/dexfs/go-twitter-clone/mocks" "github.com/google/uuid" "io" "log" @@ -19,18 +22,18 @@ import ( "time" ) -// users +// user func TestUserInfoResource_WithNoFoundUser_ReturnsErrorMessage(t *testing.T) { server := http.NewServeMux() dbMocks := mocks.GetTestMocks() - userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) - getUserInfoUseCase, err := app.NewGetUserInfoUseCase(userRepo) + getUserInfoUseCase, err := user_usecase.NewGetUserInfoUseCase(userRepo) if err != nil { log.Fatal(err) } - server.HandleFunc("/users/{username}/info", handlers.NewGetUserInfoHandler(getUserInfoUseCase).Handle) + server.HandleFunc("/users/{username}/info", user_handler.NewGetUserInfoHandler(getUserInfoUseCase).Handle) request, _ := http.NewRequest("GET", "/users/not_found/info", nil) response := httptest.NewRecorder() @@ -53,19 +56,19 @@ func TestUserInfoResource(t *testing.T) { server := http.NewServeMux() dbMocks := mocks.GetTestMocks() - userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) - getUserInfoUseCase, err := app.NewGetUserInfoUseCase(userRepo) + getUserInfoUseCase, err := user_usecase.NewGetUserInfoUseCase(userRepo) if err != nil { log.Fatal(err) } - server.HandleFunc("/users/{username}/info", handlers.NewGetUserInfoHandler(getUserInfoUseCase).Handle) + server.HandleFunc("/users/{username}/info", user_handler.NewGetUserInfoHandler(getUserInfoUseCase).Handle) request, _ := http.NewRequest("GET", "/users/user0/info", nil) response := httptest.NewRecorder() server.ServeHTTP(response, request) - var got app.GetUserInfoOutput + var got user_usecase.GetUserInfoOutput if err := helperDecodeJSON(response.Body, &got); err != nil { fmt.Errorf("could not decode JSON: %v", err) @@ -76,24 +79,25 @@ func TestUserInfoResource(t *testing.T) { t.Errorf("got %q, want %q", got.Username, want) } } + func TestUserFeedResource(t *testing.T) { server := http.NewServeMux() dbMocks := mocks.GetTestMocks() - userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) - postRepo := inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + postRepo := repo_inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) - getUserFeedUseCase, err := app.NewGetUserFeedUseCase(userRepo, postRepo) + getUserFeedUseCase, err := user_usecase.NewGetUserFeedUseCase(userRepo, postRepo) if err != nil { log.Fatal(err) } - server.HandleFunc("/users/{username}/feed", handlers.NewGetFeedHandler(getUserFeedUseCase).Handle) + server.HandleFunc("/users/{username}/feed", user_handler.NewGetFeedHandler(getUserFeedUseCase).Handle) request, _ := http.NewRequest("GET", "/users/user0/feed", nil) response := httptest.NewRecorder() server.ServeHTTP(response, request) - var got app.GetUserFeedUseCaseOutput + var got user_usecase.GetUserFeedUseCaseOutput if err := helperDecodeJSON(response.Body, &got); err != nil { fmt.Errorf("could not decode JSON: %v", err) @@ -103,18 +107,19 @@ func TestUserFeedResource(t *testing.T) { 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) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + postRepo := repo_inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) - getUserFeedUseCase, err := app.NewGetUserFeedUseCase(userRepo, postRepo) + getUserFeedUseCase, err := user_usecase.NewGetUserFeedUseCase(userRepo, postRepo) if err != nil { log.Fatal(err) } - server.HandleFunc("/users/{username}/feed", handlers.NewGetFeedHandler(getUserFeedUseCase).Handle) + server.HandleFunc("/users/{username}/feed", user_handler.NewGetFeedHandler(getUserFeedUseCase).Handle) request, _ := http.NewRequest("GET", "/users/not_found/feed", nil) response := httptest.NewRecorder() @@ -134,15 +139,15 @@ func TestUserFeedResource_WithNoFoundUser_ReturnsErrorMessage(t *testing.T) { } } -// posts +// // post func TestCreatePostResource(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) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + postRepo := repo_inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) + createPostUseCase := post_usecase.NewCreatePostUseCase(userRepo, postRepo) + createPostHandler := post_handler.NewCreatePostHandler(createPostUseCase) server.HandleFunc(createPostHandler.Path, createPostHandler.Handle) userID := strconv.Quote(dbMocks.MockUserSeed[0].ID) @@ -152,7 +157,7 @@ func TestCreatePostResource(t *testing.T) { response := httptest.NewRecorder() server.ServeHTTP(response, request) - var got app.CreatePostOutput + var got post_usecase.CreatePostOutput if err := helperDecodeJSON(response.Body, &got); err != nil { fmt.Errorf("could not decode JSON: %v", err) @@ -162,24 +167,25 @@ 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{ + aInput := post.NewPostInput{ User: dbMocks.MockUserSeed[0], Content: "Content post" + strconv.Itoa(i), } - aPost, _ := domain.NewPost(aInput) + aPost, _ := post.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) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + postRepo := repo_inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) + createPostUseCase := post_usecase.NewCreatePostUseCase(userRepo, postRepo) + createPostHandler := post_handler.NewCreatePostHandler(createPostUseCase) server.HandleFunc(createPostHandler.Path, createPostHandler.Handle) userID := strconv.Quote(dbMocks.MockUserSeed[0].ID) @@ -196,7 +202,7 @@ func TestCreatePostResource_WithoutLimit_ReturnsError(t *testing.T) { if err := helperDecodeJSON(response.Body, &got); err != nil { log.Fatal(err) } - want := "you reached your posts day limit" + want := "you reached your post day limit" if got.Error != want { t.Errorf("got %s want %s", got.Error, want) @@ -208,10 +214,10 @@ func TestCreatePostResource_WithNotFoundUser_ReturnsError(t *testing.T) { dbMocks := mocks.GetTestMocks() - userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) - postRepo := inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) - createPostUseCase := app.NewCreatePostUseCase(userRepo, postRepo) - createPostHandler := handlers.NewCreatePostHandler(createPostUseCase) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + postRepo := repo_inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) + createPostUseCase := post_usecase.NewCreatePostUseCase(userRepo, postRepo) + createPostHandler := post_handler.NewCreatePostHandler(createPostUseCase) server.HandleFunc(createPostHandler.Path, createPostHandler.Handle) userID := strconv.Quote(uuid.NewString()) @@ -239,7 +245,7 @@ func TestCreateQuotePostResource(t *testing.T) { server := http.NewServeMux() dbMocks := mocks.GetTestMocks() - newUser := &domain.User{ + newUser := &user.User{ ID: "4cfe67a9-defc-42b9-8410-cb5086bec2f5", Username: "alucard", CreatedAt: time.Time{}, @@ -247,11 +253,11 @@ func TestCreateQuotePostResource(t *testing.T) { } dbMocks.MockUserDB.Insert(newUser) - userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) - postRepo := inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) - createQuotePostUseCase := app.NewCreateQuotePostUseCase(userRepo, postRepo) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + postRepo := repo_inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) + createQuotePostUseCase := post_usecase.NewCreateQuotePostUseCase(userRepo, postRepo) - createQuotePostHandler := handlers.NewCreateQuoteHandler(createQuotePostUseCase) + createQuotePostHandler := post_handler.NewCreateQuoteHandler(createQuotePostUseCase) server.HandleFunc(createQuotePostHandler.Path, createQuotePostHandler.Handle) userID := strconv.Quote(newUser.ID) @@ -261,7 +267,7 @@ func TestCreateQuotePostResource(t *testing.T) { response := httptest.NewRecorder() server.ServeHTTP(response, request) - var got app.CreateQuotePostUseCaseOutput + var got post_usecase.CreateQuotePostUseCaseOutput if err := helperDecodeJSON(response.Body, &got); err != nil { t.Fatalf("could not decode JSON: %v", err) @@ -276,11 +282,11 @@ 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) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + postRepo := repo_inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) + createQuotePostUseCase := post_usecase.NewCreateQuotePostUseCase(userRepo, postRepo) - createQuotePostHandler := handlers.NewCreateQuoteHandler(createQuotePostUseCase) + createQuotePostHandler := post_handler.NewCreateQuoteHandler(createQuotePostUseCase) server.HandleFunc(createQuotePostHandler.Path, createQuotePostHandler.Handle) userID := strconv.Quote(dbMocks.MockUserSeed[0].ID) @@ -307,7 +313,7 @@ func TestCreateRepostResource(t *testing.T) { server := http.NewServeMux() dbMocks := mocks.GetTestMocks() - newUser := &domain.User{ + newUser := &user.User{ ID: "4cfe67a9-defc-42b9-8410-cb5086bec2f5", Username: "alucard", CreatedAt: time.Time{}, @@ -315,11 +321,11 @@ func TestCreateRepostResource(t *testing.T) { } dbMocks.MockUserDB.Insert(newUser) - userRepo := inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) - postRepo := inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) - createRepostUseCase := app.NewCreateRepostUseCase(userRepo, postRepo) + userRepo := repo_inmemory.NewInMemoryUserRepo(dbMocks.MockUserDB) + postRepo := repo_inmemory.NewInMemoryPostRepo(dbMocks.MockPostDB) + createRepostUseCase := post_usecase.NewCreateRepostUseCase(userRepo, postRepo) - createRepostHandler := handlers.NewRepostHandler(createRepostUseCase) + createRepostHandler := post_handler.NewRepostHandler(createRepostUseCase) server.HandleFunc(createRepostHandler.Path, createRepostHandler.Handle) userID := strconv.Quote(newUser.ID) @@ -329,7 +335,7 @@ func TestCreateRepostResource(t *testing.T) { response := httptest.NewRecorder() server.ServeHTTP(response, request) - var got app.CreateRepostUseCaseOutput + var got post_usecase.CreateRepostUseCaseOutput if err := helperDecodeJSON(response.Body, &got); err != nil { t.Fatalf("could not decode JSON: %v", err)