From e1a451df958058df9bfaac012b7735849ddc1874 Mon Sep 17 00:00:00 2001 From: richbl Date: Fri, 13 Dec 2024 15:17:21 -0800 Subject: [PATCH] test: Minor refactor to Go tests; revved Go to 1.23.1 to manage ongoing CVE issues Signed-off-by: richbl --- go.mod | 2 +- internal/ble/sensor_controller_test.go | 33 +++++++++++++++----------- internal/configuration/config_test.go | 33 ++++++++++++++------------ 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index 0d1c2f3..83dee50 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/richbl/go-ble-sync-cycle -go 1.23.0 +go 1.23.1 require github.com/BurntSushi/toml v1.4.0 diff --git a/internal/ble/sensor_controller_test.go b/internal/ble/sensor_controller_test.go index 608fc68..9508e69 100644 --- a/internal/ble/sensor_controller_test.go +++ b/internal/ble/sensor_controller_test.go @@ -155,10 +155,7 @@ func TestProcessBLESpeed(t *testing.T) { // TestNewBLEControllerIntegration tests the creation of a new BLEController func TestNewBLEControllerIntegration(t *testing.T) { - waitForScanReset() - - // Create test BLE and speed controllers - controller, err := createTestController(speedUnitsKMH) + controller, err := controllersIntegrationTest() if err != nil { t.Skip(noBLEAdapterError) return @@ -171,11 +168,7 @@ func TestNewBLEControllerIntegration(t *testing.T) { // TestScanForBLEPeripheralIntegration tests the ScanForBLEPeripheral() function func TestScanForBLEPeripheralIntegration(t *testing.T) { - // Pause for scan reset - waitForScanReset() - - // Create test BLE and speed controllers - controller, err := createTestController(speedUnitsKMH) + controller, err := controllersIntegrationTest() if err != nil { t.Skip(noBLEAdapterError) return @@ -193,11 +186,7 @@ func TestScanForBLEPeripheralIntegration(t *testing.T) { // TestGetBLECharacteristicIntegration tests the GetBLECharacteristic() function func TestGetBLECharacteristicIntegration(t *testing.T) { - // Pause for scan reset - waitForScanReset() - - // Create test BLE and speed controllers - controller, err := createTestController(speedUnitsKMH) + controller, err := controllersIntegrationTest() if err != nil { t.Skip(noBLEAdapterError) return @@ -211,3 +200,19 @@ func TestGetBLECharacteristicIntegration(t *testing.T) { assert.Error(t, err) } + +// controllersIntegrationTest pauses BLE scan and then creates controllers +func controllersIntegrationTest() (*ble.BLEController, error) { + + // Pause to permit BLE adapter to reset + waitForScanReset() + + // Create test BLE and speed controllers + controller, err := createTestController(speedUnitsKMH) + if err != nil { + return nil, err + } + + return controller, nil + +} diff --git a/internal/configuration/config_test.go b/internal/configuration/config_test.go index aa7163e..725282f 100644 --- a/internal/configuration/config_test.go +++ b/internal/configuration/config_test.go @@ -11,6 +11,9 @@ const ( invalidTestFilename = "non-existent-file.mp4" sensorTestUUID = "test-uuid" logLevelInvalid = "invalid" + validConfig = "Valid config" + invalidConfig = "Invalid config" + validateErrMessage = "validate() error = %v, wantErr %v" ) // configTestCase is a helper struct for running validation tests @@ -129,7 +132,7 @@ func runValidationTest[T any](t *testing.T, tests []configTestCase[T]) { } if (err != nil) != tt.wantErr { - t.Errorf("validate() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf(validateErrMessage, err, tt.wantErr) } }) @@ -142,8 +145,8 @@ func TestLoadFile(t *testing.T) { // Define test cases tests := []configTestCase[string]{ - {name: "valid config", config: validConfigTOML, wantErr: false}, - {name: "invalid config", config: invalidConfigTOML, wantErr: true}, + {name: validConfig, config: validConfigTOML, wantErr: false}, + {name: invalidConfig, config: invalidConfigTOML, wantErr: true}, } // Run tests @@ -168,8 +171,8 @@ func TestValidate(t *testing.T) { // Define test cases tests := []configTestCase[string]{ - {name: "valid config", config: validConfigTOML, wantErr: false}, - {name: "invalid config", config: invalidConfigTOML, wantErr: true}, + {name: validConfig, config: validConfigTOML, wantErr: false}, + {name: invalidConfig, config: invalidConfigTOML, wantErr: true}, } // Run tests @@ -192,7 +195,7 @@ func TestValidate(t *testing.T) { } if err := config.validate(); (err != nil) != tt.wantErr { - t.Errorf("validate() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf(validateErrMessage, err, tt.wantErr) } }) } @@ -205,7 +208,7 @@ func TestValidateVideoConfig(t *testing.T) { // Define test cases tests := []configTestCase[VideoConfig]{ { - name: "valid config", + name: validConfig, config: VideoConfig{ FilePath: testFilename, OnScreenDisplay: VideoOSDConfig{ @@ -218,7 +221,7 @@ func TestValidateVideoConfig(t *testing.T) { wantErr: false, }, { - name: "invalid config", + name: invalidConfig, config: VideoConfig{ FilePath: invalidTestFilename, OnScreenDisplay: VideoOSDConfig{ @@ -250,7 +253,7 @@ func TestValidateVideoConfig(t *testing.T) { } if err := tt.config.validate(); (err != nil) != tt.wantErr { - t.Errorf("validate() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf(validateErrMessage, err, tt.wantErr) } }) @@ -263,12 +266,12 @@ func TestValidateAppConfig(t *testing.T) { tests := []configTestCase[AppConfig]{ { - name: "valid config", + name: validConfig, config: AppConfig{LogLevel: logLevelDebug}, wantErr: false, }, { - name: "invalid config", + name: invalidConfig, config: AppConfig{LogLevel: logLevelInvalid}, wantErr: true, }, @@ -283,7 +286,7 @@ func TestValidateBLEConfig(t *testing.T) { tests := []configTestCase[BLEConfig]{ { - name: "valid config", + name: validConfig, config: BLEConfig{ SensorUUID: sensorTestUUID, ScanTimeoutSecs: 10, @@ -291,7 +294,7 @@ func TestValidateBLEConfig(t *testing.T) { wantErr: false, }, { - name: "invalid config", + name: invalidConfig, config: BLEConfig{ SensorUUID: "", ScanTimeoutSecs: -1, @@ -309,7 +312,7 @@ func TestValidateSpeedConfig(t *testing.T) { tests := []configTestCase[SpeedConfig]{ { - name: "valid config", + name: validConfig, config: SpeedConfig{ SmoothingWindow: 5, SpeedThreshold: 10.0, @@ -319,7 +322,7 @@ func TestValidateSpeedConfig(t *testing.T) { wantErr: false, }, { - name: "invalid config", + name: invalidConfig, config: SpeedConfig{ SmoothingWindow: -1, SpeedThreshold: -10.0,