You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

  1. package common
  2. import (
  3. "database/sql"
  4. "database/sql/driver"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestSignatureConstant(t *testing.T) {
  11. signatureConstant := uint32(3322668559)
  12. var signatureConstantBytes [4]byte
  13. binary.BigEndian.PutUint32(signatureConstantBytes[:], signatureConstant)
  14. assert.Equal(t, SignatureConstantBytes, signatureConstantBytes[:])
  15. assert.Equal(t, "c60be60f", hex.EncodeToString(SignatureConstantBytes))
  16. }
  17. func TestTxIDScannerValue(t *testing.T) {
  18. txid0 := &TxID{}
  19. txid1 := &TxID{}
  20. txid0B := [12]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
  21. txid1B := [12]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  22. copy(txid0[:], txid0B[:])
  23. copy(txid1[:], txid1B[:])
  24. var value driver.Valuer
  25. var scan sql.Scanner
  26. value = txid0
  27. scan = txid1
  28. fromDB, err := value.Value()
  29. assert.NoError(t, err)
  30. assert.NoError(t, scan.Scan(fromDB))
  31. assert.Equal(t, value, scan)
  32. }
  33. func TestTxIDMarshalers(t *testing.T) {
  34. h := []byte("0x00000000000001e240004700")
  35. var txid TxID
  36. err := txid.UnmarshalText(h)
  37. assert.NoError(t, err)
  38. assert.Equal(t, h, []byte(txid.String()))
  39. h2, err := txid.MarshalText()
  40. assert.NoError(t, err)
  41. assert.Equal(t, h, h2)
  42. var txid2 TxID
  43. err = txid2.UnmarshalText(h2)
  44. assert.NoError(t, err)
  45. assert.Equal(t, h2, []byte(txid2.String()))
  46. assert.Equal(t, h, h2)
  47. }