2018-09-14 23:30:05 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-06-18 17:06:28 +00:00
|
|
|
"sort"
|
2019-12-26 07:35:42 +00:00
|
|
|
"strings"
|
2019-06-18 17:06:28 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2019-06-18 22:14:07 +00:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-04-03 17:39:20 +00:00
|
|
|
platform "github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/mock"
|
|
|
|
"github.com/influxdata/influxdb/v2/pkg/testing/assert"
|
2019-12-26 07:35:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-09-14 23:30:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
idA = "020f755c3c082000"
|
|
|
|
idB = "020f755c3c082001"
|
2019-01-10 18:04:17 +00:00
|
|
|
idC = "020f755c3c082002"
|
|
|
|
idD = "020f755c3c082003"
|
2018-09-14 23:30:05 +00:00
|
|
|
)
|
|
|
|
|
2019-06-18 22:25:45 +00:00
|
|
|
var oldFakeDate = time.Date(2002, 8, 5, 2, 2, 3, 0, time.UTC)
|
|
|
|
var fakeDate = time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)
|
2019-06-20 17:22:33 +00:00
|
|
|
var fakeGenerator = mock.TimeGenerator{FakeValue: fakeDate}
|
2019-06-18 17:06:28 +00:00
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
var variableCmpOptions = cmp.Options{
|
2018-09-14 23:30:05 +00:00
|
|
|
cmp.Comparer(func(x, y []byte) bool {
|
|
|
|
return bytes.Equal(x, y)
|
|
|
|
}),
|
2019-02-14 20:32:54 +00:00
|
|
|
cmp.Transformer("Sort", func(in []*platform.Variable) []*platform.Variable {
|
|
|
|
out := append([]*platform.Variable(nil), in...)
|
2018-09-14 23:30:05 +00:00
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].ID.String() > out[j].ID.String()
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
// VariableFields defines fields for a variable test
|
|
|
|
type VariableFields struct {
|
2019-06-18 22:14:07 +00:00
|
|
|
Variables []*platform.Variable
|
|
|
|
IDGenerator platform.IDGenerator
|
2019-06-18 17:06:28 +00:00
|
|
|
TimeGenerator platform.TimeGenerator
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
// VariableService tests all the service functions.
|
|
|
|
func VariableService(
|
|
|
|
init func(VariableFields, *testing.T) (platform.VariableService, string, func()), t *testing.T,
|
2018-09-18 15:30:52 +00:00
|
|
|
) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-02-14 20:32:54 +00:00
|
|
|
fn func(init func(VariableFields, *testing.T) (platform.VariableService, string, func()),
|
2018-09-18 15:30:52 +00:00
|
|
|
t *testing.T)
|
|
|
|
}{
|
2019-01-15 15:02:08 +00:00
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "CreateVariable",
|
|
|
|
fn: CreateVariable,
|
2019-01-15 15:02:08 +00:00
|
|
|
},
|
2019-06-20 17:22:33 +00:00
|
|
|
{
|
|
|
|
name: "FindVariableByID",
|
|
|
|
fn: FindVariableByID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindVariables",
|
|
|
|
fn: FindVariables,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "UpdateVariable",
|
|
|
|
fn: UpdateVariable,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "DeleteVariable",
|
|
|
|
fn: DeleteVariable,
|
|
|
|
},
|
2018-09-18 15:30:52 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tt.fn(init, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
// CreateVariable tests platform.VariableService CreateVariable interface method
|
|
|
|
func CreateVariable(init func(VariableFields, *testing.T) (platform.VariableService, string, func()), t *testing.T) {
|
2018-09-14 23:30:05 +00:00
|
|
|
type args struct {
|
2019-02-14 20:32:54 +00:00
|
|
|
variable *platform.Variable
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
type wants struct {
|
2020-01-06 23:31:50 +00:00
|
|
|
err *platform.Error
|
2019-02-14 20:32:54 +00:00
|
|
|
variables []*platform.Variable
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-02-14 20:32:54 +00:00
|
|
|
fields VariableFields
|
2018-09-14 23:30:05 +00:00
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
2019-01-15 15:02:08 +00:00
|
|
|
{
|
|
|
|
name: "basic create with missing id",
|
2019-02-14 20:32:54 +00:00
|
|
|
fields: VariableFields{
|
2019-01-15 15:02:08 +00:00
|
|
|
IDGenerator: &mock.IDGenerator{
|
|
|
|
IDFn: func() platform.ID {
|
|
|
|
return MustIDBase16(idD)
|
|
|
|
},
|
|
|
|
},
|
2019-06-18 22:25:45 +00:00
|
|
|
TimeGenerator: fakeGenerator,
|
2019-02-14 20:32:54 +00:00
|
|
|
Variables: []*platform.Variable{
|
2019-01-15 15:02:08 +00:00
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(1),
|
|
|
|
Name: "already there",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2019-02-14 20:32:54 +00:00
|
|
|
variable: &platform.Variable{
|
2019-01-15 15:02:08 +00:00
|
|
|
OrganizationID: platform.ID(3),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "basic variable",
|
2019-01-15 15:02:08 +00:00
|
|
|
Selected: []string{"a"},
|
2019-02-14 20:32:54 +00:00
|
|
|
Arguments: &platform.VariableArguments{
|
2019-01-15 15:02:08 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{"a"},
|
2019-01-15 15:02:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
2019-02-14 20:32:54 +00:00
|
|
|
variables: []*platform.Variable{
|
2019-01-15 15:02:08 +00:00
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(1),
|
|
|
|
Name: "already there",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idD),
|
|
|
|
OrganizationID: platform.ID(3),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "basic variable",
|
2019-01-15 15:02:08 +00:00
|
|
|
Selected: []string{"a"},
|
2019-02-14 20:32:54 +00:00
|
|
|
Arguments: &platform.VariableArguments{
|
2019-01-15 15:02:08 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{"a"},
|
2019-01-15 15:02:08 +00:00
|
|
|
},
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-15 15:02:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-20 17:22:33 +00:00
|
|
|
{
|
|
|
|
name: "creating a variable assigns the variable an id and adds it to the store",
|
|
|
|
fields: VariableFields{
|
|
|
|
IDGenerator: &mock.IDGenerator{
|
|
|
|
IDFn: func() platform.ID {
|
|
|
|
return MustIDBase16(idA)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TimeGenerator: fakeGenerator,
|
|
|
|
Variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"b"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
variable: &platform.Variable{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(3),
|
2020-01-07 17:09:16 +00:00
|
|
|
Name: "MY-variable",
|
2019-06-20 17:22:33 +00:00
|
|
|
Selected: []string{"a"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"a"},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: nil,
|
|
|
|
variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"b"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(3),
|
2020-01-07 17:09:16 +00:00
|
|
|
Name: "MY-variable",
|
2019-06-20 17:22:33 +00:00
|
|
|
Selected: []string{"a"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"a"},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-04 20:28:21 +00:00
|
|
|
{
|
|
|
|
name: "cant create a new variable with a name that exists",
|
|
|
|
fields: VariableFields{
|
|
|
|
IDGenerator: &mock.IDGenerator{
|
|
|
|
IDFn: func() platform.ID {
|
|
|
|
return MustIDBase16(idA)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TimeGenerator: fakeGenerator,
|
|
|
|
Variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: MustIDBase16(idD),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"b"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
variable: &platform.Variable{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: MustIDBase16(idD),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"a"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"a"},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: &platform.Error{
|
|
|
|
Code: platform.EConflict,
|
2020-01-06 23:31:50 +00:00
|
|
|
Msg: "variable is not unique",
|
2019-11-04 20:28:21 +00:00
|
|
|
},
|
|
|
|
variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: MustIDBase16(idD),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"b"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "variable names should be unique and case-insensitive",
|
|
|
|
fields: VariableFields{
|
|
|
|
IDGenerator: &mock.IDGenerator{
|
|
|
|
IDFn: func() platform.ID {
|
|
|
|
return MustIDBase16(idA)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TimeGenerator: fakeGenerator,
|
|
|
|
Variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"b"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
variable: &platform.Variable{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: "EXISTING-variable",
|
|
|
|
Selected: []string{"a"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"a"},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: &platform.Error{
|
|
|
|
Code: platform.EConflict,
|
2020-01-06 23:31:50 +00:00
|
|
|
Msg: "variable is not unique for key ",
|
2019-11-04 20:28:21 +00:00
|
|
|
},
|
|
|
|
variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"b"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cant create a new variable when variable name exists with a different type",
|
|
|
|
fields: VariableFields{
|
|
|
|
IDGenerator: &mock.IDGenerator{
|
|
|
|
IDFn: func() platform.ID {
|
|
|
|
return MustIDBase16(idA)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TimeGenerator: fakeGenerator,
|
|
|
|
Variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"b"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
variable: &platform.Variable{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"a"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
2019-12-07 18:54:03 +00:00
|
|
|
Type: "constant",
|
2019-11-04 20:28:21 +00:00
|
|
|
Values: platform.VariableConstantValues{"a"},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: &platform.Error{
|
|
|
|
Code: platform.EConflict,
|
2020-01-06 23:31:50 +00:00
|
|
|
Msg: "variable is not unique",
|
2019-11-04 20:28:21 +00:00
|
|
|
},
|
|
|
|
variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"b"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "trims white space, but fails when variable name already exists",
|
|
|
|
fields: VariableFields{
|
|
|
|
IDGenerator: &mock.IDGenerator{
|
|
|
|
IDFn: func() platform.ID {
|
|
|
|
return MustIDBase16(idA)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TimeGenerator: fakeGenerator,
|
|
|
|
Variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"b"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
variable: &platform.Variable{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: " existing-variable ",
|
|
|
|
Selected: []string{"a"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"a"},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: &platform.Error{
|
|
|
|
Code: platform.EConflict,
|
2020-01-06 23:31:50 +00:00
|
|
|
Msg: "variable is not unique",
|
2019-11-04 20:28:21 +00:00
|
|
|
},
|
|
|
|
variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(3),
|
|
|
|
Name: "existing-variable",
|
|
|
|
Selected: []string{"b"},
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{"b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2019-01-15 15:02:08 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-01-06 23:31:50 +00:00
|
|
|
s, _, done := init(tt.fields, t)
|
2019-01-15 15:02:08 +00:00
|
|
|
defer done()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
err := s.CreateVariable(ctx, tt.args.variable)
|
2020-01-06 23:31:50 +00:00
|
|
|
influxErrsEqual(t, tt.wants.err, err)
|
2019-01-15 15:02:08 +00:00
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
variables, err := s.FindVariables(ctx, platform.VariableFilter{})
|
2019-01-15 15:02:08 +00:00
|
|
|
if err != nil {
|
2019-02-14 20:32:54 +00:00
|
|
|
t.Fatalf("failed to retrieve variables: %v", err)
|
2019-01-15 15:02:08 +00:00
|
|
|
}
|
2019-02-14 20:32:54 +00:00
|
|
|
if diff := cmp.Diff(variables, tt.wants.variables, variableCmpOptions...); diff != "" {
|
|
|
|
t.Fatalf("found unexpected variables -got/+want\ndiff %s", diff)
|
2019-01-15 15:02:08 +00:00
|
|
|
}
|
|
|
|
})
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
// FindVariableByID tests platform.VariableService FindVariableByID interface method
|
|
|
|
func FindVariableByID(init func(VariableFields, *testing.T) (platform.VariableService, string, func()), t *testing.T) {
|
2018-09-14 23:30:05 +00:00
|
|
|
type args struct {
|
|
|
|
id platform.ID
|
|
|
|
}
|
|
|
|
type wants struct {
|
2019-12-26 07:35:42 +00:00
|
|
|
err *platform.Error
|
2019-02-14 20:32:54 +00:00
|
|
|
variable *platform.Variable
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-02-14 20:32:54 +00:00
|
|
|
fields VariableFields
|
2018-09-14 23:30:05 +00:00
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "finding a variable that exists by id",
|
|
|
|
fields: VariableFields{
|
|
|
|
Variables: []*platform.Variable{
|
2018-10-08 18:32:29 +00:00
|
|
|
{
|
2019-01-10 18:04:17 +00:00
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(5),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "existing-variable-a",
|
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-14 23:30:05 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2019-06-19 23:08:03 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2018-10-08 18:32:29 +00:00
|
|
|
{
|
2019-01-10 18:04:17 +00:00
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(5),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "existing-variable-b",
|
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-14 23:30:05 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-10-10 19:39:09 +00:00
|
|
|
id: MustIDBase16(idB),
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: nil,
|
2019-02-14 20:32:54 +00:00
|
|
|
variable: &platform.Variable{
|
2019-01-10 18:04:17 +00:00
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(5),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "existing-variable-b",
|
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-14 23:30:05 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-04-17 20:30:22 +00:00
|
|
|
name: "finding a non-existent variable",
|
2019-02-14 20:32:54 +00:00
|
|
|
fields: VariableFields{
|
|
|
|
Variables: []*platform.Variable{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2018-10-10 19:39:09 +00:00
|
|
|
id: MustIDBase16(idA),
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
2018-12-13 14:57:00 +00:00
|
|
|
err: &platform.Error{
|
|
|
|
Code: platform.ENotFound,
|
2019-02-14 20:32:54 +00:00
|
|
|
Op: platform.OpFindVariableByID,
|
|
|
|
Msg: platform.ErrVariableNotFound,
|
2018-12-13 14:57:00 +00:00
|
|
|
},
|
2019-02-14 20:32:54 +00:00
|
|
|
variable: nil,
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2019-01-15 15:02:08 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-26 07:35:42 +00:00
|
|
|
s, _, done := init(tt.fields, t)
|
2019-01-15 15:02:08 +00:00
|
|
|
defer done()
|
|
|
|
ctx := context.Background()
|
2018-09-14 23:30:05 +00:00
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
variable, err := s.FindVariableByID(ctx, tt.args.id)
|
2019-12-26 07:35:42 +00:00
|
|
|
if err != nil {
|
|
|
|
if tt.wants.err == nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
iErr, ok := err.(*platform.Error)
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, iErr.Code, tt.wants.err.Code)
|
|
|
|
assert.Equal(t, strings.HasPrefix(iErr.Error(), tt.wants.err.Error()), true)
|
|
|
|
return
|
|
|
|
}
|
2018-09-14 23:30:05 +00:00
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
if diff := cmp.Diff(variable, tt.wants.variable); diff != "" {
|
|
|
|
t.Fatalf("found unexpected variable -got/+want\ndiff %s", diff)
|
2019-01-15 15:02:08 +00:00
|
|
|
}
|
|
|
|
})
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
// FindVariables tests platform.variableService FindVariables interface method
|
|
|
|
func FindVariables(init func(VariableFields, *testing.T) (platform.VariableService, string, func()), t *testing.T) {
|
2018-12-20 18:14:48 +00:00
|
|
|
// todo(leodido)
|
2019-01-10 18:04:17 +00:00
|
|
|
type args struct {
|
2019-02-14 20:32:54 +00:00
|
|
|
// todo(leodido) > use VariableFilter as arg
|
2019-01-10 18:04:17 +00:00
|
|
|
orgID *platform.ID
|
|
|
|
findOpts platform.FindOptions
|
|
|
|
}
|
|
|
|
type wants struct {
|
2019-02-14 20:32:54 +00:00
|
|
|
variables []*platform.Variable
|
|
|
|
err error
|
2019-01-10 18:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-02-14 20:32:54 +00:00
|
|
|
fields VariableFields
|
2019-01-10 18:04:17 +00:00
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
2019-01-14 17:50:09 +00:00
|
|
|
{
|
|
|
|
name: "find nothing (empty set)",
|
2019-02-14 20:32:54 +00:00
|
|
|
fields: VariableFields{
|
|
|
|
Variables: []*platform.Variable{},
|
2019-01-14 17:50:09 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2019-02-14 20:32:54 +00:00
|
|
|
findOpts: platform.DefaultVariableFindOptions,
|
2019-01-14 17:50:09 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
2019-02-14 20:32:54 +00:00
|
|
|
variables: []*platform.Variable{},
|
2019-01-14 17:50:09 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "find all variables",
|
|
|
|
fields: VariableFields{
|
|
|
|
Variables: []*platform.Variable{
|
2019-01-10 18:04:17 +00:00
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(22),
|
|
|
|
Name: "a",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(22),
|
|
|
|
Name: "b",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2019-02-14 20:32:54 +00:00
|
|
|
findOpts: platform.DefaultVariableFindOptions,
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
2019-02-14 20:32:54 +00:00
|
|
|
variables: []*platform.Variable{
|
2019-01-10 18:04:17 +00:00
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(22),
|
|
|
|
Name: "a",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(22),
|
|
|
|
Name: "b",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-01-14 17:50:09 +00:00
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "find variables by wrong org id",
|
|
|
|
fields: VariableFields{
|
|
|
|
Variables: []*platform.Variable{
|
2019-01-14 17:50:09 +00:00
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(22),
|
|
|
|
Name: "a",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-14 17:50:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(22),
|
|
|
|
Name: "b",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-14 17:50:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2019-02-14 20:32:54 +00:00
|
|
|
findOpts: platform.DefaultVariableFindOptions,
|
2019-01-14 17:50:09 +00:00
|
|
|
orgID: idPtr(platform.ID(1)),
|
|
|
|
},
|
|
|
|
wants: wants{
|
2019-02-14 20:32:54 +00:00
|
|
|
variables: []*platform.Variable{},
|
2019-01-14 17:50:09 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "find all variables by org 22",
|
|
|
|
fields: VariableFields{
|
|
|
|
Variables: []*platform.Variable{
|
2019-01-10 18:04:17 +00:00
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(1),
|
|
|
|
Name: "a",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(22),
|
|
|
|
Name: "b",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idC),
|
|
|
|
OrganizationID: platform.ID(2),
|
|
|
|
Name: "c",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idD),
|
|
|
|
OrganizationID: platform.ID(22),
|
|
|
|
Name: "d",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2019-02-14 20:32:54 +00:00
|
|
|
findOpts: platform.DefaultVariableFindOptions,
|
2019-01-10 18:04:17 +00:00
|
|
|
orgID: idPtr(platform.ID(22)),
|
|
|
|
},
|
|
|
|
wants: wants{
|
2019-02-14 20:32:54 +00:00
|
|
|
variables: []*platform.Variable{
|
2019-01-10 18:04:17 +00:00
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(22),
|
|
|
|
Name: "b",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idD),
|
|
|
|
OrganizationID: platform.ID(22),
|
|
|
|
Name: "d",
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: fakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2019-01-10 18:04:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s, opPrefix, done := init(tt.fields, t)
|
|
|
|
defer done()
|
2019-12-31 03:11:53 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
2019-01-10 18:04:17 +00:00
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
filter := platform.VariableFilter{}
|
2019-01-10 18:04:17 +00:00
|
|
|
if tt.args.orgID != nil {
|
|
|
|
filter.OrganizationID = tt.args.orgID
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
variables, err := s.FindVariables(ctx, filter, tt.args.findOpts)
|
2019-01-10 18:04:17 +00:00
|
|
|
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
if diff := cmp.Diff(variables, tt.wants.variables, variableCmpOptions...); diff != "" {
|
|
|
|
t.Errorf("variables are different -got/+want\ndiff %s", diff)
|
2019-01-10 18:04:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-12-20 18:14:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
// UpdateVariable tests platform.VariableService UpdateVariable interface method
|
|
|
|
func UpdateVariable(init func(VariableFields, *testing.T) (platform.VariableService, string, func()), t *testing.T) {
|
2018-09-14 23:30:05 +00:00
|
|
|
type args struct {
|
|
|
|
id platform.ID
|
2019-02-14 20:32:54 +00:00
|
|
|
update *platform.VariableUpdate
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
type wants struct {
|
2019-12-26 07:35:42 +00:00
|
|
|
err *platform.Error
|
2019-02-14 20:32:54 +00:00
|
|
|
variables []*platform.Variable
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-02-14 20:32:54 +00:00
|
|
|
fields VariableFields
|
2018-09-14 23:30:05 +00:00
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "updating a variable's name",
|
|
|
|
fields: VariableFields{
|
2019-06-18 22:25:45 +00:00
|
|
|
TimeGenerator: fakeGenerator,
|
2019-02-14 20:32:54 +00:00
|
|
|
Variables: []*platform.Variable{
|
2018-10-08 18:32:29 +00:00
|
|
|
{
|
2019-01-10 18:04:17 +00:00
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(7),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "existing-variable-a",
|
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-14 23:30:05 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
2019-06-19 20:10:50 +00:00
|
|
|
UpdatedAt: fakeDate,
|
2019-06-18 22:25:45 +00:00
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2018-10-08 18:32:29 +00:00
|
|
|
{
|
2019-01-10 18:04:17 +00:00
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(7),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "existing-variable-b",
|
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-14 23:30:05 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
2019-06-19 20:10:50 +00:00
|
|
|
UpdatedAt: fakeDate,
|
2019-06-18 22:25:45 +00:00
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-10-10 19:39:09 +00:00
|
|
|
id: MustIDBase16(idB),
|
2019-02-14 20:32:54 +00:00
|
|
|
update: &platform.VariableUpdate{
|
|
|
|
Name: "new-variable-b-name",
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: nil,
|
2019-02-14 20:32:54 +00:00
|
|
|
variables: []*platform.Variable{
|
2018-10-08 18:32:29 +00:00
|
|
|
{
|
2019-01-10 18:04:17 +00:00
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(7),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "existing-variable-a",
|
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-14 23:30:05 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
2019-06-19 20:10:50 +00:00
|
|
|
UpdatedAt: fakeDate,
|
2019-06-18 22:25:45 +00:00
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2018-10-08 18:32:29 +00:00
|
|
|
{
|
2019-01-10 18:04:17 +00:00
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(7),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "new-variable-b-name",
|
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-14 23:30:05 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-04-17 20:30:22 +00:00
|
|
|
name: "updating a non-existent variable fails",
|
2019-02-14 20:32:54 +00:00
|
|
|
fields: VariableFields{
|
|
|
|
Variables: []*platform.Variable{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2018-10-10 19:39:09 +00:00
|
|
|
id: MustIDBase16(idA),
|
2019-02-14 20:32:54 +00:00
|
|
|
update: &platform.VariableUpdate{
|
2018-09-18 15:30:52 +00:00
|
|
|
Name: "howdy",
|
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
2018-12-13 14:57:00 +00:00
|
|
|
err: &platform.Error{
|
2019-02-14 20:32:54 +00:00
|
|
|
Op: platform.OpUpdateVariable,
|
|
|
|
Msg: platform.ErrVariableNotFound,
|
2018-12-13 14:57:00 +00:00
|
|
|
Code: platform.ENotFound,
|
|
|
|
},
|
2019-02-14 20:32:54 +00:00
|
|
|
variables: []*platform.Variable{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
},
|
2019-11-04 20:28:21 +00:00
|
|
|
{
|
|
|
|
name: "updating fails when variable name already exists",
|
|
|
|
fields: VariableFields{
|
|
|
|
TimeGenerator: fakeGenerator,
|
|
|
|
Variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(7),
|
|
|
|
Name: "variable-a",
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(7),
|
|
|
|
Name: "variable-b",
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: MustIDBase16(idB),
|
|
|
|
update: &platform.VariableUpdate{
|
|
|
|
Name: "variable-a",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: &platform.Error{
|
|
|
|
Code: platform.EConflict,
|
2020-01-06 23:31:50 +00:00
|
|
|
Msg: "variable entity update conflicts with an existing entity",
|
2019-11-04 20:28:21 +00:00
|
|
|
},
|
|
|
|
variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(7),
|
|
|
|
Name: "variable-a",
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(7),
|
|
|
|
Name: "variable-b",
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-12-26 07:35:42 +00:00
|
|
|
name: "trims the variable name but updating fails when variable name already exists",
|
2019-11-04 20:28:21 +00:00
|
|
|
fields: VariableFields{
|
|
|
|
TimeGenerator: fakeGenerator,
|
|
|
|
Variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(7),
|
|
|
|
Name: "variable-a",
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(7),
|
|
|
|
Name: "variable-b",
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: MustIDBase16(idB),
|
|
|
|
update: &platform.VariableUpdate{
|
|
|
|
Name: " variable-a ",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: &platform.Error{
|
|
|
|
Code: platform.EConflict,
|
2020-01-06 23:31:50 +00:00
|
|
|
Msg: "variable entity update conflicts with an existing entity",
|
2019-11-04 20:28:21 +00:00
|
|
|
},
|
|
|
|
variables: []*platform.Variable{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(7),
|
|
|
|
Name: "variable-a",
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(idB),
|
|
|
|
OrganizationID: platform.ID(7),
|
|
|
|
Name: "variable-b",
|
|
|
|
Arguments: &platform.VariableArguments{
|
|
|
|
Type: "constant",
|
|
|
|
Values: platform.VariableConstantValues{},
|
|
|
|
},
|
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: fakeDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2018-09-18 15:30:52 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-26 07:35:42 +00:00
|
|
|
s, _, done := init(tt.fields, t)
|
2018-09-18 15:30:52 +00:00
|
|
|
defer done()
|
2018-12-28 23:02:19 +00:00
|
|
|
ctx := context.Background()
|
2018-09-14 23:30:05 +00:00
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
variable, err := s.UpdateVariable(ctx, tt.args.id, tt.args.update)
|
2020-01-06 23:31:50 +00:00
|
|
|
influxErrsEqual(t, tt.wants.err, err)
|
2019-12-26 07:35:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-09-14 23:30:05 +00:00
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
if variable != nil {
|
|
|
|
if tt.args.update.Name != "" && variable.Name != tt.args.update.Name {
|
|
|
|
t.Fatalf("variable name not updated")
|
2018-09-18 15:30:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-14 23:30:05 +00:00
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
variables, err := s.FindVariables(ctx, platform.VariableFilter{})
|
2018-09-18 15:30:52 +00:00
|
|
|
if err != nil {
|
2019-02-14 20:32:54 +00:00
|
|
|
t.Fatalf("failed to retrieve variables: %v", err)
|
2018-09-18 15:30:52 +00:00
|
|
|
}
|
2019-02-14 20:32:54 +00:00
|
|
|
if diff := cmp.Diff(variables, tt.wants.variables, variableCmpOptions...); diff != "" {
|
|
|
|
t.Fatalf("found unexpected variables -got/+want\ndiff %s", diff)
|
2018-09-18 15:30:52 +00:00
|
|
|
}
|
|
|
|
})
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
// DeleteVariable tests platform.VariableService DeleteVariable interface method
|
|
|
|
func DeleteVariable(init func(VariableFields, *testing.T) (platform.VariableService, string, func()), t *testing.T) {
|
2018-09-14 23:30:05 +00:00
|
|
|
type args struct {
|
|
|
|
id platform.ID
|
|
|
|
}
|
|
|
|
type wants struct {
|
2019-12-26 07:35:42 +00:00
|
|
|
err *platform.Error
|
2019-02-14 20:32:54 +00:00
|
|
|
variables []*platform.Variable
|
2018-09-14 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-02-14 20:32:54 +00:00
|
|
|
fields VariableFields
|
2018-09-14 23:30:05 +00:00
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
2019-01-15 15:02:08 +00:00
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "deleting a variable",
|
|
|
|
fields: VariableFields{
|
|
|
|
Variables: []*platform.Variable{
|
2019-01-15 15:02:08 +00:00
|
|
|
{
|
|
|
|
ID: MustIDBase16(idA),
|
|
|
|
OrganizationID: platform.ID(9),
|
|
|
|
Name: "m",
|
2019-02-14 20:32:54 +00:00
|
|
|
Arguments: &platform.VariableArguments{
|
2019-01-15 15:02:08 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2019-01-15 15:02:08 +00:00
|
|
|
},
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: oldFakeDate,
|
|
|
|
},
|
2019-01-15 15:02:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: MustIDBase16(idA),
|
|
|
|
},
|
|
|
|
wants: wants{
|
2019-02-14 20:32:54 +00:00
|
|
|
err: nil,
|
|
|
|
variables: []*platform.Variable{},
|
2019-01-15 15:02:08 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "deleting a variable that doesn't exist",
|
|
|
|
fields: VariableFields{
|
|
|
|
Variables: []*platform.Variable{
|
2018-10-08 18:32:29 +00:00
|
|
|
{
|
2019-01-11 16:28:45 +00:00
|
|
|
ID: MustIDBase16(idD),
|
2019-01-10 18:04:17 +00:00
|
|
|
OrganizationID: platform.ID(1),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "existing-variable",
|
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-14 23:30:05 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2019-06-18 22:25:45 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: oldFakeDate,
|
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-10-10 19:39:09 +00:00
|
|
|
id: MustIDBase16(idB),
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
2018-12-13 14:57:00 +00:00
|
|
|
err: &platform.Error{
|
|
|
|
Code: platform.ENotFound,
|
2019-02-14 20:32:54 +00:00
|
|
|
Op: platform.OpDeleteVariable,
|
|
|
|
Msg: platform.ErrVariableNotFound,
|
2018-12-13 14:57:00 +00:00
|
|
|
},
|
2019-02-14 20:32:54 +00:00
|
|
|
variables: []*platform.Variable{
|
2018-10-08 18:32:29 +00:00
|
|
|
{
|
2019-01-11 16:28:45 +00:00
|
|
|
ID: MustIDBase16(idD),
|
2019-01-10 18:04:17 +00:00
|
|
|
OrganizationID: platform.ID(1),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "existing-variable",
|
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-14 23:30:05 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
2019-06-19 20:10:50 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: oldFakeDate,
|
|
|
|
UpdatedAt: oldFakeDate,
|
|
|
|
},
|
2018-09-14 23:30:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2019-01-15 15:02:08 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-26 07:35:42 +00:00
|
|
|
s, _, done := init(tt.fields, t)
|
2019-01-15 15:02:08 +00:00
|
|
|
defer done()
|
|
|
|
ctx := context.Background()
|
2018-09-14 23:30:05 +00:00
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
defer s.ReplaceVariable(ctx, &platform.Variable{
|
2019-01-15 15:02:08 +00:00
|
|
|
ID: tt.args.id,
|
|
|
|
OrganizationID: platform.ID(1),
|
|
|
|
})
|
2019-12-27 02:15:14 +00:00
|
|
|
|
|
|
|
err := s.DeleteVariable(ctx, tt.args.id)
|
2019-12-26 07:35:42 +00:00
|
|
|
if err != nil {
|
|
|
|
if tt.wants.err == nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
iErr, ok := err.(*platform.Error)
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, iErr.Code, tt.wants.err.Code)
|
|
|
|
assert.Equal(t, strings.HasPrefix(iErr.Error(), tt.wants.err.Error()), true)
|
|
|
|
return
|
|
|
|
}
|
2019-01-15 15:02:08 +00:00
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
variables, err := s.FindVariables(ctx, platform.VariableFilter{})
|
2019-01-15 15:02:08 +00:00
|
|
|
if err != nil {
|
2019-02-14 20:32:54 +00:00
|
|
|
t.Fatalf("failed to retrieve variables: %v", err)
|
2019-01-15 15:02:08 +00:00
|
|
|
}
|
2019-02-14 20:32:54 +00:00
|
|
|
if diff := cmp.Diff(variables, tt.wants.variables, variableCmpOptions...); diff != "" {
|
|
|
|
t.Fatalf("found unexpected variables -got/+want\ndiff %s", diff)
|
2019-01-15 15:02:08 +00:00
|
|
|
}
|
2018-09-14 23:30:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|