2018-05-16 18:59:35 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/influxdata/platform"
|
|
|
|
"github.com/influxdata/platform/mock"
|
|
|
|
)
|
|
|
|
|
2018-07-20 17:43:22 +00:00
|
|
|
const (
|
|
|
|
orgOneID = "020f755c3c082000"
|
|
|
|
orgTwoID = "020f755c3c082001"
|
|
|
|
)
|
|
|
|
|
2018-05-16 18:59:35 +00:00
|
|
|
var organizationCmpOptions = cmp.Options{
|
|
|
|
cmp.Comparer(func(x, y []byte) bool {
|
|
|
|
return bytes.Equal(x, y)
|
|
|
|
}),
|
|
|
|
cmp.Transformer("Sort", func(in []*platform.Organization) []*platform.Organization {
|
|
|
|
out := append([]*platform.Organization(nil), in...) // Copy input to avoid mutating it
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].ID.String() > out[j].ID.String()
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// OrganizationFields will include the IDGenerator, and organizations
|
|
|
|
type OrganizationFields struct {
|
|
|
|
IDGenerator platform.IDGenerator
|
|
|
|
Organizations []*platform.Organization
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:08:49 +00:00
|
|
|
// OrganizationService tests all the service functions.
|
|
|
|
func OrganizationService(
|
|
|
|
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()), t *testing.T,
|
|
|
|
) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fn func(init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
|
|
|
|
t *testing.T)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "CreateOrganization",
|
|
|
|
fn: CreateOrganization,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindOrganizationByID",
|
|
|
|
fn: FindOrganizationByID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindOrganizations",
|
|
|
|
fn: FindOrganizations,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "DeleteOrganization",
|
|
|
|
fn: DeleteOrganization,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindOrganization",
|
|
|
|
fn: FindOrganization,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "UpdateOrganization",
|
|
|
|
fn: UpdateOrganization,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tt.fn(init, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 18:59:35 +00:00
|
|
|
// CreateOrganization testing
|
|
|
|
func CreateOrganization(
|
|
|
|
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
|
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
|
|
|
organization *platform.Organization
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
err error
|
|
|
|
organizations []*platform.Organization
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields OrganizationFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "create organizations with empty set",
|
|
|
|
fields: OrganizationFields{
|
2018-07-20 17:43:22 +00:00
|
|
|
IDGenerator: mock.NewIDGenerator(orgOneID, t),
|
2018-05-16 18:59:35 +00:00
|
|
|
Organizations: []*platform.Organization{},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: &platform.Organization{
|
|
|
|
Name: "name1",
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
organizations: []*platform.Organization{
|
|
|
|
{
|
|
|
|
Name: "name1",
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic create organization",
|
|
|
|
fields: OrganizationFields{
|
2018-07-20 17:43:22 +00:00
|
|
|
IDGenerator: mock.NewIDGenerator(orgTwoID, t),
|
2018-05-16 18:59:35 +00:00
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: &platform.Organization{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization1",
|
|
|
|
},
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-07-20 10:24:07 +00:00
|
|
|
name: "names should be unique",
|
2018-05-16 18:59:35 +00:00
|
|
|
fields: OrganizationFields{
|
2018-07-20 17:43:22 +00:00
|
|
|
IDGenerator: mock.NewIDGenerator(orgTwoID, t),
|
2018-05-16 18:59:35 +00:00
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: &platform.Organization{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: fmt.Errorf("organization with name organization1 already exists"),
|
|
|
|
},
|
|
|
|
},
|
2018-10-12 01:06:43 +00:00
|
|
|
{
|
|
|
|
name: "create organization with no id",
|
|
|
|
fields: OrganizationFields{
|
|
|
|
IDGenerator: mock.NewIDGenerator(orgTwoID, t),
|
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(orgOneID),
|
|
|
|
Name: "organization1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: &platform.Organization{
|
|
|
|
Name: "organization2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
organizations: []*platform.Organization{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(orgOneID),
|
|
|
|
Name: "organization1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(orgTwoID),
|
|
|
|
Name: "organization2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-05-16 18:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s, done := init(tt.fields, t)
|
|
|
|
defer done()
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := s.CreateOrganization(ctx, tt.args.organization)
|
|
|
|
if (err != nil) != (tt.wants.err != nil) {
|
|
|
|
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && tt.wants.err != nil {
|
|
|
|
if err.Error() != tt.wants.err.Error() {
|
|
|
|
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 10:24:07 +00:00
|
|
|
|
|
|
|
// Delete only newly created organizations
|
|
|
|
// if tt.args.organization.ID != nil {
|
2018-07-30 14:29:52 +00:00
|
|
|
defer s.DeleteOrganization(ctx, tt.args.organization.ID)
|
2018-07-20 10:24:07 +00:00
|
|
|
// }
|
2018-05-16 18:59:35 +00:00
|
|
|
|
|
|
|
organizations, _, err := s.FindOrganizations(ctx, platform.OrganizationFilter{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve organizations: %v", err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(organizations, tt.wants.organizations, organizationCmpOptions...); diff != "" {
|
|
|
|
t.Errorf("organizations are different -got/+want\ndiff %s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindOrganizationByID testing
|
|
|
|
func FindOrganizationByID(
|
|
|
|
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
|
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
|
|
|
id platform.ID
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
err error
|
|
|
|
organization *platform.Organization
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields OrganizationFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic find organization by id",
|
|
|
|
fields: OrganizationFields{
|
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization1",
|
|
|
|
},
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-10-10 19:39:09 +00:00
|
|
|
id: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
organization: &platform.Organization{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s, done := init(tt.fields, t)
|
|
|
|
defer done()
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
organization, err := s.FindOrganizationByID(ctx, tt.args.id)
|
|
|
|
if (err != nil) != (tt.wants.err != nil) {
|
|
|
|
t.Fatalf("expected errors to be equal '%v' got '%v'", tt.wants.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && tt.wants.err != nil {
|
|
|
|
if err.Error() != tt.wants.err.Error() {
|
|
|
|
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(organization, tt.wants.organization, organizationCmpOptions...); diff != "" {
|
|
|
|
t.Errorf("organization is different -got/+want\ndiff %s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindOrganizations testing
|
|
|
|
func FindOrganizations(
|
|
|
|
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
|
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
2018-07-30 14:29:52 +00:00
|
|
|
ID platform.ID
|
2018-05-16 18:59:35 +00:00
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type wants struct {
|
|
|
|
organizations []*platform.Organization
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields OrganizationFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "find all organizations",
|
|
|
|
fields: OrganizationFields{
|
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "abc",
|
|
|
|
},
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "xyz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{},
|
|
|
|
wants: wants{
|
|
|
|
organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "abc",
|
|
|
|
},
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "xyz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "find organization by id",
|
|
|
|
fields: OrganizationFields{
|
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "abc",
|
|
|
|
},
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "xyz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "xyz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "find organization by name",
|
|
|
|
fields: OrganizationFields{
|
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "abc",
|
|
|
|
},
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "xyz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
name: "xyz",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "xyz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s, done := init(tt.fields, t)
|
|
|
|
defer done()
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
filter := platform.OrganizationFilter{}
|
2018-07-20 10:24:07 +00:00
|
|
|
if tt.args.ID.Valid() {
|
2018-07-30 14:29:52 +00:00
|
|
|
filter.ID = &tt.args.ID
|
2018-05-16 18:59:35 +00:00
|
|
|
}
|
|
|
|
if tt.args.name != "" {
|
|
|
|
filter.Name = &tt.args.name
|
|
|
|
}
|
|
|
|
|
|
|
|
organizations, _, err := s.FindOrganizations(ctx, filter)
|
|
|
|
if (err != nil) != (tt.wants.err != nil) {
|
|
|
|
t.Fatalf("expected errors to be equal '%v' got '%v'", tt.wants.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && tt.wants.err != nil {
|
|
|
|
if err.Error() != tt.wants.err.Error() {
|
|
|
|
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(organizations, tt.wants.organizations, organizationCmpOptions...); diff != "" {
|
|
|
|
t.Errorf("organizations are different -got/+want\ndiff %s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteOrganization testing
|
|
|
|
func DeleteOrganization(
|
|
|
|
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
|
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
err error
|
|
|
|
organizations []*platform.Organization
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields OrganizationFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "delete organizations using exist id",
|
|
|
|
fields: OrganizationFields{
|
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
|
|
|
Name: "orgA",
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "orgB",
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-07-20 10:24:07 +00:00
|
|
|
ID: orgOneID,
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
organizations: []*platform.Organization{
|
|
|
|
{
|
|
|
|
Name: "orgB",
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete organizations using id that does not exist",
|
|
|
|
fields: OrganizationFields{
|
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
|
|
|
Name: "orgA",
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "orgB",
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-07-20 10:24:07 +00:00
|
|
|
ID: "1234567890654321",
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: fmt.Errorf("organization not found"),
|
|
|
|
organizations: []*platform.Organization{
|
|
|
|
{
|
|
|
|
Name: "orgA",
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "orgB",
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s, done := init(tt.fields, t)
|
|
|
|
defer done()
|
|
|
|
ctx := context.TODO()
|
2018-10-10 19:39:09 +00:00
|
|
|
err := s.DeleteOrganization(ctx, MustIDBase16(tt.args.ID))
|
2018-05-16 18:59:35 +00:00
|
|
|
if (err != nil) != (tt.wants.err != nil) {
|
|
|
|
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && tt.wants.err != nil {
|
|
|
|
if err.Error() != tt.wants.err.Error() {
|
|
|
|
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := platform.OrganizationFilter{}
|
|
|
|
organizations, _, err := s.FindOrganizations(ctx, filter)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve organizations: %v", err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(organizations, tt.wants.organizations, organizationCmpOptions...); diff != "" {
|
|
|
|
t.Errorf("organizations are different -got/+want\ndiff %s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindOrganization testing
|
|
|
|
func FindOrganization(
|
|
|
|
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
|
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type wants struct {
|
|
|
|
organization *platform.Organization
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields OrganizationFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "find organization by name",
|
|
|
|
fields: OrganizationFields{
|
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "abc",
|
|
|
|
},
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "xyz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
name: "abc",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
organization: &platform.Organization{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "abc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-10-11 18:31:46 +00:00
|
|
|
{
|
|
|
|
name: "missing organization returns error",
|
|
|
|
fields: OrganizationFields{
|
|
|
|
Organizations: []*platform.Organization{},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
name: "abc",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: fmt.Errorf("organization not found"),
|
|
|
|
},
|
|
|
|
},
|
2018-05-16 18:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s, done := init(tt.fields, t)
|
|
|
|
defer done()
|
|
|
|
ctx := context.TODO()
|
|
|
|
filter := platform.OrganizationFilter{}
|
|
|
|
if tt.args.name != "" {
|
|
|
|
filter.Name = &tt.args.name
|
|
|
|
}
|
|
|
|
|
|
|
|
organization, err := s.FindOrganization(ctx, filter)
|
|
|
|
if (err != nil) != (tt.wants.err != nil) {
|
|
|
|
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(organization, tt.wants.organization, organizationCmpOptions...); diff != "" {
|
|
|
|
t.Errorf("organizations are different -got/+want\ndiff %s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateOrganization testing
|
|
|
|
func UpdateOrganization(
|
|
|
|
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
|
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
|
|
|
name string
|
|
|
|
id platform.ID
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
err error
|
|
|
|
organization *platform.Organization
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields OrganizationFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "update name",
|
|
|
|
fields: OrganizationFields{
|
|
|
|
Organizations: []*platform.Organization{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization1",
|
|
|
|
},
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgTwoID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "organization2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-10-10 19:39:09 +00:00
|
|
|
id: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
name: "changed",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
organization: &platform.Organization{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(orgOneID),
|
2018-05-16 18:59:35 +00:00
|
|
|
Name: "changed",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s, done := init(tt.fields, t)
|
|
|
|
defer done()
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
upd := platform.OrganizationUpdate{}
|
|
|
|
if tt.args.name != "" {
|
|
|
|
upd.Name = &tt.args.name
|
|
|
|
}
|
|
|
|
|
|
|
|
organization, err := s.UpdateOrganization(ctx, tt.args.id, upd)
|
|
|
|
if (err != nil) != (tt.wants.err != nil) {
|
|
|
|
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && tt.wants.err != nil {
|
|
|
|
if err.Error() != tt.wants.err.Error() {
|
|
|
|
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(organization, tt.wants.organization, organizationCmpOptions...); diff != "" {
|
|
|
|
t.Errorf("organization is different -got/+want\ndiff %s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|