Skip to content

Instantly share code, notes, and snippets.

View RicardoLinck's full-sized avatar

Ricardo Linck RicardoLinck

View GitHub Profile
var runner runner
r := defaultRunner{url}
if dryRun {
runner = &dryRunner{&r}
} else {
runner = &fileRunner{&r, "output.txt"}
}
package cmd
import (
"log"
"os"
"github.com/RicardoLinck/decorators/cache"
"github.com/RicardoLinck/decorators/service"
)
package fetch
import (
"context"
"time"
)
type PartnerFetcher struct {
partners map[string]int
}
package fetch
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
package fetch
import (
"context"
"log"
)
// Result represents the result of a fetcher
type Result struct {
Key string
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/RicardoLinck/scatter-gather/fetch"
"github.com/RicardoLinck/scatter-gather/nameservice"
func Test_FetchData(t *testing.T) {
router := http.NewServeMux()
router.HandleFunc("/api/v1/some-data", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Write([]byte(`{"user_name":"test_user",
"dob":"1999-05-23T18:25:43.511Z",
"fav_sports":[{"name":"football","order":2},{"name":"hockey", "order":1}]}`))
// Configuration contain parameters to call the api
type Configuration struct {
baseURL string
apiVersion int
}
// FetchData fetches the data from the api using the configuration
func (c Configuration) FetchData() (Person, error) {
person := Person{}
endpoint, decoderFunc, err := getEndpointAndDecodeFunc(c.apiVersion)
func TestConfiguration_FetchData(t *testing.T) {
t.Run("errors when invalid api version", func(t *testing.T) {
c := Configuration{
baseURL: "",
apiVersion: 3,
fetcher: nil,
}
_, err := c.FetchData()
assert.Error(t, ErrInvalidAPIVersion, err)
})
func TestConfiguration_FetchData(t *testing.T) {
errFromFetcher := errors.New("mock error")
type fields struct {
baseURL string
apiVersion int
fetcherFunc func(string) (io.ReadCloser, error)
}
tests := []struct {
name string
fields fields