Skip to content

Commit

Permalink
touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
cynicaljoy committed Oct 23, 2024
1 parent 79f29e7 commit e4eea78
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
42 changes: 27 additions & 15 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,36 @@ func NewClient(secret string, timeouts Timeouts, configFns ...ClientConfigFn) *C
return client
}

func (c *Client) parseQueryURL() (url *url.URL, err error) {
if c.queryURL != nil {
url = c.queryURL
} else if url, err = url.Parse(c.url); err == nil {
url = url.JoinPath("query", "1")
c.queryURL = url
func (c *Client) parseQueryURL() (*url.URL, error) {
if c.queryURL == nil {
if queryURL, err := url.Parse(c.url); err != nil {
return nil, err
} else {
c.queryURL = queryURL.JoinPath("query", "1")
}
}
return

if c.queryURL == nil {
return nil, fmt.Errorf("query url is not set")
}

return c.queryURL, nil
}

func (c *Client) parseStreamURL() (url *url.URL, err error) {
if c.streamURL != nil {
url = c.streamURL
} else if url, err = url.Parse(c.url); err == nil {
url = url.JoinPath("stream", "1")
c.streamURL = url
func (c *Client) parseStreamURL() (*url.URL, error) {
if c.streamURL == nil {
if streamURL, err := url.Parse(c.url); err != nil {
return nil, err
} else {
c.streamURL = streamURL.JoinPath("stream", "1")
}
}
return

if c.streamURL == nil {
return nil, fmt.Errorf("stream url is not set")
}

return c.streamURL, nil
}

func (c *Client) doWithRetry(req *http.Request) (attempts int, r *http.Response, err error) {
Expand Down Expand Up @@ -316,7 +328,7 @@ func (c *Client) StreamFromQuery(fql *Query, streamOpts []StreamOptFn, opts ...Q
return c.Stream(stream, streamOpts...)
}

return nil, fmt.Errorf("expected query to return a fauna.StreamFromQuery but got %T", res.Data)
return nil, fmt.Errorf("query should return a fauna.EventSource but got %T", res.Data)
}

// Stream initiates a stream subscription for the given stream value.
Expand Down
7 changes: 3 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/fauna/fauna-go/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDefaultClient(t *testing.T) {
Expand All @@ -29,9 +30,7 @@ func TestDefaultClient(t *testing.T) {
query, _ := fauna.FQL(`${arg0}.length`, map[string]any{"arg0": s})

res, queryErr := client.Query(query)
if !assert.NoError(t, queryErr) {
return
}
require.NoError(t, queryErr)

var i int
marshalErr := res.Unmarshal(&i)
Expand Down Expand Up @@ -258,7 +257,7 @@ func TestBasicCRUDRequests(t *testing.T) {
}

coll := fmt.Sprintf("Person_%v", randomString(12))
collMod := &fauna.Module{coll}
collMod := &fauna.Module{Name: coll}

t.Run("Create a collection", func(t *testing.T) {
q, _ := fauna.FQL(`Collection.create({ name: ${name} })`, map[string]any{"name": coll})
Expand Down
2 changes: 1 addition & 1 deletion stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestStreaming(t *testing.T) {
t.Run("Fails on non-streamable values", func(t *testing.T) {
streamQ, _ := fauna.FQL(`"I'm a string"`, nil)
events, err := client.StreamFromQuery(streamQ, nil)
require.ErrorContains(t, err, "expected query to return a fauna.StreamFromQuery but got string")
require.ErrorContains(t, err, "query should return a fauna.EventSource but got string")
require.Nil(t, events)
})
})
Expand Down

0 comments on commit e4eea78

Please sign in to comment.