2019-08-20 19:37:45 +00:00
|
|
|
package db_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-flucky/flucky/pkg/db"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
|
|
"github.com/go-flucky/flucky/test/goldenfiles"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
Name string
|
|
|
|
Test func(*testing.T)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
database db.Database
|
|
|
|
|
|
|
|
postgresContainerImage string = "docker.io/postgres/postgres"
|
|
|
|
postgresHost string = "localhost"
|
|
|
|
postgresPort string = "5432"
|
|
|
|
postgresUser string = "postgres"
|
|
|
|
postgresPassword string = "postgres"
|
|
|
|
postgresDatabase string = "postgres"
|
|
|
|
|
|
|
|
goldenDevicesFilePath string = "test/goldenfiles/json/goldenDevices.json"
|
|
|
|
goldenSensorsFilePath string = "test/goldenfiles/json/goldenSensors.json"
|
|
|
|
goldenMeasuredValuesFilePath string = "test/goldenfiles/json/goldenMeasuredValues.json"
|
|
|
|
goldenPressuresFilePath string = "test/goldenfiles/json/goldenPressures.json"
|
|
|
|
goldenHumiditiesFilePath string = "test/goldenfiles/json/goldenHumidities.json"
|
|
|
|
goldenTemperaturesFilePath string = "test/goldenfiles/json/goldenTemperatures.json"
|
|
|
|
|
|
|
|
goldenDevices []*types.Device
|
|
|
|
goldenSensors []*types.Sensor
|
|
|
|
goldenMeasuredValues []*types.MeasuredValue
|
|
|
|
goldenPressures []*types.MeasuredValue
|
|
|
|
goldenHumidites []*types.MeasuredValue
|
|
|
|
goldenTemperatures []*types.MeasuredValue
|
|
|
|
)
|
|
|
|
|
|
|
|
func load(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
d, err := goldenfiles.GetGoldenDevices(goldenDevicesFilePath)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenDevices = d
|
|
|
|
|
|
|
|
s, err := goldenfiles.GetGoldenSensors(goldenSensorsFilePath)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenSensors = s
|
|
|
|
|
|
|
|
hum, err := goldenfiles.GetGoldenMeasuredValues(goldenHumiditiesFilePath)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenHumidites = hum
|
|
|
|
|
|
|
|
mv, err := goldenfiles.GetGoldenMeasuredValues(goldenMeasuredValuesFilePath)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenMeasuredValues = mv
|
|
|
|
|
|
|
|
pres, err := goldenfiles.GetGoldenMeasuredValues(goldenPressuresFilePath)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenPressures = pres
|
|
|
|
|
|
|
|
temp, err := goldenfiles.GetGoldenMeasuredValues(goldenTemperaturesFilePath)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenTemperatures = temp
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostgres(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
load(t)
|
|
|
|
|
|
|
|
db, err := db.New(db.DBOTypePostgres, postgresHost, postgresPort, postgresDatabase, postgresUser, postgresPassword)
|
|
|
|
database = db
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
tests := []*test{
|
|
|
|
&test{
|
2019-09-01 10:27:06 +00:00
|
|
|
Name: "schema",
|
|
|
|
Test: testSchemaCreate,
|
2019-08-20 19:37:45 +00:00
|
|
|
},
|
2019-09-01 10:27:06 +00:00
|
|
|
// &test{
|
|
|
|
// Name: "insertDevices",
|
|
|
|
// Test: testInsertDevices,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "insertSensors",
|
|
|
|
// Test: testInsertSensors,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "insertHumidity",
|
|
|
|
// Test: testInsertHumidity,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "insertPressure",
|
|
|
|
// Test: testInsertPressure,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "insertTemperatures",
|
|
|
|
// Test: testInsertTemperatures,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "deleteHumidities",
|
|
|
|
// Test: testDeleteHumidity,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "deletePressures",
|
|
|
|
// Test: testDeletePressures,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "deleteTemperatures",
|
|
|
|
// Test: testDeleteTemperatures,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "insertMeasuredValues",
|
|
|
|
// Test: testInsertMeasuredValues,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "deleteMeasuredValues",
|
|
|
|
// Test: testDeleteMeasuredValues,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "deleteSensors",
|
|
|
|
// Test: testDeleteSensors,
|
|
|
|
// },
|
|
|
|
// &test{
|
|
|
|
// Name: "deleteDevices",
|
|
|
|
// Test: testDeleteDevices,
|
|
|
|
// },
|
2019-08-20 19:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.Name, test.Test)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-01 10:27:06 +00:00
|
|
|
func testSchemaCreate(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.Schema(ctx, "v0.1.0")
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
|
2019-08-20 19:37:45 +00:00
|
|
|
func testInsertDevices(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.InsertDevices(ctx, goldenDevices)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenDevice := range goldenDevices {
|
|
|
|
testDevice, err := database.SelectDeviceByID(ctx, goldenDevice.DeviceID)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenfiles.CompareMeasuredValues(t, goldenDevice, testDevice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testInsertSensors(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.InsertSensors(ctx, goldenSensors)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenSensor := range goldenSensors {
|
|
|
|
testSensor, err := database.SelectSensorByID(ctx, goldenSensor.SensorID)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenfiles.CompareMeasuredValues(t, goldenSensor, testSensor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testInsertHumidity(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.InsertMeasuredValues(ctx, goldenHumidites)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenHumidity := range goldenHumidites {
|
|
|
|
testHumidity, err := database.SelectHumidityByID(ctx, goldenHumidity.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenfiles.CompareMeasuredValues(t, []*types.MeasuredValue{testHumidity}, []*types.MeasuredValue{testHumidity})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testInsertMeasuredValues(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.InsertMeasuredValues(ctx, goldenMeasuredValues)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenMeasuredValue := range goldenMeasuredValues {
|
|
|
|
testMeasuredValue, err := database.SelectMeasuredValuesByIDAndType(ctx, goldenMeasuredValue.ID, goldenMeasuredValue.ValueType)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenfiles.CompareMeasuredValues(t, []*types.MeasuredValue{goldenMeasuredValue}, []*types.MeasuredValue{testMeasuredValue})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testInsertPressure(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.InsertMeasuredValues(ctx, goldenPressures)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenPressure := range goldenPressures {
|
|
|
|
testPressure, err := database.SelectPressureByID(ctx, goldenPressure.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenfiles.CompareMeasuredValues(t, []*types.MeasuredValue{testPressure}, []*types.MeasuredValue{testPressure})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testInsertTemperatures(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.InsertMeasuredValues(ctx, goldenTemperatures)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenTemperature := range goldenTemperatures {
|
|
|
|
testTemperature, err := database.SelectTemperatureByID(ctx, goldenTemperature.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenfiles.CompareMeasuredValues(t, []*types.MeasuredValue{goldenTemperature}, []*types.MeasuredValue{testTemperature})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeleteDevices(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.DeleteDevices(ctx, goldenDevices)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenDevice := range goldenDevices {
|
|
|
|
_, err := database.SelectDeviceByID(ctx, goldenDevice.DeviceID)
|
|
|
|
require.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeleteSensors(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.DeleteSensors(ctx, goldenSensors)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenSensor := range goldenSensors {
|
|
|
|
_, err := database.SelectDeviceByID(ctx, goldenSensor.SensorID)
|
|
|
|
require.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeleteHumidity(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.DeleteMeasuredValues(ctx, goldenHumidites)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenHumidity := range goldenHumidites {
|
|
|
|
_, err := database.SelectHumidityByID(ctx, goldenHumidity.ID)
|
|
|
|
require.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeleteMeasuredValues(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.DeleteMeasuredValues(ctx, goldenMeasuredValues)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenMeasuredValue := range goldenMeasuredValues {
|
|
|
|
_, err := database.SelectPressureByID(ctx, goldenMeasuredValue.ID)
|
|
|
|
require.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeletePressures(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.DeleteMeasuredValues(ctx, goldenPressures)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenPressure := range goldenPressures {
|
|
|
|
_, err := database.SelectPressureByID(ctx, goldenPressure.ID)
|
|
|
|
require.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeleteTemperatures(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.DeleteMeasuredValues(ctx, goldenTemperatures)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenTemperature := range goldenTemperatures {
|
|
|
|
_, err := database.SelectTemperatureByID(ctx, goldenTemperature.ID)
|
|
|
|
require.Error(err)
|
|
|
|
}
|
|
|
|
}
|