Commit cd1b4f25 authored by endk-mirror-svc's avatar endk-mirror-svc

Polished the Do() tests and added some CI

parent 6e4eb637
Pipeline #24431505 failed with stages
in 43 seconds
image: golang:1.10
stages:
- build
- test
before_script:
- go get github.com/tools/godep
- cp -r /builds/user /go/src/github.com/user/
- cd /go/src/github.com/user/repo
build-my-project:
stage: build
script:
- godep restore
- godep go build
test-my-project:
stage: test
script:
- godep restore
- godep go test -v -cover ./...
......@@ -2,6 +2,7 @@ package eds
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
......@@ -118,49 +119,3 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt
}
return resp, err
}
//type response struct {
// Help string `json:"help"`
// Success bool `json:"success"`
// Result []string `json:"result"`
//}
//
//// ListPackages returns a list of packages from Energi Data Service.
////
//// The limit may be set to 0 to get all packages.
//func (c *Client) ListPackages(limit int, offset int) ([]string, error) {
// if c.BaseURL == nil {
// u, err := url.Parse(DefaultURL)
// if err != nil {
// return nil, err
// }
// c.BaseURL = u
// }
// if c.httpClient == nil {
// c.httpClient = http.DefaultClient
// }
//
// rel := &url.URL{Path: "/package_list"}
// u := c.BaseURL.ResolveReference(rel)
//
// query := u.Query()
// query.Add("limit", string(limit))
// query.Add("offset", string(offset))
// u.RawQuery = query.Encode()
//
// req, err := http.NewRequest("GET", u.String(), nil)
// if err != nil {
// return nil, err
// }
// req.Header.Set("Accept", "application/json")
// req.Header.Set("User-Agent", c.UserAgent)
//
// resp, err := c.httpClient.Do(req)
// if err != nil {
// return nil, err
// }
// defer resp.Body.Close()
// var apiResponse response
// err = json.NewDecoder(resp.Body).Decode(&apiResponse)
// return apiResponse.Result, err
//}
package eds_test
import (
"bytes"
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
eds "gitlab.com/MadsRC/go-eds"
)
//func TestListPackages(t *testing.T) {
// ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("Content-Type", "application/json")
// fmt.Fprintln(w, `{"help": "https://api.energidataservice.dk/help_show?name=package_list", "success": true, "result": ["afrrreservesdk1", "capacityauctiondk1", "capacityauctiondk2", "co2emis", "co2emisprog", "communityconsumption", "communityproduction", "electricitybalance", "electricitybalancenonv", "electricityprodex5minrealtime", "elspotprices", "entryexitgasquality", "fcrreservesdk1", "fcrreservesdk2", "fixedresidualconsumption", "gascomposition", "mfrrreservesdk1", "mfrrreservesdk2", "mrstationsgasquality", "nordpoolmarket", "realtimemarket", "storagecapacity", "transmissionlines"]}`)
// }))
// defer ts.Close()
// tURL, err := url.Parse(ts.URL)
// if err != nil {
// t.Error(err)
// }
// eds := edsgo.Client{
// BaseURL: tURL,
// }
// e, err := eds.ListPackages(0, 0)
// if err != nil {
// t.Error(err)
// }
// t.Log(e)
//}
const ()
func TestNewClient(t *testing.T) {
......@@ -70,5 +55,45 @@ func TestNewRequest(t *testing.T) {
}
func TestDo(t *testing.T) {
type resp struct {
JSON string `json:"json"`
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"json": "stringi"}`)
}))
defer ts.Close()
tURL, err := url.Parse(ts.URL + "/")
if err != nil {
t.Error(err)
}
tests := []struct {
v interface{}
fail bool
}{
{&resp{}, false},
{new(bytes.Buffer), false},
{"", true},
}
for i, test := range tests {
c := eds.NewClient(nil)
c.BaseURL = tURL
ctx := context.Background()
req, err := c.NewRequest("GET", "package_list", "")
if err != nil {
t.Error(err)
}
_, err = c.Do(ctx, req, test.v)
if test.fail {
if err == nil {
t.Errorf("Test failed at iteration %d with nil error\n", i)
}
} else {
if err != nil {
t.Errorf("Test failed at iteration %d with error: %v\n", i, err)
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment