-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wrap db instance in handler struct, add integration tests
- Loading branch information
Dara Hayes
committed
Feb 22, 2018
1 parent
21cef30
commit a2d1003
Showing
6 changed files
with
272 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
postgres: | ||
image: timescale/timescaledb | ||
image: postgres:9.5 | ||
ports: | ||
- 5432:5432 | ||
environment: | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_USER: postgres | ||
PGDATA : /var/lib/postgresql/data/pgdata | ||
PGDATABASE: aerogear_mobile_metrics | ||
POSTGRES_DB: aerogear_mobile_metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// +build integration | ||
|
||
package dao | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aerogear/aerogear-metrics-api/pkg/config" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockDB struct { | ||
mock.Mock | ||
} | ||
|
||
func TestIshealthy(t *testing.T) { | ||
config := config.GetConfig() | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode) | ||
|
||
if err != nil { | ||
t.Errorf("Connect() returned an error: %s", err.Error()) | ||
} | ||
|
||
dao := NewMetricsDAO(dbHandler.DB) | ||
|
||
isHealthy, err := dao.IsHealthy() | ||
|
||
if err != nil { | ||
t.Errorf("isHealthy returned an error %s", err.Error()) | ||
} | ||
|
||
if !isHealthy { | ||
t.Errorf("isHealthy returned false") | ||
} | ||
} | ||
|
||
func TestIshealthyWhenDisconnected(t *testing.T) { | ||
config := config.GetConfig() | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode) | ||
|
||
if err != nil { | ||
t.Errorf("Connect() returned an error: %s", err.Error()) | ||
} | ||
|
||
dao := NewMetricsDAO(dbHandler.DB) | ||
|
||
dbHandler.Disconnect() | ||
|
||
isHealthy, err := dao.IsHealthy() | ||
|
||
if err == nil { | ||
t.Errorf("isHealthy returned no error when disconnected") | ||
} | ||
|
||
if isHealthy { | ||
t.Errorf("isHealthy returned true when disconnected") | ||
} | ||
} | ||
|
||
func TestCreate(t *testing.T) { | ||
config := config.GetConfig() | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode) | ||
dbHandler.DoInitialSetup() | ||
|
||
if err != nil { | ||
t.Errorf("Connect() returned an error: %s", err.Error()) | ||
} | ||
|
||
dao := NewMetricsDAO(dbHandler.DB) | ||
|
||
clientId := "org.aerogear.metrics.testing" | ||
metricsData := []byte("{\"app\":{\"id\":\"com.example.someApp\",\"sdkVersion\":\"2.4.6\",\"appVersion\":\"256\"},\"device\":{\"platform\":\"android\",\"platformVersion\":\"27\"}}") | ||
|
||
err = dao.Create(clientId, metricsData) | ||
|
||
if err != nil { | ||
t.Errorf("Create() returned an error %s", err.Error()) | ||
} | ||
} | ||
|
||
func TestCreateBadJSON(t *testing.T) { | ||
config := config.GetConfig() | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode) | ||
|
||
if err != nil { | ||
t.Errorf("Connect() returned an error: %s", err.Error()) | ||
} | ||
|
||
dao := NewMetricsDAO(dbHandler.DB) | ||
|
||
clientId := "org.aerogear.metrics.testing" | ||
metricsData := []byte("InvalidJSON") | ||
|
||
err = dao.Create(clientId, metricsData) | ||
|
||
if err == nil { | ||
t.Errorf("Create() with invalid JSON did not return an error") | ||
} | ||
} | ||
|
||
func TestCreateEmptyClientID(t *testing.T) { | ||
config := config.GetConfig() | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode) | ||
dbHandler.DoInitialSetup() | ||
|
||
if err != nil { | ||
t.Errorf("Connect() returned an error: %s", err.Error()) | ||
} | ||
|
||
dao := NewMetricsDAO(dbHandler.DB) | ||
|
||
clientId := "" | ||
metricsData := []byte("{\"app\":{\"id\":\"com.example.someApp\",\"sdkVersion\":\"2.4.6\",\"appVersion\":\"256\"},\"device\":{\"platform\":\"android\",\"platformVersion\":\"27\"}}") | ||
|
||
err = dao.Create(clientId, metricsData) | ||
|
||
if err == nil { | ||
t.Errorf("Create() with empty clientId did not return an error") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// +build integration | ||
|
||
package dao | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aerogear/aerogear-metrics-api/pkg/config" | ||
) | ||
|
||
func TestConnect(t *testing.T) { | ||
config := config.GetConfig() | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode) | ||
|
||
if err != nil { | ||
t.Errorf("Connect() returned an error: %s", err.Error()) | ||
} | ||
|
||
err = dbHandler.DB.Ping() | ||
|
||
if err != nil { | ||
t.Errorf("Failed to Ping the database after Connect(): %s", err.Error()) | ||
} | ||
} | ||
|
||
func TestConnectAlreadyConnected(t *testing.T) { | ||
config := config.GetConfig() | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode) | ||
|
||
if err != nil { | ||
t.Errorf("Connect() returned an error: %s", err.Error()) | ||
} | ||
|
||
err = dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode) | ||
|
||
if err != nil { | ||
t.Errorf("Connect() returned an error: %s", err.Error()) | ||
} | ||
} | ||
|
||
func TestDisconnect(t *testing.T) { | ||
config := config.GetConfig() | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode) | ||
|
||
if err != nil { | ||
t.Errorf("Connect() returned an error: %s", err.Error()) | ||
} | ||
|
||
err = dbHandler.Disconnect() | ||
|
||
if err != nil { | ||
t.Errorf("Disconnect() returned an error: %s", err.Error()) | ||
} | ||
|
||
err = dbHandler.DB.Ping() | ||
|
||
if err == nil { | ||
t.Errorf("Ping did not return an error after calling Disconnect()") | ||
} | ||
} | ||
|
||
func TestDisconnectNotConnected(t *testing.T) { | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.Disconnect() | ||
|
||
if err != nil { | ||
t.Errorf("Disconnect() returned an error: %s", err.Error()) | ||
} | ||
} | ||
|
||
func TestDoInitialSetup(t *testing.T) { | ||
config := config.GetConfig() | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode) | ||
|
||
if err != nil { | ||
t.Errorf("Connect() returned an error: %s", err.Error()) | ||
} | ||
|
||
err = dbHandler.DoInitialSetup() | ||
|
||
if err != nil { | ||
t.Errorf("DoInitialSetup() returned an error: %s", err.Error()) | ||
} | ||
|
||
var exists bool | ||
|
||
err = dbHandler.DB.QueryRow("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'mobileappmetrics');").Scan(&exists) | ||
|
||
if err != nil { | ||
t.Errorf("Database returned an error while checking if table exists: %s", err.Error()) | ||
} | ||
|
||
if !exists { | ||
t.Errorf("Expected table mobileappmetrics does not exist") | ||
} | ||
} | ||
|
||
func TestDoInitialSetupNotConnected(t *testing.T) { | ||
dbHandler := DatabaseHandler{} | ||
|
||
err := dbHandler.DoInitialSetup() | ||
|
||
if err == nil { | ||
t.Errorf("DoInitialSetup did not return an error") | ||
} | ||
} |