Skip to content

Commit

Permalink
Fix array params (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliehastings authored Apr 16, 2024
1 parent 6299775 commit 8c8415a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
17 changes: 17 additions & 0 deletions elation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package elation
import (
"net/http"
"strconv"
"strings"
)

func tokenRequest(w http.ResponseWriter, r *http.Request) bool {
Expand All @@ -17,6 +18,22 @@ func tokenRequest(w http.ResponseWriter, r *http.Request) bool {
return true
}

func commaStrToInt64(in string) []int64 {
var out []int64

for _, v := range strings.Split(in, ",") {
i, err := strconv.ParseInt(v, 10, 64)

if err != nil {
panic(err)
}

out = append(out, i)
}

return out
}

func sliceStrToInt64(in []string) []int64 {
var out []int64

Expand Down
4 changes: 2 additions & 2 deletions recurring_event_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (s *RecurringEventGroupService) Create(ctx context.Context, create *Recurri
type FindRecurringEventGroupsOptions struct {
*Pagination

Physician []int64 `url:"physician,omitempty"`
Practice []int64 `url:"practice,omitempty"`
Physician []int64 `url:"physician,omitempty,comma"`
Practice []int64 `url:"practice,omitempty,comma"`
Reason string `url:"reason,omitempty"`
StartDate string `url:"start_date,omitempty"`
EndDate string `url:"end_date,omitempty"`
Expand Down
76 changes: 74 additions & 2 deletions recurring_event_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,80 @@ func TestRecurringEventGroupService_Find(t *testing.T) {
limit := r.URL.Query().Get("limit")
offset := r.URL.Query().Get("offset")

assert.Equal(opts.Practice, sliceStrToInt64(practice))
assert.Equal(opts.Physician, sliceStrToInt64(physician))
assert.Equal(opts.Practice, commaStrToInt64(practice[0]))
assert.Equal(opts.Physician, commaStrToInt64(physician[0]))
assert.Equal(opts.Reason, reason)
assert.Equal(opts.TimeSlotType, TimeSlotType(timeSlotType))
assert.Equal(opts.StartDate, startDate)
assert.Equal(opts.EndDate, endDate)

assert.Equal(opts.Pagination.Limit, strToInt(limit))
assert.Equal(opts.Pagination.Offset, strToInt(offset))

b, err := json.Marshal(Response[[]*RecurringEventGroup]{
Results: []*RecurringEventGroup{
{
ID: 1,
},
{
ID: 2,
},
},
})
assert.NoError(err)

w.Header().Set("Content-Type", "application/json")
//nolint
w.Write(b)
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := RecurringEventGroupService{client}

found, res, err := svc.Find(context.Background(), opts)
assert.NotNil(found)
assert.NotNil(res)
assert.NoError(err)
}

func TestRecurringEventGroupService_Find_Multiple_Params(t *testing.T) {
assert := assert.New(t)

opts := &FindRecurringEventGroupsOptions{
Pagination: &Pagination{
Limit: 1,
Offset: 2,
},

Physician: []int64{1, 2, 3},
Practice: []int64{7, 8, 9},
Reason: "Some reason",
TimeSlotType: AppointmentTimeSlotTypeEvent,
StartDate: "2024-01-01",
EndDate: "2024-03-01",
}

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tokenRequest(w, r) {
return
}

assert.Equal(http.MethodGet, r.Method)
assert.Equal("/recurring_event_groups", r.URL.Path)

practice := r.URL.Query()["practice"]
physician := r.URL.Query()["physician"]
reason := r.URL.Query().Get("reason")
timeSlotType := r.URL.Query().Get("time_slot_type")
startDate := r.URL.Query().Get("start_date")
endDate := r.URL.Query().Get("end_date")

limit := r.URL.Query().Get("limit")
offset := r.URL.Query().Get("offset")

assert.Equal(opts.Practice, commaStrToInt64(practice[0]))
assert.Equal(opts.Physician, commaStrToInt64(physician[0]))
assert.Equal(opts.Reason, reason)
assert.Equal(opts.TimeSlotType, TimeSlotType(timeSlotType))
assert.Equal(opts.StartDate, startDate)
Expand Down

0 comments on commit 8c8415a

Please sign in to comment.