Skip to content

Instantly share code, notes, and snippets.

@managedkaos
Last active February 18, 2023 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save managedkaos/d6859066d9397c90bfd7b7be7b665f23 to your computer and use it in GitHub Desktop.
Save managedkaos/d6859066d9397c90bfd7b7be7b665f23 to your computer and use it in GitHub Desktop.
Simple Golang API
[
{
"guid": "05024756-765e-41a9-89d7-1407436d9a58",
"school": "Iowa State University",
"mascot": "Cy",
"nickname": "Cyclones",
"location": "Ames, IA, USA",
"latlong": "42.026111,-93.648333"
},
]
package main
import (
"encoding/json"
"io/ioutil"
"github.com/gin-gonic/gin"
)
type Data struct {
GUID string `json:"guid"`
School string `json:"school"`
Mascot string `json:"mascot"`
Nickname string `json:"nickname"`
Location string `json:"location"`
LatLong string `json:"latlong"`
}
func main() {
r := setupRouter()
r.Run(":3000")
}
func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/", getAllData)
r.GET("/:guid", getDataByID)
return r
}
func getAllData(c *gin.Context) {
var data []Data
file, err := ioutil.ReadFile("data.json")
if err != nil {
c.JSON(500, gin.H{"error": "Could not read data file"})
return
}
err = json.Unmarshal(file, &data)
if err != nil {
c.JSON(500, gin.H{"error": "Could not unmarshal data"})
return
}
c.JSON(200, data)
}
func getDataByID(c *gin.Context) {
var data []Data
guid := c.Param("guid")
file, err := ioutil.ReadFile("data.json")
if err != nil {
c.JSON(500, gin.H{"error": "Could not read data file"})
return
}
err = json.Unmarshal(file, &data)
if err != nil {
c.JSON(500, gin.H{"error": "Could not unmarshal data"})
return
}
for _, d := range data {
if d.GUID == guid {
c.JSON(200, d)
return
}
}
c.JSON(404, gin.H{"error": "Data not found"})
}
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
var testGUID = "05024756-765e-41a9-89d7-1407436d9a58"
var testData = []Data{
{
GUID: testGUID,
School: "Iowa State University",
Mascot: "Cy",
Nickname: "Cyclones",
Location: "Ames, IA, USA",
LatLong: "42.026111,-93.648333",
},
}
func TestSetupRouter(t *testing.T) {
router := setupRouter()
assert.IsType(t, router, gin.New())
}
func TestGetAllData(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request, _ = http.NewRequest("GET", "/", nil)
getAllData(c)
assert.Equal(t, http.StatusOK, w.Code)
}
func TestGetDataByID(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request, _ = http.NewRequest("GET", "/"+testGUID, nil)
getDataByID(c)
assert.Equal(t, http.StatusOK, w.Code)
var result Data
err := json.Unmarshal(w.Body.Bytes(), &result)
assert.NoError(t, err)
assert.Equal(t, testData[0], result)
}
func TestGetDataByIDNotFound(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request, _ = http.NewRequest("GET", "/123", nil)
getDataByID(c)
assert.Equal(t, http.StatusNotFound, w.Code)
var result map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &result)
assert.NoError(t, err)
assert.Equal(t, "Data not found", result["error"])
}
GIN_MODE=release go test -v
=== RUN TestSetupRouter
--- PASS: TestSetupRouter (0.00s)
=== RUN TestGetAllData
--- PASS: TestGetAllData (0.00s)
=== RUN TestGetDataByID
main_test.go:51:
Error Trace: main_test.go:51
Error: Not equal:
expected: 200
actual : 404
Test: TestGetDataByID
main_test.go:56:
Error Trace: main_test.go:56
Error: Not equal:
expected: main.Data{GUID:"05024756-765e-41a9-89d7-1407436d9a58", School:"Iowa State University", Mascot:"Cy", Nickname:"Cyclones", Location:"Ames, IA, USA", LatLong:"42.026111,-93.648333"}
actual : main.Data{GUID:"", School:"", Mascot:"", Nickname:"", Location:"", LatLong:""}
Diff:
--- Expected
+++ Actual
@@ -1,8 +1,8 @@
(main.Data) {
- GUID: (string) (len=36) "05024756-765e-41a9-89d7-1407436d9a58",
- School: (string) (len=21) "Iowa State University",
- Mascot: (string) (len=2) "Cy",
- Nickname: (string) (len=8) "Cyclones",
- Location: (string) (len=13) "Ames, IA, USA",
- LatLong: (string) (len=20) "42.026111,-93.648333"
+ GUID: (string) "",
+ School: (string) "",
+ Mascot: (string) "",
+ Nickname: (string) "",
+ Location: (string) "",
+ LatLong: (string) ""
}
Test: TestGetDataByID
--- FAIL: TestGetDataByID (0.00s)
=== RUN TestGetDataByIDNotFound
--- PASS: TestGetDataByIDNotFound (0.00s)
FAIL
exit status 1
FAIL main.go 0.127s
GIN_MODE=release go test -v
=== RUN TestSetupRouter
--- PASS: TestSetupRouter (0.00s)
=== RUN TestGetAllData
[GIN] 2023/02/18 - 13:08:58 | 200 | 162.791µs | | GET "/"
--- PASS: TestGetAllData (0.00s)
=== RUN TestGetDataByID
[GIN] 2023/02/18 - 13:08:58 | 200 | 53.625µs | | GET "/05024756-765e-41a9-89d7-1407436d9a58"
--- PASS: TestGetDataByID (0.00s)
=== RUN TestGetDataByIDNotFound
[GIN] 2023/02/18 - 13:08:58 | 404 | 41.25µs | | GET "/this-is-a-bad-guid"
--- PASS: TestGetDataByIDNotFound (0.00s)
PASS
ok main.go 0.692s
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
var testGUID = "05024756-765e-41a9-89d7-1407436d9a58"
var testData = []Data{
{
GUID: testGUID,
School: "Iowa State University",
Mascot: "Cy",
Nickname: "Cyclones",
Location: "Ames, IA, USA",
LatLong: "42.026111,-93.648333",
},
}
func TestSetupRouter(t *testing.T) {
router := setupRouter()
assert.IsType(t, router, gin.New())
}
func TestGetAllData(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
}
func TestGetDataByID(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/"+testGUID, nil)
router.ServeHTTP(w, req)
res := w.Result()
if res.StatusCode != 200 {
b, _ := ioutil.ReadAll(res.Body)
t.Error(res.StatusCode, string(b))
}
assert.Equal(t, http.StatusOK, w.Code)
var result Data
err := json.Unmarshal(w.Body.Bytes(), &result)
assert.NoError(t, err)
assert.Equal(t, testData[0], result)
}
func TestGetDataByIDNotFound(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/this-is-a-bad-guid", nil)
router.ServeHTTP(w, req)
res := w.Result()
if res.StatusCode != 404 {
b, _ := ioutil.ReadAll(res.Body)
t.Error(res.StatusCode, string(b))
}
assert.Equal(t, http.StatusNotFound, w.Code)
var result map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &result)
assert.NoError(t, err)
assert.Equal(t, "Data not found", result["error"])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment