Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Uses subtests to separate happy path from error cases.
Browse files Browse the repository at this point in the history
Signed-off-by: Leah Hanson <lhanson@pivotal.io>
  • Loading branch information
Urvashi Reddy authored and astrieanna committed Jul 30, 2019
1 parent 3f81a33 commit a4c2465
Showing 1 changed file with 87 additions and 98 deletions.
185 changes: 87 additions & 98 deletions cmd/duffle/claims_show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,53 @@ import (
"github.com/stretchr/testify/assert"
)

//happy path: show claim
func TestRunClaimShow(t *testing.T) {
var buf bytes.Buffer

csc := claimsShowCmd{
Name: "myclaim",
OnlyBundle: false,
Storage: mockClaimStore(),
}
expectedClaim := claim.Claim{
Name: "myclaim",
Bundle: &bundle.Bundle{
Name: "mybundle",
Version: "0.1.2",
Outputs: &bundle.OutputsDefinition{
Fields: map[string]bundle.OutputDefinition{
"some-output": bundle.OutputDefinition{
Path: "/some-output-path",
t.Run("happy path", func(t *testing.T) {
csc := claimsShowCmd{
Name: "myclaim",
OnlyBundle: false,
Storage: mockClaimStore(),
}
expectedClaim := claim.Claim{
Name: "myclaim",
Bundle: &bundle.Bundle{
Name: "mybundle",
Version: "0.1.2",
Outputs: &bundle.OutputsDefinition{
Fields: map[string]bundle.OutputDefinition{
"some-output": bundle.OutputDefinition{
Path: "/some-output-path",
},
},
},
},
},
Outputs: map[string]interface{}{
"some-output": "some output contents",
},
}
csc.Storage.Store(expectedClaim)
err := csc.runClaimShow(&buf)
assert.NoError(t, err)

var got claim.Claim
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Fatalf("Attempted to unmarshal claim, but got %s", err)
}
assert.Equal(t, expectedClaim, got)

// error case: when storage returns error
csc = claimsShowCmd{
Name: "unknownclaim",
OnlyBundle: false,
Storage: mockClaimStore(),
}

err = csc.runClaimShow(&buf)
assert.EqualError(t, err, "not found")
Outputs: map[string]interface{}{
"some-output": "some output contents",
},
}
csc.Storage.Store(expectedClaim)
err := csc.runClaimShow(&buf)
assert.NoError(t, err)

var got claim.Claim
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Fatalf("Attempted to unmarshal claim, but got %s", err)
}
assert.Equal(t, expectedClaim, got)
})

t.Run("error case: when storage returns error", func(t *testing.T) {
csc := claimsShowCmd{
Name: "unknownclaim",
OnlyBundle: false,
Storage: mockClaimStore(),
}

err := csc.runClaimShow(&buf)
assert.EqualError(t, err, "not found")
})
}

// happy path: when --bundle is specified
Expand Down Expand Up @@ -96,15 +98,8 @@ func TestRunClaimShow_Bundle(t *testing.T) {
assert.Equal(t, *expectedClaim.Bundle, got)
}

// happy path: when --output is specified
func TestRunClaimShow_Output(t *testing.T) {
var buf bytes.Buffer
csc := claimsShowCmd{
Name: "myclaim",
Output: "some-output",
Storage: mockClaimStore(),
}

expectedClaim := claim.Claim{
Name: "myclaim",
Bundle: &bundle.Bundle{
Expand All @@ -122,10 +117,32 @@ func TestRunClaimShow_Output(t *testing.T) {
"some-output": "some output contents",
},
}
csc.Storage.Store(expectedClaim)
err := csc.runClaimShow(&buf)
assert.NoError(t, err)
assert.Equal(t, expectedClaim.Outputs["some-output"], string(buf.Bytes()))

t.Run("happy path", func(t *testing.T) {
csc := claimsShowCmd{
Name: "myclaim",
Output: "some-output",
Storage: mockClaimStore(),
}
csc.Storage.Store(expectedClaim)

err := csc.runClaimShow(&buf)
assert.NoError(t, err)
assert.Equal(t, expectedClaim.Outputs["some-output"], string(buf.Bytes()))
})

t.Run("error case: when --output name is an unknown output", func(t *testing.T) {
csc := claimsShowCmd{
Name: "myclaim",
Output: "not-an-output",
Storage: mockClaimStore(),
}
csc.Storage.Store(expectedClaim)

err := csc.runClaimShow(&buf)
assert.EqualError(t, err, "unknown output name: not-an-output")
})

}

//error case: when --bundle and --output are both specified
Expand All @@ -141,41 +158,8 @@ func TestRunClaimShow_InvalidFlags(t *testing.T) {
assert.EqualError(t, err, "invalid flags: at most one of --bundle and --output can be specified")
}

// error case: when --output name is an unknown output
func TestRunClaimShow_UnknownOutput(t *testing.T) {
var buf bytes.Buffer
csc := claimsShowCmd{
Name: "myclaim",
Output: "not-an-output",
Storage: mockClaimStore(),
}

expectedClaim := claim.Claim{
Name: "myclaim",
Bundle: &bundle.Bundle{
Name: "mybundle",
Version: "0.1.2",
Outputs: &bundle.OutputsDefinition{
Fields: map[string]bundle.OutputDefinition{
"some-output": bundle.OutputDefinition{
Path: "/some-output-path",
},
},
},
},
Outputs: map[string]interface{}{
"some-output": "some output contents",
},
}
csc.Storage.Store(expectedClaim)
err := csc.runClaimShow(&buf)
assert.EqualError(t, err, "unknown output name: not-an-output")
}

// happy path: printing the output
func TestDisplayJSON(t *testing.T) {
buf := bytes.NewBuffer([]byte{})

expectedClaim := claim.Claim{
Name: "myclaim",
Bundle: &bundle.Bundle{
Expand All @@ -193,22 +177,27 @@ func TestDisplayJSON(t *testing.T) {
"some-output": "some output contents",
},
}
err := displayAsJSON(buf, expectedClaim)
assert.NoError(t, err)

var got claim.Claim
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Fatalf("Attempted to unmarshal claim, but got %s", err)
}
assert.Equal(t, expectedClaim, got)

// error case: can't marshal claim
err = displayAsJSON(buf, func() {})
assert.EqualError(t, err, "json: unsupported type: func()")

// error case: can't write to output
err = displayAsJSON(mockWriter{err: errors.New("failed to write")}, expectedClaim)
assert.EqualError(t, err, "failed to write")
t.Run("happy path", func(t *testing.T) {
err := displayAsJSON(buf, expectedClaim)
assert.NoError(t, err)

var got claim.Claim
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Fatalf("Attempted to unmarshal claim, but got %s", err)
}
assert.Equal(t, expectedClaim, got)
})

t.Run("error case: can't marshal claim", func(t *testing.T) {
err := displayAsJSON(buf, func() {})
assert.EqualError(t, err, "json: unsupported type: func()")
})

t.Run("error case: can't write to output", func(t *testing.T) {
err := displayAsJSON(mockWriter{err: errors.New("failed to write")}, expectedClaim)
assert.EqualError(t, err, "failed to write")
})
}

type mockWriter struct {
Expand Down

0 comments on commit a4c2465

Please sign in to comment.