-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Khabi/tests
[Feat] Adding Tests and fixes
- Loading branch information
Showing
10 changed files
with
175 additions
and
19 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,19 @@ | ||
name: Pull Request | ||
on: | ||
- pull_request | ||
|
||
jobs: | ||
test: | ||
name: Test Golang Code | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
- uses: dominikh/[email protected] | ||
- uses: arduino/setup-task@v1 | ||
|
||
- name: Run Tests | ||
run: task go:test | ||
|
||
- name: Run Staticcheck | ||
run: task go:check |
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 |
---|---|---|
|
@@ -19,3 +19,6 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
# Ignore Taskfile cache | ||
.task/ |
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,24 @@ | ||
version: "3" | ||
|
||
tasks: | ||
go:generate: | ||
desc: Generate Code | ||
sources: | ||
- catalog.go | ||
cmds: | ||
- go generate | ||
|
||
go:test: | ||
desc: Run Tests | ||
cmds: | ||
- go test ./... | ||
|
||
go:check: | ||
desc: Run GoLang Checks | ||
cmds: | ||
- staticcheck ./... | ||
|
||
go:fmt: | ||
desc: Run GoLang formatter | ||
cmds: | ||
- gofmt ./ |
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,41 @@ | ||
package govee | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBroadcast(t *testing.T) { | ||
bcast := &Broadcaster{ | ||
interval: 1 * time.Second, | ||
address: "127.0.0.1:4100", | ||
message: []byte(`test message`), | ||
} | ||
ctx, cancel := context.WithCancel(context.TODO()) | ||
|
||
go bcast.Run(ctx) | ||
|
||
addr, err := net.ResolveUDPAddr("udp4", "239.255.255.250:4100") | ||
if err != nil { | ||
assert.NoError(t, err) | ||
} | ||
|
||
conn, err := net.ListenMulticastUDP("udp4", nil, addr) | ||
if err != nil { | ||
assert.NoError(t, err) | ||
} | ||
|
||
buffer := make([]byte, 8192) | ||
n, _, err := conn.ReadFromUDP(buffer) | ||
if err != nil { | ||
assert.NoError(t, err) | ||
} | ||
|
||
cancel() | ||
|
||
assert.Equal(t, string("test message"), string(buffer[:n])) | ||
} |
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,44 @@ | ||
package govee | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidLookupBySKU(t *testing.T) { | ||
for i := 1; i <= 5; i++ { | ||
entry := catalog[i] | ||
sku := entry.SKU | ||
|
||
lentry := LookupBySKU(sku) | ||
assert.Equal(t, sku, lentry.SKU) | ||
assert.Equal(t, entry.Kind, lentry.Kind) | ||
assert.Equal(t, entry.Name, lentry.Name) | ||
assert.Equal(t, entry.ProductURL, lentry.ProductURL) | ||
} | ||
} | ||
|
||
func TestInvalidLookupBySKU(t *testing.T) { | ||
lentry := LookupBySKU("FAKE") | ||
assert.Nil(t, lentry) | ||
} | ||
|
||
func TestLookupByKind(t *testing.T) { | ||
lentry := LookupByKind(WallLights) | ||
assert.NotNil(t, lentry) | ||
count := 0 | ||
for _, e := range catalog { | ||
if e.Kind == WallLights { | ||
count++ | ||
} | ||
} | ||
assert.Len(t, lentry, count) | ||
} | ||
|
||
func TestInvalidLookupByKind(t *testing.T) { | ||
var invalid Kind = "INVALID" | ||
|
||
lentry := LookupByKind(invalid) | ||
assert.Nil(t, lentry) | ||
} |
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 |
---|---|---|
@@ -1,2 +1,12 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= | ||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |