gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/ina3221_driver_test.go (about) 1 package i2c 2 3 import ( 4 "testing" 5 6 "errors" 7 8 "strings" 9 10 "gobot.io/x/gobot/v2" 11 "gobot.io/x/gobot/v2/gobottest" 12 ) 13 14 // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver 15 // and tests all implementations, so no further tests needed here for gobot.Driver interface 16 var _ gobot.Driver = (*INA3221Driver)(nil) 17 18 func initTestINA3221DriverWithStubbedAdaptor() (*INA3221Driver, *i2cTestAdaptor) { 19 a := newI2cTestAdaptor() 20 d := NewINA3221Driver(a) 21 if err := d.Start(); err != nil { 22 panic(err) 23 } 24 return d, a 25 } 26 27 func TestNewINA3221Driver(t *testing.T) { 28 var di interface{} = NewINA3221Driver(newI2cTestAdaptor()) 29 d, ok := di.(*INA3221Driver) 30 if !ok { 31 t.Error("NewINA3221Driver() should return a *INA3221Driver") 32 } 33 gobottest.Refute(t, d.Driver, nil) 34 gobottest.Assert(t, strings.HasPrefix(d.Name(), "INA3221"), true) 35 gobottest.Assert(t, d.defaultAddress, 0x40) 36 } 37 38 func TestINA3221Options(t *testing.T) { 39 // This is a general test, that options are applied in constructor by using the common WithBus() option and 40 // least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)". 41 d := NewINA3221Driver(newI2cTestAdaptor(), WithBus(2)) 42 gobottest.Assert(t, d.GetBusOrDefault(1), 2) 43 } 44 45 func TestINA3221Start(t *testing.T) { 46 d := NewINA3221Driver(newI2cTestAdaptor()) 47 gobottest.Assert(t, d.Start(), nil) 48 } 49 50 func TestINA3221Halt(t *testing.T) { 51 d, _ := initTestINA3221DriverWithStubbedAdaptor() 52 gobottest.Assert(t, d.Halt(), nil) 53 } 54 55 func TestINA3221GetBusVoltage(t *testing.T) { 56 d, a := initTestINA3221DriverWithStubbedAdaptor() 57 a.i2cReadImpl = func(b []byte) (int, error) { 58 // bus voltage sensor values from 12V battery 59 copy(b, []byte{0x36, 0x68}) 60 return 2, nil 61 } 62 63 v, err := d.GetBusVoltage(INA3221Channel1) 64 gobottest.Assert(t, v, float64(13.928)) 65 gobottest.Assert(t, err, nil) 66 } 67 68 func TestINA3221GetBusVoltageReadError(t *testing.T) { 69 d, a := initTestINA3221DriverWithStubbedAdaptor() 70 a.i2cReadImpl = func(b []byte) (int, error) { 71 return 0, errors.New("read error") 72 } 73 74 _, err := d.GetBusVoltage(INA3221Channel1) 75 gobottest.Assert(t, err, errors.New("read error")) 76 } 77 78 func TestINA3221GetShuntVoltage(t *testing.T) { 79 d, a := initTestINA3221DriverWithStubbedAdaptor() 80 a.i2cReadImpl = func(b []byte) (int, error) { 81 // shunt voltage sensor values from 12V battery 82 copy(b, []byte{0x05, 0xD8}) 83 return 2, nil 84 } 85 86 v, err := d.GetShuntVoltage(INA3221Channel1) 87 gobottest.Assert(t, v, float64(7.48)) 88 gobottest.Assert(t, err, nil) 89 } 90 91 func TestINA3221GetShuntVoltageReadError(t *testing.T) { 92 d, a := initTestINA3221DriverWithStubbedAdaptor() 93 a.i2cReadImpl = func(b []byte) (int, error) { 94 return 0, errors.New("read error") 95 } 96 97 _, err := d.GetShuntVoltage(INA3221Channel1) 98 gobottest.Assert(t, err, errors.New("read error")) 99 } 100 101 func TestINA3221GetCurrent(t *testing.T) { 102 d, a := initTestINA3221DriverWithStubbedAdaptor() 103 a.i2cReadImpl = func(b []byte) (int, error) { 104 // shunt voltage sensor values from 12V battery 105 copy(b, []byte{0x05, 0x0D8}) 106 return 2, nil 107 } 108 109 v, err := d.GetCurrent(INA3221Channel1) 110 gobottest.Assert(t, v, float64(74.8)) 111 gobottest.Assert(t, err, nil) 112 } 113 114 func TestINA3221CurrentReadError(t *testing.T) { 115 d, a := initTestINA3221DriverWithStubbedAdaptor() 116 a.i2cReadImpl = func(b []byte) (int, error) { 117 return 0, errors.New("read error") 118 } 119 120 _, err := d.GetCurrent(INA3221Channel1) 121 gobottest.Assert(t, err, errors.New("read error")) 122 } 123 124 func TestINA3221GetLoadVoltage(t *testing.T) { 125 d, a := initTestINA3221DriverWithStubbedAdaptor() 126 i := 0 127 a.i2cReadImpl = func(b []byte) (int, error) { 128 // TODO: return test data as read from actual sensor 129 copy(b, []byte{0x36, 0x68, 0x05, 0xd8}[i:i+2]) 130 i += 2 131 return 2, nil 132 } 133 134 v, err := d.GetLoadVoltage(INA3221Channel2) 135 gobottest.Assert(t, v, float64(13.935480)) 136 gobottest.Assert(t, err, nil) 137 } 138 139 func TestINA3221GetLoadVoltageReadError(t *testing.T) { 140 d, a := initTestINA3221DriverWithStubbedAdaptor() 141 a.i2cReadImpl = func(b []byte) (int, error) { 142 return 0, errors.New("read error") 143 } 144 145 _, err := d.GetLoadVoltage(INA3221Channel2) 146 gobottest.Assert(t, err, errors.New("read error")) 147 }