2019-08-05 18:28:06 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
2019-10-03 16:17:08 +00:00
|
|
|
//
|
2019-08-05 18:28:06 +00:00
|
|
|
// 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.
|
2019-10-03 16:17:08 +00:00
|
|
|
//
|
2019-08-05 18:28:06 +00:00
|
|
|
// 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.
|
2019-10-03 16:17:08 +00:00
|
|
|
//
|
2019-08-05 18:28:06 +00:00
|
|
|
// 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/>.
|
2019-08-01 20:24:02 +00:00
|
|
|
|
2019-10-03 16:17:08 +00:00
|
|
|
package repository_test
|
2019-08-01 20:24:02 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-19 21:51:17 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-08-02 13:52:14 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2019-08-01 20:24:02 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
2019-08-02 13:52:14 +00:00
|
|
|
. "github.com/onsi/gomega"
|
2019-08-19 21:51:17 +00:00
|
|
|
|
2019-10-24 16:35:39 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/repository"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2019-08-01 20:24:02 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
"github.com/vulcanize/vulcanizedb/test_config"
|
|
|
|
)
|
|
|
|
|
2019-08-02 13:52:14 +00:00
|
|
|
var _ = Describe("address lookup", func() {
|
2019-08-01 20:24:02 +00:00
|
|
|
var (
|
2019-08-02 13:52:14 +00:00
|
|
|
db *postgres.DB
|
2019-08-01 20:24:02 +00:00
|
|
|
address = fakes.FakeAddress.Hex()
|
|
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
|
|
db = test_config.NewTestDB(test_config.NewTestNode())
|
|
|
|
test_config.CleanTestDB(db)
|
|
|
|
})
|
|
|
|
|
2019-08-19 21:55:38 +00:00
|
|
|
AfterEach(func() {
|
|
|
|
test_config.CleanTestDB(db)
|
|
|
|
})
|
|
|
|
|
2019-08-01 20:24:02 +00:00
|
|
|
type dbAddress struct {
|
2019-10-23 18:14:49 +00:00
|
|
|
Id int64
|
|
|
|
Address string
|
|
|
|
HashedAddress string `db:"hashed_address"`
|
2019-08-01 20:24:02 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 13:52:14 +00:00
|
|
|
Describe("GetOrCreateAddress", func() {
|
|
|
|
It("creates an address record", func() {
|
2019-10-03 16:17:08 +00:00
|
|
|
addressId, createErr := repository.GetOrCreateAddress(db, address)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(createErr).NotTo(HaveOccurred())
|
2019-08-01 20:24:02 +00:00
|
|
|
|
2019-08-02 13:52:14 +00:00
|
|
|
var actualAddress dbAddress
|
2019-10-23 18:14:49 +00:00
|
|
|
getErr := db.Get(&actualAddress, `SELECT id, address, hashed_address FROM public.addresses LIMIT 1`)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
2019-10-23 18:14:49 +00:00
|
|
|
hashedAddress := utils.HexToKeccak256Hash(address).Hex()
|
|
|
|
expectedAddress := dbAddress{Id: addressId, Address: address, HashedAddress: hashedAddress}
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(actualAddress).To(Equal(expectedAddress))
|
|
|
|
})
|
2019-08-01 20:24:02 +00:00
|
|
|
|
2019-08-02 13:52:14 +00:00
|
|
|
It("returns the existing record id if the address already exists", func() {
|
2019-10-03 16:17:08 +00:00
|
|
|
createId, createErr := repository.GetOrCreateAddress(db, address)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(createErr).NotTo(HaveOccurred())
|
2019-08-01 20:24:02 +00:00
|
|
|
|
2019-10-03 16:17:08 +00:00
|
|
|
getId, getErr := repository.GetOrCreateAddress(db, address)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
2019-08-01 20:24:02 +00:00
|
|
|
|
2019-08-02 13:52:14 +00:00
|
|
|
var addressCount int
|
|
|
|
addressErr := db.Get(&addressCount, `SELECT count(*) FROM public.addresses`)
|
|
|
|
Expect(addressErr).NotTo(HaveOccurred())
|
2019-08-05 14:04:29 +00:00
|
|
|
Expect(addressCount).To(Equal(1))
|
2019-08-19 21:55:38 +00:00
|
|
|
Expect(createId).To(Equal(getId))
|
2019-08-02 13:52:14 +00:00
|
|
|
})
|
2019-08-01 20:24:02 +00:00
|
|
|
|
2019-08-02 13:52:14 +00:00
|
|
|
It("gets upper-cased addresses", func() {
|
|
|
|
upperAddress := strings.ToUpper(address)
|
2019-10-03 16:17:08 +00:00
|
|
|
upperAddressId, createErr := repository.GetOrCreateAddress(db, upperAddress)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(createErr).NotTo(HaveOccurred())
|
2019-08-01 20:24:02 +00:00
|
|
|
|
2019-10-03 16:17:08 +00:00
|
|
|
mixedCaseAddressId, getErr := repository.GetOrCreateAddress(db, address)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
|
|
|
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("gets lower-cased addresses", func() {
|
|
|
|
lowerAddress := strings.ToLower(address)
|
2019-10-03 16:17:08 +00:00
|
|
|
upperAddressId, createErr := repository.GetOrCreateAddress(db, lowerAddress)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(createErr).NotTo(HaveOccurred())
|
|
|
|
|
2019-10-03 16:17:08 +00:00
|
|
|
mixedCaseAddressId, getErr := repository.GetOrCreateAddress(db, address)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
|
|
|
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
|
|
|
})
|
2019-08-01 20:24:02 +00:00
|
|
|
})
|
|
|
|
|
2019-08-02 13:52:14 +00:00
|
|
|
Describe("GetOrCreateAddressInTransaction", func() {
|
|
|
|
var (
|
|
|
|
tx *sqlx.Tx
|
|
|
|
txErr error
|
|
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
|
|
tx, txErr = db.Beginx()
|
|
|
|
Expect(txErr).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2019-08-19 21:55:38 +00:00
|
|
|
AfterEach(func() {
|
|
|
|
tx.Rollback()
|
|
|
|
})
|
|
|
|
|
2019-08-02 13:52:14 +00:00
|
|
|
It("creates an address record", func() {
|
2019-10-03 16:17:08 +00:00
|
|
|
addressId, createErr := repository.GetOrCreateAddressInTransaction(tx, address)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(createErr).NotTo(HaveOccurred())
|
2019-08-19 21:55:38 +00:00
|
|
|
commitErr := tx.Commit()
|
|
|
|
Expect(commitErr).NotTo(HaveOccurred())
|
2019-08-02 13:52:14 +00:00
|
|
|
|
|
|
|
var actualAddress dbAddress
|
2019-10-23 18:14:49 +00:00
|
|
|
getErr := db.Get(&actualAddress, `SELECT id, address, hashed_address FROM public.addresses LIMIT 1`)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
2019-10-23 18:14:49 +00:00
|
|
|
hashedAddress := utils.HexToKeccak256Hash(address).Hex()
|
|
|
|
expectedAddress := dbAddress{Id: addressId, Address: address, HashedAddress: hashedAddress}
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(actualAddress).To(Equal(expectedAddress))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns the existing record id if the address already exists", func() {
|
2019-10-03 16:17:08 +00:00
|
|
|
_, createErr := repository.GetOrCreateAddressInTransaction(tx, address)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(createErr).NotTo(HaveOccurred())
|
|
|
|
|
2019-10-03 16:17:08 +00:00
|
|
|
_, getErr := repository.GetOrCreateAddressInTransaction(tx, address)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
var addressCount int
|
|
|
|
addressErr := db.Get(&addressCount, `SELECT count(*) FROM public.addresses`)
|
|
|
|
Expect(addressErr).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("gets upper-cased addresses", func() {
|
|
|
|
upperAddress := strings.ToUpper(address)
|
2019-10-03 16:17:08 +00:00
|
|
|
upperAddressId, createErr := repository.GetOrCreateAddressInTransaction(tx, upperAddress)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(createErr).NotTo(HaveOccurred())
|
|
|
|
|
2019-10-03 16:17:08 +00:00
|
|
|
mixedCaseAddressId, getErr := repository.GetOrCreateAddressInTransaction(tx, address)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("gets lower-cased addresses", func() {
|
|
|
|
lowerAddress := strings.ToLower(address)
|
2019-10-03 16:17:08 +00:00
|
|
|
upperAddressId, createErr := repository.GetOrCreateAddressInTransaction(tx, lowerAddress)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(createErr).NotTo(HaveOccurred())
|
|
|
|
|
2019-10-03 16:17:08 +00:00
|
|
|
mixedCaseAddressId, getErr := repository.GetOrCreateAddressInTransaction(tx, address)
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
|
|
|
tx.Commit()
|
2019-08-01 20:24:02 +00:00
|
|
|
|
2019-08-02 13:52:14 +00:00
|
|
|
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
|
|
|
})
|
2019-08-01 20:24:02 +00:00
|
|
|
})
|
2019-08-02 14:58:30 +00:00
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
Describe("GetAddressByID", func() {
|
2019-08-02 14:58:30 +00:00
|
|
|
It("gets and address by it's id", func() {
|
2019-10-03 16:17:08 +00:00
|
|
|
addressId, createErr := repository.GetOrCreateAddress(db, address)
|
2019-08-02 14:58:30 +00:00
|
|
|
Expect(createErr).NotTo(HaveOccurred())
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
actualAddress, getErr := repository.GetAddressByID(db, addressId)
|
2019-08-02 14:58:30 +00:00
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
|
|
|
Expect(actualAddress).To(Equal(address))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error if the id doesn't exist", func() {
|
2019-10-18 16:16:19 +00:00
|
|
|
_, getErr := repository.GetAddressByID(db, 0)
|
2019-08-02 14:58:30 +00:00
|
|
|
Expect(getErr).To(HaveOccurred())
|
|
|
|
Expect(getErr).To(MatchError("sql: no rows in result set"))
|
|
|
|
})
|
|
|
|
})
|
2019-08-02 13:52:14 +00:00
|
|
|
})
|