Skip to content

Commit

Permalink
test(hass): ✅ add gobRegistry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Apr 16, 2024
1 parent 6ef7088 commit 5ae352e
Showing 1 changed file with 300 additions and 0 deletions.
300 changes: 300 additions & 0 deletions internal/hass/sensor/registry/registry_gob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
// Copyright (c) 2024 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

package registry

import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

var testSensorMap = map[string]metadata{
"registeredSensor": {Disabled: false, Registered: true},
"unRegisteredSensor": {Disabled: false, Registered: false},
"disabledSensor": {Disabled: true, Registered: true},
}

func newTestRegistry(t *testing.T) *gobRegistry {
registryPath = t.TempDir()
testRegistry, err := Load()
assert.Nil(t, err)
testRegistry.sensors = testSensorMap
err = testRegistry.write()
assert.Nil(t, err)
return testRegistry
}

func Test_gobRegistry_write(t *testing.T) {
type fields struct {
sensors map[string]metadata
mu sync.Mutex
}
type args struct {
path string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "default",
args: args{path: t.TempDir()},
fields: fields{sensors: testSensorMap},
wantErr: false,
},
{
name: "invalid path",
args: args{path: "/nonexistent"},
fields: fields{sensors: testSensorMap},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := &gobRegistry{
sensors: tt.fields.sensors,
mu: tt.fields.mu,
}
registryPath = tt.args.path
if err := g.write(); (err != nil) != tt.wantErr {
t.Errorf("gobRegistry.write() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func Test_gobRegistry_read(t *testing.T) {
type fields struct {
sensors map[string]metadata
mu sync.Mutex
}
type args struct {
path string
}

tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "default",
args: args{path: t.TempDir()},
fields: fields{sensors: testSensorMap},
wantErr: false,
},
{
name: "invalid path",
args: args{path: "/nonexistent"},
fields: fields{sensors: testSensorMap},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := &gobRegistry{
sensors: tt.fields.sensors,
mu: tt.fields.mu,
}
registryPath = tt.args.path
if err := g.read(); (err != nil) != tt.wantErr {
t.Errorf("gobRegistry.read() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func Test_gobRegistry_IsDisabled(t *testing.T) {
type fields struct {
sensors map[string]metadata
mu sync.Mutex
}
type args struct {
id string
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "disabled sensor",
fields: fields{sensors: testSensorMap},
args: args{id: "disabledSensor"},
want: true,
},
{
name: "not disabled sensor",
fields: fields{sensors: testSensorMap},
args: args{id: "registeredSensor"},
want: false,
},
{
name: "not found",
fields: fields{sensors: testSensorMap},
args: args{id: "nonexistent"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := newTestRegistry(t)
if got := g.IsDisabled(tt.args.id); got != tt.want {
t.Errorf("gobRegistry.IsDisabled() = %v, want %v", got, tt.want)
}
})
}
}

func Test_gobRegistry_IsRegistered(t *testing.T) {
type fields struct {
sensors map[string]metadata
mu sync.Mutex
}
type args struct {
id string
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "registered sensor",
fields: fields{sensors: testSensorMap},
args: args{id: "registeredSensor"},
want: true,
},
{
name: "not registered sensor",
fields: fields{sensors: testSensorMap},
args: args{id: "unRegistered"},
want: false,
},
{
name: "not found",
fields: fields{sensors: testSensorMap},
args: args{id: "nonexistent"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := newTestRegistry(t)
if got := g.IsRegistered(tt.args.id); got != tt.want {
t.Errorf("gobRegistry.IsRegistered() = %v, want %v", got, tt.want)
}
})
}
}

func Test_gobRegistry_SetDisabled(t *testing.T) {
type fields struct {
sensors map[string]metadata
mu sync.Mutex
}
type args struct {
id string
value bool
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "change disabled state",
fields: fields{sensors: testSensorMap},
args: args{id: "disabledSensor", value: false},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := newTestRegistry(t)
if err := g.SetDisabled(tt.args.id, tt.args.value); (err != nil) != tt.wantErr {
t.Errorf("gobRegistry.SetDisabled() error = %v, wantErr %v", err, tt.wantErr)
}
if g.IsDisabled(tt.args.id) != tt.args.value {
t.Error("gobRegistry.SetDisabled() not changed")
}
})
}
}

func Test_gobRegistry_SetRegistered(t *testing.T) {
type fields struct {
sensors map[string]metadata
mu sync.Mutex
}
type args struct {
id string
value bool
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "change registered state",
fields: fields{sensors: testSensorMap},
args: args{id: "unRegisteredSensor", value: true},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := newTestRegistry(t)
if err := g.SetRegistered(tt.args.id, tt.args.value); (err != nil) != tt.wantErr {
t.Errorf("gobRegistry.SetRegistered() error = %v, wantErr %v", err, tt.wantErr)
}
if g.IsRegistered(tt.args.id) != tt.args.value {
t.Error("gobRegistry.SetRegistered() not changed")
}
})
}
}

func TestLoad(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "good path",
args: args{path: t.TempDir()},
wantErr: false,
},
{
name: "bad path",
args: args{path: "/nonexistent"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
registryPath = tt.args.path
_, err := Load()
if (err != nil) != tt.wantErr {
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}

0 comments on commit 5ae352e

Please sign in to comment.