Skip to content

Commit

Permalink
Add tests for Unmarshal API payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
ljagiello committed Oct 10, 2023
1 parent 7b913a6 commit 195933e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
22 changes: 11 additions & 11 deletions airgradient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package main

import (
"encoding/json"
"errors"
"io"
"net/http"
"time"
)

const AIR_GRADIENT_API_URL = "https://api.airgradient.com/public/api/v1/locations/measures/current"

type AirGradientMeasures []struct {
LocationID int `json:"locationId"`
LocationName string `json:"locationName"`
Expand All @@ -32,10 +31,10 @@ type AirGradientMeasures []struct {
NoxIndex float64 `json:"noxIndex"`
}

func fetchMeasures(token string) ([]byte, error) {
func fetchMeasures(airGradientAPIUrl string, token string) ([]byte, error) {
client := &http.Client{}

req, err := http.NewRequest("GET", AIR_GRADIENT_API_URL, nil)
req, err := http.NewRequest("GET", airGradientAPIUrl, nil)
if err != nil {
logger.Error("Creating HTTP request", "error", err)
return nil, err
Expand All @@ -61,17 +60,18 @@ func fetchMeasures(token string) ([]byte, error) {
return body, nil
}

func getAirGradientMeasures(token string) (airGradientMeasures AirGradientMeasures) {
payload, err := fetchMeasures(token)
func getAirGradientMeasures(airGradientAPIUrl string, token string) (AirGradientMeasures, error) {
payload, err := fetchMeasures(airGradientAPIUrl, token)
if err != nil {
logger.Error("Fetching measures", "error", err)
return
return nil, err
}

var airGradientMeasures AirGradientMeasures

err = json.Unmarshal(payload, &airGradientMeasures)
if err != nil {
logger.Error("Parsing JSON payload", "error", err)
return
return nil, errors.New("Error unmarshalling JSON")
}
return airGradientMeasures

return airGradientMeasures, nil
}
46 changes: 46 additions & 0 deletions airgradient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetAirGradientMeasures(t *testing.T) {
var testCases = []struct {
name string
payloadFile string
err error
}{
{
"correct-response",
"testdata/correct_response1.json",
nil,
},
{
"correct-response2",
"testdata/correct_response2.json",
nil,
},
{
"incorrect-response",
"testdata/incorrect_response1.json",
errors.New("Error unmarshalling JSON"),
},
}

for _, tC := range testCases {
t.Run(tC.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, tC.payloadFile)
}))
defer server.Close()

_, err := getAirGradientMeasures(server.URL, "SECRET-TOKEN")
assert.Equal(t, tC.err, err)
})
}
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/progrium/macdriver/objc"
)

const AIR_GRADIENT_API_URL = "https://api.airgradient.com/public/api/v1/locations/measures/current"

func main() {
macos.RunApp(launched)
}
Expand Down Expand Up @@ -44,7 +46,11 @@ func launched(app appkit.Application, delegate *appkit.ApplicationDelegate) {
for {
select {
case <-time.After(time.Duration(cfg.Interval) * time.Second):
airGradientMeasures = getAirGradientMeasures(cfg.Token)
airGradientMeasures, err = getAirGradientMeasures(AIR_GRADIENT_API_URL, cfg.Token)
if err != nil {
logger.Error("Fetching measures", "error", err)
continue
}
}

if len(airGradientMeasures) == 0 {
Expand Down
1 change: 1 addition & 0 deletions testdata/correct_response1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"locationId":12345,"locationName":"Test Loc","pm01":null,"pm02":4,"pm10":null,"pm003Count":null,"atmp":24.3,"rhum":52,"rco2":548,"tvoc":93.979355,"wifi":-58,"timestamp":"2023-10-10T03:42:11.000Z","ledMode":"co2","ledCo2Threshold1":1000,"ledCo2Threshold2":2000,"ledCo2ThresholdEnd":4000,"serialno":"aabb12","firmwareVersion":null,"tvocIndex":100,"noxIndex":1}]
1 change: 1 addition & 0 deletions testdata/correct_response2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"locationId":12345,"locationName":"Test Loc","pm01":null,"pm02":4,"pm10":null,"pm003Count":null,"atmp":24.3,"rhum":53.5,"rco2":548,"tvoc":93.979355,"wifi":-58,"timestamp":"2023-10-10T03:42:11.000Z","ledMode":"co2","ledCo2Threshold1":1000,"ledCo2Threshold2":2000,"ledCo2ThresholdEnd":4000,"serialno":"aabb12","firmwareVersion":null,"tvocIndex":100,"noxIndex":1}]
10 changes: 10 additions & 0 deletions testdata/incorrect_response1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /public/api/v1/locations/measures/current1</pre>
</body>
</html>

0 comments on commit 195933e

Please sign in to comment.