Skip to content

Commit

Permalink
add DoCommand tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martha-johnston committed Sep 11, 2024
1 parent 362ebbb commit f652d0d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions components/base/sensorcontrolled/sensorcontrolled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sensorcontrolled
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -511,3 +512,26 @@ func TestSensorBaseMoveStraight(t *testing.T) {
orientationValue = defaultOrientationValue
})
}

func TestSensorBaseDoCommand(t *testing.T) {
ctx := context.Background()
logger := logging.NewTestLogger(t)
deps, cfg := msDependencies(t, []string{"setvel1", "position1", "orientation1"})
b, err := createSensorBase(ctx, deps, cfg, logger)
test.That(t, err, test.ShouldBeNil)

sb, ok := b.(*sensorBase)
test.That(t, ok, test.ShouldBeTrue)

expectedPID := control.PIDConfig{P: 0.1, I: 2.0, D: 0.0}
sb.tunedVals = &[]control.PIDConfig{expectedPID, {}}
expectedeMap := make(map[string]interface{})
expectedeMap["get_tuned_pid"] = (fmt.Sprintf("{p: %v, i: %v, d: %v, type: %v} ",
expectedPID.P, expectedPID.I, expectedPID.D, expectedPID.Type))

req := make(map[string]interface{})
req["get_tuned_pid"] = true
resp, err := b.DoCommand(ctx, req)
test.That(t, err, test.ShouldBeNil)
test.That(t, resp, test.ShouldResemble, expectedeMap)
}
15 changes: 15 additions & 0 deletions components/motor/gpio/controlled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package gpio

import (
"context"
"fmt"
"testing"

"go.viam.com/test"
"go.viam.com/utils/testutils"

"go.viam.com/rdk/components/board"
"go.viam.com/rdk/components/encoder"
"go.viam.com/rdk/control"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/resource"
Expand Down Expand Up @@ -99,4 +101,17 @@ func TestControlledMotorCreation(t *testing.T) {

test.That(t, cm.enc.Name().ShortName(), test.ShouldEqual, encoderName)
test.That(t, cm.real.Name().ShortName(), test.ShouldEqual, motorName)

// test DoCommand
expectedPID := control.PIDConfig{P: 0.1, I: 2.0, D: 0.0}
cm.tunedVals = &[]control.PIDConfig{expectedPID, {}}
expectedeMap := make(map[string]interface{})
expectedeMap["get_tuned_pid"] = (fmt.Sprintf("{p: %v, i: %v, d: %v, type: %v} ",
expectedPID.P, expectedPID.I, expectedPID.D, expectedPID.Type))

req := make(map[string]interface{})
req["get_tuned_pid"] = true
resp, err := m.DoCommand(context.Background(), req)
test.That(t, err, test.ShouldBeNil)
test.That(t, resp, test.ShouldResemble, expectedeMap)
}

0 comments on commit f652d0d

Please sign in to comment.