2020-06-03 20:42:11 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 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/>.
|
|
|
|
|
2020-06-29 21:11:29 +00:00
|
|
|
package pgipfsethdb_test
|
2020-06-03 20:42:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
2021-08-24 05:12:22 +00:00
|
|
|
"time"
|
2020-06-03 20:42:11 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2020-07-09 19:49:01 +00:00
|
|
|
pgipfsethdb "github.com/vulcanize/ipfs-ethdb/postgres"
|
2020-06-03 20:42:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
database ethdb.Database
|
|
|
|
db *sqlx.DB
|
|
|
|
err error
|
|
|
|
testHeader = types.Header{Number: big.NewInt(1337)}
|
|
|
|
testValue, _ = rlp.EncodeToBytes(testHeader)
|
|
|
|
testEthKey = testHeader.Hash().Bytes()
|
2020-07-09 19:49:01 +00:00
|
|
|
testMhKey, _ = pgipfsethdb.MultihashKeyFromKeccak256(testEthKey)
|
2020-06-03 20:42:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Database", func() {
|
|
|
|
BeforeEach(func() {
|
2020-07-09 19:49:01 +00:00
|
|
|
db, err = pgipfsethdb.TestDB()
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-24 05:12:22 +00:00
|
|
|
|
|
|
|
cacheConfig := pgipfsethdb.CacheConfig{
|
|
|
|
Name: "db",
|
|
|
|
Size: 3000000, // 3MB
|
|
|
|
ExpiryDuration: time.Hour,
|
|
|
|
}
|
|
|
|
|
|
|
|
database = pgipfsethdb.NewDatabase(db, cacheConfig)
|
2020-06-03 20:42:11 +00:00
|
|
|
})
|
|
|
|
AfterEach(func() {
|
2020-07-09 19:49:01 +00:00
|
|
|
err = pgipfsethdb.ResetTestDB(db)
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Has", func() {
|
|
|
|
It("returns false if a key-pair doesn't exist in the db", func() {
|
|
|
|
has, err := database.Has(testEthKey)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(has).ToNot(BeTrue())
|
|
|
|
})
|
|
|
|
It("returns true if a key-pair exists in the db", func() {
|
|
|
|
_, err = db.Exec("INSERT into public.blocks (key, data) VALUES ($1, $2)", testMhKey, testValue)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
has, err := database.Has(testEthKey)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(has).To(BeTrue())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Get", func() {
|
|
|
|
It("throws an err if the key-pair doesn't exist in the db", func() {
|
|
|
|
_, err = database.Get(testEthKey)
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("sql: no rows in result set"))
|
|
|
|
})
|
|
|
|
It("returns the value associated with the key, if the pair exists", func() {
|
|
|
|
_, err = db.Exec("INSERT into public.blocks (key, data) VALUES ($1, $2)", testMhKey, testValue)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
val, err := database.Get(testEthKey)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(val).To(Equal(testValue))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Put", func() {
|
|
|
|
It("persists the key-value pair in the database", func() {
|
|
|
|
_, err = database.Get(testEthKey)
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("sql: no rows in result set"))
|
|
|
|
|
|
|
|
err = database.Put(testEthKey, testValue)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
val, err := database.Get(testEthKey)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(val).To(Equal(testValue))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Delete", func() {
|
|
|
|
It("removes the key-value pair from the database", func() {
|
|
|
|
err = database.Put(testEthKey, testValue)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
val, err := database.Get(testEthKey)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(val).To(Equal(testValue))
|
|
|
|
|
|
|
|
err = database.Delete(testEthKey)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
_, err = database.Get(testEthKey)
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("sql: no rows in result set"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|