github.com/mantzas/incata@v0.3.0/reader/sql_reader_test.go (about)

     1  package reader_test
     2  
     3  import (
     4  	"database/sql/driver"
     5  
     6  	"errors"
     7  	"time"
     8  
     9  	"github.com/DATA-DOG/go-sqlmock"
    10  	. "github.com/mantzas/incata/marshal"
    11  	. "github.com/mantzas/incata/reader"
    12  	. "github.com/mantzas/incata/storage"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	"github.com/satori/go.uuid"
    16  )
    17  
    18  type AnyType struct{}
    19  
    20  // Match satisfies sqlmock.Argument interface
    21  func (a AnyType) Match(v driver.Value) bool {
    22  	return true
    23  }
    24  
    25  var _ = Describe("Reader", func() {
    26  
    27  	It("read data from mocked storage succeeds", func() {
    28  
    29  		db, mock, _ := sqlmock.New()
    30  
    31  		defer db.Close()
    32  
    33  		var sourceID = uuid.NewV4()
    34  
    35  		rows := sqlmock.NewRows([]string{"Id", "SourceId", "Created", "EventType", "Version", "Payload"}).AddRow(1, uuid.NewV4().String(), time.Now(), "Test", 1, "123")
    36  		defer rows.Close()
    37  
    38  		mock.ExpectQuery("SELECT").WithArgs(sourceID.String()).WillReturnRows(rows)
    39  
    40  		storage, _ := NewStorageFinalized(db, MSSQL, "Event")
    41  		marshaller := NewJSONMarshaller()
    42  		reader := NewSQLReader(storage, marshaller)
    43  
    44  		_, err := reader.Read(sourceID)
    45  		Expect(err).NotTo(HaveOccurred())
    46  
    47  		err = mock.ExpectationsWereMet()
    48  		Expect(err).NotTo(HaveOccurred(), "there were unfulfilled expections: %s", err)
    49  	})
    50  
    51  	It("read data from mocked storage returns query error", func() {
    52  
    53  		const QUERYERROR = "QUERY ERROR"
    54  
    55  		db, mock, _ := sqlmock.New()
    56  		defer db.Close()
    57  
    58  		var sourceID = uuid.NewV4()
    59  
    60  		mock.ExpectQuery("SELECT").WithArgs(sourceID.String()).WillReturnError(errors.New(QUERYERROR))
    61  
    62  		storage, _ := NewStorageFinalized(db, MSSQL, "Event")
    63  		marshaller := NewJSONMarshaller()
    64  		reader := NewSQLReader(storage, marshaller)
    65  
    66  		_, err := reader.Read(sourceID)
    67  		Expect(err).To(MatchError(QUERYERROR))
    68  
    69  		err = mock.ExpectationsWereMet()
    70  		Expect(err).NotTo(HaveOccurred(), "there were unfulfilled expections: %s", err)
    71  	})
    72  })