gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/sht3x_driver_test.go (about)

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"gobot.io/x/gobot/v2"
     9  	"gobot.io/x/gobot/v2/gobottest"
    10  )
    11  
    12  // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
    13  // and tests all implementations, so no further tests needed here for gobot.Driver interface
    14  var _ gobot.Driver = (*SHT3xDriver)(nil)
    15  
    16  func initTestSHT3xDriverWithStubbedAdaptor() (*SHT3xDriver, *i2cTestAdaptor) {
    17  	a := newI2cTestAdaptor()
    18  	d := NewSHT3xDriver(a)
    19  	if err := d.Start(); err != nil {
    20  		panic(err)
    21  	}
    22  	return d, a
    23  }
    24  
    25  func TestNewSHT3xDriver(t *testing.T) {
    26  	var di interface{} = NewSHT3xDriver(newI2cTestAdaptor())
    27  	d, ok := di.(*SHT3xDriver)
    28  	if !ok {
    29  		t.Errorf("NewSHT3xDriver() should have returned a *SHT3xDriver")
    30  	}
    31  	gobottest.Refute(t, d.Driver, nil)
    32  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "SHT3x"), true)
    33  	gobottest.Assert(t, d.defaultAddress, 0x44)
    34  }
    35  
    36  func TestSHT3xOptions(t *testing.T) {
    37  	// This is a general test, that options are applied in constructor by using the common WithBus() option and
    38  	// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
    39  	d := NewSHT3xDriver(newI2cTestAdaptor(), WithBus(2))
    40  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    41  }
    42  
    43  func TestSHT3xStart(t *testing.T) {
    44  	d := NewSHT3xDriver(newI2cTestAdaptor())
    45  	gobottest.Assert(t, d.Start(), nil)
    46  }
    47  
    48  func TestSHT3xHalt(t *testing.T) {
    49  	d, _ := initTestSHT3xDriverWithStubbedAdaptor()
    50  	gobottest.Assert(t, d.Halt(), nil)
    51  }
    52  
    53  func TestSHT3xSampleNormal(t *testing.T) {
    54  	d, a := initTestSHT3xDriverWithStubbedAdaptor()
    55  	a.i2cReadImpl = func(b []byte) (int, error) {
    56  		copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef, 0x92})
    57  		return 6, nil
    58  	}
    59  
    60  	temp, rh, _ := d.Sample()
    61  	gobottest.Assert(t, temp, float32(85.523003))
    62  	gobottest.Assert(t, rh, float32(74.5845))
    63  
    64  	// check the temp with the units in F
    65  	d.Units = "F"
    66  	temp, _, _ = d.Sample()
    67  	gobottest.Assert(t, temp, float32(185.9414))
    68  }
    69  
    70  func TestSHT3xSampleBadCrc(t *testing.T) {
    71  	d, a := initTestSHT3xDriverWithStubbedAdaptor()
    72  	// Check that the 1st crc failure is caught
    73  	a.i2cReadImpl = func(b []byte) (int, error) {
    74  		copy(b, []byte{0xbe, 0xef, 0x00, 0xbe, 0xef, 0x92})
    75  		return 6, nil
    76  	}
    77  
    78  	_, _, err := d.Sample()
    79  	gobottest.Assert(t, err, ErrInvalidCrc)
    80  
    81  	// Check that the 2nd crc failure is caught
    82  	a.i2cReadImpl = func(b []byte) (int, error) {
    83  		copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef, 0x00})
    84  		return 6, nil
    85  	}
    86  
    87  	_, _, err = d.Sample()
    88  	gobottest.Assert(t, err, ErrInvalidCrc)
    89  }
    90  
    91  func TestSHT3xSampleBadRead(t *testing.T) {
    92  	d, a := initTestSHT3xDriverWithStubbedAdaptor()
    93  	// Check that the 1st crc failure is caught
    94  	a.i2cReadImpl = func(b []byte) (int, error) {
    95  		copy(b, []byte{0xbe, 0xef, 0x00, 0xbe, 0xef})
    96  		return 5, nil
    97  	}
    98  
    99  	_, _, err := d.Sample()
   100  	gobottest.Assert(t, err, ErrNotEnoughBytes)
   101  }
   102  
   103  func TestSHT3xSampleUnits(t *testing.T) {
   104  	d, a := initTestSHT3xDriverWithStubbedAdaptor()
   105  	// Check that the 1st crc failure is caught
   106  	a.i2cReadImpl = func(b []byte) (int, error) {
   107  		copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef, 0x92})
   108  		return 6, nil
   109  	}
   110  
   111  	d.Units = "K"
   112  	_, _, err := d.Sample()
   113  	gobottest.Assert(t, err, ErrInvalidTemp)
   114  }
   115  
   116  // Test internal sendCommandDelayGetResponse
   117  func TestSHT3xSCDGRIoFailures(t *testing.T) {
   118  	d, a := initTestSHT3xDriverWithStubbedAdaptor()
   119  	invalidRead := errors.New("Read error")
   120  	invalidWrite := errors.New("Write error")
   121  
   122  	// Only send 5 bytes
   123  	a.i2cReadImpl = func(b []byte) (int, error) {
   124  		copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef})
   125  		return 5, nil
   126  	}
   127  
   128  	_, err := d.sendCommandDelayGetResponse(nil, nil, 2)
   129  	gobottest.Assert(t, err, ErrNotEnoughBytes)
   130  
   131  	// Don't read any bytes and return an error
   132  	a.i2cReadImpl = func([]byte) (int, error) {
   133  		return 0, invalidRead
   134  	}
   135  
   136  	_, err = d.sendCommandDelayGetResponse(nil, nil, 1)
   137  	gobottest.Assert(t, err, invalidRead)
   138  
   139  	// Don't write any bytes and return an error
   140  	a.i2cWriteImpl = func([]byte) (int, error) {
   141  		return 42, invalidWrite
   142  	}
   143  
   144  	_, err = d.sendCommandDelayGetResponse(nil, nil, 1)
   145  	gobottest.Assert(t, err, invalidWrite)
   146  }
   147  
   148  // Test Heater and getStatusRegister
   149  func TestSHT3xHeater(t *testing.T) {
   150  	d, a := initTestSHT3xDriverWithStubbedAdaptor()
   151  	// heater enabled
   152  	a.i2cReadImpl = func(b []byte) (int, error) {
   153  		copy(b, []byte{0x20, 0x00, 0x5d})
   154  		return 3, nil
   155  	}
   156  
   157  	status, err := d.Heater()
   158  	gobottest.Assert(t, err, nil)
   159  	gobottest.Assert(t, status, true)
   160  
   161  	// heater disabled
   162  	a.i2cReadImpl = func(b []byte) (int, error) {
   163  		copy(b, []byte{0x00, 0x00, 0x81})
   164  		return 3, nil
   165  	}
   166  
   167  	status, err = d.Heater()
   168  	gobottest.Assert(t, err, nil)
   169  	gobottest.Assert(t, status, false)
   170  
   171  	// heater crc failed
   172  	a.i2cReadImpl = func(b []byte) (int, error) {
   173  		copy(b, []byte{0x00, 0x00, 0x00})
   174  		return 3, nil
   175  	}
   176  
   177  	_, err = d.Heater()
   178  	gobottest.Assert(t, err, ErrInvalidCrc)
   179  
   180  	// heater read failed
   181  	a.i2cReadImpl = func(b []byte) (int, error) {
   182  		copy(b, []byte{0x00, 0x00})
   183  		return 2, nil
   184  	}
   185  
   186  	_, err = d.Heater()
   187  	gobottest.Refute(t, err, nil)
   188  }
   189  
   190  func TestSHT3xSetHeater(t *testing.T) {
   191  	d, _ := initTestSHT3xDriverWithStubbedAdaptor()
   192  	d.SetHeater(false)
   193  	d.SetHeater(true)
   194  }
   195  
   196  func TestSHT3xSetAccuracy(t *testing.T) {
   197  	d, _ := initTestSHT3xDriverWithStubbedAdaptor()
   198  
   199  	gobottest.Assert(t, d.Accuracy(), byte(SHT3xAccuracyHigh))
   200  
   201  	err := d.SetAccuracy(SHT3xAccuracyMedium)
   202  	gobottest.Assert(t, err, nil)
   203  	gobottest.Assert(t, d.Accuracy(), byte(SHT3xAccuracyMedium))
   204  
   205  	err = d.SetAccuracy(SHT3xAccuracyLow)
   206  	gobottest.Assert(t, err, nil)
   207  	gobottest.Assert(t, d.Accuracy(), byte(SHT3xAccuracyLow))
   208  
   209  	err = d.SetAccuracy(0xff)
   210  	gobottest.Assert(t, err, ErrInvalidAccuracy)
   211  }
   212  
   213  func TestSHT3xSerialNumber(t *testing.T) {
   214  	d, a := initTestSHT3xDriverWithStubbedAdaptor()
   215  	a.i2cReadImpl = func(b []byte) (int, error) {
   216  		copy(b, []byte{0x20, 0x00, 0x5d, 0xbe, 0xef, 0x92})
   217  		return 6, nil
   218  	}
   219  
   220  	sn, err := d.SerialNumber()
   221  
   222  	gobottest.Assert(t, err, nil)
   223  	gobottest.Assert(t, sn, uint32(0x2000beef))
   224  }