mirror of
https://github.com/unpoller/unpoller.git
synced 2026-03-31 06:33:57 -04:00
rename package per per comment, add comments, and drop unnecessary compile time check
This commit is contained in:
66
pkg/unittest/dep.go
Normal file
66
pkg/unittest/dep.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package unittest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/unpoller/unifi/mocks"
|
||||
"github.com/unpoller/unpoller/pkg/inputunifi"
|
||||
"github.com/unpoller/unpoller/pkg/poller"
|
||||
)
|
||||
|
||||
type TestRig struct {
|
||||
MockServer *mocks.MockHTTPTestServer
|
||||
Collector *poller.TestCollector
|
||||
InputUnifi *inputunifi.InputUnifi
|
||||
Controller *inputunifi.Controller
|
||||
}
|
||||
|
||||
func NewTestSetup(t *testing.T) *TestRig {
|
||||
srv := mocks.NewMockHTTPTestServer()
|
||||
testCollector := poller.NewTestCollector(t)
|
||||
|
||||
enabled := true
|
||||
controller := inputunifi.Controller{
|
||||
SaveAnomal: &enabled,
|
||||
SaveAlarms: &enabled,
|
||||
SaveEvents: &enabled,
|
||||
SaveIDS: &enabled,
|
||||
SaveDPI: &enabled,
|
||||
SaveRogue: &enabled,
|
||||
SaveSites: &enabled,
|
||||
URL: srv.Server.URL,
|
||||
}
|
||||
in := &inputunifi.InputUnifi{
|
||||
Logger: testCollector.Logger,
|
||||
Config: &inputunifi.Config{
|
||||
Disable: false,
|
||||
Default: controller,
|
||||
Controllers: []*inputunifi.Controller{&controller},
|
||||
},
|
||||
}
|
||||
testCollector.AddInput(&poller.InputPlugin{
|
||||
Name: "unifi",
|
||||
Input: in,
|
||||
})
|
||||
|
||||
return &TestRig{
|
||||
MockServer: srv,
|
||||
Collector: testCollector,
|
||||
InputUnifi: in,
|
||||
Controller: &controller,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TestRig) Initialize() {
|
||||
_ = t.InputUnifi.Initialize(t.Collector.Logger)
|
||||
_, _ = t.InputUnifi.Metrics(nil)
|
||||
_, _ = t.InputUnifi.Events(nil)
|
||||
}
|
||||
|
||||
func (t *TestRig) Close() {
|
||||
t.MockServer.Server.Close()
|
||||
}
|
||||
|
||||
func PBool(v bool) *bool {
|
||||
return &v
|
||||
}
|
||||
70
pkg/unittest/sets.go
Normal file
70
pkg/unittest/sets.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package unittest
|
||||
|
||||
// Set provides a generic way to compare sets of type K. This is only used for unit testing.
|
||||
type Set[K comparable] struct {
|
||||
entities map[K]any
|
||||
}
|
||||
|
||||
// NewSetFromMap will create a Set of type K from a map[K]V. V is useless here as we are only comparing the set of keys
|
||||
// in the map.
|
||||
func NewSetFromMap[K comparable, V any](m map[K]V) *Set[K] {
|
||||
entities := make(map[K]any, 0)
|
||||
|
||||
for k := range m {
|
||||
entities[k] = true
|
||||
}
|
||||
|
||||
return &Set[K]{
|
||||
entities: entities,
|
||||
}
|
||||
}
|
||||
|
||||
// NewSetFromSlice will create a Set of type K from a slice of keys. Duplicates will be dropped as this is a set.
|
||||
func NewSetFromSlice[K comparable](s []K) *Set[K] {
|
||||
entities := make(map[K]any, 0)
|
||||
|
||||
for _, k := range s {
|
||||
entities[k] = true
|
||||
}
|
||||
|
||||
return &Set[K]{
|
||||
entities: entities,
|
||||
}
|
||||
}
|
||||
|
||||
// Difference will compare two this Set against another Set of the same type K. This will return entries that
|
||||
// exist in this set but not the other as `additions` and entries that exist in the other set but not this set
|
||||
// as `deletions`.
|
||||
func (s *Set[K]) Difference(other *Set[K]) (additions []K, deletions []K) {
|
||||
additions = make([]K, 0)
|
||||
|
||||
for i := range s.entities {
|
||||
if _, ok := other.entities[i]; !ok {
|
||||
additions = append(additions, i)
|
||||
}
|
||||
}
|
||||
|
||||
deletions = make([]K, 0)
|
||||
|
||||
for j := range other.entities {
|
||||
if _, ok := s.entities[j]; !ok {
|
||||
deletions = append(deletions, j)
|
||||
}
|
||||
}
|
||||
|
||||
return additions, deletions
|
||||
}
|
||||
|
||||
func (s *Set[K]) Len() int {
|
||||
return len(s.entities)
|
||||
}
|
||||
|
||||
// Slice will return the set back as a slice of type K
|
||||
func (s *Set[K]) Slice() []K {
|
||||
ret := make([]K, 0)
|
||||
for k := range s.entities {
|
||||
ret = append(ret, k)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
39
pkg/unittest/sets_test.go
Normal file
39
pkg/unittest/sets_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package unittest_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/unpoller/unpoller/pkg/unittest"
|
||||
)
|
||||
|
||||
func TestSets(t *testing.T) {
|
||||
s1 := unittest.NewSetFromSlice[string]([]string{"a", "b", "c", "c"})
|
||||
|
||||
assert.Len(t, s1.Slice(), 3)
|
||||
assert.Contains(t, s1.Slice(), "a")
|
||||
assert.Contains(t, s1.Slice(), "b")
|
||||
assert.Contains(t, s1.Slice(), "c")
|
||||
|
||||
s2 := unittest.NewSetFromMap[string](map[string]bool{
|
||||
"c": true,
|
||||
"d": false,
|
||||
"e": true,
|
||||
})
|
||||
|
||||
assert.Len(t, s2.Slice(), 3)
|
||||
assert.Contains(t, s2.Slice(), "c")
|
||||
assert.Contains(t, s2.Slice(), "d")
|
||||
assert.Contains(t, s2.Slice(), "e")
|
||||
|
||||
additions, deletions := s1.Difference(s2)
|
||||
|
||||
assert.Len(t, additions, 2)
|
||||
assert.Len(t, deletions, 2)
|
||||
|
||||
assert.Contains(t, additions, "a")
|
||||
assert.Contains(t, additions, "b")
|
||||
|
||||
assert.Contains(t, deletions, "d")
|
||||
assert.Contains(t, deletions, "e")
|
||||
}
|
||||
Reference in New Issue
Block a user