Files
job-search/internal/findATender/findATender_test.go
T

125 lines
3.0 KiB
Go

package findATender
import (
"context"
"errors"
"io"
"net/http"
"slices"
"strings"
"testing"
"time"
"git.avsdev.uk/Go/job-search/internal/types"
)
func TestNew(t *testing.T) {
conf := testConfig("warn")
ft := New(conf, "https://example/api")
defer ft.Shutdown()
if ft.config != conf || ft.apiRoot != "https://example/api" {
t.Errorf("New did not keep its arguments: %+v", ft)
}
if ft.httpClient == nil {
t.Fatal("no http client")
}
if ft.httpClient.Timeout() != 60*time.Second || ft.httpClient.RetryCount() != 1 {
t.Errorf("client timeout %v, retries %d", ft.httpClient.Timeout(), ft.httpClient.RetryCount())
}
if ft.ctx != nil {
t.Error("New should not set a context")
}
}
func TestReloadConfig(t *testing.T) {
ft := New(testConfig("warn"), "")
defer ft.Shutdown()
conf := testConfig("debug")
ft.ReloadConfig(conf)
if ft.config != conf {
t.Error("config not replaced")
}
}
func TestWith(t *testing.T) {
ft := New(testConfig("warn"), "")
defer ft.Shutdown()
// Passing nil on purpose to test that case.
//nolint:staticcheck // SA1012
if got := ft.With(nil); got != ft {
t.Error("With(nil) should return the receiver")
}
type key struct{}
ctx := context.WithValue(context.Background(), key{}, "v")
got := ft.With(ctx)
if got == ft {
t.Fatal("With should return a copy")
}
if got.ctx != ctx {
t.Error("copy does not carry the context")
}
if ft.ctx != nil {
t.Error("With changed the original")
}
if got.httpClient != ft.httpClient || got.config != ft.config {
t.Error("copy should share the client and config")
}
}
func opportunityIDs(ops []*types.Opportunity) []string {
var ids []string
for _, op := range ops {
ids = append(ids, op.UUID)
}
slices.Sort(ids)
return ids
}
func TestFetchOncePublic(t *testing.T) {
ft, _ := pagedServer(t,
[]string{releaseJSON("ocds-1", "1"), releaseJSON("ocds-1", "2"), releaseJSON("ocds-2", "1")},
[]string{releaseJSON("ocds-3", "1")},
)
ops, err := ft.FetchOnce(time.Now(), false)
if err != nil {
t.Fatal(err)
}
if got := strings.Join(opportunityIDs(ops), ","); got != "ocds-1,ocds-2" {
t.Errorf("opportunities = %s", got)
}
ft, _ = pagedServer(t, []string{})
if _, err := ft.FetchOnce(time.Now(), false); !errors.Is(err, ErrNoReleases) {
t.Errorf("err = %v, want ErrNoReleases", err)
}
}
func TestFetchAllPublic(t *testing.T) {
ft, _ := pagedServer(t,
[]string{releaseJSON("ocds-1", "1"), releaseJSON("ocds-2", "1")},
[]string{releaseJSON("ocds-1", "2"), releaseJSON("ocds-3", "1")},
)
ops, err := ft.FetchAll(time.Now(), false)
if err != nil {
t.Fatal(err)
}
if got := strings.Join(opportunityIDs(ops), ","); got != "ocds-1,ocds-2,ocds-3" {
t.Errorf("opportunities = %s", got)
}
for _, op := range ops {
if op.UUID == "ocds-1" && len(op.Releases) != 2 {
t.Errorf("ocds-1 has %d releases, want 2", len(op.Releases))
}
}
ft, _ = newTestFT(t, "warn", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "not json")
})
if _, err := ft.FetchAll(time.Now(), false); err == nil {
t.Error("expected an error")
}
}