gobot.io/x/gobot@v1.16.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" 11 "gobot.io/x/gobot/gobottest" 12 ) 13 14 var _ gobot.Driver = (*INA3221Driver)(nil) 15 16 func initTestINA3221Driver() *INA3221Driver { 17 d, _ := initTestINA3221DriverWithStubbedAdaptor() 18 return d 19 } 20 21 func initTestINA3221DriverWithStubbedAdaptor() (*INA3221Driver, *i2cTestAdaptor) { 22 a := newI2cTestAdaptor() 23 return NewINA3221Driver(a), a 24 } 25 26 func TestNewINA3221Driver(t *testing.T) { 27 var d interface{} = NewINA3221Driver(newI2cTestAdaptor()) 28 if _, ok := d.(*INA3221Driver); !ok { 29 t.Error("NewINA3221Driver() should return a *INA3221Driver") 30 } 31 } 32 33 func TestINA3221Driver_Connection(t *testing.T) { 34 d := initTestINA3221Driver() 35 gobottest.Refute(t, d.Connection(), nil) 36 } 37 38 func TestINA3221Driver_Start(t *testing.T) { 39 d := initTestINA3221Driver() 40 gobottest.Assert(t, d.Start(), nil) 41 } 42 43 func TestINA3221Driver_ConnectError(t *testing.T) { 44 d, a := initTestINA3221DriverWithStubbedAdaptor() 45 a.Testi2cConnectErr(true) 46 gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection")) 47 } 48 49 func TestINA3221Driver_StartWriteError(t *testing.T) { 50 d, a := initTestINA3221DriverWithStubbedAdaptor() 51 a.i2cWriteImpl = func([]byte) (int, error) { 52 return 0, errors.New("write error") 53 } 54 gobottest.Assert(t, d.Start(), errors.New("write error")) 55 } 56 57 func TestINA3221Driver_Halt(t *testing.T) { 58 d := initTestINA3221Driver() 59 gobottest.Assert(t, d.Halt(), nil) 60 } 61 62 func TestINA3221DriverGetBusVoltage(t *testing.T) { 63 d, a := initTestINA3221DriverWithStubbedAdaptor() 64 gobottest.Assert(t, d.Start(), nil) 65 66 a.i2cReadImpl = func(b []byte) (int, error) { 67 // bus voltage sensor values from 12V battery 68 copy(b, []byte{0x36, 0x68}) 69 return 2, nil 70 } 71 72 v, err := d.GetBusVoltage(INA3221Channel1) 73 gobottest.Assert(t, v, float64(13.928)) 74 gobottest.Assert(t, err, nil) 75 } 76 77 func TestINA3221DriverGetBusVoltageReadError(t *testing.T) { 78 d, a := initTestINA3221DriverWithStubbedAdaptor() 79 gobottest.Assert(t, d.Start(), nil) 80 81 a.i2cReadImpl = func(b []byte) (int, error) { 82 return 0, errors.New("read error") 83 } 84 85 _, err := d.GetBusVoltage(INA3221Channel1) 86 gobottest.Assert(t, err, errors.New("read error")) 87 } 88 89 func TestINA3221DriverGetShuntVoltage(t *testing.T) { 90 d, a := initTestINA3221DriverWithStubbedAdaptor() 91 gobottest.Assert(t, d.Start(), nil) 92 93 a.i2cReadImpl = func(b []byte) (int, error) { 94 // shunt voltage sensor values from 12V battery 95 copy(b, []byte{0x05, 0xD8}) 96 return 2, nil 97 } 98 99 v, err := d.GetShuntVoltage(INA3221Channel1) 100 gobottest.Assert(t, v, float64(7.48)) 101 gobottest.Assert(t, err, nil) 102 } 103 104 func TestINA3221DriverGetShuntVoltageReadError(t *testing.T) { 105 d, a := initTestINA3221DriverWithStubbedAdaptor() 106 gobottest.Assert(t, d.Start(), nil) 107 108 a.i2cReadImpl = func(b []byte) (int, error) { 109 return 0, errors.New("read error") 110 } 111 112 _, err := d.GetShuntVoltage(INA3221Channel1) 113 gobottest.Assert(t, err, errors.New("read error")) 114 } 115 116 func TestINA3221DriverGetCurrent(t *testing.T) { 117 d, a := initTestINA3221DriverWithStubbedAdaptor() 118 gobottest.Assert(t, d.Start(), nil) 119 120 a.i2cReadImpl = func(b []byte) (int, error) { 121 // shunt voltage sensor values from 12V battery 122 copy(b, []byte{0x05, 0x0D8}) 123 return 2, nil 124 } 125 126 v, err := d.GetCurrent(INA3221Channel1) 127 gobottest.Assert(t, v, float64(74.8)) 128 gobottest.Assert(t, err, nil) 129 } 130 131 func TestINA3221DriverCurrentReadError(t *testing.T) { 132 d, a := initTestINA3221DriverWithStubbedAdaptor() 133 gobottest.Assert(t, d.Start(), nil) 134 135 a.i2cReadImpl = func(b []byte) (int, error) { 136 return 0, errors.New("read error") 137 } 138 139 _, err := d.GetCurrent(INA3221Channel1) 140 gobottest.Assert(t, err, errors.New("read error")) 141 } 142 143 func TestINA3221DriverGetLoadVoltage(t *testing.T) { 144 d, a := initTestINA3221DriverWithStubbedAdaptor() 145 gobottest.Assert(t, d.Start(), nil) 146 147 i := 0 148 a.i2cReadImpl = func(b []byte) (int, error) { 149 // TODO: return test data as read from actual sensor 150 copy(b, []byte{0x36, 0x68, 0x05, 0xd8}[i:i+2]) 151 i += 2 152 return 2, nil 153 } 154 155 v, err := d.GetLoadVoltage(INA3221Channel2) 156 gobottest.Assert(t, v, float64(13.935480)) 157 gobottest.Assert(t, err, nil) 158 } 159 160 func TestINA3221DriverGetLoadVoltageReadError(t *testing.T) { 161 d, a := initTestINA3221DriverWithStubbedAdaptor() 162 gobottest.Assert(t, d.Start(), nil) 163 164 a.i2cReadImpl = func(b []byte) (int, error) { 165 return 0, errors.New("read error") 166 } 167 168 _, err := d.GetLoadVoltage(INA3221Channel2) 169 gobottest.Assert(t, err, errors.New("read error")) 170 } 171 172 func TestINA3221DriverName(t *testing.T) { 173 d := initTestINA3221Driver() 174 gobottest.Assert(t, strings.HasPrefix(d.Name(), "INA3221"), true) 175 } 176 177 func TestINA3221DriverSetName(t *testing.T) { 178 d := initTestINA3221Driver() 179 d.SetName("foobot") 180 gobottest.Assert(t, d.Name(), "foobot") 181 }