From 5ae352e3f950da3a9f1f6ea361a135e35e608a05 Mon Sep 17 00:00:00 2001 From: Joshua Rich Date: Tue, 16 Apr 2024 21:13:31 +1000 Subject: [PATCH] test(hass): :white_check_mark: add gobRegistry tests --- .../hass/sensor/registry/registry_gob_test.go | 300 ++++++++++++++++++ 1 file changed, 300 insertions(+) create mode 100644 internal/hass/sensor/registry/registry_gob_test.go diff --git a/internal/hass/sensor/registry/registry_gob_test.go b/internal/hass/sensor/registry/registry_gob_test.go new file mode 100644 index 000000000..e1f8d2b5b --- /dev/null +++ b/internal/hass/sensor/registry/registry_gob_test.go @@ -0,0 +1,300 @@ +// Copyright (c) 2024 Joshua Rich +// +// 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 + } + }) + } +}