ipld-eth-server/libraries/shared/repository/address_repository_test.go

184 lines
6.1 KiB
Go
Raw Normal View History

2019-08-05 18:28:06 +00:00
// VulcanizeDB
// Copyright © 2019 Vulcanize
//
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-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-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
package repository_test
2019-08-01 20:24:02 +00:00
import (
2019-08-19 21:51:17 +00:00
"strings"
"github.com/jmoiron/sqlx"
2019-08-01 20:24:02 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2019-08-19 21:51:17 +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"
)
var _ = Describe("address lookup", func() {
2019-08-01 20:24:02 +00:00
var (
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 {
Id int64
Address string
HashedAddress string `db:"hashed_address"`
2019-08-01 20:24:02 +00:00
}
Describe("GetOrCreateAddress", func() {
It("creates an address record", func() {
addressId, createErr := repository.GetOrCreateAddress(db, address)
Expect(createErr).NotTo(HaveOccurred())
2019-08-01 20:24:02 +00:00
var actualAddress dbAddress
getErr := db.Get(&actualAddress, `SELECT id, address, hashed_address FROM public.addresses LIMIT 1`)
Expect(getErr).NotTo(HaveOccurred())
hashedAddress := utils.HexToKeccak256Hash(address).Hex()
expectedAddress := dbAddress{Id: addressId, Address: address, HashedAddress: hashedAddress}
Expect(actualAddress).To(Equal(expectedAddress))
})
2019-08-01 20:24:02 +00:00
It("returns the existing record id if the address already exists", func() {
createId, createErr := repository.GetOrCreateAddress(db, address)
Expect(createErr).NotTo(HaveOccurred())
2019-08-01 20:24:02 +00:00
getId, getErr := repository.GetOrCreateAddress(db, address)
Expect(getErr).NotTo(HaveOccurred())
2019-08-01 20:24:02 +00:00
var addressCount int
addressErr := db.Get(&addressCount, `SELECT count(*) FROM public.addresses`)
Expect(addressErr).NotTo(HaveOccurred())
Expect(addressCount).To(Equal(1))
2019-08-19 21:55:38 +00:00
Expect(createId).To(Equal(getId))
})
2019-08-01 20:24:02 +00:00
It("gets upper-cased addresses", func() {
upperAddress := strings.ToUpper(address)
upperAddressId, createErr := repository.GetOrCreateAddress(db, upperAddress)
Expect(createErr).NotTo(HaveOccurred())
2019-08-01 20:24:02 +00:00
mixedCaseAddressId, getErr := repository.GetOrCreateAddress(db, address)
Expect(getErr).NotTo(HaveOccurred())
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
})
It("gets lower-cased addresses", func() {
lowerAddress := strings.ToLower(address)
upperAddressId, createErr := repository.GetOrCreateAddress(db, lowerAddress)
Expect(createErr).NotTo(HaveOccurred())
mixedCaseAddressId, getErr := repository.GetOrCreateAddress(db, address)
Expect(getErr).NotTo(HaveOccurred())
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
})
2019-08-01 20:24:02 +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()
})
It("creates an address record", func() {
addressId, createErr := repository.GetOrCreateAddressInTransaction(tx, address)
Expect(createErr).NotTo(HaveOccurred())
2019-08-19 21:55:38 +00:00
commitErr := tx.Commit()
Expect(commitErr).NotTo(HaveOccurred())
var actualAddress dbAddress
getErr := db.Get(&actualAddress, `SELECT id, address, hashed_address FROM public.addresses LIMIT 1`)
Expect(getErr).NotTo(HaveOccurred())
hashedAddress := utils.HexToKeccak256Hash(address).Hex()
expectedAddress := dbAddress{Id: addressId, Address: address, HashedAddress: hashedAddress}
Expect(actualAddress).To(Equal(expectedAddress))
})
It("returns the existing record id if the address already exists", func() {
_, createErr := repository.GetOrCreateAddressInTransaction(tx, address)
Expect(createErr).NotTo(HaveOccurred())
_, getErr := repository.GetOrCreateAddressInTransaction(tx, address)
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)
upperAddressId, createErr := repository.GetOrCreateAddressInTransaction(tx, upperAddress)
Expect(createErr).NotTo(HaveOccurred())
mixedCaseAddressId, getErr := repository.GetOrCreateAddressInTransaction(tx, address)
Expect(getErr).NotTo(HaveOccurred())
tx.Commit()
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
})
It("gets lower-cased addresses", func() {
lowerAddress := strings.ToLower(address)
upperAddressId, createErr := repository.GetOrCreateAddressInTransaction(tx, lowerAddress)
Expect(createErr).NotTo(HaveOccurred())
mixedCaseAddressId, getErr := repository.GetOrCreateAddressInTransaction(tx, address)
Expect(getErr).NotTo(HaveOccurred())
tx.Commit()
2019-08-01 20:24:02 +00:00
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
})
2019-08-01 20:24:02 +00:00
})
2019-08-02 14:58:30 +00:00
Describe("GetAddressByID", func() {
2019-08-02 14:58:30 +00:00
It("gets and address by it's id", func() {
addressId, createErr := repository.GetOrCreateAddress(db, address)
2019-08-02 14:58:30 +00:00
Expect(createErr).NotTo(HaveOccurred())
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() {
_, 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"))
})
})
})