-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
treewide: Add tests to all Go packages and cleanup (#2)
Test all packages, use better naming and clean up.
- Loading branch information
Showing
7 changed files
with
417 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Run all Go tests | ||
on: | ||
pull_request: | ||
jobs: | ||
go-test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Install devbox | ||
uses: jetify-com/[email protected] | ||
with: | ||
enable-cache: 'true' | ||
|
||
- name: Prepare Go build cache | ||
id: go-cache-paths | ||
run: | | ||
devbox run -- echo "go_build_cache=$(go env GOCACHE)" >> $GITHUB_OUTPUT | ||
devbox run -- echo "go_mod_cache=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT | ||
# Cache go build cache, used to speedup go test | ||
- name: Go Build Cache | ||
uses: actions/cache@v2 | ||
with: | ||
path: ${{ steps.go-cache-paths.outputs.go_build_cache }} | ||
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }} | ||
|
||
# Cache go mod cache, used to speedup builds | ||
- name: Go Mod Cache | ||
uses: actions/cache@v2 | ||
with: | ||
path: ${{ steps.go-cache-paths.outputs.go_mod_cache }} | ||
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }} | ||
|
||
- name: Run the tests | ||
if: success() | ||
run: devbox run -- go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,7 @@ go.work.sum | |
|
||
# env file | ||
.env | ||
|
||
# Binaries | ||
/main | ||
/signal-receiver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package receiver | ||
|
||
import ( | ||
"reflect" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestFlush(t *testing.T) { | ||
t.Run("returns empty list when no messages was found", func(t *testing.T) { | ||
c := &Client{messages: []Message{}} | ||
if want, got := []Message{}, c.Flush(); !reflect.DeepEqual(want, got) { | ||
t.Errorf("want %#v got %#v", want, got) | ||
} | ||
}) | ||
|
||
t.Run("return the message if only one is there", func(t *testing.T) { | ||
c := &Client{messages: []Message{Message{Account: "1"}}} | ||
|
||
if want, got := []Message{Message{Account: "1"}}, c.Flush(); !reflect.DeepEqual(want, got) { | ||
t.Errorf("want %#v got %#v", want, got) | ||
} | ||
}) | ||
|
||
t.Run("return messages in order", func(t *testing.T) { | ||
c := &Client{messages: []Message{ | ||
Message{Account: "0"}, | ||
Message{Account: "1"}, | ||
Message{Account: "2"}, | ||
}} | ||
|
||
want := []Message{ | ||
Message{Account: "0"}, | ||
Message{Account: "1"}, | ||
Message{Account: "2"}, | ||
} | ||
got := c.Flush() | ||
if !reflect.DeepEqual(want, got) { | ||
t.Errorf("want\n%#v\ngot\n%#v", want, got) | ||
} | ||
}) | ||
} | ||
|
||
func TestPop(t *testing.T) { | ||
t.Run("returns null when no messages was found", func(t *testing.T) { | ||
c := &Client{messages: []Message{}} | ||
var want *Message | ||
if got := c.Pop(); want != got { | ||
t.Errorf("want %#v got %#v", want, got) | ||
} | ||
}) | ||
|
||
t.Run("return the message if only one is there", func(t *testing.T) { | ||
c := &Client{messages: []Message{Message{Account: "1"}}} | ||
|
||
want := Message{Account: "1"} | ||
got := c.Pop() | ||
if !reflect.DeepEqual(want, *got) { | ||
t.Errorf("want\n%#v\ngot\n%#v", want, got) | ||
} | ||
}) | ||
|
||
t.Run("return messages in order", func(t *testing.T) { | ||
c := &Client{messages: []Message{ | ||
Message{Account: "0"}, | ||
Message{Account: "1"}, | ||
Message{Account: "2"}, | ||
}} | ||
|
||
for i := range c.messages { | ||
want := Message{Account: strconv.Itoa(i)} | ||
got := c.Pop() | ||
if !reflect.DeepEqual(want, *got) { | ||
t.Errorf("want\n%#v\ngot\n%#v", want, got) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.