2022-05-18 16:12:54 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2022 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-04-22 12:28:01 +00:00
|
|
|
package postgres_test
|
2022-04-20 16:12:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-22 12:28:01 +00:00
|
|
|
"fmt"
|
2022-04-20 16:12:55 +00:00
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
2022-04-22 12:28:01 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
2022-06-09 20:12:08 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/database/sql"
|
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/database/sql/postgres"
|
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/testhelpers"
|
2022-04-20 16:12:55 +00:00
|
|
|
)
|
|
|
|
|
2022-04-22 12:28:01 +00:00
|
|
|
var _ = Describe("Pgx", func() {
|
2022-04-20 16:12:55 +00:00
|
|
|
|
2022-04-22 12:28:01 +00:00
|
|
|
var (
|
|
|
|
ctx context.Context
|
|
|
|
)
|
2022-04-20 16:12:55 +00:00
|
|
|
|
2022-04-22 12:28:01 +00:00
|
|
|
BeforeEach(func() {
|
|
|
|
ctx = context.Background()
|
2022-04-20 16:12:55 +00:00
|
|
|
})
|
|
|
|
|
2022-04-22 16:27:54 +00:00
|
|
|
Describe("Connecting to the DB", Label("integration"), func() {
|
2022-04-22 12:28:01 +00:00
|
|
|
Context("But connection is unsucessful", func() {
|
|
|
|
It("throws error when can't connect to the database", func() {
|
|
|
|
_, err := postgres.NewPostgresDB(postgres.Config{
|
|
|
|
Driver: "PGX",
|
|
|
|
})
|
2022-05-12 13:52:13 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
2022-04-22 12:28:01 +00:00
|
|
|
|
|
|
|
present, err := doesContainsSubstring(err.Error(), sql.DbConnectionFailedMsg)
|
|
|
|
Expect(present).To(BeTrue())
|
2022-05-12 13:52:13 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2022-04-22 12:28:01 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
Context("The connection is successful", func() {
|
|
|
|
It("Should create a DB object", func() {
|
|
|
|
db, err := postgres.NewPostgresDB(postgres.DefaultConfig)
|
|
|
|
Expect(err).To(BeNil())
|
2022-05-12 13:52:13 +00:00
|
|
|
defer db.Close()
|
2022-04-22 12:28:01 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2022-04-22 16:27:54 +00:00
|
|
|
Describe("Write to the DB", Label("integration"), func() {
|
2022-04-22 12:28:01 +00:00
|
|
|
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")
|
2022-04-22 16:27:54 +00:00
|
|
|
Expect(err).To(BeNil())
|
2022-04-22 12:28:01 +00:00
|
|
|
Expect(isEqual).To(BeTrue())
|
|
|
|
|
2022-04-22 16:27:54 +00:00
|
|
|
defer func() {
|
|
|
|
_, err := dbPool.Exec(ctx, `DROP TABLE IF EXISTS example`)
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
}()
|
2022-04-22 12:28:01 +00:00
|
|
|
_, 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-22 12:28:01 +00:00
|
|
|
_, err = dbPool.Exec(ctx, sqlStatement, bi.String())
|
|
|
|
Expect(err).To(BeNil())
|
2022-04-20 16:12:55 +00:00
|
|
|
|
2022-04-22 12:28:01 +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-22 12:28:01 +00:00
|
|
|
isEqual, err = testhelpers.IsEqual(data, bi.String())
|
|
|
|
Expect(isEqual).To(BeTrue())
|
2022-04-22 16:27:54 +00:00
|
|
|
Expect(err).To(BeNil())
|
2022-04-22 12:28:01 +00:00
|
|
|
actual := new(big.Int)
|
|
|
|
actual.SetString(data, 10)
|
2022-04-20 16:12:55 +00:00
|
|
|
|
2022-04-22 12:28:01 +00:00
|
|
|
isEqual, err = testhelpers.IsEqual(actual, bi)
|
|
|
|
Expect(isEqual).To(BeTrue())
|
2022-04-22 16:27:54 +00:00
|
|
|
Expect(err).To(BeNil())
|
2022-04-22 12:28:01 +00:00
|
|
|
})
|
2022-04-20 22:12:44 +00:00
|
|
|
})
|
2022-04-20 16:12:55 +00:00
|
|
|
})
|
2022-04-22 12:28:01 +00:00
|
|
|
})
|
2022-04-20 17:06:00 +00:00
|
|
|
|
2022-04-22 12:28:01 +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
|
|
|
}
|