Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Address more PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Aug 23, 2019
1 parent 1b37863 commit edc0bdf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 5 additions & 5 deletions pkg/datastore/postgres/repositories/address_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import (

type AddressRepository struct{}

func (AddressRepository) GetOrCreateAddress(db *postgres.DB, address string) (int, error) {
func (AddressRepository) GetOrCreateAddress(db *postgres.DB, address string) (int64, error) {
stringAddressToCommonAddress := common.HexToAddress(address)
hexAddress := stringAddressToCommonAddress.Hex()

var addressId int
var addressId int64
getErr := db.Get(&addressId, `SELECT id FROM public.addresses WHERE address = $1`, hexAddress)
if getErr == sql.ErrNoRows {
insertErr := db.QueryRow(`INSERT INTO public.addresses (address) VALUES($1) RETURNING id`, hexAddress).Scan(&addressId)
Expand All @@ -41,11 +41,11 @@ func (AddressRepository) GetOrCreateAddress(db *postgres.DB, address string) (in
return addressId, getErr
}

func (AddressRepository) GetOrCreateAddressInTransaction(tx *sqlx.Tx, address string) (int, error) {
func (AddressRepository) GetOrCreateAddressInTransaction(tx *sqlx.Tx, address string) (int64, error) {
stringAddressToCommonAddress := common.HexToAddress(address)
hexAddress := stringAddressToCommonAddress.Hex()

var addressId int
var addressId int64
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)
Expand All @@ -55,7 +55,7 @@ func (AddressRepository) GetOrCreateAddressInTransaction(tx *sqlx.Tx, address st
return addressId, getErr
}

func (AddressRepository) GetAddressById(db *postgres.DB, id int) (string, error) {
func (AddressRepository) GetAddressById(db *postgres.DB, id int64) (string, error) {
var address string
getErr := db.Get(&address, `SELECT address FROM public.addresses WHERE id = $1`, id)
return address, getErr
Expand Down
18 changes: 14 additions & 4 deletions pkg/datastore/postgres/repositories/address_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ var _ = Describe("address lookup", func() {
repo = repositories.AddressRepository{}
})

AfterEach(func() {
test_config.CleanTestDB(db)
})

type dbAddress struct {
Id int
Id int64
Address string
}

Expand All @@ -59,16 +63,17 @@ var _ = Describe("address lookup", func() {
})

It("returns the existing record id if the address already exists", func() {
_, createErr := repo.GetOrCreateAddress(db, address)
createId, createErr := repo.GetOrCreateAddress(db, address)
Expect(createErr).NotTo(HaveOccurred())

_, getErr := repo.GetOrCreateAddress(db, address)
getId, getErr := repo.GetOrCreateAddress(db, address)
Expect(getErr).NotTo(HaveOccurred())

var addressCount int
addressErr := db.Get(&addressCount, `SELECT count(*) FROM public.addresses`)
Expect(addressErr).NotTo(HaveOccurred())
Expect(addressCount).To(Equal(1))
Expect(createId).To(Equal(getId))
})

It("gets upper-cased addresses", func() {
Expand Down Expand Up @@ -102,10 +107,15 @@ var _ = Describe("address lookup", func() {
Expect(txErr).NotTo(HaveOccurred())
})

AfterEach(func() {
tx.Rollback()
})

It("creates an address record", func() {
addressId, createErr := repo.GetOrCreateAddressInTransaction(tx, address)
Expect(createErr).NotTo(HaveOccurred())
tx.Commit()
commitErr := tx.Commit()
Expect(commitErr).NotTo(HaveOccurred())

var actualAddress dbAddress
getErr := db.Get(&actualAddress, `SELECT id, address FROM public.addresses LIMIT 1`)
Expand Down

0 comments on commit edc0bdf

Please sign in to comment.