github.com/mantzas/incata@v0.3.0/writer/sql_writer_test.go (about) 1 package writer_test 2 3 import ( 4 "errors" 5 "testing" 6 7 "database/sql/driver" 8 9 "github.com/DATA-DOG/go-sqlmock" 10 . "github.com/mantzas/incata/marshal" 11 . "github.com/mantzas/incata/mocks" 12 . "github.com/mantzas/incata/model" 13 . "github.com/mantzas/incata/storage" 14 . "github.com/mantzas/incata/writer" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 18 "time" 19 20 "github.com/satori/go.uuid" 21 ) 22 23 type AnyTime struct{} 24 25 // Match satisfies sqlmock.Argument interface 26 func (a AnyTime) Match(v driver.Value) bool { 27 _, ok := v.(time.Time) 28 return ok 29 } 30 31 var _ = Describe("Reader", func() { 32 33 It("write data to mocked storage succeeds", func() { 34 35 db, mock, _ := sqlmock.New() 36 defer db.Close() 37 38 ser := NewJSONMarshaller() 39 storage, _ := NewStorageFinalized(db, MSSQL, "Event") 40 wr := NewSQLWriter(storage, ser) 41 42 event := NewEvent(uuid.NewV4(), 1, "TEST", 1) 43 payload, _ := ser.Serialize(event.Payload) 44 45 mock.ExpectExec("INSERT INTO Event").WithArgs(event.SourceID.String(), AnyTime{}, "TEST", event.Version, payload).WillReturnResult(sqlmock.NewResult(1, 1)) 46 47 err := wr.Write(*event) 48 Expect(err).NotTo(HaveOccurred()) 49 50 err = mock.ExpectationsWereMet() 51 Expect(err).NotTo(HaveOccurred(), "there were unfulfilled expections: %s", err) 52 }) 53 54 It("write data to mocked storage returns db error", func() { 55 56 db, mock, _ := sqlmock.New() 57 defer db.Close() 58 59 ser := NewJSONMarshaller() 60 storage, _ := NewStorageFinalized(db, MSSQL, "Event") 61 wr := NewSQLWriter(storage, ser) 62 63 event := NewEvent(uuid.NewV4(), 1, "TEST", 1) 64 payload, _ := ser.Serialize(event.Payload) 65 66 mock.ExpectExec("INSERT INTO Event").WithArgs(event.SourceID.String(), AnyTime{}, "TEST", event.Version, payload).WillReturnError(errors.New("TEST")) 67 68 err := wr.Write(*event) 69 Expect(err).To(HaveOccurred()) 70 71 err = mock.ExpectationsWereMet() 72 Expect(err).NotTo(HaveOccurred(), "there were unfulfilled expections: %s", err) 73 }) 74 75 It("write data to mocked storage returns serialization error", func() { 76 77 db, _, _ := sqlmock.New() 78 defer db.Close() 79 80 ser := NewJSONMarshaller() 81 storage, _ := NewStorageFinalized(db, MSSQL, "Event") 82 wr := NewSQLWriter(storage, ser) 83 84 event := NewEvent(uuid.NewV4(), make(map[int]int), "TEST", 1) 85 86 err := wr.Write(*event) 87 Expect(err).To(HaveOccurred()) 88 }) 89 90 }) 91 92 func BenchmarkAppenderPostgresql(b *testing.B) { 93 94 b.Skipf("Postgresql benchmark!") 95 96 storage, err := NewStorage(PostgreSQL, "postgres://user:pwd@server/linear?sslmode=disable", "linearevents") 97 98 if err != nil { 99 b.Fatalf("Fatal error %s", err.Error()) 100 } 101 102 runDatabaseBenchmark(b, storage) 103 } 104 105 func BenchmarkAppenderMsSql(b *testing.B) { 106 107 b.Skipf("SQL Server benchmark!") 108 109 db, err := NewStorage(MSSQL, "Server=xxx;Database=sss;User Id=xx;Password=xxx;", "Event") 110 111 if err != nil { 112 b.Fatalf("Fatal error %s", err.Error()) 113 } 114 115 runDatabaseBenchmark(b, db) 116 } 117 118 func runDatabaseBenchmark(b *testing.B, storage *Storage) { 119 120 ser := NewJSONMarshaller() 121 wr := NewSQLWriter(storage, ser) 122 123 event := NewEvent(uuid.NewV4(), GetTestData(), "TEST", 1) 124 125 for n := 0; n < b.N; n++ { 126 err := wr.Write(*event) 127 if err != nil { 128 b.Fatalf("Append error occured! %s", err.Error()) 129 } 130 } 131 }