Skip to content

Commit

Permalink
update unit tests for iac-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
XanSmarty committed Oct 20, 2023
1 parent efcd67e commit 95a30d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
6 changes: 3 additions & 3 deletions international-autocomplete-api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func deserializeResponse(response []byte, lookup *Lookup) error {
}

func buildRequest(lookup *Lookup) *http.Request {
var addressURL = ""
var addressID = ""
if len(lookup.AddressID) > 0 {
addressURL = "/" + lookup.AddressID
addressID = "/" + lookup.AddressID
}
request, _ := http.NewRequest("GET", suggestURL+addressURL, nil) // We control the method and the URL. This is safe.
request, _ := http.NewRequest("GET", suggestURL+addressID, nil) // We control the method and the URL. This is safe.
query := request.URL.Query()
lookup.populate(query)
request.URL.RawQuery = query.Encode()
Expand Down
57 changes: 35 additions & 22 deletions international-autocomplete-api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,20 @@ func (f *ClientFixture) TestAddressLookupSerializedAndSentWithContext__ResponseS
"street": "1",
"locality": "2",
"administrative_area": "3",
"super_administrative_area": "4",
"postal_code": "5",
"country_iso3": "6"
"postal_code": "4",
"country_iso3": "5"
},
{
"street": "7",
"locality": "8",
"administrative_area": "9",
"super_administrative_area": "10",
"postal_code": "11",
"country_iso3": "12"
"street": "6",
"locality": "7",
"administrative_area": "8",
"postal_code": "9",
"country_iso3": "10"
}
]
}`
f.input.Search = "42"
f.input.Country = "FRA"

ctx := context.WithValue(context.Background(), "key", "value")
err := f.client.SendLookupWithContext(ctx, f.input)
Expand All @@ -60,25 +59,23 @@ func (f *ClientFixture) TestAddressLookupSerializedAndSentWithContext__ResponseS
f.So(f.sender.request.Method, should.Equal, "GET")
f.So(f.sender.request.URL.Path, should.Equal, suggestURL)
f.So(f.sender.request.URL.Query().Get("search"), should.Equal, "42")
f.So(f.sender.request.URL.String(), should.Equal, suggestURL+"?distance=5&max_results=5&search=42")
f.So(f.sender.request.URL.String(), should.Equal, suggestURL+"?country=FRA&distance=5&max_results=5&search=42")
f.So(f.sender.request.Context(), should.Resemble, ctx)

f.So(f.input.Result, should.Resemble, &Result{Candidates: []*Candidate{
{
Street: "1",
Locality: "2",
AdministrativeArea: "3",
SuperAdministrativeArea: "4",
PostalCode: "5",
CountryIso3: "6",
Street: "1",
Locality: "2",
AdministrativeArea: "3",
PostalCode: "4",
CountryIso3: "5",
},
{
Street: "7",
Locality: "8",
AdministrativeArea: "9",
SuperAdministrativeArea: "10",
PostalCode: "11",
CountryIso3: "12",
Street: "6",
Locality: "7",
AdministrativeArea: "8",
PostalCode: "9",
CountryIso3: "10",
},
}})
}
Expand All @@ -102,6 +99,7 @@ func (f *ClientFixture) TestSenderErrorPreventsDeserialization() {
{"text": "3"}
]}` // would be deserialized if not for the err (above)
f.input.Search = "HI"
f.input.Country = "FRA"

err := f.client.SendLookup(f.input)

Expand All @@ -112,13 +110,28 @@ func (f *ClientFixture) TestSenderErrorPreventsDeserialization() {
func (f *ClientFixture) TestDeserializationErrorPreventsDeserialization() {
f.sender.response = `I can't haz JSON`
f.input.Search = "HI"
f.input.Country = "FRA"

err := f.client.SendLookup(f.input)

f.So(err, should.NotBeNil)
f.So(f.input.Result, should.BeNil)
}

func (f *ClientFixture) TestAddressIDAppendsToURL() {
f.input.Country = "FRA"
f.input.AddressID = "thisisid"

err := f.client.SendLookup(f.input)

f.So(err, should.NotBeNil)

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, suggestURL+"/"+f.input.AddressID)
f.So(f.sender.request.URL.String(), should.Equal, suggestURL+"/"+f.input.AddressID+"?country=FRA&distance=5&max_results=5")
}

//////////////////////////////////////////////////////////////////

type FakeSender struct {
Expand Down

0 comments on commit 95a30d3

Please sign in to comment.