Skip to content

Commit

Permalink
test(bolt): add tests for missing orgs and buckets (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
goller authored Oct 11, 2018
1 parent 8a54ce3 commit 3be5656
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 26 deletions.
7 changes: 6 additions & 1 deletion bolt/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ func (c *Client) FindAuthorizationByToken(ctx context.Context, n string) (*platf
}

func (c *Client) findAuthorizationByToken(ctx context.Context, tx *bolt.Tx, n string) (*platform.Authorization, error) {
a := tx.Bucket(authorizationIndex).Get(authorizationIndexKey(n))
if a == nil {
// TODO: Make standard error
return nil, fmt.Errorf("authorization not found")
}
var id platform.ID
if err := id.Decode(tx.Bucket(authorizationIndex).Get(authorizationIndexKey(n))); err != nil {
if err := id.Decode(a); err != nil {
return nil, err
}
return c.findAuthorizationByID(ctx, tx, id)
Expand Down
9 changes: 7 additions & 2 deletions bolt/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func (c *Client) findBucketByID(ctx context.Context, tx *bolt.Tx, id platform.ID
}

v := tx.Bucket(bucketBucket).Get(encodedID)

if len(v) == 0 {
// TODO: Make standard error
return nil, fmt.Errorf("bucket not found")
Expand Down Expand Up @@ -106,8 +105,14 @@ func (c *Client) findBucketByName(ctx context.Context, tx *bolt.Tx, orgID platfo
return nil, err
}

buf := tx.Bucket(bucketIndex).Get(key)
if buf == nil {
// TODO: Make standard error
return nil, fmt.Errorf("bucket not found")
}

var id platform.ID
if err := id.Decode(tx.Bucket(bucketIndex).Get(key)); err != nil {
if err := id.Decode(buf); err != nil {
return nil, err
}
return c.findBucketByID(ctx, tx, id)
Expand Down
3 changes: 1 addition & 2 deletions bolt/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ func (c *Client) findDashboardByID(ctx context.Context, tx *bolt.Tx, id platform
return nil, err
}

var d platform.Dashboard
v := tx.Bucket(dashboardBucket).Get(encodedID)

if len(v) == 0 {
return nil, platform.ErrDashboardNotFound
}

var d platform.Dashboard
if err := json.Unmarshal(v, &d); err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions bolt/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func (c *Client) findMacroByID(ctx context.Context, tx *bolt.Tx, id platform.ID)
if err != nil {
return nil, err
}
d := tx.Bucket(macroBucket).Get(encID)

d := tx.Bucket(macroBucket).Get(encID)
if d == nil {
return nil, kerrors.Errorf(kerrors.NotFound, "macro with ID %v not found", id)
}
Expand Down Expand Up @@ -151,7 +151,6 @@ func (c *Client) DeleteMacro(ctx context.Context, id platform.ID) error {
}

d := b.Get(encID)

if d == nil {
return kerrors.Errorf(kerrors.NotFound, "macro with ID %v not found", id)
}
Expand Down
11 changes: 8 additions & 3 deletions bolt/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ func (c *Client) findOrganizationByID(ctx context.Context, tx *bolt.Tx, id platf
return nil, err
}

var o platform.Organization
v := tx.Bucket(organizationBucket).Get(encodedID)

if len(v) == 0 {
// TODO: Make standard error
return nil, fmt.Errorf("organization not found")
}

var o platform.Organization
if err := json.Unmarshal(v, &o); err != nil {
return nil, err
}
Expand All @@ -84,8 +83,14 @@ func (c *Client) FindOrganizationByName(ctx context.Context, n string) (*platfor
}

func (c *Client) findOrganizationByName(ctx context.Context, tx *bolt.Tx, n string) (*platform.Organization, error) {
o := tx.Bucket(organizationIndex).Get(organizationIndexKey(n))
if o == nil {
// TODO: Make standard error
return nil, fmt.Errorf("organization not found")
}

var id platform.ID
if err := id.Decode(tx.Bucket(organizationIndex).Get(organizationIndexKey(n))); err != nil {
if err := id.Decode(o); err != nil {
return nil, err
}
return c.findOrganizationByID(ctx, tx, id)
Expand Down
1 change: 0 additions & 1 deletion bolt/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (c *Client) FindSession(ctx context.Context, key string) (*platform.Session

func (c *Client) findSession(ctx context.Context, tx *bolt.Tx, key string) (*platform.Session, error) {
v := tx.Bucket(sessionBucket).Get([]byte(key))

if len(v) == 0 {
return nil, fmt.Errorf("session not found")
}
Expand Down
1 change: 0 additions & 1 deletion bolt/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func (c *Client) findSourceByID(ctx context.Context, tx *bolt.Tx, id platform.ID
}

v := tx.Bucket(sourceBucket).Get(encodedID)

if len(v) == 0 {
return nil, platform.ErrSourceNotFound
}
Expand Down
1 change: 0 additions & 1 deletion bolt/user_resource_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func (c *Client) findUserResourceMapping(ctx context.Context, tx *bolt.Tx, resou
}

v := tx.Bucket(userResourceMappingBucket).Get(key)

if len(v) == 0 {
return nil, fmt.Errorf("userResource mapping not found")
}
Expand Down
1 change: 0 additions & 1 deletion bolt/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func (c *Client) findViewByID(ctx context.Context, tx *bolt.Tx, id platform.ID)
}

v := tx.Bucket(viewBucket).Get(encodedID)

if len(v) == 0 {
return nil, platform.ErrViewNotFound
}
Expand Down
41 changes: 35 additions & 6 deletions testing/bucket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,22 @@ func FindBuckets(
},
},
},
{
name: "missing bucket returns no buckets",
fields: BucketFields{
Organizations: []*platform.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
},
},
Buckets: []*platform.Bucket{},
},
args: args{
name: "xyz",
},
wants: wants{},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -904,6 +920,25 @@ func FindBucket(
},
},
},
{
name: "missing bucket returns error",
fields: BucketFields{
Organizations: []*platform.Organization{
{
Name: "theorg",
ID: MustIDBase16(orgOneID),
},
},
Buckets: []*platform.Bucket{},
},
args: args{
name: "xyz",
organizationID: MustIDBase16(orgOneID),
},
wants: wants{
err: fmt.Errorf("no results found"),
},
},
}

for _, tt := range tests {
Expand All @@ -924,12 +959,6 @@ func FindBucket(
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(bucket, tt.wants.bucket, bucketCmpOptions...); diff != "" {
t.Errorf("buckets are different -got/+want\ndiff %s", diff)
}
Expand Down
18 changes: 12 additions & 6 deletions testing/organization_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,18 @@ func FindOrganization(
},
},
},
{
name: "missing organization returns error",
fields: OrganizationFields{
Organizations: []*platform.Organization{},
},
args: args{
name: "abc",
},
wants: wants{
err: fmt.Errorf("organization not found"),
},
},
}

for _, tt := range tests {
Expand All @@ -578,12 +590,6 @@ func FindOrganization(
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("organizations are different -got/+want\ndiff %s", diff)
}
Expand Down

0 comments on commit 3be5656

Please sign in to comment.