2019-08-20 19:37:45 +00:00
|
|
|
package db_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-07 15:53:49 +00:00
|
|
|
"net/url"
|
2019-09-04 11:37:50 +00:00
|
|
|
"strings"
|
2019-08-20 19:37:45 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-09-04 11:37:50 +00:00
|
|
|
"github.com/Masterminds/semver"
|
2019-08-20 19:37:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-19 12:30:47 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/storage/db"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/types"
|
|
|
|
"github.com/volker-raschek/flucky/test/goldenfiles"
|
2019-08-20 19:37:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
Name string
|
|
|
|
Test func(*testing.T)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
database db.Database
|
|
|
|
|
|
|
|
postgresContainerImage string = "docker.io/postgres/postgres"
|
2019-09-04 11:37:50 +00:00
|
|
|
|
2019-12-07 15:53:49 +00:00
|
|
|
storageEndpointString string = "postgres://flucky:flucky@markus-pc.trier.cryptic.systems/postgres?sslmode=disable"
|
2019-08-20 19:37:45 +00:00
|
|
|
|
|
|
|
goldenDevicesFilePath string = "test/goldenfiles/json/goldenDevices.json"
|
|
|
|
goldenSensorsFilePath string = "test/goldenfiles/json/goldenSensors.json"
|
2019-09-29 18:08:54 +00:00
|
|
|
goldenMeasuredValuesFilePath string = "test/goldenfiles/json/goldenMeasuredValuesUncompressedRounded.json"
|
|
|
|
goldenPressuresFilePath string = "test/goldenfiles/json/goldenPressuresUncompressedRounded.json"
|
|
|
|
goldenHumiditiesFilePath string = "test/goldenfiles/json/goldenHumiditiesUncompressedRounded.json"
|
|
|
|
goldenTemperaturesFilePath string = "test/goldenfiles/json/goldenTemperaturesUncompressedRounded.json"
|
2019-08-20 19:37:45 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2019-12-07 15:53:49 +00:00
|
|
|
storageEndpoint, err := url.Parse(storageEndpointString)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
db, err := db.New(storageEndpoint)
|
2019-08-20 19:37:45 +00:00
|
|
|
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
|
|
|
},
|
2020-01-18 13:42:15 +00:00
|
|
|
&test{
|
|
|
|
Name: "insertInfo",
|
|
|
|
Test: testInsertInfo,
|
|
|
|
},
|
2019-09-04 11:37:50 +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,
|
|
|
|
},
|
2020-01-18 13:42:15 +00:00
|
|
|
&test{
|
|
|
|
Name: "selectHumidities",
|
|
|
|
Test: testSelectHumidities,
|
|
|
|
},
|
|
|
|
&test{
|
|
|
|
Name: "selectPressures",
|
|
|
|
Test: testSelectPressures,
|
|
|
|
},
|
|
|
|
&test{
|
|
|
|
Name: "selectTemperatures",
|
|
|
|
Test: testSelectTemperatures,
|
|
|
|
},
|
|
|
|
// &test{
|
|
|
|
// Name: "selectMeasuredValues",
|
|
|
|
// Test: testSelectMeasuredValues,
|
|
|
|
// },
|
2019-09-04 11:37:50 +00:00
|
|
|
&test{
|
|
|
|
Name: "deleteHumidities",
|
|
|
|
Test: testDeleteHumidity,
|
|
|
|
},
|
2020-01-18 13:42:15 +00:00
|
|
|
&test{
|
|
|
|
Name: "deleteInfo",
|
|
|
|
Test: testDeleteInfo,
|
|
|
|
},
|
2019-09-04 11:37:50 +00:00
|
|
|
&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,
|
|
|
|
},
|
2020-01-18 13:42:15 +00:00
|
|
|
&test{
|
|
|
|
Name: "updateInfo",
|
|
|
|
Test: testUpdateInfo,
|
|
|
|
},
|
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)
|
2019-09-04 11:37:50 +00:00
|
|
|
|
|
|
|
homePath := "pkg/storage/db/sql/psql/schema"
|
|
|
|
sqlAssetFiles, err := db.AssetDir(homePath)
|
2019-09-01 10:27:06 +00:00
|
|
|
require.NoError(err)
|
2019-09-04 11:37:50 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, sqlAssetFile := range sqlAssetFiles {
|
|
|
|
fromVersion, err := semver.NewVersion(strings.ReplaceAll(sqlAssetFile, ".sql", ""))
|
|
|
|
require.NoError(err)
|
|
|
|
err = database.Schema(ctx, fromVersion)
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
2019-09-01 10:27:06 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-10 21:03:49 +00:00
|
|
|
testDevice, err := database.SelectDeviceByID(ctx, goldenDevice.ID)
|
2019-08-20 19:37:45 +00:00
|
|
|
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 {
|
2020-01-10 21:03:49 +00:00
|
|
|
testSensor, err := database.SelectSensorByID(ctx, goldenSensor.ID)
|
2019-08-20 19:37:45 +00:00
|
|
|
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})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 13:42:15 +00:00
|
|
|
func testInsertInfo(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.InsertInfo(ctx, "test", "value")
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
value, err := database.SelectInfo(ctx, "test")
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
require.Equal("value", value)
|
|
|
|
}
|
|
|
|
|
2019-08-20 19:37:45 +00:00
|
|
|
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})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 13:42:15 +00:00
|
|
|
func testSelectHumidities(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
humidities, err := database.SelectHumidities(ctx)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenfiles.CompareMeasuredValues(t, goldenHumidites, humidities)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSelectMeasuredValues(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
measuredValues, err := database.SelectMeasuredValues(ctx)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenfiles.CompareMeasuredValues(t, goldenMeasuredValues, measuredValues)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSelectPressures(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
pressures, err := database.SelectPressures(ctx)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenfiles.CompareMeasuredValues(t, goldenPressures, pressures)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSelectTemperatures(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
temperatures, err := database.SelectTemperatures(ctx)
|
|
|
|
require.NoError(err)
|
|
|
|
goldenfiles.CompareMeasuredValues(t, goldenTemperatures, temperatures)
|
|
|
|
}
|
|
|
|
|
2019-08-20 19:37:45 +00:00
|
|
|
func testDeleteDevices(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.DeleteDevices(ctx, goldenDevices)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, goldenDevice := range goldenDevices {
|
2020-01-10 21:03:49 +00:00
|
|
|
_, err := database.SelectDeviceByID(ctx, goldenDevice.ID)
|
2019-08-20 19:37:45 +00:00
|
|
|
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 {
|
2020-01-10 21:03:49 +00:00
|
|
|
_, err := database.SelectDeviceByID(ctx, goldenSensor.ID)
|
2019-08-20 19:37:45 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 13:42:15 +00:00
|
|
|
func testDeleteInfo(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
err := database.DeleteInfo(ctx, "test")
|
|
|
|
require.NoError(err)
|
|
|
|
_, err = database.SelectInfo(ctx, "test")
|
|
|
|
require.Error(err)
|
|
|
|
}
|
|
|
|
|
2019-08-20 19:37:45 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 13:42:15 +00:00
|
|
|
|
|
|
|
func testUpdateInfo(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// VALID
|
|
|
|
err := database.InsertInfo(ctx, "key", "value")
|
|
|
|
require.NoError(err)
|
|
|
|
err = database.UpdateInfo(ctx, "key", "value2")
|
|
|
|
require.NoError(err)
|
|
|
|
value, err := database.SelectInfo(ctx, "key")
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal("value2", value)
|
|
|
|
err = database.DeleteInfo(ctx, "key")
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// INVALID
|
|
|
|
err = database.UpdateInfo(ctx, "key2", "value")
|
|
|
|
require.Error(err)
|
|
|
|
}
|