-
Notifications
You must be signed in to change notification settings - Fork 0
Tests/inc coverage #13
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
14 commits
Select commit
Hold shift + click to select a range
e29d00d
Add tests on file_operations
rm-hull 8123b7c
Merge branch 'main' of github.com:rm-hull/postcode-polygons into test…
rm-hull 8a46382
Add tests on CSV parser
rm-hull d34a584
Merge branch 'main' of github.com:rm-hull/postcode-polygons into test…
rm-hull 2b5581b
Add tests on codepoint spatial index
rm-hull 60c5cad
Update spatial-index/codepoint_test.go
rm-hull 4a491e8
Update spatial-index/codepoint_test.go
rm-hull 08897df
Fix test error
rm-hull 78b0f91
Merge branch 'main' of github.com:rm-hull/postcode-polygons into test…
rm-hull 411a337
wip
rm-hull e944454
Merge branch 'main' of github.com:rm-hull/postcode-polygons into test…
rm-hull a294235
Replace with a mock
rm-hull 25da00f
change to table tests
rm-hull 6279427
Address PR comments
rm-hull 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,188 @@ | ||
| package routes | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| spatialindex "postcode-polygons/spatial-index" | ||
| "testing" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/kofalt/go-memoize" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type mockSpatialIndex struct { | ||
| SearchFunc func(bounds []uint32) (*[]spatialindex.CodePoint, error) | ||
| SearchIterFunc func(bounds []uint32, iter func([2]uint32, [2]uint32, string) bool) error | ||
| LenFunc func() int | ||
| } | ||
|
|
||
| func (m *mockSpatialIndex) Search(bounds []uint32) (*[]spatialindex.CodePoint, error) { | ||
| if m.SearchFunc != nil { | ||
| return m.SearchFunc(bounds) | ||
| } | ||
| return nil, nil | ||
| } | ||
| func (m *mockSpatialIndex) SearchIter(bounds []uint32, iter func([2]uint32, [2]uint32, string) bool) error { | ||
| if m.SearchIterFunc != nil { | ||
| return m.SearchIterFunc(bounds, iter) | ||
| } | ||
| return nil | ||
| } | ||
| func (m *mockSpatialIndex) Len() int { | ||
| if m.LenFunc != nil { | ||
| return m.LenFunc() | ||
| } | ||
| return 0 | ||
| } | ||
|
|
||
| func TestCodePointSearch_BadBBox(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| w := httptest.NewRecorder() | ||
| c, _ := gin.CreateTestContext(w) | ||
| c.Request = httptest.NewRequest("GET", "/search?bbox=bad,bbox,values", nil) | ||
| c.Request.URL.RawQuery = "bbox=bad,bbox,values" | ||
|
|
||
| spatialIdx := &mockSpatialIndex{} | ||
| handler := CodePointSearch(spatialIdx) | ||
| handler(c) | ||
|
|
||
| require.Equal(t, http.StatusBadRequest, w.Code) | ||
| require.Contains(t, w.Body.String(), "bbox must have 4 comma-separated values") | ||
| } | ||
|
|
||
| func TestCodePointSearch_TooBig(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| w := httptest.NewRecorder() | ||
| c, _ := gin.CreateTestContext(w) | ||
| c.Request = httptest.NewRequest("GET", "/search?bbox=0,0,10000,10000", nil) | ||
| c.Request.URL.RawQuery = "bbox=0,0,10000,10000" | ||
|
|
||
| spatialIdx := &mockSpatialIndex{} | ||
| handler := CodePointSearch(spatialIdx) | ||
| handler(c) | ||
|
|
||
| require.Equal(t, http.StatusBadRequest, w.Code) | ||
| require.Contains(t, w.Body.String(), "bbox is too large") | ||
| } | ||
|
|
||
| func TestCodePointSearch_InternalError(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| w := httptest.NewRecorder() | ||
| c, _ := gin.CreateTestContext(w) | ||
| c.Request = httptest.NewRequest("GET", "/search?bbox=0,0,1,1", nil) | ||
| c.Request.URL.RawQuery = "bbox=0,0,1,1" | ||
|
|
||
| spatialIdx := &mockSpatialIndex{ | ||
| SearchFunc: func(bounds []uint32) (*[]spatialindex.CodePoint, error) { | ||
| return nil, errors.New("fail") | ||
| }, | ||
| } | ||
| handler := CodePointSearch(spatialIdx) | ||
| handler(c) | ||
|
|
||
| require.Equal(t, http.StatusInternalServerError, w.Code) | ||
| require.Contains(t, w.Body.String(), "An internal server error occurred") | ||
| } | ||
|
|
||
| func TestCodePointSearch_Success(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| w := httptest.NewRecorder() | ||
| c, _ := gin.CreateTestContext(w) | ||
| c.Request = httptest.NewRequest("GET", "/search?bbox=0,0,1,1", nil) | ||
| c.Request.URL.RawQuery = "bbox=0,0,1,1" | ||
|
|
||
| spatialIdx := &mockSpatialIndex{ | ||
| SearchFunc: func(bounds []uint32) (*[]spatialindex.CodePoint, error) { | ||
| results := []spatialindex.CodePoint{{PostCode: "AB1 2CD", Easting: 1, Northing: 2}} | ||
| return &results, nil | ||
| }, | ||
| } | ||
| handler := CodePointSearch(spatialIdx) | ||
| handler(c) | ||
|
|
||
| require.Equal(t, http.StatusOK, w.Code) | ||
| require.Contains(t, w.Body.String(), "AB1 2CD") | ||
| } | ||
|
|
||
| func TestPolygonSearch_BadBBox(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| w := httptest.NewRecorder() | ||
| c, _ := gin.CreateTestContext(w) | ||
| c.Request = httptest.NewRequest("GET", "/polygon?bbox=bad,bbox,values", nil) | ||
| c.Request.URL.RawQuery = "bbox=bad,bbox,values" | ||
|
|
||
| spatialIdx := &mockSpatialIndex{} | ||
| cache := memoize.NewMemoizer(0, 0) | ||
| handler := PolygonSearch(spatialIdx, cache) | ||
| handler(c) | ||
|
|
||
| require.Equal(t, http.StatusBadRequest, w.Code) | ||
| require.Contains(t, w.Body.String(), "bbox must have 4 comma-separated values") | ||
| } | ||
|
|
||
| func TestPolygonSearch_InternalError(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| w := httptest.NewRecorder() | ||
| c, _ := gin.CreateTestContext(w) | ||
| c.Request = httptest.NewRequest("GET", "/polygon?bbox=0,0,1,1", nil) | ||
| c.Request.URL.RawQuery = "bbox=0,0,1,1" | ||
|
|
||
| spatialIdx := &mockSpatialIndex{ | ||
| SearchIterFunc: func(bounds []uint32, iter func([2]uint32, [2]uint32, string) bool) error { | ||
| return errors.New("fail") | ||
| }, | ||
| } | ||
| cache := memoize.NewMemoizer(0, 0) | ||
| handler := PolygonSearch(spatialIdx, cache) | ||
| handler(c) | ||
|
|
||
| require.Equal(t, http.StatusInternalServerError, w.Code) | ||
| require.Contains(t, w.Body.String(), "An internal server error occurred") | ||
| } | ||
|
|
||
| func TestParseBBox(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| bboxStr string | ||
| expectErr bool | ||
| errContains string | ||
| }{ | ||
| {name: "valid", bboxStr: "1,2,3,4", expectErr: false}, | ||
| {name: "too few parts", bboxStr: "1,2,3", expectErr: true, errContains: "bbox must have 4 comma-separated values"}, | ||
| {name: "not numbers", bboxStr: "a,b,c,d", expectErr: true, errContains: "invalid bbox value"}, | ||
| {name: "min greater than max", bboxStr: "4,3,2,1", expectErr: true, errContains: "invalid bbox: min values must be less than or equal to max values"}, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| _, err := parseBBox(tc.bboxStr) | ||
| if tc.expectErr { | ||
| require.Error(t, err) | ||
| if tc.errContains != "" { | ||
| require.Contains(t, err.Error(), tc.errContains) | ||
| } | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestIsTooBig(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| bbox []uint32 | ||
| expected bool | ||
| }{ | ||
| {name: "not too big", bbox: []uint32{0, 0, 100, 100}, expected: false}, | ||
| {name: "too wide", bbox: []uint32{0, 0, 6000, 100}, expected: true}, | ||
| {name: "too high", bbox: []uint32{0, 0, 100, 6000}, expected: true}, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| require.Equal(t, tc.expected, isTooBig(tc.bbox)) | ||
| }) | ||
| } | ||
| } | ||
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.