Skip to content

Commit

Permalink
chore: update imports
Browse files Browse the repository at this point in the history
  • Loading branch information
dexfs committed Apr 19, 2024
1 parent a6e0775 commit 011f98f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 76 deletions.
46 changes: 24 additions & 22 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}

Expand Down
114 changes: 60 additions & 54 deletions cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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())
Expand Down Expand Up @@ -239,19 +245,19 @@ 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{},
UpdatedAt: time.Time{},
}

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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -307,19 +313,19 @@ 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{},
UpdatedAt: time.Time{},
}

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)
Expand All @@ -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)
Expand Down

0 comments on commit 011f98f

Please sign in to comment.