Implement ToReader for ExchangeData (#161)

This commit is contained in:
Vaibhav Kamra 2022-06-08 10:28:33 -07:00 committed by GitHub
parent 33e8e978ba
commit 13ca33fae0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 5 deletions

View File

@ -1,6 +1,9 @@
package connector
import "io"
import (
"bytes"
"io"
)
// A DataCollection represents a collection of data of the
// same type (e.g. mail)
@ -13,7 +16,8 @@ type DataCollection interface {
// DataStream represents a single item within a DataCollection
// that can be consumed as a stream (it embeds io.Reader)
type DataStream interface {
io.Reader
// Returns an io.Reader for the DataStream
ToReader() io.Reader
// Provides a unique identifier for this data
UUID() string
}
@ -72,7 +76,6 @@ func (ed *ExchangeData) UUID() string {
return ed.id
}
func (ed *ExchangeData) Read(bytes []byte) (int, error) {
// TODO: Copy ed.message into []bytes. Will need to take care of partial reads
return 0, nil
func (ed *ExchangeData) ToReader() io.Reader {
return bytes.NewReader(ed.message)
}

View File

@ -0,0 +1,27 @@
package connector
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type ExchangeDataCollectionSuite struct {
suite.Suite
}
func TestExchangeDataCollectionSuite(t *testing.T) {
suite.Run(t, new(ExchangeDataCollectionSuite))
}
func (suite *ExchangeDataCollectionSuite) TestExchangeDataReader() {
m := []byte("test message")
ed := &ExchangeData{message: m}
// Read the message using the `ExchangeData` reader and validate it matches what we set
buf := &bytes.Buffer{}
buf.ReadFrom(ed.ToReader())
assert.Equal(suite.T(), buf.Bytes(), m)
}