Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update client write to error when trying to write nothing. #1691

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions client/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
"github.com/influxdb/influxdb/influxql"
)

var (
// ErrNoWrites is when Write() is called with No Parameters.
ErrNoWrites = errors.New("nothing to write")
)

type Config struct {
URL url.URL
Username string
Expand Down Expand Up @@ -82,6 +87,10 @@ func (c *Client) Query(q Query) (*Results, error) {
}

func (c *Client) Write(writes ...Write) (*Results, error) {
if len(writes) <= 0 {
return nil, ErrNoWrites
}

c.url.Path = "write"
type data struct {
Points []Point `json:"points"`
Expand Down
21 changes: 21 additions & 0 deletions client/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ func TestClient_Write(t *testing.T) {
}
}

func TestClient_Write_ErrNoWrites(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var data influxdb.Results
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(data)
}))
defer ts.Close()

u, _ := url.Parse(ts.URL)
config := client.Config{URL: *u}
c, err := client.NewClient(config)
if err != nil {
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
}

_, err = c.Write()
if err != client.ErrNoWrites {
t.Fatalf("expected error. expected %v, actual %v", client.ErrNoWrites, err)
}
}

func TestClient_UserAgent(t *testing.T) {
receivedUserAgent := ""
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down