Skip to content

Commit

Permalink
Add tests and untangle the secondary response struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanLCox1 committed Jul 17, 2024
1 parent 63aef67 commit 8f9133e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 35 deletions.
9 changes: 7 additions & 2 deletions examples/us-enrichment-api/secondary-count/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
usenrich "github.com/smartystreets/smartystreets-go-sdk/us-enrichment-api"
"github.com/smartystreets/smartystreets-go-sdk/wireup"
Expand Down Expand Up @@ -39,7 +40,11 @@ func main() {
}

fmt.Printf("Results for input: (%s, %s)\n", smartyKey, "secondary")
for s, response := range results {
fmt.Printf("#%d: %+v\n", s, response)
for i, response := range results {
prettyPrinted, err := json.MarshalIndent(response, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Response %d: %s", i, prettyPrinted)
}
}
9 changes: 7 additions & 2 deletions examples/us-enrichment-api/secondary/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
usenrich "github.com/smartystreets/smartystreets-go-sdk/us-enrichment-api"
"github.com/smartystreets/smartystreets-go-sdk/wireup"
Expand Down Expand Up @@ -39,7 +40,11 @@ func main() {
}

fmt.Printf("Results for input: (%s, %s)\n", smartyKey, "secondary")
for s, response := range results {
fmt.Printf("#%d: %+v\n", s, response)
for i, response := range results {
prettyPrinted, err := json.MarshalIndent(response, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Response %d: %s", i, prettyPrinted)
}
}
50 changes: 50 additions & 0 deletions us-enrichment-api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package us_enrichment

import (
"context"
"encoding/json"
"errors"
"net/http"
"testing"
Expand Down Expand Up @@ -116,8 +117,57 @@ func (f *ClientFixture) TestUniversalLookupUnmarshallingWithNoEtag() {
f.So(lookup.Response, should.Equal, []byte(validPrincipalResponse))
}

func (f *ClientFixture) TestSecondaryLookup() {
smartyKey := "123"
f.sender.response = validSecondaryResponse
f.input = &secondaryLookup{Lookup: &Lookup{SmartyKey: smartyKey}}

ctx := context.WithValue(context.Background(), "key", "value")
err := f.client.sendLookupWithContext(ctx, f.input)

f.So(err, should.BeNil)
f.So(f.sender.request, should.NotBeNil)
f.So(f.sender.request.Method, should.Equal, "GET")
f.So(f.sender.request.URL.Path, should.Equal, "/lookup/"+smartyKey+"/"+f.input.getDataSet())
f.So(f.sender.request.Context(), should.Resemble, ctx)

response := f.input.(*secondaryLookup).Response
var secondaryResponse []*SecondaryResponse
err = json.Unmarshal([]byte(validSecondaryResponse), &secondaryResponse)
secondaryResponse[0].Etag = "ABCDEFG"
f.So(err, should.BeNil)
f.So(response, should.Resemble, secondaryResponse)
}

func (f *ClientFixture) FocusTestSecondaryCount() {
smartyKey := "123"
f.sender.response = validSecondaryCountResponse
f.input = &secondaryCountLookup{Lookup: &Lookup{SmartyKey: smartyKey}}

ctx := context.WithValue(context.Background(), "key", "value")
err := f.client.sendLookupWithContext(ctx, f.input)

f.So(err, should.BeNil)
f.So(f.sender.request, should.NotBeNil)
f.So(f.sender.request.Method, should.Equal, "GET")
f.So(f.sender.request.URL.Path, should.Equal, "/lookup/"+smartyKey+"/"+f.input.getDataSet()+"/"+f.input.getDataSubset())
f.So(f.sender.request.Context(), should.Resemble, ctx)

response := f.input.(*secondaryCountLookup).Response
secondaryCountResponse := []*SecondaryCountResponse{
{
SmartyKey: smartyKey,
Count: 3,
Etag: "ABCDEFG",
},
}
f.So(response, should.Resemble, secondaryCountResponse)
}

var validFinancialResponse = `[{"smarty_key":"123","data_set_name":"property","data_subset_name":"financial","attributes":{"assessed_improvement_percent":"Assessed_Improvement_Percent","veteran_tax_exemption":"Veteran_Tax_Exemption","widow_tax_exemption":"Widow_Tax_Exemption"}}]`
var validPrincipalResponse = `[{"smarty_key":"123","data_set_name":"property","data_subset_name":"principal","attributes":{"1st_floor_sqft":"1st_Floor_Sqft",lender_name_2":"Lender_Name_2","lender_seller_carry_back":"Lender_Seller_Carry_Back","year_built":"Year_Built","zoning":"Zoning"}}]`
var validSecondaryResponse = `[{"smarty_key":"123","root_address":{"secondary_count":10,"smarty_key":"123","primary_number":"3105","street_name":"National Park Service","street_suffix":"Rd","city_name":"Juneau","state_abbreviation":"AK","zipcode":"99801","plus4_code":"8437"},"aliases":[{"smarty_key":"1882749021","primary_number":"3105","street_name":"National Park","street_suffix":"Rd","city_name":"Juneau","state_abbreviation":"AK","zipcode":"99801","plus4_code":"8437"}],"secondaries":[{"smarty_key":"1785903890","secondary_designator":"Apt","secondary_number":"A5","plus4_code":"8437"},{"smarty_key":"696702050","secondary_designator":"Apt","secondary_number":"B1","plus4_code":"8441"}]}]`
var validSecondaryCountResponse = `[{"smarty_key":"123","count":3}]`

/**************************************************************************/

Expand Down
68 changes: 37 additions & 31 deletions us-enrichment-api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,40 +556,46 @@ type GeoReferenceAttributes struct {
} `json:"place"`
}

type RootAddress struct {
SecondaryCount uint64 `json:"secondary_count"`
SmartyKey string `json:"smarty_key"`
PrimaryNumber string `json:"primary_number"`
StreetPredirection string `json:"street_predirection"`
StreetName string `json:"street_name"`
StreetSuffix string `json:"street_suffix"`
StreetPostdirection string `json:"street_postdirection"`
CityName string `json:"city_name"`
StateAbbreviation string `json:"state_abbreviation"`
Zipcode string `json:"zipcode"`
Plus4Code string `json:"plus4_code"`
}

type Alias struct {
SmartyKey string `json:"smarty_key"`
PrimaryNumber string `json:"primary_number"`
StreetPredirection string `json:"street_predirection"`
StreetName string `json:"street_name"`
StreetSuffix string `json:"street_suffix"`
StreetPostdirection string `json:"street_postdirection"`
CityName string `json:"city_name"`
StateAbbreviation string `json:"state_abbreviation"`
Zipcode string `json:"zipcode"`
Plus4Code string `json:"plus4_code"`
}

type Secondary struct {
SmartyKey string `json:"smarty_key"`
SecondaryDesignator string `json:"secondary_designator"`
SecondaryNumber string `json:"secondary_number"`
Plus4Code string `json:"plus4_code"`
}

type SecondaryResponse struct {
SmartyKey string `json:"smarty_key"`
Etag string
RootAddress struct {
SecondaryCount uint64 `json:"secondary_count"`
SmartyKey string `json:"smarty_key"`
PrimaryNumber string `json:"primary_number"`
StreetPredirection string `json:"street_predirection"`
StreetName string `json:"street_name"`
StreetSuffix string `json:"street_suffix"`
StreetPostdirection string `json:"street_postdirection"`
CityName string `json:"city_name"`
StateAbbreviation string `json:"state_abbreviation"`
Zipcode string `json:"zipcode"`
Plus4Code string `json:"plus4_code"`
} `json:"root_address"`
Aliases []struct {
SmartyKey string `json:"smarty_key"`
PrimaryNumber string `json:"primary_number"`
StreetPredirection string `json:"street_predirection"`
StreetName string `json:"street_name"`
StreetSuffix string `json:"street_suffix"`
StreetPostdirection string `json:"street_postdirection"`
CityName string `json:"city_name"`
StateAbbreviation string `json:"state_abbreviation"`
Zipcode string `json:"zipcode"`
Plus4Code string `json:"plus4_code"`
} `json:"aliases"`
Secondaries []struct {
SmartyKey string `json:"smarty_key"`
SecondaryDesignator string `json:"secondary_designator"`
SecondaryNumber string `json:"secondary_number"`
Plus4Code string `json:"plus4_code"`
} `json:"secondaries"`
RootAddress RootAddress `json:"root_address"`
Aliases []Alias `json:"aliases"`
Secondaries []Secondary `json:"secondaries"`
}

type SecondaryCountResponse struct {
Expand Down

0 comments on commit 8f9133e

Please sign in to comment.