Skip to content

Commit

Permalink
Merge pull request #1 from Khabi/tests
Browse files Browse the repository at this point in the history
[Feat] Adding Tests and fixes
  • Loading branch information
Khabi authored May 29, 2024
2 parents a38e807 + 65e9eeb commit b4467c2
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 19 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/pull_request.yaml
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@

# Go workspace file
go.work

# Ignore Taskfile cache
.task/
24 changes: 24 additions & 0 deletions Taskfile.yaml
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 ./
41 changes: 41 additions & 0 deletions broadcast_test.go
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]))
}
23 changes: 13 additions & 10 deletions catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var catalog = []Spec{
{FloorLamps, "H607C", "Govee RGBICWW Floor Lamp 2", "https://app-mall.govee.com/product-detail?sku=H607C111"},
{LandscapeLighting, "H7050", "Smart Ground Lights", ""},
{LandscapeLighting, "H7051", "Smart Ground Lights", "https://app-mall.govee.com/product-detail?sku=H7051111"},
{LandscapeLighting, "H7052", "15m Outdoor Ground Lights", ""},
{LandscapeLighting, "H7053", "30m Outdoor Ground Lights", ""},
{LandscapeLighting, "H7052", "15m Outdoor Ground Lights", "https://app-mall.govee.com/product-detail?sku=H7052111"},
{LandscapeLighting, "H7053", "30m Outdoor Ground Lights", "https://app-mall.govee.com/product-detail?sku=H7053111"},
{LandscapeLighting, "H7055", "Govee RGBIC Path Lights", "https://app-mall.govee.com/product-detail?sku=H7055111"},
{LandscapeLighting, "H705A", "30m Permanent Outdoor Lights", "https://app-mall.govee.com/product-detail?sku=H705A1D1"},
{LandscapeLighting, "H705B", "15m Permanent Outdoor Lights", "https://app-mall.govee.com/product-detail?sku=H705B1D1"},
Expand All @@ -45,7 +45,7 @@ var catalog = []Spec{
{LandscapeLighting, "H7060", "Govee RGBIC Flood lights", "https://app-mall.govee.com/product-detail?sku=H7060112"},
{LandscapeLighting, "H7061", "2 Pack RGBIC Flood Lights", ""},
{LandscapeLighting, "H7062", "6 Pack RGBIC Flood Lights", "https://app-mall.govee.com/product-detail?sku=H7062111"},
{LandscapeLighting, "H7063", "Govee Outdoor Flood Light", ""},
{LandscapeLighting, "H7063", "Govee Outdoor Flood Light", "https://app-mall.govee.com/product-detail?sku=H7063111"},
{LandscapeLighting, "H7065", "Govee RGBIC Spotlights", ""},
{LandscapeLighting, "H7066", "Govee RGBIC Spotlights", "https://app-mall.govee.com/product-detail?sku=H7066111"},
{LandscapeLighting, "H706A", "30m Permanent Outdoor Lights Pro", "https://app-mall.govee.com/product-detail?sku=H706A101"},
Expand Down Expand Up @@ -143,26 +143,29 @@ var catalog = []Spec{

// LookupBySKU searches the catalog for a device
// matching the given SKU.
func LookupBySKU(sku string) Spec {
func LookupBySKU(sku string) *Spec {
for _, device := range catalog {
if device.SKU == sku {
return device
return &device
}
}

return Spec{}
return nil
}

// LookupByKind searches the catalog and returns
// all the devices matching it.
func LookupByKind(kind Kind) []Spec {
data := []Spec{}
func LookupByKind(kind Kind) []*Spec {
data := []*Spec{}

for _, device := range catalog {
if device.Kind == kind {
data = append(data, device)
data = append(data, &device)
}
}

return data
if len(data) > 0 {
return data
}
return nil
}
17 changes: 10 additions & 7 deletions catalog_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,30 @@ var catalog = []Spec{
// LookupBySKU searches the catalog for a device
// matching the given SKU.
func LookupBySKU(sku string) Spec {
func LookupBySKU(sku string) *Spec {
for _, device := range catalog{
if device.SKU == sku {
return device
return &device
}
}
return Spec{}
return nil
}
// LookupByKind searches the catalog and returns
// all the devices matching it.
func LookupByKind(kind Kind) []Spec {
data := []Spec{}
func LookupByKind(kind Kind) []*Spec {
data := []*Spec{}
for _, device := range catalog {
if device.Kind == kind {
data = append(data, device)
data = append(data, &device)
}
}
return data
if len(data) > 0 {
return data
}
return nil
}
`
44 changes: 44 additions & 0 deletions catalog_test.go
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)
}
2 changes: 1 addition & 1 deletion device.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Device struct {
IPAddr net.IP
MAC net.HardwareAddr
LastSeen time.Time
Spec Spec
Spec *Spec
}

// Status is the current status of the device.
Expand Down
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/Khabi/go-vee

go 1.22.2

require golang.org/x/sync v0.7.0
require (
github.com/stretchr/testify v1.9.0
golang.org/x/sync v0.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
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=

0 comments on commit b4467c2

Please sign in to comment.