2022-04-21 13:28:54 +00:00
|
|
|
package postgres_test
|
2022-04-20 16:12:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-21 13:28:54 +00:00
|
|
|
"fmt"
|
2022-04-20 16:12:55 +00:00
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
2022-04-21 13:28:54 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
2022-04-20 19:44:15 +00:00
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql"
|
2022-04-21 13:28:54 +00:00
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql/postgres"
|
2022-04-20 16:12:55 +00:00
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/testhelpers"
|
|
|
|
)
|
|
|
|
|
2022-04-21 13:28:54 +00:00
|
|
|
var _ = Describe("Pgx", func() {
|
2022-04-20 16:12:55 +00:00
|
|
|
|
2022-04-21 13:28:54 +00:00
|
|
|
var (
|
|
|
|
ctx context.Context
|
|
|
|
)
|
2022-04-20 16:12:55 +00:00
|
|
|
|
2022-04-21 13:28:54 +00:00
|
|
|
BeforeEach(func() {
|
|
|
|
ctx = context.Background()
|
2022-04-20 16:12:55 +00:00
|
|
|
})
|
|
|
|
|
2022-04-21 13:28:54 +00:00
|
|
|
Describe("Connecting to the DB", func() {
|
|
|
|
Context("But connection is unsucessful", func() {
|
|
|
|
It("throws error when can't connect to the database", func() {
|
|
|
|
_, err := postgres.NewPostgresDB(postgres.Config{
|
|
|
|
Driver: "PGX",
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
|
|
|
|
present, err := doesContainsSubstring(err.Error(), sql.DbConnectionFailedMsg)
|
|
|
|
Expect(present).To(BeTrue())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
Context("The connection is successful", func() {
|
|
|
|
It("Should create a DB object", func() {
|
|
|
|
db, err := postgres.NewPostgresDB(postgres.DefaultConfig)
|
|
|
|
defer db.Close()
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
Describe("Write to the DB", func() {
|
|
|
|
Context("Serialize big.Int to DB", func() {
|
|
|
|
It("Should serialize successfully", func() {
|
|
|
|
dbPool, err := postgres.NewPostgresDB(postgres.DefaultConfig)
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
defer dbPool.Close()
|
|
|
|
|
|
|
|
bi := new(big.Int)
|
|
|
|
bi.SetString("34940183920000000000", 10)
|
|
|
|
isEqual, err := testhelpers.IsEqual(bi.String(), "34940183920000000000")
|
|
|
|
Expect(isEqual).To(BeTrue())
|
|
|
|
|
|
|
|
defer dbPool.Exec(ctx, `DROP TABLE IF EXISTS example`)
|
|
|
|
_, err = dbPool.Exec(ctx, "CREATE TABLE example ( id INTEGER, data NUMERIC )")
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
|
|
|
sqlStatement := `
|
2022-04-20 16:12:55 +00:00
|
|
|
INSERT INTO example (id, data)
|
|
|
|
VALUES (1, cast($1 AS NUMERIC))`
|
2022-04-21 13:28:54 +00:00
|
|
|
_, err = dbPool.Exec(ctx, sqlStatement, bi.String())
|
|
|
|
Expect(err).To(BeNil())
|
2022-04-20 16:12:55 +00:00
|
|
|
|
2022-04-21 13:28:54 +00:00
|
|
|
var data string
|
|
|
|
err = dbPool.QueryRow(ctx, `SELECT cast(data AS TEXT) FROM example WHERE id = 1`).Scan(&data)
|
|
|
|
Expect(err).To(BeNil())
|
2022-04-20 16:12:55 +00:00
|
|
|
|
2022-04-21 13:28:54 +00:00
|
|
|
isEqual, err = testhelpers.IsEqual(data, bi.String())
|
|
|
|
Expect(isEqual).To(BeTrue())
|
|
|
|
actual := new(big.Int)
|
|
|
|
actual.SetString(data, 10)
|
2022-04-20 16:12:55 +00:00
|
|
|
|
2022-04-21 13:28:54 +00:00
|
|
|
isEqual, err = testhelpers.IsEqual(actual, bi)
|
|
|
|
Expect(isEqual).To(BeTrue())
|
|
|
|
})
|
2022-04-20 22:12:44 +00:00
|
|
|
})
|
2022-04-20 16:12:55 +00:00
|
|
|
})
|
2022-04-21 13:28:54 +00:00
|
|
|
})
|
2022-04-20 17:06:00 +00:00
|
|
|
|
2022-04-21 13:28:54 +00:00
|
|
|
func doesContainsSubstring(full string, sub string) (bool, error) {
|
|
|
|
if !strings.Contains(full, sub) {
|
|
|
|
return false, fmt.Errorf("Expected \"%v\" to contain substring \"%v\"\n", full, sub)
|
|
|
|
}
|
|
|
|
return true, nil
|
2022-04-20 16:12:55 +00:00
|
|
|
}
|