Mock ExchangeDataCollection (#166)

Implements a mock exchange data collection that can be used to test without connecting to graph.

Currently returns a "fixed message" but we can extend this to return anything that implements io.Reader
e.g. a file that contains a serialized message.
This commit is contained in:
Vaibhav Kamra 2022-06-08 16:33:59 -07:00 committed by GitHub
parent 2104e66fea
commit 0da0c786b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 0 deletions

View File

@ -77,6 +77,7 @@ require (
github.com/rs/xid v1.4.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
github.com/zeebo/blake3 v0.2.3 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect

View File

@ -336,6 +336,8 @@ github.com/spf13/viper v1.12.0 h1:CZ7eSOd3kZoaYDLbXnmzgQI5RlciuXBMA+18HwHRfZQ=
github.com/spf13/viper v1.12.0/go.mod h1:b6COn30jlNxbm/V2IqWiNWkJ+vZNiMNksliPCiuKtSI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=

View File

@ -0,0 +1,63 @@
package mockconnector
import (
"bytes"
"io"
"github.com/google/uuid"
"github.com/alcionai/corso/internal/connector"
)
// MockExchangeDataCollection represents a mock exchange mailbox
type MockExchangeDataCollection struct {
fullPath []string
messageCount int
messagesRead int
}
var (
_ connector.DataCollection = &MockExchangeDataCollection{}
_ connector.DataStream = &MockExchangeData{}
)
// NewMockExchangeDataCollection creates an data collection that will return the specified number of
// mock messages when iterated
func NewMockExchangeDataCollection(pathRepresentation []string, numMessagesToReturn int) *MockExchangeDataCollection {
collection := &MockExchangeDataCollection{
fullPath: pathRepresentation,
messageCount: numMessagesToReturn,
messagesRead: 0,
}
return collection
}
func (medc *MockExchangeDataCollection) FullPath() []string {
return append([]string{}, medc.fullPath...)
}
// NextItem returns either the next item in the collection or an error if one occurred.
// If not more items are available in the collection, returns (nil, nil).
func (medc *MockExchangeDataCollection) NextItem() (connector.DataStream, error) {
if medc.messagesRead < medc.messageCount {
medc.messagesRead++
// We can plug in whatever data we want here (can be an io.Reader to a test data file if needed)
m := []byte("test message")
return &MockExchangeData{uuid.NewString(), bytes.NewReader(m)}, nil
}
return nil, io.EOF
}
// ExchangeData represents a single item retrieved from exchange
type MockExchangeData struct {
id string
reader io.Reader
}
func (med *MockExchangeData) UUID() string {
return med.id
}
func (med *MockExchangeData) ToReader() io.Reader {
return med.reader
}

View File

@ -0,0 +1,33 @@
package mockconnector_test
import (
"io"
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/alcionai/corso/internal/connector/mockconnector"
)
type MockExchangeDataCollectionSuite struct {
suite.Suite
}
func TestMockExchangeDataCollectionSuite(t *testing.T) {
suite.Run(t, new(MockExchangeDataCollectionSuite))
}
func (suite *MockExchangeDataCollectionSuite) TestMockExchangeDataCollection() {
mdc := mockconnector.NewMockExchangeDataCollection([]string{"foo", "bar"}, 2)
messagesRead := 0
for item, err := mdc.NextItem(); err != io.EOF; item, err = mdc.NextItem() {
_, err := ioutil.ReadAll(item.ToReader())
assert.NoError(suite.T(), err)
messagesRead++
}
assert.Equal(suite.T(), 2, messagesRead)
}