Skip to content

Commit

Permalink
Fully parallelize tests.
Browse files Browse the repository at this point in the history
Remove globals, make all commands fully independent
  • Loading branch information
airforce270 committed Nov 23, 2023
1 parent 19e5e8a commit ad9e58f
Show file tree
Hide file tree
Showing 42 changed files with 994 additions and 685 deletions.
2 changes: 2 additions & 0 deletions apiclients/apiclients.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package apiclients contains API client definitions.
package apiclients
46 changes: 30 additions & 16 deletions apiclients/bible/bible.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,36 @@ import (
"net/url"
)

// Base URL for API requests. Should only be changed for testing.
var BaseURL = "https://bible-api.com"
// DefaultBaseURL is the default base URL for the Kick API.
const DefaultBaseURL = "https://bible-api.com"

// NewDefaultClient returns a new default Bible API client.
func NewDefaultClient() *Client { return NewClient(DefaultBaseURL) }

// NewClient creates a new Bible API client.
func NewClient(baseURL string) *Client {
return &Client{baseURL: baseURL}
}

// Client is a client for the IVR API.
type Client struct {
baseURL string
}

// FetchVerses fetches the verses matching a given verse query.
func (c *Client) FetchVerses(verse string) (*GetVersesResponse, error) {
body, err := get(fmt.Sprintf("%s/%s", c.baseURL, url.QueryEscape(verse)))
if err != nil {
return nil, fmt.Errorf("failed to fetch verses for %q: %w", verse, err)
}

resp := GetVersesResponse{}
if err = json.Unmarshal(body, &resp); err != nil {
return nil, fmt.Errorf("failed to unmarshal response from Bible API: %w", err)
}

return &resp, nil
}

// GetVersesResponse represents the response from the Bible API for a GetVerses request.
// https://bible-api.com/
Expand Down Expand Up @@ -70,17 +98,3 @@ func get(reqURL string) (respBody []byte, err error) {

return body, nil
}

func FetchVerses(verse string) (*GetVersesResponse, error) {
body, err := get(fmt.Sprintf("%s/%s", BaseURL, url.QueryEscape(verse)))
if err != nil {
return nil, fmt.Errorf("failed to fetch verses for %q: %w", verse, err)
}

resp := GetVersesResponse{}
if err = json.Unmarshal(body, &resp); err != nil {
return nil, fmt.Errorf("failed to unmarshal response from Bible API: %w", err)
}

return &resp, nil
}
34 changes: 16 additions & 18 deletions apiclients/bible/bible_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
package bible
package bible_test

import (
"testing"

"github.com/airforce270/airbot/apiclients/bible"
"github.com/airforce270/airbot/apiclients/bible/bibletest"
"github.com/airforce270/airbot/testing/fakeserver"

"github.com/google/go-cmp/cmp"
)

var (
originalBaseURL = BaseURL
)

func TestFetchUser(t *testing.T) {
server := fakeserver.New()
server.AddOnClose(func() { originalBaseURL = BaseURL })
defer server.Close()
BaseURL = server.URL()

t.Parallel()
tests := []struct {
desc string
useResp string
want *GetVersesResponse
want *bible.GetVersesResponse
}{
{
desc: "single verse",
useResp: bibletest.LookupVerseSingleVerse1Resp,
want: &GetVersesResponse{
want: &bible.GetVersesResponse{
Reference: "Philippians 4:8",
Verses: []Verse{
Verses: []bible.Verse{
{
BookID: "PHP",
BookName: "Philippians",
Expand All @@ -47,17 +40,22 @@ func TestFetchUser(t *testing.T) {
}

for _, tc := range tests {
server.Resps = []string{tc.useResp}
tc := tc
t.Run(tc.desc, func(t *testing.T) {
got, err := FetchVerses("Philippians 4:8")
t.Parallel()
server := fakeserver.New()
defer server.Close()
server.Resps = []string{tc.useResp}

client := bible.NewClient(server.URL())
got, err := client.FetchVerses("Philippians 4:8")
if err != nil {
t.Fatalf("FetchVerse() unexpected error: %v", err)
t.Fatalf("FetchVerses() unexpected error: %v", err)
}

if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("FetchVerse() diff (-want +got):\n%s", diff)
t.Errorf("FetchVerses() diff (-want +got):\n%s", diff)
}
})
server.Reset()
}
}
Loading

0 comments on commit ad9e58f

Please sign in to comment.