fix failures in variable http testing

pull/14173/head
Jade McGough 2019-06-18 15:05:40 -07:00 committed by lisale0
parent 3330d38984
commit 2cd14c7cdb
2 changed files with 55 additions and 19 deletions

View File

@ -5,11 +5,13 @@ import (
"context"
"encoding/json"
"fmt"
"go.uber.org/zap"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"go.uber.org/zap"
platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/inmem"
@ -18,6 +20,8 @@ import (
"github.com/julienschmidt/httprouter"
)
var faketime = time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)
// NewMockVariableBackend returns a VariableBackend with mock services.
func NewMockVariableBackend() *VariableBackend {
return &VariableBackend{
@ -62,6 +66,10 @@ func TestVariableService_handleGetVariables(t *testing.T) {
Type: "constant",
Values: platform.VariableConstantValues{"a", "b"},
},
CRUDLog: platform.CRUDLog{
CreatedAt: faketime,
UpdatedAt: faketime,
},
},
{
ID: platformtesting.MustIDBase16("61726920617a696f"),
@ -72,6 +80,10 @@ func TestVariableService_handleGetVariables(t *testing.T) {
Type: "map",
Values: platform.VariableMapValues{"a": "b", "c": "d"},
},
CRUDLog: platform.CRUDLog{
CreatedAt: faketime,
UpdatedAt: faketime,
},
},
}, nil
},
@ -107,7 +119,8 @@ func TestVariableService_handleGetVariables(t *testing.T) {
"b"
]
},
"createdAt": "2009-11-10 23:00:00 +0000 UTC",
"createdAt": "2006-05-04T01:02:03Z",
"updatedAt": "2006-05-04T01:02:03Z",
"description":"",
"id":"6162207574726f71",
"labels":[
@ -138,7 +151,8 @@ func TestVariableService_handleGetVariables(t *testing.T) {
"c":"d"
}
},
"createdAt": "2009-11-10 23:00:00 +0000 UTC",
"createdAt": "2006-05-04T01:02:03Z",
"updatedAt": "2006-05-04T01:02:03Z",
"description":"",
"id":"61726920617a696f",
"labels":[
@ -205,6 +219,10 @@ func TestVariableService_handleGetVariables(t *testing.T) {
Type: "constant",
Values: platform.VariableConstantValues{"a", "b"},
},
CRUDLog: platform.CRUDLog{
CreatedAt: faketime,
UpdatedAt: faketime,
},
},
}, nil
},
@ -245,8 +263,7 @@ func TestVariableService_handleGetVariables(t *testing.T) {
"b"
]
},
"createdAt": "2009-11-10 23:00:00 +0000 UTC",
"description": "",
"description": "",
"id": "6162207574726f71",
"labels": [
{
@ -266,7 +283,9 @@ func TestVariableService_handleGetVariables(t *testing.T) {
"orgID": "0000000000000001",
"selected": [
"b"
]
],
"createdAt": "2006-05-04T01:02:03Z",
"updatedAt": "2006-05-04T01:02:03Z"
}
]
}`,
@ -350,6 +369,10 @@ func TestVariableService_handleGetVariable(t *testing.T) {
Type: "constant",
Values: platform.VariableConstantValues{"a", "b"},
},
CRUDLog: platform.CRUDLog{
CreatedAt: faketime,
UpdatedAt: faketime,
},
}, nil
},
},
@ -357,7 +380,7 @@ func TestVariableService_handleGetVariable(t *testing.T) {
wants: wants{
statusCode: 200,
contentType: "application/json; charset=utf-8",
body: `{"id":"75650d0a636f6d70","orgID":"0000000000000001","name":"variable-a","description":"","selected":["b"],"arguments":{"type":"constant","values":["a","b"]},"createdAt":"2009-11-10 23:00:00 +0000 UTC","labels":[],"links":{"self":"/api/v2/variables/75650d0a636f6d70","labels":"/api/v2/variables/75650d0a636f6d70/labels","org":"/api/v2/orgs/0000000000000001"}}`,
body: `{"id":"75650d0a636f6d70","orgID":"0000000000000001","name":"variable-a","description":"","selected":["b"],"arguments":{"type":"constant","values":["a","b"]},"createdAt":"2006-05-04T01:02:03Z","updatedAt":"2006-05-04T01:02:03Z","labels":[],"links":{"self":"/api/v2/variables/75650d0a636f6d70","labels":"/api/v2/variables/75650d0a636f6d70/labels","org":"/api/v2/orgs/0000000000000001"}}`,
},
},
{
@ -431,8 +454,10 @@ func TestVariableService_handleGetVariable(t *testing.T) {
if contentType != tt.wants.contentType {
t.Errorf("got = %v, want %v", contentType, tt.wants.contentType)
}
if body != tt.wants.body {
t.Errorf("got = %v, want %v", body, tt.wants.body)
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
t.Errorf("%q, error unmarshaling json %v", tt.name, err)
} else if tt.wants.body != "" && !eq {
t.Errorf("%q. ***%s***", tt.name, diff)
}
})
}
@ -464,6 +489,8 @@ func TestVariableService_handlePostVariable(t *testing.T) {
CreateVariableF: func(ctx context.Context, m *platform.Variable) error {
m.ID = platformtesting.MustIDBase16("75650d0a636f6d70")
m.OrganizationID = platform.ID(1)
m.UpdatedAt = faketime
m.CreatedAt = faketime
return nil
},
},
@ -480,17 +507,18 @@ func TestVariableService_handlePostVariable(t *testing.T) {
"foo"
]
},
"createdAt":"2009-11-10 23:00:00 +0000 UTC",
"selected": [
"'foo'"
]
],
"createdAt": "2006-05-04T01:02:03Z",
"updatedAt": "2006-05-04T01:02:03Z"
}
`,
},
wants: wants{
statusCode: 201,
contentType: "application/json; charset=utf-8",
body: `{"id":"75650d0a636f6d70","orgID":"0000000000000001","name":"my-great-variable","description":"","selected":["'foo'"],"arguments":{"type":"constant","values":["bar","foo"]},"createdAt":"2009-11-10 23:00:00 +0000 UTC","labels":[],"links":{"self":"/api/v2/variables/75650d0a636f6d70","labels":"/api/v2/variables/75650d0a636f6d70/labels","org":"/api/v2/orgs/0000000000000001"}}
body: `{"id":"75650d0a636f6d70","orgID":"0000000000000001","name":"my-great-variable","description":"","selected":["'foo'"],"arguments":{"type":"constant","values":["bar","foo"]},"createdAt":"2006-05-04T01:02:03Z","updatedAt":"2006-05-04T01:02:03Z","labels":[],"links":{"self":"/api/v2/variables/75650d0a636f6d70","labels":"/api/v2/variables/75650d0a636f6d70/labels","org":"/api/v2/orgs/0000000000000001"}}
`,
},
},
@ -555,8 +583,10 @@ func TestVariableService_handlePostVariable(t *testing.T) {
if contentType != tt.wants.contentType {
t.Errorf("got = %v, want %v", contentType, tt.wants.contentType)
}
if body != tt.wants.body {
t.Errorf("got = %v, want %v", body, tt.wants.body)
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
t.Errorf("%q, error unmarshaling json %v", tt.name, err)
} else if tt.wants.body != "" && !eq {
t.Errorf("%q. ***%s***", tt.name, diff)
}
})
}
@ -596,6 +626,10 @@ func TestVariableService_handlePatchVariable(t *testing.T) {
Values: platform.VariableConstantValues{},
},
Selected: []string{},
CRUDLog: platform.CRUDLog{
CreatedAt: faketime,
UpdatedAt: faketime,
},
}, nil
},
},
@ -607,8 +641,7 @@ func TestVariableService_handlePatchVariable(t *testing.T) {
wants: wants{
statusCode: 200,
contentType: "application/json; charset=utf-8",
body: `{"id":"75650d0a636f6d70","orgID":"0000000000000002","name":"new-name","description":"","selected":[],"arguments":{"type":"constant","values":[]},"createdAt":"2009-11-10 23:00:00 +0000 UTC","labels":[],"links":{"self":"/api/v2/variables/75650d0a636f6d70","labels":"/api/v2/variables/75650d0a636f6d70/labels","org":"/api/v2/orgs/0000000000000002"}}
`,
body: `{"id":"75650d0a636f6d70","orgID":"0000000000000002","name":"new-name","description":"","selected":[],"arguments":{"type":"constant","values":[]},"createdAt":"2006-05-04T01:02:03Z","updatedAt": "2006-05-04T01:02:03Z","labels":[],"links":{"self":"/api/v2/variables/75650d0a636f6d70","labels":"/api/v2/variables/75650d0a636f6d70/labels","org":"/api/v2/orgs/0000000000000002"}}`,
},
},
{
@ -658,8 +691,10 @@ func TestVariableService_handlePatchVariable(t *testing.T) {
if contentType != tt.wants.contentType {
t.Errorf("got = %v, want %v", contentType, tt.wants.contentType)
}
if body != tt.wants.body {
t.Errorf("got = %v, want %v", body, tt.wants.body)
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
t.Errorf("%q, error unmarshaling json %v", tt.name, err)
} else if tt.wants.body != "" && !eq {
t.Errorf("%q. ***%s***", tt.name, diff)
}
})
}

View File

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/influxdata/influxdb"
)
@ -368,7 +369,7 @@ func (s *Service) UpdateVariable(ctx context.Context, id influxdb.ID, update *in
Err: pe,
}
}
m.UpdatedAt = s.Now() 
m.UpdatedAt = s.Now()
if err := update.Apply(m); err != nil {
return &influxdb.Error{
Err: err,