Add GetAddressById

This commit is contained in:
Elizabeth Engelman 2019-08-02 09:58:30 -05:00
parent 4b61c87b55
commit 258035833b
2 changed files with 27 additions and 0 deletions

View File

@ -50,3 +50,12 @@ func (repo AddressRepository) GetOrCreateAddressInTransaction(tx *sqlx.Tx, addre
return addressId, getErr
}
func (repo AddressRepository) GetAddressById(db *postgres.DB, id int) (string, error){
var address string
getErr := db.Get(&address, `SELECT address FROM public.addresses WHERE id = $1`, id)
if getErr != nil {
return "", getErr
}
return address, nil
}

View File

@ -146,4 +146,22 @@ var _ = Describe("address lookup", func() {
Expect(upperAddressId).To(Equal(mixedCaseAddressId))
})
})
Describe("GetAddressById", func() {
It("gets and address by it's id", func() {
addressId, createErr := repo.GetOrCreateAddress(db, address)
Expect(createErr).NotTo(HaveOccurred())
actualAddress, getErr := repo.GetAddressById(db, addressId)
Expect(getErr).NotTo(HaveOccurred())
Expect(actualAddress).To(Equal(address))
})
It("returns an error if the id doesn't exist", func() {
_, getErr := repo.GetAddressById(db, 0)
Expect(getErr).To(HaveOccurred())
Expect(getErr).To(MatchError("sql: no rows in result set"))
})
})
})