Skip to content

Commit

Permalink
Merge pull request #30 from richbl/dev
Browse files Browse the repository at this point in the history
test: Minor refactor to Go tests; revved Go to 1.23.1 to manage ongoi…
  • Loading branch information
richbl authored Dec 13, 2024
2 parents 3ac192d + e1a451d commit 828d061
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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

Expand Down
33 changes: 19 additions & 14 deletions internal/ble/sensor_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

}
33 changes: 18 additions & 15 deletions internal/configuration/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

})
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
})
}
Expand All @@ -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{
Expand All @@ -218,7 +221,7 @@ func TestValidateVideoConfig(t *testing.T) {
wantErr: false,
},
{
name: "invalid config",
name: invalidConfig,
config: VideoConfig{
FilePath: invalidTestFilename,
OnScreenDisplay: VideoOSDConfig{
Expand Down Expand Up @@ -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)
}

})
Expand All @@ -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,
},
Expand All @@ -283,15 +286,15 @@ func TestValidateBLEConfig(t *testing.T) {

tests := []configTestCase[BLEConfig]{
{
name: "valid config",
name: validConfig,
config: BLEConfig{
SensorUUID: sensorTestUUID,
ScanTimeoutSecs: 10,
},
wantErr: false,
},
{
name: "invalid config",
name: invalidConfig,
config: BLEConfig{
SensorUUID: "",
ScanTimeoutSecs: -1,
Expand All @@ -309,7 +312,7 @@ func TestValidateSpeedConfig(t *testing.T) {

tests := []configTestCase[SpeedConfig]{
{
name: "valid config",
name: validConfig,
config: SpeedConfig{
SmoothingWindow: 5,
SpeedThreshold: 10.0,
Expand All @@ -319,7 +322,7 @@ func TestValidateSpeedConfig(t *testing.T) {
wantErr: false,
},
{
name: "invalid config",
name: invalidConfig,
config: SpeedConfig{
SmoothingWindow: -1,
SpeedThreshold: -10.0,
Expand Down

0 comments on commit 828d061

Please sign in to comment.