Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

story(gRPC): event store constructor #59

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
updated mongo test
  • Loading branch information
erictg committed Nov 10, 2022

Unverified

This user has not yet uploaded their public signing key.
commit 3fff1dc3771e27c6105b70922a3ec2984fc35fc6
10 changes: 10 additions & 0 deletions eventstore/errors.go
Original file line number Diff line number Diff line change
@@ -49,6 +49,11 @@ func (m *MarshalError) Error() string {
return fmt.Sprintf("failed to marshal %s to %s. %s", m.From, m.To, m.Err)
}

// Unwrap returns the inner error
func (m *MarshalError) Unwrap() error {
return m.Err
}

// PutError defines an error when putting data into a database
type PutError struct {
Source string
@@ -70,6 +75,11 @@ func (p *PutError) Error() string {
return fmt.Sprintf("failed to put %s into %s. %s", p.InsertedType, p.Source, p.Err)
}

// Unwrap returns the inner error
func (p *PutError) Unwrap() error {
return p.Err
}

// ValidationError defines an error where a config is not valid
type ValidationError struct {
Type string
8 changes: 2 additions & 6 deletions eventstore/mongo.go
Original file line number Diff line number Diff line change
@@ -66,21 +66,17 @@ func (m *MongoConfig) getURI() string {

// MongoEventStoreImpl is the event store implementation for mongodb
type MongoEventStoreImpl struct {
config *MongoConfig
config MongoConfig
logger *zap.Logger
client *mongo.Client
}

// NewMongoEventStoreImpl constructs and initializes a *MongoEventStoreImpl
func NewMongoEventStoreImpl(ctx context.Context, _config *MongoConfig) (*MongoEventStoreImpl, error) {
func NewMongoEventStoreImpl(ctx context.Context, _config MongoConfig) (*MongoEventStoreImpl, error) {
if ctx == nil {
return nil, errors.New("context can not be nil")
}

if _config == nil {
return nil, errors.New("config can not be nil")
}

err := _config.Validate()
if err != nil {
return nil, fmt.Errorf("invalid config, %w", err)
35 changes: 16 additions & 19 deletions eventstore/mongo_test.go
Original file line number Diff line number Diff line change
@@ -120,15 +120,10 @@ func TestMongoConfig_Validate(t *testing.T) {
func TestNewMongoEventStoreImpl(t *testing.T) {
req := require.New(t)
t.Run("nil ctx", func(t *testing.T) {
_, err := NewMongoEventStoreImpl(nil, nil)
_, err := NewMongoEventStoreImpl(nil, MongoConfig{})
req.ErrorContains(err, "context can not be nil", "error is not target error")
})

t.Run("nil config", func(t *testing.T) {
_, err := NewMongoEventStoreImpl(context.TODO(), nil)
req.ErrorContains(err, "config can not be nil", "error is not target error")
})

t.Run("invalid config", func(t *testing.T) {
conf := MongoConfig{
Host: "something",
@@ -137,7 +132,7 @@ func TestNewMongoEventStoreImpl(t *testing.T) {
Database: "dfasdfas",
Collection: "dfads",
}
_, err := NewMongoEventStoreImpl(context.TODO(), &conf)
_, err := NewMongoEventStoreImpl(context.TODO(), conf)
var valError *ValidationError
req.ErrorAs(err, &valError, "expected validation error")
})
@@ -151,7 +146,7 @@ func TestNewMongoEventStoreImpl(t *testing.T) {
Database: "testdb",
Collection: "testcoll",
}
_, err := NewMongoEventStoreImpl(context.TODO(), &conf)
_, err := NewMongoEventStoreImpl(context.TODO(), conf)
var connErr *ConnectionError
req.ErrorAs(err, &connErr, "expected connection error")
})
@@ -190,7 +185,7 @@ func TestMongoIntegration(t *testing.T) {
// impl setup
db := "testdb"
collName := "testcoll"
config := &MongoConfig{
config := MongoConfig{
Host: "localhost",
Port: "27017",
Username: "root",
@@ -230,45 +225,47 @@ func TestMongoIntegration(t *testing.T) {
req.Len(results, 1, "returned slice not of correct length")

result := results[0]
var idv, spec, source, _type, sub, dct, tm, dt = false, false, false, false, false, false, false, false
var idCheck, specVersionCheck, sourceCheck, typeCheck, subjectCheck, dataContentTypeCheck,
timeCheck, dataCheck = false, false, false, false, false, false, false, false
for key, value := range result.Map() {
switch key {
case "id":
req.Equal(id, value, "id not expected value")
idv = true
idCheck = true
continue
case "specversion":
req.Equal("1.0", value, "spec version not expected value")
spec = true
specVersionCheck = true
continue
case "source":
req.Equal("mongo_test", value, "source not expected value")
source = true
sourceCheck = true
continue
case "type":
req.Equal("test", value, "type not expected value")
_type = true
typeCheck = true
continue
case "subject":
req.Equal("test", value, "subject not expected value")
sub = true
subjectCheck = true
continue
case "datacontenttype":
req.Equal("application/json", value, "datacontenttype not expected value")
dct = true
dataContentTypeCheck = true
continue
case "time":
req.Equal(curTime.Format(time.RFC3339Nano), value, "time is not equal")
tm = true
timeCheck = true
continue
case "data":
d := value.(primitive.D)
v := d[0]
req.Equal("hello", v.Key, "key not expected value")
req.Equal("world", v.Value, "value not of expected value")
dt = true
dataCheck = true
continue
}
}
req.True(idv && spec && source && _type && sub && dct && tm && dt, "all values have not been verified")
req.True(idCheck && specVersionCheck && sourceCheck && typeCheck && subjectCheck && dataContentTypeCheck && timeCheck && dataCheck,
"all values have not been verified")
}