Pass a db into GetOrCreateAddress
This commit is contained in:
parent
b13eefbce7
commit
1373fe83a1
@ -20,18 +20,16 @@ import (
|
|||||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AddressRepository struct {
|
type AddressRepository struct {}
|
||||||
*postgres.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo AddressRepository) CreateOrGetAddress(address string) (int, error) {
|
func (repo AddressRepository) CreateOrGetAddress(db *postgres.DB, address string) (int, error) {
|
||||||
stringAddressToCommonAddress := common.HexToAddress(address)
|
stringAddressToCommonAddress := common.HexToAddress(address)
|
||||||
hexAddress := stringAddressToCommonAddress.Hex()
|
hexAddress := stringAddressToCommonAddress.Hex()
|
||||||
|
|
||||||
var addressId int
|
var addressId int
|
||||||
getErr := repo.DB.Get(&addressId, `SELECT id FROM public.addresses WHERE address = $1`, hexAddress)
|
getErr := db.Get(&addressId, `SELECT id FROM public.addresses WHERE address = $1`, hexAddress)
|
||||||
if getErr == sql.ErrNoRows {
|
if getErr == sql.ErrNoRows {
|
||||||
insertErr := repo.DB.QueryRow(`INSERT INTO public.addresses (address) VALUES($1) RETURNING id`, hexAddress).Scan(&addressId)
|
insertErr := db.QueryRow(`INSERT INTO public.addresses (address) VALUES($1) RETURNING id`, hexAddress).Scan(&addressId)
|
||||||
return addressId, insertErr
|
return addressId, insertErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ var _ = Describe("address repository", func() {
|
|||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
db = test_config.NewTestDB(test_config.NewTestNode())
|
db = test_config.NewTestDB(test_config.NewTestNode())
|
||||||
test_config.CleanTestDB(db)
|
test_config.CleanTestDB(db)
|
||||||
repo = repositories.AddressRepository{DB: db}
|
repo = repositories.AddressRepository{}
|
||||||
})
|
})
|
||||||
|
|
||||||
type dbAddress struct {
|
type dbAddress struct {
|
||||||
@ -43,7 +43,7 @@ var _ = Describe("address repository", func() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
It("creates an address record", func() {
|
It("creates an address record", func() {
|
||||||
addressId, createErr := repo.CreateOrGetAddress(address)
|
addressId, createErr := repo.CreateOrGetAddress(db, address)
|
||||||
Expect(createErr).NotTo(HaveOccurred())
|
Expect(createErr).NotTo(HaveOccurred())
|
||||||
|
|
||||||
var actualAddress dbAddress
|
var actualAddress dbAddress
|
||||||
@ -54,24 +54,24 @@ var _ = Describe("address repository", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("returns the existing record id if the address already exists", func() {
|
It("returns the existing record id if the address already exists", func() {
|
||||||
_, createErr := repo.CreateOrGetAddress(address)
|
_, createErr := repo.CreateOrGetAddress(db, address)
|
||||||
Expect(createErr).NotTo(HaveOccurred())
|
Expect(createErr).NotTo(HaveOccurred())
|
||||||
|
|
||||||
_, getErr := repo.CreateOrGetAddress(address)
|
_, getErr := repo.CreateOrGetAddress(db, address)
|
||||||
Expect(getErr).NotTo(HaveOccurred())
|
Expect(getErr).NotTo(HaveOccurred())
|
||||||
|
|
||||||
var addressCount int
|
var addressCount int
|
||||||
addressErr := repo.DB.Get(&addressCount, `SELECT count(*) FROM public.addresses`)
|
addressErr := db.Get(&addressCount, `SELECT count(*) FROM public.addresses`)
|
||||||
Expect(addressErr).NotTo(HaveOccurred())
|
Expect(addressErr).NotTo(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
It("gets upper-cased addresses", func() {
|
It("gets upper-cased addresses", func() {
|
||||||
//insert it as all upper
|
//insert it as all upper
|
||||||
upperAddress := strings.ToUpper(address)
|
upperAddress := strings.ToUpper(address)
|
||||||
upperAddressId, createErr := repo.CreateOrGetAddress(upperAddress)
|
upperAddressId, createErr := repo.CreateOrGetAddress(db, upperAddress)
|
||||||
Expect(createErr).NotTo(HaveOccurred())
|
Expect(createErr).NotTo(HaveOccurred())
|
||||||
|
|
||||||
mixedCaseAddressId, getErr := repo.CreateOrGetAddress(address)
|
mixedCaseAddressId, getErr := repo.CreateOrGetAddress(db, address)
|
||||||
Expect(getErr).NotTo(HaveOccurred())
|
Expect(getErr).NotTo(HaveOccurred())
|
||||||
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
||||||
})
|
})
|
||||||
@ -79,10 +79,10 @@ var _ = Describe("address repository", func() {
|
|||||||
It("gets lower-cased addresses", func() {
|
It("gets lower-cased addresses", func() {
|
||||||
//insert it as all upper
|
//insert it as all upper
|
||||||
lowerAddress := strings.ToLower(address)
|
lowerAddress := strings.ToLower(address)
|
||||||
upperAddressId, createErr := repo.CreateOrGetAddress(lowerAddress)
|
upperAddressId, createErr := repo.CreateOrGetAddress(db, lowerAddress)
|
||||||
Expect(createErr).NotTo(HaveOccurred())
|
Expect(createErr).NotTo(HaveOccurred())
|
||||||
|
|
||||||
mixedCaseAddressId, getErr := repo.CreateOrGetAddress(address)
|
mixedCaseAddressId, getErr := repo.CreateOrGetAddress(db, address)
|
||||||
Expect(getErr).NotTo(HaveOccurred())
|
Expect(getErr).NotTo(HaveOccurred())
|
||||||
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user