forked from cerc-io/ipld-eth-server
Get or create address record in a transaction
This commit is contained in:
parent
1373fe83a1
commit
4b61c87b55
@ -17,12 +17,13 @@ package repositories
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
type AddressRepository struct{}
|
||||
|
||||
func (repo AddressRepository) CreateOrGetAddress(db *postgres.DB, address string) (int, error) {
|
||||
func (repo AddressRepository) GetOrCreateAddress(db *postgres.DB, address string) (int, error) {
|
||||
stringAddressToCommonAddress := common.HexToAddress(address)
|
||||
hexAddress := stringAddressToCommonAddress.Hex()
|
||||
|
||||
@ -36,3 +37,16 @@ func (repo AddressRepository) CreateOrGetAddress(db *postgres.DB, address string
|
||||
return addressId, getErr
|
||||
}
|
||||
|
||||
func (repo AddressRepository) GetOrCreateAddressInTransaction(tx *sqlx.Tx, address string) (int, error) {
|
||||
stringAddressToCommonAddress := common.HexToAddress(address)
|
||||
hexAddress := stringAddressToCommonAddress.Hex()
|
||||
|
||||
var addressId int
|
||||
getErr := tx.Get(&addressId, `SELECT id FROM public.addresses WHERE address = $1`, hexAddress)
|
||||
if getErr == sql.ErrNoRows {
|
||||
insertErr := tx.QueryRow(`INSERT INTO public.addresses (address) VALUES($1) RETURNING id`, hexAddress).Scan(&addressId)
|
||||
return addressId, insertErr
|
||||
}
|
||||
|
||||
return addressId, getErr
|
||||
}
|
||||
|
@ -15,17 +15,17 @@
|
||||
package repositories_test
|
||||
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
||||
"github.com/vulcanize/vulcanizedb/test_config"
|
||||
"strings"
|
||||
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("address repository", func() {
|
||||
var _ = Describe("address lookup", func() {
|
||||
var (
|
||||
db *postgres.DB
|
||||
repo repositories.AddressRepository
|
||||
@ -42,8 +42,9 @@ var _ = Describe("address repository", func() {
|
||||
Address string
|
||||
}
|
||||
|
||||
Describe("GetOrCreateAddress", func() {
|
||||
It("creates an address record", func() {
|
||||
addressId, createErr := repo.CreateOrGetAddress(db, address)
|
||||
addressId, createErr := repo.GetOrCreateAddress(db, address)
|
||||
Expect(createErr).NotTo(HaveOccurred())
|
||||
|
||||
var actualAddress dbAddress
|
||||
@ -54,10 +55,10 @@ var _ = Describe("address repository", func() {
|
||||
})
|
||||
|
||||
It("returns the existing record id if the address already exists", func() {
|
||||
_, createErr := repo.CreateOrGetAddress(db, address)
|
||||
_, createErr := repo.GetOrCreateAddress(db, address)
|
||||
Expect(createErr).NotTo(HaveOccurred())
|
||||
|
||||
_, getErr := repo.CreateOrGetAddress(db, address)
|
||||
_, getErr := repo.GetOrCreateAddress(db, address)
|
||||
Expect(getErr).NotTo(HaveOccurred())
|
||||
|
||||
var addressCount int
|
||||
@ -66,24 +67,83 @@ var _ = Describe("address repository", func() {
|
||||
})
|
||||
|
||||
It("gets upper-cased addresses", func() {
|
||||
//insert it as all upper
|
||||
upperAddress := strings.ToUpper(address)
|
||||
upperAddressId, createErr := repo.CreateOrGetAddress(db, upperAddress)
|
||||
upperAddressId, createErr := repo.GetOrCreateAddress(db, upperAddress)
|
||||
Expect(createErr).NotTo(HaveOccurred())
|
||||
|
||||
mixedCaseAddressId, getErr := repo.CreateOrGetAddress(db, address)
|
||||
mixedCaseAddressId, getErr := repo.GetOrCreateAddress(db, address)
|
||||
Expect(getErr).NotTo(HaveOccurred())
|
||||
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
||||
})
|
||||
|
||||
It("gets lower-cased addresses", func() {
|
||||
//insert it as all upper
|
||||
lowerAddress := strings.ToLower(address)
|
||||
upperAddressId, createErr := repo.CreateOrGetAddress(db, lowerAddress)
|
||||
upperAddressId, createErr := repo.GetOrCreateAddress(db, lowerAddress)
|
||||
Expect(createErr).NotTo(HaveOccurred())
|
||||
|
||||
mixedCaseAddressId, getErr := repo.CreateOrGetAddress(db, address)
|
||||
mixedCaseAddressId, getErr := repo.GetOrCreateAddress(db, address)
|
||||
Expect(getErr).NotTo(HaveOccurred())
|
||||
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetOrCreateAddressInTransaction", func() {
|
||||
var (
|
||||
tx *sqlx.Tx
|
||||
txErr error
|
||||
)
|
||||
BeforeEach(func() {
|
||||
tx, txErr = db.Beginx()
|
||||
Expect(txErr).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("creates an address record", func() {
|
||||
addressId, createErr := repo.GetOrCreateAddressInTransaction(tx, address)
|
||||
Expect(createErr).NotTo(HaveOccurred())
|
||||
tx.Commit()
|
||||
|
||||
var actualAddress dbAddress
|
||||
getErr := db.Get(&actualAddress, `SELECT id, address FROM public.addresses LIMIT 1`)
|
||||
Expect(getErr).NotTo(HaveOccurred())
|
||||
expectedAddress := dbAddress{Id: addressId, Address: address}
|
||||
Expect(actualAddress).To(Equal(expectedAddress))
|
||||
})
|
||||
|
||||
It("returns the existing record id if the address already exists", func() {
|
||||
_, createErr := repo.GetOrCreateAddressInTransaction(tx, address)
|
||||
Expect(createErr).NotTo(HaveOccurred())
|
||||
|
||||
_, getErr := repo.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 := repo.GetOrCreateAddressInTransaction(tx, upperAddress)
|
||||
Expect(createErr).NotTo(HaveOccurred())
|
||||
|
||||
mixedCaseAddressId, getErr := repo.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 := repo.GetOrCreateAddressInTransaction(tx, lowerAddress)
|
||||
Expect(createErr).NotTo(HaveOccurred())
|
||||
|
||||
mixedCaseAddressId, getErr := repo.GetOrCreateAddressInTransaction(tx, address)
|
||||
Expect(getErr).NotTo(HaveOccurred())
|
||||
tx.Commit()
|
||||
|
||||
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user