mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-31 23:41:09 +02:00
databroker: add utility recordset and changeset
This commit is contained in:
parent
0b79a28328
commit
b616e42768
3 changed files with 260 additions and 0 deletions
77
pkg/grpc/databroker/recordset_test.go
Normal file
77
pkg/grpc/databroker/recordset_test.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package databroker_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/pomerium/pomerium/pkg/grpc/databroker"
|
||||
)
|
||||
|
||||
type testRecord struct {
|
||||
Type string
|
||||
ID string
|
||||
Val string
|
||||
}
|
||||
|
||||
func (r testRecord) GetID() string {
|
||||
return r.ID
|
||||
}
|
||||
|
||||
func (r testRecord) GetType() string {
|
||||
return r.Type
|
||||
}
|
||||
|
||||
func (r testRecord) Equal(other testRecord) bool {
|
||||
return r.ID == other.ID && r.Type == other.Type && r.Val == other.Val
|
||||
}
|
||||
|
||||
func TestRecords(t *testing.T) {
|
||||
initial := make(databroker.RecordSetBundle[testRecord])
|
||||
initial.Add(testRecord{ID: "1", Type: "a", Val: "a-1"})
|
||||
initial.Add(testRecord{ID: "2", Type: "a", Val: "a-2"})
|
||||
initial.Add(testRecord{ID: "1", Type: "b", Val: "b-1"})
|
||||
|
||||
// test record types
|
||||
assert.ElementsMatch(t, []string{"a", "b"}, initial.RecordTypes())
|
||||
|
||||
// test added, deleted and modified
|
||||
updated := make(databroker.RecordSetBundle[testRecord])
|
||||
updated.Add(testRecord{ID: "1", Type: "a", Val: "a-1-1"})
|
||||
updated.Add(testRecord{ID: "3", Type: "a", Val: "a-3"})
|
||||
updated.Add(testRecord{ID: "1", Type: "b", Val: "b-1"})
|
||||
updated.Add(testRecord{ID: "2", Type: "b", Val: "b-2"})
|
||||
updated.Add(testRecord{ID: "1", Type: "c", Val: "c-1"})
|
||||
|
||||
assert.ElementsMatch(t, []string{"a", "b", "c"}, updated.RecordTypes())
|
||||
|
||||
added := initial.GetAdded(updated)
|
||||
assert.Equal(t,
|
||||
databroker.RecordSetBundle[testRecord]{
|
||||
"a": databroker.RecordSet[testRecord]{
|
||||
"3": {ID: "3", Type: "a", Val: "a-3"},
|
||||
},
|
||||
"b": databroker.RecordSet[testRecord]{
|
||||
"2": {ID: "2", Type: "b", Val: "b-2"},
|
||||
},
|
||||
"c": databroker.RecordSet[testRecord]{
|
||||
"1": {ID: "1", Type: "c", Val: "c-1"},
|
||||
},
|
||||
}, added)
|
||||
|
||||
removed := initial.GetRemoved(updated)
|
||||
assert.Equal(t,
|
||||
databroker.RecordSetBundle[testRecord]{
|
||||
"a": databroker.RecordSet[testRecord]{
|
||||
"2": {ID: "2", Type: "a", Val: "a-2"},
|
||||
},
|
||||
}, removed)
|
||||
|
||||
modified := initial.GetModified(updated)
|
||||
assert.Equal(t,
|
||||
databroker.RecordSetBundle[testRecord]{
|
||||
"a": databroker.RecordSet[testRecord]{
|
||||
"1": {ID: "1", Type: "a", Val: "a-1-1"},
|
||||
},
|
||||
}, modified)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue