-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
300 additions
and
0 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,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 | ||
} | ||
}) | ||
} | ||
} |