-
Notifications
You must be signed in to change notification settings - Fork 0
chore: increase coverage #4
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f2d01ac
refactor: main tests
gustavosett c6f5738
chore: increase reader coverage
gustavosett 5df863b
refactor: service tests
gustavosett c765e95
feat: changelog
gustavosett c78c0c0
chore: golangci lint migrate
gustavosett 37189e7
fix: lint problems
gustavosett a1cae6e
docs: mark increase coverage
gustavosett f2e4142
fix: golang ci pipe
gustavosett 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| name: Update Changelog | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| release: | ||
| types: [published] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: write | ||
|
|
||
| jobs: | ||
| changelog: | ||
| name: Update Changelog | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Generate Changelog | ||
| uses: orhun/git-cliff-action@v4 | ||
| with: | ||
| config: cliff.toml | ||
| args: --verbose | ||
| env: | ||
| OUTPUT: CHANGELOG.md | ||
|
|
||
| - name: Commit | ||
| uses: stefanzweifel/git-auto-commit-action@v5 | ||
| with: | ||
| commit_message: "chore(changelog): update changelog" | ||
| file_pattern: CHANGELOG.md |
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,9 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to this project will be documented in this file. | ||
|
|
||
| ## [unreleased] | ||
|
|
||
| ### Miscellaneous Tasks | ||
|
|
||
| - Initial changelog setup |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # git-cliff ~ default configuration file | ||
| # https://git-cliff.org/docs/configuration | ||
| # | ||
| # Lines starting with "#" are comments. | ||
|
|
||
| [changelog] | ||
| # template for the changelog header | ||
| header = """ | ||
| # Changelog\n | ||
| All notable changes to this project will be documented in this file.\n | ||
| """ | ||
| # template for the changelog body | ||
| # https://keats.github.io/tera/docs/#introduction | ||
| body = """ | ||
| {% if version %}\ | ||
| ## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }} | ||
| {% else %}\ | ||
| ## [unreleased] | ||
| {% endif %}\ | ||
| {% for group, commits in commits | group_by(attribute="group") %} | ||
| ### {{ group | striptags | trim | upper_first }} | ||
| {% for commit in commits %} | ||
| - {% if commit.scope %}*({{ commit.scope }})* {% endif %}\ | ||
| {% if commit.breaking %}[**breaking**] {% endif %}\ | ||
| {{ commit.message | upper_first }}\ | ||
| {% endfor %} | ||
| {% endfor %}\n | ||
| """ | ||
| # template for the changelog footer | ||
| footer = """ | ||
| <!-- generated by git-cliff --> | ||
| """ | ||
| # remove the leading and trailing s | ||
| trim = true | ||
|
|
||
| [git] | ||
| # parse the commits based on https://www.conventionalcommits.org | ||
| conventional_commits = true | ||
| # filter out the commits that are not conventional | ||
| filter_unconventional = false | ||
| # process each line of a commit as an individual commit | ||
| split_commits = false | ||
| # regex for preprocessing the commit messages | ||
| commit_preprocessors = [ | ||
| # { pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](<REPO>/issues/${2}))"}, | ||
| # Check spelling of the commit with https://github.com/crate-ci/typos | ||
| # { pattern = '.*', replace_command = 'typos --write-changes -' }, | ||
| ] | ||
| # regex for parsing and grouping commits | ||
| commit_parsers = [ | ||
| { message = "^feat", group = "<!-- 0 -->Features" }, | ||
| { message = "^fix", group = "<!-- 1 -->Bug Fixes" }, | ||
| { message = "^doc", group = "<!-- 3 -->Documentation" }, | ||
| { message = "^perf", group = "<!-- 4 -->Performance" }, | ||
| { message = "^refactor", group = "<!-- 2 -->Refactor" }, | ||
| { message = "^style", group = "<!-- 5 -->Styling" }, | ||
| { message = "^test", group = "<!-- 6 -->Testing" }, | ||
| { message = "^chore\\(release\\): prepare for", skip = true }, | ||
| { message = "^chore\\(deps.*\\)", skip = true }, | ||
| { message = "^chore\\(pr\\)", skip = true }, | ||
| { message = "^chore\\(pull\\)", skip = true }, | ||
| { message = "^chore|^ci", group = "<!-- 7 -->Miscellaneous Tasks" }, | ||
| { body = ".*security", group = "<!-- 8 -->Security" }, | ||
| { message = "^revert", group = "<!-- 9 -->Revert" }, | ||
| ] | ||
| # protect breaking changes from being skipped due to matching a skipping commit_parser | ||
| protect_breaking_commits = false | ||
| # filter out the commits that are not matched by commit parsers | ||
| filter_commits = false | ||
| # regex for matching git tags | ||
| tag_pattern = "v[0-9].*" | ||
| # regex for skipping tags | ||
| # skip_tags = "" | ||
| # regex for ignoring tags | ||
| # ignore_tags = "" | ||
| # sort the tags topologically | ||
| topo_order = false | ||
| # sort the commits inside sections by oldest/newest order | ||
| sort_commits = "oldest" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/labstack/echo/v4" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestJSONSerializer_Serialize covers serialization scenarios using a table-driven approach. | ||
| func TestJSONSerializer_Serialize(t *testing.T) { | ||
| e := echo.New() | ||
| serializer := &JSONSerializer{} | ||
| e.JSONSerializer = serializer | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| input interface{} | ||
| indent string | ||
| expectedBody string | ||
| }{ | ||
| { | ||
| name: "Default Serialization", | ||
| input: map[string]string{"hello": "world"}, | ||
| indent: "", | ||
| expectedBody: `{"hello":"world"}`, | ||
| }, | ||
| { | ||
| name: "Indented Serialization", | ||
| input: map[string]string{"hello": "world"}, | ||
| indent: " ", | ||
| expectedBody: "{\n \"hello\": \"world\"\n}\n", | ||
| }, | ||
| { | ||
| name: "Complex Struct", | ||
| input: struct{ ID int }{ID: 1}, | ||
| indent: "", | ||
| expectedBody: `{"ID":1}`, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| rec := httptest.NewRecorder() | ||
| c := e.NewContext(req, rec) | ||
|
|
||
| err := serializer.Serialize(c, tc.input, tc.indent) | ||
| assert.NoError(t, err) | ||
| assert.JSONEq(t, tc.expectedBody, rec.Body.String()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestJSONSerializer_Deserialize covers deserialization scenarios. | ||
| func TestJSONSerializer_Deserialize(t *testing.T) { | ||
| e := echo.New() | ||
| serializer := &JSONSerializer{} | ||
| e.JSONSerializer = serializer | ||
|
|
||
| t.Run("Success", func(t *testing.T) { | ||
| jsonBody := `{"hello":"world"}` | ||
| req := httptest.NewRequest(http.MethodPost, "/", bytes.NewBufferString(jsonBody)) | ||
| req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) | ||
| rec := httptest.NewRecorder() | ||
| c := e.NewContext(req, rec) | ||
|
|
||
| var result map[string]string | ||
| err := serializer.Deserialize(c, &result) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "world", result["hello"]) | ||
| }) | ||
|
|
||
| t.Run("Malformed JSON", func(t *testing.T) { | ||
| jsonBody := `{"hello":` // Invalid JSON | ||
| req := httptest.NewRequest(http.MethodPost, "/", bytes.NewBufferString(jsonBody)) | ||
| rec := httptest.NewRecorder() | ||
| c := e.NewContext(req, rec) | ||
|
|
||
| var result map[string]string | ||
| err := serializer.Deserialize(c, &result) | ||
| assert.Error(t, err) | ||
| }) | ||
| } | ||
|
|
||
| func TestNewServer(t *testing.T) { | ||
| dbPath := "../../data/city.db" | ||
|
|
||
| t.Run("Success Initialization", func(t *testing.T) { | ||
| // Check if file exists before trying, to avoid failing in CI environments without the DB. | ||
| if _, err := os.Stat(dbPath); os.IsNotExist(err) { | ||
| t.Skipf("Skipping test: Database file not found at %s", dbPath) | ||
| } | ||
|
|
||
| e, svc, err := NewServer(dbPath) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, e) | ||
| require.NotNil(t, svc) | ||
|
|
||
| // Clean up | ||
| svcErr := svc.DB.Close() | ||
| require.NoError(t, svcErr) | ||
|
|
||
| // Verify Routes are Registered | ||
| foundRoutes := 0 | ||
| for _, r := range e.Routes() { | ||
| if r.Path == "/health" && r.Method == http.MethodGet { | ||
| foundRoutes++ | ||
| } | ||
| if r.Path == "/lookup/:ip" && r.Method == http.MethodGet { | ||
| foundRoutes++ | ||
| } | ||
| } | ||
| assert.Equal(t, 2, foundRoutes, "Expected /health and /lookup/:ip routes to be registered") | ||
| }) | ||
|
|
||
| t.Run("Failure Invalid Path", func(t *testing.T) { | ||
| e, svc, err := NewServer("invalid/path/to/db.mmdb") | ||
| assert.Error(t, err) | ||
| assert.Nil(t, e) | ||
| assert.Nil(t, svc) | ||
| }) | ||
| } | ||
|
|
||
| func TestHealthCheck_Integration(t *testing.T) { | ||
| dbPath := "../../data/city.db" | ||
| if _, err := os.Stat(dbPath); os.IsNotExist(err) { | ||
| t.Skip("Skipping integration test: Database not found") | ||
| } | ||
|
|
||
| e, svc, err := NewServer(dbPath) | ||
| require.NoError(t, err) | ||
| defer func() { | ||
| closeErr := svc.DB.Close() | ||
| require.NoError(t, closeErr) | ||
| }() | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/health", nil) | ||
| rec := httptest.NewRecorder() | ||
|
|
||
| // Inject request into Echo | ||
| e.ServeHTTP(rec, req) | ||
|
|
||
| assert.Equal(t, http.StatusOK, rec.Code) | ||
| assert.Contains(t, rec.Body.String(), "status") | ||
| } | ||
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
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.