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.

78 lines
1.7 KiB

  1. package db
  2. import (
  3. "math/big"
  4. "os"
  5. "testing"
  6. "github.com/russross/meddler"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. type foo struct {
  11. V int
  12. }
  13. func TestSliceToSlicePtrs(t *testing.T) {
  14. n := 16
  15. a := make([]foo, n)
  16. for i := 0; i < n; i++ {
  17. a[i] = foo{V: i}
  18. }
  19. b := SliceToSlicePtrs(a).([]*foo)
  20. for i := 0; i < len(a); i++ {
  21. assert.Equal(t, a[i], *b[i])
  22. }
  23. }
  24. func TestSlicePtrsToSlice(t *testing.T) {
  25. n := 16
  26. a := make([]*foo, n)
  27. for i := 0; i < n; i++ {
  28. a[i] = &foo{V: i}
  29. }
  30. b := SlicePtrsToSlice(a).([]foo)
  31. for i := 0; i < len(a); i++ {
  32. assert.Equal(t, *a[i], b[i])
  33. }
  34. }
  35. func TestBigInt(t *testing.T) {
  36. pass := os.Getenv("POSTGRES_PASS")
  37. db, err := InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  38. require.NoError(t, err)
  39. defer func() {
  40. _, err := db.Exec("DROP TABLE IF EXISTS test_big_int;")
  41. require.NoError(t, err)
  42. err = db.Close()
  43. require.NoError(t, err)
  44. }()
  45. _, err = db.Exec("DROP TABLE IF EXISTS test_big_int;")
  46. require.NoError(t, err)
  47. _, err = db.Exec(`CREATE TABLE test_big_int (
  48. item_id SERIAL PRIMARY KEY,
  49. value1 DECIMAL(78, 0) NOT NULL,
  50. value2 DECIMAL(78, 0),
  51. value3 DECIMAL(78, 0)
  52. );`)
  53. require.NoError(t, err)
  54. type Entry struct {
  55. ItemID int `meddler:"item_id"`
  56. Value1 *big.Int `meddler:"value1,bigint"`
  57. Value2 *big.Int `meddler:"value2,bigintnull"`
  58. Value3 *big.Int `meddler:"value3,bigintnull"`
  59. }
  60. entry := Entry{ItemID: 1, Value1: big.NewInt(1234567890), Value2: big.NewInt(9876543210), Value3: nil}
  61. err = meddler.Insert(db, "test_big_int", &entry)
  62. require.NoError(t, err)
  63. var dbEntry Entry
  64. err = meddler.QueryRow(db, &dbEntry, "SELECT * FROM test_big_int WHERE item_id = 1;")
  65. require.NoError(t, err)
  66. assert.Equal(t, entry, dbEntry)
  67. }