-
-
Notifications
You must be signed in to change notification settings - Fork 0
[refactor] Remove useless interfaces + tests improvements #71
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
Merged
gocanto
merged 16 commits into
main
from
hk1dre-codex/remove-categoriesrepo-and-postsrepo-interfaces
Aug 6, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
eaaf18d
refactor: remove repo interfaces
gocanto a2e85dd
refactor: simplify categories handler
gocanto 0f019d1
refactor: inject categories repo struct
gocanto 74d0a4b
refactor: inject posts repository
gocanto f5ae0d1
refactor: inject posts repository pointer
gocanto 5e06491
test: split handlers and payload tests
gocanto 01c9109
test: assert file handler payloads
gocanto 0c43cf3
test: add happy path coverage
gocanto e5b66f4
test: run posts handler against postgres container
gocanto 404b2a1
test: add categories postgres test helpers
gocanto a5b183d
test: add payload response coverage
gocanto 38a1635
test: fix UUIDs in handler tests
gocanto 8370b5a
test: export test helpers
gocanto 31fdd3a
chore: rename handler test package
gocanto 097210a
test: add shared helpers
gocanto 59b2783
test: let db assign user id
gocanto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,53 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/oullin/database" | ||
| "github.com/oullin/database/repository" | ||
| "github.com/oullin/database/repository/pagination" | ||
| "github.com/oullin/handler/payload" | ||
| handlertests "github.com/oullin/handler/tests" | ||
| ) | ||
|
|
||
| func TestCategoriesHandlerIndex_Success(t *testing.T) { | ||
| conn, author := handlertests.MakeTestDB(t) | ||
| published := time.Now() | ||
| post := database.Post{UUID: uuid.NewString(), AuthorID: author.ID, Slug: "hello", Title: "Hello", Excerpt: "Ex", Content: "Body", PublishedAt: &published} | ||
| if err := conn.Sql().Create(&post).Error; err != nil { | ||
| t.Fatalf("create post: %v", err) | ||
| } | ||
| cat := database.Category{UUID: uuid.NewString(), Name: "Cat", Slug: "cat", Description: "desc"} | ||
| if err := conn.Sql().Create(&cat).Error; err != nil { | ||
| t.Fatalf("create category: %v", err) | ||
| } | ||
| link := database.PostCategory{PostID: post.ID, CategoryID: cat.ID} | ||
| if err := conn.Sql().Create(&link).Error; err != nil { | ||
| t.Fatalf("create link: %v", err) | ||
| } | ||
|
|
||
| h := MakeCategoriesHandler(&repository.Categories{DB: conn}) | ||
|
|
||
| req := httptest.NewRequest("GET", "/categories", nil) | ||
| rec := httptest.NewRecorder() | ||
|
|
||
| if err := h.Index(rec, req); err != nil { | ||
| t.Fatalf("index err: %v", err) | ||
| } | ||
| if rec.Code != http.StatusOK { | ||
| t.Fatalf("status %d", rec.Code) | ||
| } | ||
|
|
||
| var resp pagination.Pagination[payload.CategoryResponse] | ||
| if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
| if len(resp.Data) != 1 || resp.Data[0].Slug != "cat" { | ||
| t.Fatalf("unexpected data: %+v", resp.Data) | ||
| } | ||
| } |
This file contains hidden or 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,12 @@ | ||
| package handler | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestEducationHandler(t *testing.T) { | ||
| runFileHandlerTest(t, fileHandlerTestCase{ | ||
| make: func(f string) fileHandler { return MakeEducationHandler(f) }, | ||
| endpoint: "/education", | ||
| data: []map[string]string{{"uuid": "1"}}, | ||
| assert: assertArrayUUID1, | ||
| }) | ||
| } |
This file contains hidden or 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,12 @@ | ||
| package handler | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestExperienceHandler(t *testing.T) { | ||
| runFileHandlerTest(t, fileHandlerTestCase{ | ||
| make: func(f string) fileHandler { return MakeExperienceHandler(f) }, | ||
| endpoint: "/experience", | ||
| data: []map[string]string{{"uuid": "1"}}, | ||
| assert: assertArrayUUID1, | ||
| }) | ||
| } |
This file contains hidden or 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,87 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| baseHttp "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "testing" | ||
|
|
||
| handlertests "github.com/oullin/handler/tests" | ||
| pkgHttp "github.com/oullin/pkg/http" | ||
| ) | ||
|
|
||
| type fileHandler interface { | ||
| Handle(baseHttp.ResponseWriter, *baseHttp.Request) *pkgHttp.ApiError | ||
| } | ||
|
|
||
| type fileHandlerTestCase struct { | ||
| make func(string) fileHandler | ||
| endpoint string | ||
| data interface{} | ||
| assert func(*testing.T, any) | ||
| } | ||
|
|
||
| func runFileHandlerTest(t *testing.T, tc fileHandlerTestCase) { | ||
| file := handlertests.WriteJSON(t, handlertests.TestEnvelope{Version: "v1", Data: tc.data}) | ||
| defer os.Remove(file) | ||
|
|
||
| h := tc.make(file) | ||
| req := httptest.NewRequest("GET", tc.endpoint, nil) | ||
| rec := httptest.NewRecorder() | ||
| if err := h.Handle(rec, req); err != nil { | ||
| t.Fatalf("err: %v", err) | ||
| } | ||
| if rec.Code != baseHttp.StatusOK { | ||
| t.Fatalf("status %d", rec.Code) | ||
| } | ||
|
|
||
| var resp handlertests.TestEnvelope | ||
| if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
| if resp.Version != "v1" { | ||
| t.Fatalf("version %s", resp.Version) | ||
| } | ||
| tc.assert(t, resp.Data) | ||
|
|
||
| req2 := httptest.NewRequest("GET", tc.endpoint, nil) | ||
| req2.Header.Set("If-None-Match", "\"v1\"") | ||
| rec2 := httptest.NewRecorder() | ||
| if err := h.Handle(rec2, req2); err != nil { | ||
| t.Fatalf("err: %v", err) | ||
| } | ||
| if rec2.Code != baseHttp.StatusNotModified { | ||
| t.Fatalf("status %d", rec2.Code) | ||
| } | ||
|
|
||
| badF, _ := os.CreateTemp("", "bad.json") | ||
| badF.WriteString("{") | ||
| badF.Close() | ||
| defer os.Remove(badF.Name()) | ||
|
|
||
| bad := tc.make(badF.Name()) | ||
| rec3 := httptest.NewRecorder() | ||
| req3 := httptest.NewRequest("GET", tc.endpoint, nil) | ||
| if bad.Handle(rec3, req3) == nil { | ||
| t.Fatalf("expected error") | ||
| } | ||
| } | ||
|
|
||
| func assertArrayUUID1(t *testing.T, data any) { | ||
| arr, ok := data.([]interface{}) | ||
| if !ok || len(arr) != 1 { | ||
| t.Fatalf("unexpected data: %+v", data) | ||
| } | ||
| m, ok := arr[0].(map[string]interface{}) | ||
| if !ok || m["uuid"] != "1" { | ||
| t.Fatalf("unexpected payload: %+v", data) | ||
| } | ||
| } | ||
|
|
||
| func assertNicknameNick(t *testing.T, data any) { | ||
| obj, ok := data.(map[string]interface{}) | ||
| if !ok || obj["nickname"] != "nick" { | ||
| t.Fatalf("unexpected payload: %+v", data) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.