|
| 1 | +package calculations |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "testify-tutorial/mocks" |
| 8 | + "testify-tutorial/stocks" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + _ "github.com/lib/pq" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | + "github.com/stretchr/testify/suite" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + dbHost = "localhost" |
| 19 | + dbPort = 5432 |
| 20 | + dbUser = "postgres" |
| 21 | + dbPassword = "mysecretpassword" |
| 22 | + dbName = "postgres" |
| 23 | +) |
| 24 | + |
| 25 | +// Integration tests |
| 26 | + |
| 27 | +type IntTestSuite struct { |
| 28 | + suite.Suite |
| 29 | + db *sql.DB |
| 30 | + calculator PriceIncreaseCalculator |
| 31 | +} |
| 32 | + |
| 33 | +func TestIntTestSuite(t *testing.T) { |
| 34 | + suite.Run(t, &IntTestSuite{}) |
| 35 | +} |
| 36 | + |
| 37 | +func (its *IntTestSuite) SetupSuite() { |
| 38 | + psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", dbHost, dbPort, dbUser, dbPassword, dbName) |
| 39 | + db, err := sql.Open("postgres", psqlInfo) |
| 40 | + if err != nil { |
| 41 | + its.FailNowf("unable to connect to database", err.Error()) |
| 42 | + } |
| 43 | + |
| 44 | + setupDatabase(its, db) |
| 45 | + |
| 46 | + pp := stocks.NewPriceProvider(db) |
| 47 | + calculator := NewPriceIncreaseCalculator(pp) |
| 48 | + |
| 49 | + its.db = db |
| 50 | + its.calculator = calculator |
| 51 | +} |
| 52 | + |
| 53 | +func (its *IntTestSuite) BeforeTest(suiteName, testName string) { |
| 54 | + if testName == "TestCalculate_Error" { |
| 55 | + return |
| 56 | + } |
| 57 | + seedTestTable(its, its.db) // ts -> price=1, ts+1min -> price=2 |
| 58 | +} |
| 59 | + |
| 60 | +func (its *IntTestSuite) TearDownSuite() { |
| 61 | + tearDownDatabase(its) |
| 62 | +} |
| 63 | + |
| 64 | +func (its *IntTestSuite) TearDownTest() { |
| 65 | + cleanTable(its) |
| 66 | +} |
| 67 | + |
| 68 | +func (its *IntTestSuite) TestCalculate_Error() { |
| 69 | + |
| 70 | + actual, err := its.calculator.PriceIncrease() |
| 71 | + |
| 72 | + its.EqualError(err, "not enough data") |
| 73 | + its.Equal(0.0, actual) |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +func (its *IntTestSuite) TestCalculate() { |
| 78 | + |
| 79 | + actual, err := its.calculator.PriceIncrease() |
| 80 | + |
| 81 | + its.Nil(err) |
| 82 | + its.Equal(100.0, actual) |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +// Helper functions |
| 87 | + |
| 88 | +func setupDatabase(its *IntTestSuite, db *sql.DB) { |
| 89 | + its.T().Log("setting up database") |
| 90 | + |
| 91 | + _, err := db.Exec(`CREATE DATABASE stockprices_test`) |
| 92 | + if err != nil { |
| 93 | + its.FailNowf("unable to create database", err.Error()) |
| 94 | + } |
| 95 | + |
| 96 | + _, err = db.Exec(`CREATE TABLE IF NOT EXISTS stockprices ( |
| 97 | + timestamp TIMESTAMPTZ PRIMARY KEY, |
| 98 | + price DECIMAL NOT NULL |
| 99 | + )`) |
| 100 | + |
| 101 | + if err != nil { |
| 102 | + its.FailNowf("unable to create table", err.Error()) |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +func seedTestTable(its *IntTestSuite, db *sql.DB) { |
| 108 | + its.T().Log("seeding test table") |
| 109 | + |
| 110 | + for i := 1; i <= 2; i++ { |
| 111 | + _, err := db.Exec("INSERT INTO stockprices (timestamp, price) VALUES ($1,$2)", time.Now().Add(time.Duration(i)*time.Minute), float64(i)) |
| 112 | + if err != nil { |
| 113 | + its.FailNowf("unable to seed table", err.Error()) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func cleanTable(its *IntTestSuite) { |
| 119 | + its.T().Log("cleaning database") |
| 120 | + |
| 121 | + _, err := its.db.Exec(`DELETE FROM stockprices`) |
| 122 | + if err != nil { |
| 123 | + its.FailNowf("unable to clean table", err.Error()) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func tearDownDatabase(its *IntTestSuite) { |
| 128 | + its.T().Log("tearing down database") |
| 129 | + |
| 130 | + _, err := its.db.Exec(`DROP TABLE stockprices`) |
| 131 | + if err != nil { |
| 132 | + its.FailNowf("unable to drop table", err.Error()) |
| 133 | + } |
| 134 | + |
| 135 | + _, err = its.db.Exec(`DROP DATABASE stockprices_test`) |
| 136 | + if err != nil { |
| 137 | + its.FailNowf("unable to drop database", err.Error()) |
| 138 | + } |
| 139 | + |
| 140 | + err = its.db.Close() |
| 141 | + if err != nil { |
| 142 | + its.FailNowf("unable to close database", err.Error()) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// Unit tests |
| 147 | + |
| 148 | +type UnitTestSuite struct { |
| 149 | + suite.Suite |
| 150 | + calculator PriceIncreaseCalculator |
| 151 | + priceProviderMock *mocks.PriceProvider |
| 152 | +} |
| 153 | + |
| 154 | +func TestUnitTestSuite(t *testing.T) { |
| 155 | + suite.Run(t, &UnitTestSuite{}) |
| 156 | +} |
| 157 | + |
| 158 | +func (uts *UnitTestSuite) SetupTest() { |
| 159 | + priceProviderMock := mocks.PriceProvider{} |
| 160 | + calculator := NewPriceIncreaseCalculator(&priceProviderMock) |
| 161 | + |
| 162 | + uts.calculator = calculator |
| 163 | + uts.priceProviderMock = &priceProviderMock |
| 164 | +} |
| 165 | + |
| 166 | +func (uts *UnitTestSuite) TestCalculate() { |
| 167 | + uts.priceProviderMock.On("List", mock.Anything).Return([]*stocks.PriceData{}, nil) |
| 168 | + |
| 169 | + actual, err := uts.calculator.PriceIncrease() |
| 170 | + |
| 171 | + uts.Equal(0.0, actual) |
| 172 | + uts.EqualError(err, "not enough data") |
| 173 | +} |
| 174 | + |
| 175 | +func (uts *UnitTestSuite) TestCalculate_ErrorFromPriceProvider() { |
| 176 | + expectedError := errors.New("oh my god") |
| 177 | + |
| 178 | + uts.priceProviderMock.On("List", mock.Anything).Return([]*stocks.PriceData{}, expectedError) |
| 179 | + |
| 180 | + actual, err := uts.calculator.PriceIncrease() |
| 181 | + |
| 182 | + uts.Equal(0.0, actual) |
| 183 | + uts.Equal(expectedError, err) |
| 184 | + |
| 185 | +} |
0 commit comments