Files
threshold/internal/oplog/queue_test.go
T
Todd Knight 4d86db6ba5 Introduce the proto directory structure. (#9)
* Setup buf to see and process protos correctly.

This commit adds a proposed directory structure for protos.  Included is a rough draft of the Ec2Catalog protos.

* Setup buf to see and process protos correctly.

This commit adds a proposed directory structure for protos, but leaves the existing oplog related protos alone.  Included is a rough draft of the Ec2Catalog protos.

* Update makefile and add script to generate the appropriate pb.go files.

* Add comment explaining google.golang.org/protobuf vs github.com/golang/protobuf

* Move all protos and generated files to under the internal directory.

This also required updating the buf config to explicitly name the excluded linted proto files under oplog since previously /internal/ was listed as a root in buf.  Future commit will move oplog proto files to be with the others.

* Moved oplog related protos to under the proto directory and regenerated.

* Update the oplog protos to be compliant with buf lint.
2020-04-28 11:20:37 -07:00

105 lines
3.0 KiB
Go

package oplog
import (
reflect "reflect"
"testing"
"github.com/hashicorp/watchtower/internal/oplog/oplog_test"
"google.golang.org/protobuf/proto"
"gotest.tools/assert"
)
// Test_Queue provides basic tests for the Queue type
func Test_Queue(t *testing.T) {
t.Parallel()
types, err := NewTypeCatalog(
Type{new(oplog_test.TestUser), "user"},
Type{new(oplog_test.TestCar), "car"},
Type{new(oplog_test.TestRental), "rental"},
)
assert.NilError(t, err)
queue := Queue{Catalog: types}
user := &oplog_test.TestUser{
Id: 1,
Name: "Alice",
PhoneNumber: "867-5309",
Email: "alice@bob.com",
}
car := &oplog_test.TestCar{
Id: 2,
Model: "Jeep",
Mpg: 25,
}
rental := &oplog_test.TestRental{
UserId: 1,
CarId: 2,
}
userUpdate := &oplog_test.TestUser{
Id: 1,
Name: "bob",
Email: "bob@alice.com",
}
t.Run("add/remove", func(t *testing.T) {
err = queue.Add(user, "user", OpType_OP_TYPE_CREATE)
assert.NilError(t, err)
err = queue.Add(car, "car", OpType_OP_TYPE_CREATE)
assert.NilError(t, err)
err = queue.Add(rental, "rental", OpType_OP_TYPE_CREATE)
assert.NilError(t, err)
err = queue.Add(userUpdate, "user", OpType_OP_TYPE_UPDATE, WithFieldMaskPaths([]string{"Name", "Email"}))
assert.NilError(t, err)
queuedUser, ty, fm, err := queue.Remove()
assert.NilError(t, err)
assert.Assert(t, proto.Equal(user, queuedUser))
assert.Assert(t, ty == OpType_OP_TYPE_CREATE)
assert.Assert(t, fm == nil)
queuedCar, ty, fm, err := queue.Remove()
assert.NilError(t, err)
assert.Assert(t, proto.Equal(car, queuedCar))
assert.Assert(t, ty == OpType_OP_TYPE_CREATE)
assert.Assert(t, fm == nil)
queuedRental, ty, fm, err := queue.Remove()
assert.NilError(t, err)
assert.Assert(t, proto.Equal(rental, queuedRental))
assert.Assert(t, ty == OpType_OP_TYPE_CREATE)
assert.Assert(t, fm == nil)
queuedUserUpdate, ty, fm, err := queue.Remove()
assert.NilError(t, err)
assert.Assert(t, proto.Equal(userUpdate, queuedUserUpdate))
assert.Assert(t, ty == OpType_OP_TYPE_UPDATE)
assert.Assert(t, reflect.DeepEqual(fm, []string{"Name", "Email"}))
})
t.Run("valid with nil type catalog", func(t *testing.T) {
queue := Queue{}
assert.NilError(t, queue.Add(user, "user", OpType_OP_TYPE_CREATE))
})
t.Run("error with nil type catalog", func(t *testing.T) {
queue := Queue{}
_, _, _, err := queue.Remove()
assert.Check(t, err != nil)
assert.Equal(t, err.Error(), "remove Catalog is nil")
})
t.Run("not replayable", func(t *testing.T) {
u := &oplog_test.TestNonReplayableUser{
Name: "Alice",
PhoneNumber: "867-5309",
Email: "alice@bob.com",
}
err = queue.Add(u, "user", OpType_OP_TYPE_CREATE)
assert.Check(t, err != nil)
assert.Equal(t, err.Error(), "error *oplog_test.TestNonReplayableUser is not a ReplayableMessage")
})
t.Run("nil message", func(t *testing.T) {
err = queue.Add(nil, "user", OpType_OP_TYPE_CREATE)
assert.Check(t, err != nil)
assert.Equal(t, err.Error(), "error <nil> is not a ReplayableMessage")
})
}