gobot.io/x/gobot@v1.16.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"
     9  	"gobot.io/x/gobot/gobottest"
    10  )
    11  
    12  var _ gobot.Driver = (*SHT3xDriver)(nil)
    13  
    14  // --------- HELPERS
    15  func initTestSHT3xDriver() (driver *SHT3xDriver) {
    16  	driver, _ = initTestSHT3xDriverWithStubbedAdaptor()
    17  	return
    18  }
    19  
    20  func initTestSHT3xDriverWithStubbedAdaptor() (*SHT3xDriver, *i2cTestAdaptor) {
    21  	adaptor := newI2cTestAdaptor()
    22  	return NewSHT3xDriver(adaptor), adaptor
    23  }
    24  
    25  // --------- TESTS
    26  
    27  func TestNewSHT3xDriver(t *testing.T) {
    28  	// Does it return a pointer to an instance of SHT3xDriver?
    29  	var bm interface{} = NewSHT3xDriver(newI2cTestAdaptor())
    30  	_, ok := bm.(*SHT3xDriver)
    31  	if !ok {
    32  		t.Errorf("NewSHT3xDriver() should have returned a *SHT3xDriver")
    33  	}
    34  
    35  	b := NewSHT3xDriver(newI2cTestAdaptor())
    36  	gobottest.Refute(t, b.Connection(), nil)
    37  }
    38  
    39  // Methods
    40  
    41  func TestSHT3xDriverStart(t *testing.T) {
    42  	sht3x, _ := initTestSHT3xDriverWithStubbedAdaptor()
    43  
    44  	gobottest.Assert(t, sht3x.Start(), nil)
    45  }
    46  
    47  func TestSHT3xStartConnectError(t *testing.T) {
    48  	d, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
    49  	adaptor.Testi2cConnectErr(true)
    50  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    51  }
    52  
    53  func TestSHT3xDriverHalt(t *testing.T) {
    54  	sht3x := initTestSHT3xDriver()
    55  
    56  	gobottest.Assert(t, sht3x.Halt(), nil)
    57  }
    58  
    59  // Test Name & SetName
    60  func TestSHT3xDriverName(t *testing.T) {
    61  	sht3x := initTestSHT3xDriver()
    62  
    63  	gobottest.Assert(t, strings.HasPrefix(sht3x.Name(), "SHT3x"), true)
    64  	sht3x.SetName("Sensor")
    65  	gobottest.Assert(t, sht3x.Name(), "Sensor")
    66  }
    67  
    68  func TestSHT3xDriverOptions(t *testing.T) {
    69  	d := NewSHT3xDriver(newI2cTestAdaptor(), WithBus(2))
    70  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    71  }
    72  
    73  // Test Accuracy & SetAccuracy
    74  func TestSHT3xDriverSetAccuracy(t *testing.T) {
    75  	sht3x := initTestSHT3xDriver()
    76  
    77  	gobottest.Assert(t, sht3x.Accuracy(), byte(SHT3xAccuracyHigh))
    78  
    79  	err := sht3x.SetAccuracy(SHT3xAccuracyMedium)
    80  	gobottest.Assert(t, err, nil)
    81  	gobottest.Assert(t, sht3x.Accuracy(), byte(SHT3xAccuracyMedium))
    82  
    83  	err = sht3x.SetAccuracy(SHT3xAccuracyLow)
    84  	gobottest.Assert(t, err, nil)
    85  	gobottest.Assert(t, sht3x.Accuracy(), byte(SHT3xAccuracyLow))
    86  
    87  	err = sht3x.SetAccuracy(0xff)
    88  	gobottest.Assert(t, err, ErrInvalidAccuracy)
    89  }
    90  
    91  // Test Sample
    92  func TestSHT3xDriverSampleNormal(t *testing.T) {
    93  	sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
    94  
    95  	gobottest.Assert(t, sht3x.Start(), nil)
    96  
    97  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    98  		copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef, 0x92})
    99  		return 6, nil
   100  	}
   101  
   102  	temp, rh, _ := sht3x.Sample()
   103  	gobottest.Assert(t, temp, float32(85.523003))
   104  	gobottest.Assert(t, rh, float32(74.5845))
   105  
   106  	// check the temp with the units in F
   107  	sht3x.Units = "F"
   108  	temp, _, _ = sht3x.Sample()
   109  	gobottest.Assert(t, temp, float32(185.9414))
   110  }
   111  
   112  func TestSHT3xDriverSampleBadCrc(t *testing.T) {
   113  	sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
   114  
   115  	gobottest.Assert(t, sht3x.Start(), nil)
   116  
   117  	// Check that the 1st crc failure is caught
   118  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   119  		copy(b, []byte{0xbe, 0xef, 0x00, 0xbe, 0xef, 0x92})
   120  		return 6, nil
   121  	}
   122  
   123  	_, _, err := sht3x.Sample()
   124  	gobottest.Assert(t, err, ErrInvalidCrc)
   125  
   126  	// Check that the 2nd crc failure is caught
   127  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   128  		copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef, 0x00})
   129  		return 6, nil
   130  	}
   131  
   132  	_, _, err = sht3x.Sample()
   133  	gobottest.Assert(t, err, ErrInvalidCrc)
   134  }
   135  
   136  func TestSHT3xDriverSampleBadRead(t *testing.T) {
   137  	sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
   138  
   139  	gobottest.Assert(t, sht3x.Start(), nil)
   140  
   141  	// Check that the 1st crc failure is caught
   142  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   143  		copy(b, []byte{0xbe, 0xef, 0x00, 0xbe, 0xef})
   144  		return 5, nil
   145  	}
   146  
   147  	_, _, err := sht3x.Sample()
   148  	gobottest.Assert(t, err, ErrNotEnoughBytes)
   149  }
   150  
   151  func TestSHT3xDriverSampleUnits(t *testing.T) {
   152  	sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
   153  
   154  	gobottest.Assert(t, sht3x.Start(), nil)
   155  
   156  	// Check that the 1st crc failure is caught
   157  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   158  		copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef, 0x92})
   159  		return 6, nil
   160  	}
   161  
   162  	sht3x.Units = "K"
   163  	_, _, err := sht3x.Sample()
   164  	gobottest.Assert(t, err, ErrInvalidTemp)
   165  }
   166  
   167  // Test internal sendCommandDelayGetResponse
   168  func TestSHT3xDriverSCDGRIoFailures(t *testing.T) {
   169  	sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
   170  
   171  	gobottest.Assert(t, sht3x.Start(), nil)
   172  
   173  	invalidRead := errors.New("Read error")
   174  	invalidWrite := errors.New("Write error")
   175  
   176  	// Only send 5 bytes
   177  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   178  		copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef})
   179  		return 5, nil
   180  	}
   181  
   182  	_, err := sht3x.sendCommandDelayGetResponse(nil, nil, 2)
   183  	gobottest.Assert(t, err, ErrNotEnoughBytes)
   184  
   185  	// Don't read any bytes and return an error
   186  	adaptor.i2cReadImpl = func([]byte) (int, error) {
   187  		return 0, invalidRead
   188  	}
   189  
   190  	_, err = sht3x.sendCommandDelayGetResponse(nil, nil, 1)
   191  	gobottest.Assert(t, err, invalidRead)
   192  
   193  	// Don't write any bytes and return an error
   194  	adaptor.i2cWriteImpl = func([]byte) (int, error) {
   195  		return 42, invalidWrite
   196  	}
   197  
   198  	_, err = sht3x.sendCommandDelayGetResponse(nil, nil, 1)
   199  	gobottest.Assert(t, err, invalidWrite)
   200  }
   201  
   202  // Test Heater and getStatusRegister
   203  func TestSHT3xDriverHeater(t *testing.T) {
   204  	sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
   205  
   206  	gobottest.Assert(t, sht3x.Start(), nil)
   207  
   208  	// heater enabled
   209  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   210  		copy(b, []byte{0x20, 0x00, 0x5d})
   211  		return 3, nil
   212  	}
   213  
   214  	status, err := sht3x.Heater()
   215  	gobottest.Assert(t, err, nil)
   216  	gobottest.Assert(t, status, true)
   217  
   218  	// heater disabled
   219  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   220  		copy(b, []byte{0x00, 0x00, 0x81})
   221  		return 3, nil
   222  	}
   223  
   224  	status, err = sht3x.Heater()
   225  	gobottest.Assert(t, err, nil)
   226  	gobottest.Assert(t, status, false)
   227  
   228  	// heater crc failed
   229  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   230  		copy(b, []byte{0x00, 0x00, 0x00})
   231  		return 3, nil
   232  	}
   233  
   234  	status, err = sht3x.Heater()
   235  	gobottest.Assert(t, err, ErrInvalidCrc)
   236  
   237  	// heater read failed
   238  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   239  		copy(b, []byte{0x00, 0x00})
   240  		return 2, nil
   241  	}
   242  
   243  	status, err = sht3x.Heater()
   244  	gobottest.Refute(t, err, nil)
   245  }
   246  
   247  // Test SetHeater
   248  func TestSHT3xDriverSetHeater(t *testing.T) {
   249  	sht3x, _ := initTestSHT3xDriverWithStubbedAdaptor()
   250  
   251  	gobottest.Assert(t, sht3x.Start(), nil)
   252  
   253  	sht3x.SetHeater(false)
   254  	sht3x.SetHeater(true)
   255  }
   256  
   257  // Test SerialNumber
   258  func TestSHT3xDriverSerialNumber(t *testing.T) {
   259  	sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
   260  
   261  	gobottest.Assert(t, sht3x.Start(), nil)
   262  
   263  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   264  		copy(b, []byte{0x20, 0x00, 0x5d, 0xbe, 0xef, 0x92})
   265  		return 6, nil
   266  	}
   267  
   268  	sn, err := sht3x.SerialNumber()
   269  
   270  	gobottest.Assert(t, err, nil)
   271  	gobottest.Assert(t, sn, uint32(0x2000beef))
   272  }