Skip to content

Commit

Permalink
Merge pull request #1 from Freespoke/bill/fre-5047
Browse files Browse the repository at this point in the history
[FRE-5047] use MaybeList for unmarshaling Candidates
  • Loading branch information
jdpedrie authored Apr 4, 2024
2 parents 5db052b + b049d64 commit 79f8715
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 46 deletions.
49 changes: 49 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,52 @@ func TestWithParams(t *testing.T) {
t.Fatal("unexpected value in first candidate")
}
}

func TestElectionID(t *testing.T) {
dummyKey := "apiKey"
b, err := os.ReadFile("testdata/candidates_get_by_electionid.json")
if err != nil {
t.Fatal(err)
}

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("key") == "" {
t.Fatal("missing api key")
return
}

if r.URL.Query().Get("o") != "JSON" {
t.Fatal("missing output set to json")
return
}

if r.URL.Query().Get("electionId") != "4823" {
t.Fatal("missing or incorrect electionId param")
}

_, err = w.Write(b)
if err != nil {
t.Fatal(err)
}
}))

c, err := votesmart.New(dummyKey, votesmart.WithBaseURL(svr.URL), votesmart.WithClient(svr.Client()))
if err != nil {
t.Fatal(err)
}

var a votesmarttypes.CandidatesGetByElection
v := url.Values{}
v.Add("electionId", "4823")
if err := c.Invoke(context.Background(), &v, &a); err != nil {
t.Fatal("err not nil", err)
}

if len(a.CandidateList.Candidate) == 0 {
t.Fatal("response was empty")
}

if a.CandidateList.Candidate[0].FirstName != "Dustin" {
t.Fatal("unexpected value in first candidate")
}
}
37 changes: 37 additions & 0 deletions testdata/candidates_get_by_electionid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"candidateList": {
"candidate": {
"candidateId": "48307",
"firstName": "Dustin",
"nickName": "Dusty",
"middleName": "",
"preferredName": "Dusty",
"lastName": "Johnson",
"suffix": "",
"title": "Representative",
"ballotName": "Dusty Johnson",
"electionParties": "Republican",
"electionStatus": "Running",
"electionStage": "Primary",
"electionDistrictId": "26608",
"electionDistrictName": "At-Large",
"electionOffice": "U.S. House",
"electionOfficeId": "5",
"electionStateId": "SD",
"electionOfficeTypeId": "C",
"electionYear": "2024",
"electionSpecial": "f",
"electionDate": "06\/04\/2024",
"officeParties": "Republican",
"officeStatus": "active",
"officeDistrictId": "26608",
"officeDistrictName": "At-Large",
"officeStateId": "SD",
"officeId": "5",
"officeName": "U.S. House",
"officeTypeId": "C",
"runningMateId": "",
"runningMateName": ""
}
}
}
16 changes: 8 additions & 8 deletions votesmarttypes/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Candidate struct {
// See http://api.votesmart.org/docs/Candidates.html for usage details.
type CandidatesGetByOfficeState struct {
CandidateList struct {
Candidate []Candidate `json:"candidate"`
Candidate MaybeList[Candidate] `json:"candidate"`
} `json:"candidateList"`
}

Expand All @@ -50,7 +50,7 @@ func (CandidatesGetByOfficeState) Method() string {
// See http://api.votesmart.org/docs/Candidates.html for usage details.
type CandidatesGetByOfficeTypeState struct {
CandidateList struct {
Candidate []Candidate `json:"candidate"`
Candidate MaybeList[Candidate] `json:"candidate"`
} `json:"candidateList"`
}

Expand All @@ -62,7 +62,7 @@ func (CandidatesGetByOfficeTypeState) Method() string {
// See http://api.votesmart.org/docs/Candidates.html for usage details.
type CandidatesGetByLastname struct {
CandidateList struct {
Candidate []Candidate `json:"candidate"`
Candidate MaybeList[Candidate] `json:"candidate"`
} `json:"candidateList"`
}

Expand All @@ -74,7 +74,7 @@ func (CandidatesGetByLastname) Method() string {
// See http://api.votesmart.org/docs/Candidates.html for usage details.
type CandidatesGetByLevenshtein struct {
CandidateList struct {
Candidate []Candidate `json:"candidate"`
Candidate MaybeList[Candidate] `json:"candidate"`
} `json:"candidateList"`
}

Expand All @@ -86,7 +86,7 @@ func (CandidatesGetByLevenshtein) Method() string {
// See http://api.votesmart.org/docs/Candidates.html for usage details.
type CandidatesGetByElection struct {
CandidateList struct {
Candidate []Candidate `json:"candidate"`
Candidate MaybeList[Candidate] `json:"candidate"`
} `json:"candidateList"`
}

Expand All @@ -98,7 +98,7 @@ func (CandidatesGetByElection) Method() string {
// See http://api.votesmart.org/docs/Candidates.html for usage details.
type CandidatesGetByDistrict struct {
CandidateList struct {
Candidate []Candidate `json:"candidate"`
Candidate MaybeList[Candidate] `json:"candidate"`
} `json:"candidateList"`
}

Expand All @@ -110,8 +110,8 @@ func (CandidatesGetByDistrict) Method() string {
// See http://api.votesmart.org/docs/Candidates.html for usage details.
type CandidatesGetByZip struct {
CandidateList struct {
ZipMessage string `json:"zipMessage"`
Candidate []Candidate `json:"candidate"`
ZipMessage string `json:"zipMessage"`
Candidate MaybeList[Candidate] `json:"candidate"`
} `json:"candidateList"`
}

Expand Down
2 changes: 1 addition & 1 deletion votesmarttypes/district.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type DistrictGetByZip struct {
ZipMessage string `json:"zipMessage"`
District []District `json:"district"`
ElectionDistricts struct {
District []District `json:"district"`
District MaybeList[District] `json:"district"`
} `json:"electionDistricts"`
} `json:"districtList"`
}
Expand Down
34 changes: 1 addition & 33 deletions votesmarttypes/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,7 @@ func (LocalGetCities) Method() string {
// See http://api.votesmart.org/docs/Local.html for usage details.
type LocalGetOfficials struct {
CandidateList struct {
Candidate []struct {
CandidateID string `json:"candidateId"`
FirstName string `json:"firstName"`
NickName string `json:"nickName"`
MiddleName string `json:"middleName"`
PreferredName string `json:"preferredName"`
LastName string `json:"lastName"`
Suffix string `json:"suffix"`
Title string `json:"title"`
BallotName string `json:"ballotName"`
ElectionParties string `json:"electionParties"`
ElectionStatus string `json:"electionStatus"`
ElectionStage string `json:"electionStage"`
ElectionDistrictID string `json:"electionDistrictId"`
ElectionDistrictName string `json:"electionDistrictName"`
ElectionOffice string `json:"electionOffice"`
ElectionOfficeID string `json:"electionOfficeId"`
ElectionStateID string `json:"electionStateId"`
ElectionOfficeTypeID string `json:"electionOfficeTypeId"`
ElectionYear string `json:"electionYear"`
ElectionSpecial string `json:"electionSpecial"`
ElectionDate string `json:"electionDate"`
OfficeParties string `json:"officeParties"`
OfficeStatus string `json:"officeStatus"`
OfficeDistrictID string `json:"officeDistrictId"`
OfficeDistrictName string `json:"officeDistrictName"`
OfficeStateID string `json:"officeStateId"`
OfficeID string `json:"officeId"`
OfficeName string `json:"officeName"`
OfficeTypeID string `json:"officeTypeId"`
RunningMateID string `json:"runningMateId"`
RunningMateName string `json:"runningMateName"`
} `json:"candidate"`
Candidate MaybeList[Candidate] `json:"candidate"`
} `json:"candidateList"`
}

Expand Down
8 changes: 4 additions & 4 deletions votesmarttypes/office.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (OfficeGetLevels) Method() string {
// See http://api.votesmart.org/docs/Office.html for usage details.
type OfficeGetOfficesByType struct {
Offices struct {
Office []Office `json:"office"`
Office MaybeList[Office] `json:"office"`
} `json:"offices"`
}

Expand All @@ -73,7 +73,7 @@ func (OfficeGetOfficesByType) Method() string {
// See http://api.votesmart.org/docs/Office.html for usage details.
type OfficeGetOfficesByLevel struct {
Offices struct {
Office []Office `json:"office"`
Office MaybeList[Office] `json:"office"`
} `json:"offices"`
}

Expand All @@ -85,7 +85,7 @@ func (OfficeGetOfficesByLevel) Method() string {
// See http://api.votesmart.org/docs/Office.html for usage details.
type OfficeGetOfficesByTypeLevel struct {
Offices struct {
Office []Office `json:"office"`
Office MaybeList[Office] `json:"office"`
} `json:"offices"`
}

Expand All @@ -97,7 +97,7 @@ func (OfficeGetOfficesByTypeLevel) Method() string {
// See http://api.votesmart.org/docs/Office.html for usage details.
type OfficeGetOfficesByBranchLevel struct {
Offices struct {
Office []Office `json:"office"`
Office MaybeList[Office] `json:"office"`
} `json:"offices"`
}

Expand Down

0 comments on commit 79f8715

Please # to comment.