github.com/mantzas/incata@v0.3.0/storage/storage_test.go (about)

     1  package storage_test
     2  
     3  import (
     4  	"database/sql/driver"
     5  
     6  	"github.com/DATA-DOG/go-sqlmock"
     7  	. "github.com/mantzas/incata/storage"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/ginkgo/extensions/table"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Storage", func() {
    14  
    15  	It("execute returns expected data", func() {
    16  
    17  		dbInner, mock, _ := sqlmock.New()
    18  
    19  		storage, _ := NewStorageFinalized(dbInner, MSSQL, "Event")
    20  
    21  		mock.ExpectExec("123").WithArgs(1, 2, 3).WillReturnResult(sqlmock.NewResult(1, 1))
    22  
    23  		storage.Exec("123", 1, 2, 3)
    24  
    25  		err := mock.ExpectationsWereMet()
    26  		Expect(err).NotTo(HaveOccurred(), "there were unfulfilled expections: %s", err)
    27  	})
    28  
    29  	It("query returns expected data", func() {
    30  
    31  		dbInner, mock, _ := sqlmock.New()
    32  		storage, _ := NewStorageFinalized(dbInner, PostgreSQL, "Event")
    33  
    34  		var rows driver.Rows
    35  		mock.ExpectQuery("123").WithArgs(1, 2, 3).WillReturnRows(rows)
    36  
    37  		storage.Query("123", 1, 2, 3)
    38  		err := mock.ExpectationsWereMet()
    39  		Expect(err).NotTo(HaveOccurred(), "there were unfulfilled expections: %s", err)
    40  	})
    41  
    42  	It("close suceeds", func() {
    43  
    44  		dbInner, mock, _ := sqlmock.New()
    45  		storage, _ := NewStorageFinalized(dbInner, MSSQL, "Event")
    46  
    47  		mock.ExpectClose()
    48  
    49  		storage.Close()
    50  		err := mock.ExpectationsWereMet()
    51  		Expect(err).NotTo(HaveOccurred(), "there were unfulfilled expections: %s", err)
    52  	})
    53  
    54  	It("wrong db type returns error when creating new finalized storage", func() {
    55  
    56  		dbInner, _, _ := sqlmock.New()
    57  		_, err := NewStorageFinalized(dbInner, 3, "Event")
    58  		Expect(err).To(HaveOccurred())
    59  	})
    60  
    61  	It("wrong db type returns error when creating new storage", func() {
    62  
    63  		_, err := NewStorage(3, "123", "Event")
    64  		Expect(err).To(HaveOccurred())
    65  	})
    66  
    67  	It("new storage returns error when opening", func() {
    68  
    69  		_, err := NewStorage(MSSQL, "123", "Event")
    70  		Expect(err).To(HaveOccurred())
    71  	})
    72  
    73  	DescribeTable("Convert string to DB type",
    74  		func(text string, expectedDbType DbType, hasErrors bool) {
    75  
    76  			actualDbType, err := ConvertToDbType(text)
    77  
    78  			if hasErrors {
    79  				Expect(err).To(HaveOccurred())
    80  			} else {
    81  				Expect(actualDbType).To(Equal(expectedDbType))
    82  			}
    83  		},
    84  		Entry("mssql", "mssql", MSSQL, false),
    85  		Entry("MSSQL", "MSSQL", MSSQL, false),
    86  		Entry("MsSQL", "MsSQL", MSSQL, false),
    87  		Entry("MsSql", "MsSql", MSSQL, false),
    88  		Entry("postgresql", "postgresql", PostgreSQL, false),
    89  		Entry("PostgreSQL", "PostgreSQL", PostgreSQL, false),
    90  		Entry("POSTGRESQL", "POSTGRESQL", PostgreSQL, false),
    91  		Entry("xxx", "xxx", PostgreSQL, true),
    92  	)
    93  })