Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Replace manual assertions with testify in application layer
Browse files Browse the repository at this point in the history
  • Loading branch information
dietrichm committed Aug 3, 2023
1 parent a2edc9f commit a9dee29
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 179 deletions.
44 changes: 10 additions & 34 deletions application/commands/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/dietrichm/admirer/domain"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestList(t *testing.T) {
Expand Down Expand Up @@ -34,16 +35,12 @@ func TestList(t *testing.T) {

got, err := executeList(serviceLoader, 5, "foo")

if err != nil {
t.Errorf("Unexpected error: %v", err)
}

expected := `Awesome Artist - Blam (Instrumental)
Foo & Bar - Mr. Testy
`
if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}

assert.NoError(t, err)
assert.Equal(t, expected, got)
})

t.Run("returns error for unknown service", func(t *testing.T) {
Expand All @@ -55,19 +52,8 @@ Foo & Bar - Mr. Testy

output, err := executeList(serviceLoader, 5, "foobar")

if output != "" {
t.Errorf("Unexpected output: %v", output)
}

if err == nil {
t.Fatal("Expected an error")
}

got := err.Error()

if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
assert.EqualError(t, err, expected)
assert.Empty(t, output)
})

t.Run("returns error when service is not authenticated", func(t *testing.T) {
Expand All @@ -83,13 +69,8 @@ Foo & Bar - Mr. Testy

output, err := executeList(serviceLoader, 3, "foo")

if err == nil {
t.Error("Expected an error")
}

if output != "" {
t.Errorf("Unexpected output: %v", output)
}
assert.Error(t, err)
assert.Empty(t, output)
})

t.Run("returns error when failing to load loved tracks", func(t *testing.T) {
Expand All @@ -105,13 +86,8 @@ Foo & Bar - Mr. Testy

output, err := executeList(serviceLoader, 3, "foo")

if err == nil {
t.Error("Expected an error")
}

if output != "" {
t.Errorf("Unexpected output: %v", output)
}
assert.Error(t, err)
assert.Empty(t, output)
})
}

Expand Down
68 changes: 12 additions & 56 deletions application/commands/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/dietrichm/admirer/domain"
"github.com/dietrichm/admirer/infrastructure/authentication"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestLogin(t *testing.T) {
Expand All @@ -33,13 +34,8 @@ func TestLogin(t *testing.T) {
Logged in on Service as Joe
`

if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}

if err != nil {
t.Errorf("Unexpected error: %v", err)
}
assert.NoError(t, err)
assert.Equal(t, expected, got)
})

t.Run("authenticates on service with provided auth code", func(t *testing.T) {
Expand All @@ -59,13 +55,8 @@ Logged in on Service as Joe
got, err := executeLogin(serviceLoader, callbackProvider, "foobar", "authcode")
expected := "Logged in on Service as Joe\n"

if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}

if err != nil {
t.Errorf("Unexpected error: %v", err)
}
assert.NoError(t, err)
assert.Equal(t, expected, got)
})

t.Run("returns error for unknown service", func(t *testing.T) {
Expand All @@ -79,19 +70,8 @@ Logged in on Service as Joe

output, err := executeLogin(serviceLoader, callbackProvider, "foobar")

if output != "" {
t.Errorf("Unexpected output: %v", output)
}

if err == nil {
t.Fatal("Expected an error")
}

got := err.Error()

if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
assert.EqualError(t, err, expected)
assert.Empty(t, output)
})

t.Run("returns error when failing to read code from callback provider", func(t *testing.T) {
Expand All @@ -111,9 +91,7 @@ Logged in on Service as Joe

_, err := executeLogin(serviceLoader, callbackProvider, "foobar")

if err == nil {
t.Fatal("Expected an error")
}
assert.Error(t, err)
})

t.Run("returns error for failed authentication", func(t *testing.T) {
Expand All @@ -131,19 +109,8 @@ Logged in on Service as Joe

output, err := executeLogin(serviceLoader, callbackProvider, "foobar", "authcode")

if output != "" {
t.Errorf("Unexpected output: %v", output)
}

if err == nil {
t.Fatal("Expected an error")
}

got := err.Error()

if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
assert.EqualError(t, err, expected)
assert.Empty(t, output)
})

t.Run("returns error for failed username retrieval", func(t *testing.T) {
Expand All @@ -162,19 +129,8 @@ Logged in on Service as Joe

output, err := executeLogin(serviceLoader, callbackProvider, "foobar", "authcode")

if output != "" {
t.Errorf("Unexpected output: %v", output)
}

if err == nil {
t.Fatal("Expected an error")
}

got := err.Error()

if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
assert.EqualError(t, err, expected)
assert.Empty(t, output)
})
}

Expand Down
49 changes: 9 additions & 40 deletions application/commands/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/dietrichm/admirer/domain"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestStatus(t *testing.T) {
Expand Down Expand Up @@ -37,13 +38,8 @@ Bar
`
got, err := executeStatus(serviceLoader)

if err != nil {
t.Errorf("Unexpected error: %v", err)
}

if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
assert.NoError(t, err)
assert.Equal(t, expected, got)
})

t.Run("returns error when failing to load service", func(t *testing.T) {
Expand All @@ -56,19 +52,8 @@ Bar

output, err := executeStatus(serviceLoader)

if err == nil {
t.Fatal("Expected an error")
}

got := err.Error()

if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}

if output != "" {
t.Errorf("Unexpected output: %v", output)
}
assert.EqualError(t, err, expected)
assert.Empty(t, output)
})

t.Run("returns message when not authenticated", func(t *testing.T) {
Expand All @@ -88,13 +73,8 @@ Bar
`
got, err := executeStatus(serviceLoader)

if err != nil {
t.Errorf("Unexpected error: %v", err)
}

if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
assert.NoError(t, err)
assert.Equal(t, expected, got)
})

t.Run("returns error when failed to get username", func(t *testing.T) {
Expand All @@ -112,19 +92,8 @@ Bar

output, err := executeStatus(serviceLoader)

if err == nil {
t.Fatal("Expected an error")
}

got := err.Error()

if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}

if output != "" {
t.Errorf("Unexpected output: %v", output)
}
assert.EqualError(t, err, expected)
assert.Empty(t, output)
})
}

Expand Down
Loading

0 comments on commit a9dee29

Please sign in to comment.