package storage_test import ( "testing" "github.com/volker-raschek/flucky/pkg/storage" "github.com/volker-raschek/flucky/test/goldenfiles" "github.com/stretchr/testify/require" ) type testCase struct { source string expected string compression bool round float64 } var ( testCases = []*testCase{ // CSV - Uncompressed Not Rounded -> Uncompressed Rounded 0.5 &testCase{ source: "test/goldenfiles/csv/goldenMeasuredValuesUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenMeasuredValuesUncompressedRounded.csv", compression: false, round: 0.5, }, &testCase{ source: "test/goldenfiles/csv/goldenHumiditiesUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenHumiditiesUncompressedRounded.csv", compression: false, round: 0.5, }, &testCase{ source: "test/goldenfiles/csv/goldenPressuresUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenPressuresUncompressedRounded.csv", compression: false, round: 0.5, }, &testCase{ source: "test/goldenfiles/csv/goldenTemperaturesUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenTemperaturesUncompressedRounded.csv", compression: false, round: 0.5, }, // CSV - Uncompressed Not Rounded -> Compressed Not Rounded &testCase{ source: "test/goldenfiles/csv/goldenMeasuredValuesUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenMeasuredValuesCompressedNotRounded.csv", compression: true, round: 0, }, &testCase{ source: "test/goldenfiles/csv/goldenHumiditiesUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenHumiditiesCompressedNotRounded.csv", compression: true, round: 0, }, &testCase{ source: "test/goldenfiles/csv/goldenPressuresUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenPressuresCompressedNotRounded.csv", compression: true, round: 0, }, &testCase{ source: "test/goldenfiles/csv/goldenTemperaturesUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenTemperaturesCompressedNotRounded.csv", compression: true, round: 0, }, // CSV - Uncompressed Not Rounded -> Compressed Rounded 0.5 &testCase{ source: "test/goldenfiles/csv/goldenMeasuredValuesUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenMeasuredValuesCompressedRounded.csv", compression: true, round: 0.5, }, &testCase{ source: "test/goldenfiles/csv/goldenHumiditiesUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenHumiditiesCompressedRounded.csv", compression: true, round: 0.5, }, &testCase{ source: "test/goldenfiles/csv/goldenPressuresUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenPressuresCompressedRounded.csv", compression: true, round: 0.5, }, &testCase{ source: "test/goldenfiles/csv/goldenTemperaturesUncompressedNotRounded.csv", expected: "test/goldenfiles/csv/goldenTemperaturesCompressedRounded.csv", compression: true, round: 0.5, }, // JSON - Uncompressed Not Rounded -> Uncompressed Rounded 0.5 &testCase{ source: "test/goldenfiles/json/goldenMeasuredValuesUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenMeasuredValuesUncompressedRounded.json", compression: false, round: 0.5, }, &testCase{ source: "test/goldenfiles/json/goldenHumiditiesUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenHumiditiesUncompressedRounded.json", compression: false, round: 0.5, }, &testCase{ source: "test/goldenfiles/json/goldenPressuresUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenPressuresUncompressedRounded.json", compression: false, round: 0.5, }, &testCase{ source: "test/goldenfiles/json/goldenTemperaturesUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenTemperaturesUncompressedRounded.json", compression: false, round: 0.5, }, // JSON - Uncompressed Not Rounded -> Compressed Not Rounded &testCase{ source: "test/goldenfiles/json/goldenMeasuredValuesUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenMeasuredValuesCompressedNotRounded.json", compression: true, round: 0, }, &testCase{ source: "test/goldenfiles/json/goldenHumiditiesUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenHumiditiesCompressedNotRounded.json", compression: true, round: 0, }, &testCase{ source: "test/goldenfiles/json/goldenPressuresUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenPressuresCompressedNotRounded.json", compression: true, round: 0, }, &testCase{ source: "test/goldenfiles/json/goldenTemperaturesUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenTemperaturesCompressedNotRounded.json", compression: true, round: 0, }, // JSON - Uncompressed Not Rounded -> Compressed Rounded 0.5 &testCase{ source: "test/goldenfiles/json/goldenMeasuredValuesUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenMeasuredValuesCompressedRounded.json", compression: true, round: 0.5, }, &testCase{ source: "test/goldenfiles/json/goldenHumiditiesUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenHumiditiesCompressedRounded.json", compression: true, round: 0.5, }, &testCase{ source: "test/goldenfiles/json/goldenPressuresUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenPressuresCompressedRounded.json", compression: true, round: 0.5, }, &testCase{ source: "test/goldenfiles/json/goldenTemperaturesUncompressedNotRounded.json", expected: "test/goldenfiles/json/goldenTemperaturesCompressedRounded.json", compression: true, round: 0.5, }, // XML - Uncompressed Not Rounded -> Uncompressed Rounded 0.5 &testCase{ source: "test/goldenfiles/xml/goldenMeasuredValuesUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenMeasuredValuesUncompressedRounded.xml", compression: false, round: 0.5, }, &testCase{ source: "test/goldenfiles/xml/goldenHumiditiesUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenHumiditiesUncompressedRounded.xml", compression: false, round: 0.5, }, &testCase{ source: "test/goldenfiles/xml/goldenPressuresUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenPressuresUncompressedRounded.xml", compression: false, round: 0.5, }, &testCase{ source: "test/goldenfiles/xml/goldenTemperaturesUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenTemperaturesUncompressedRounded.xml", compression: false, round: 0.5, }, // XML - Uncompressed Not Rounded -> Compressed Not Rounded &testCase{ source: "test/goldenfiles/xml/goldenMeasuredValuesUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenMeasuredValuesCompressedNotRounded.xml", compression: true, round: 0, }, &testCase{ source: "test/goldenfiles/xml/goldenHumiditiesUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenHumiditiesCompressedNotRounded.xml", compression: true, round: 0, }, &testCase{ source: "test/goldenfiles/xml/goldenPressuresUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenPressuresCompressedNotRounded.xml", compression: true, round: 0, }, &testCase{ source: "test/goldenfiles/xml/goldenTemperaturesUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenTemperaturesCompressedNotRounded.xml", compression: true, round: 0, }, // XML - Uncompressed Not Rounded -> Compressed Rounded 0.5 &testCase{ source: "test/goldenfiles/xml/goldenMeasuredValuesUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenMeasuredValuesCompressedRounded.xml", compression: true, round: 0.5, }, &testCase{ source: "test/goldenfiles/xml/goldenHumiditiesUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenHumiditiesCompressedRounded.xml", compression: true, round: 0.5, }, &testCase{ source: "test/goldenfiles/xml/goldenPressuresUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenPressuresCompressedRounded.xml", compression: true, round: 0.5, }, &testCase{ source: "test/goldenfiles/xml/goldenTemperaturesUncompressedNotRounded.xml", expected: "test/goldenfiles/xml/goldenTemperaturesCompressedRounded.xml", compression: true, round: 0.5, }, } ) func TestCompressionAndRounding(t *testing.T) { require := require.New(t) for _, testCase := range testCases { measuredValues, err := goldenfiles.GetGoldenMeasuredValues(testCase.source) require.NoError(err) expectedMeasuredValues, err := goldenfiles.GetGoldenMeasuredValues(testCase.expected) require.NoError(err) actualMeasuredValues := measuredValues if testCase.round != 0 { storage.Round(actualMeasuredValues, testCase.round) } if testCase.compression { actualMeasuredValues = storage.Compression(actualMeasuredValues) } for i, _ := range expectedMeasuredValues { require.Equal(expectedMeasuredValues[i].ID, actualMeasuredValues[i].ID, "ID of element %v is not equal between expected and actual", i) require.Equal(expectedMeasuredValues[i].Value, actualMeasuredValues[i].Value, "Value of element %v is not equal between expected and actual", i) require.Equal(expectedMeasuredValues[i].ValueType, actualMeasuredValues[i].ValueType, "ValueType of element %v is not equal between expected and actual", i) require.Equal(expectedMeasuredValues[i].FromDate, actualMeasuredValues[i].FromDate, "FromDate of element %v is not equal between expected and actual", i) require.Equal(expectedMeasuredValues[i].TillDate, actualMeasuredValues[i].TillDate, "TillDate of element %v is not equal between expected and actual", i) require.Equal(expectedMeasuredValues[i].SensorID, actualMeasuredValues[i].SensorID, "SensorID of element %v is not equal between expected and actual", i) } } }