Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

James more useful mock session #59

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,19 @@ func (s *authedRequester) Do(r *Request) *Response {

// mockRequester answers API requests by returning some stock response.
type mockRequester struct {
message *json.RawMessage
messenger Messenger
}

type Messenger func(r *Request) []byte

// MockRequester creates a new mocked requester.
func MockRequester(message *json.RawMessage) Requester {
return &mockRequester{message: message}
func MockRequester(messenger Messenger) Requester {
return &mockRequester{messenger: messenger}
}

// Do pretends to fulfil an API request, but actually returns the mockRequester's stock response.
func (s *mockRequester) Do(r *Request) *Response {
return &Response{raw: s.message, err: nil}
rm := json.RawMessage{}
_ = rm.UnmarshalJSON(s.messenger(r))
return &Response{raw: &rm, err: nil}
}
19 changes: 10 additions & 9 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package myradio

import (
"bytes"
"encoding/json"
"net/url"

"github.com/UniversityRadioYork/myradio-go/api"
Expand Down Expand Up @@ -31,14 +30,17 @@ func NewSessionForServer(apikey, server string) (*Session, error) {
return &Session{requester: api.NewRequester(apikey, *url)}, nil
}

// MockSession creates a new mocked API session returning the JSON message stored in message.
func MockSession(message []byte) (*Session, error) {
rm := json.RawMessage{}
err := rm.UnmarshalJSON(message)
if err != nil {
return nil, err
// StaticMockSession creates a new mocked API session returning the JSON message stored in message.
func StaticMockSession(message []byte) *Session {
messenger := func(_ *api.Request) []byte {
return message
}
return &Session{requester: api.MockRequester(&rm)}, nil
return &Session{requester: api.MockRequester(messenger)}
}

// MockSession creates a new mocked API session returning a JSON message defined by the messenger.
func MockSession(messenger api.Messenger) *Session {
return &Session{requester: api.MockRequester(messenger)}
}

// do fulfils, a request for the given endpoint.
Expand Down Expand Up @@ -101,4 +103,3 @@ func NewSessionFromKeyFileForServer(server string) (*Session, error) {

return NewSessionForServer(apikey, server)
}

124 changes: 124 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package myradio_test

import (
"reflect"
"strings"
"testing"
"time"

myradio "github.com/UniversityRadioYork/myradio-go"
"github.com/UniversityRadioYork/myradio-go/api"
)

func TestMockSession(t *testing.T) {
session := myradio.MockSession(func(r *api.Request) []byte {
endpoint := r.Endpoint
if strings.HasPrefix(endpoint, "/show") {
return []byte(getSearchMetaJson)
}
if strings.HasPrefix(endpoint, "/team") {
return []byte(positionJSON)
}
return []byte("")
})

expectedShow := []myradio.ShowMeta{{
ShowID: 8675309,
Title: "Jenny I've Got Your Number",
CreditsString: "Tommy Tutone",
Credits: []myradio.Credit{
{
Type: 1,
MemberID: 666,
User: myradio.User{
MemberID: 666,
Fname: "Tommy",
Sname: "Tutone",
Email: "[email protected]",
Receiveemail: false,
Photo: "/media/image_meta/MyRadioImageMetadata/1.jpeg",
Bio: "generic bio",
},
},
},
Description: "Tommy Tutone's got your number, and he's gotta make you his.",
ShowTypeID: 1,
Season: myradio.Link{
Display: "season display",
Value: "https://myradio.example.com/seasons/512",
Title: "Seasons",
URL: "https://myradio.example.com/seasons/512",
},
EditLink: myradio.Link{
Display: "edit display",
Value: "https://myradio.example.com/edit/8675309",
Title: "Edit",
URL: "https://myradio.example.com/edit/8675309",
},
ApplyLink: myradio.Link{
Display: "apply display",
Value: "https://myradio.example.com/apply/8675309",
Title: "Apply",
URL: "https://myradio.example.com/apply/8675309",
},
MicroSiteLink: myradio.Link{
Display: "microsite display",
Value: "https://myradio.example.com/microsites/8675309",
Title: "Microsites",
URL: "https://myradio.example.com/microsites/8675309",
},
Photo: "https://myradio.example.com/photos/shows/8675309",
}}

expectedTeams := []myradio.Officer{
{
User: myradio.User{
MemberID: 10,
Fname: "John",
Sname: "Smith",
Email: "[email protected]",
Receiveemail: true,
Photo: "/media/image_meta/MyRadioImageMetadata/1.jpeg",
Bio: "generic bio",
},
From: time.Unix(1479081600, 0),
FromRaw: 1479081600,
MemberOfficerID: 1,
Position: myradio.OfficerPosition{
OfficerID: 2,
Name: "Station Manager",
Alias: "station.manager",
Team: myradio.Team{
TeamID: 1,
Name: "Station Management",
Alias: "management",
Ordering: 10,
Description: "",
Status: "c",
},
Ordering: 2,
Description: "",
Status: "c",
Type: "a",
},
},
}

showMeta, err := session.GetSearchMeta("tutone")
if err != nil {
t.Error(err)
}

heads, err := session.GetTeamHeadPositions(1, nil)
if err != nil {
t.Error(err)
}

if !reflect.DeepEqual(showMeta, expectedShow) {
t.Errorf("expected:\n%v\n\ngot:\n%v", expectedShow, showMeta)
}

if !reflect.DeepEqual(heads, expectedTeams) {
t.Errorf("expected:\n%v\n\ngot:\n%v", expectedTeams, heads)
}
}
5 changes: 1 addition & 4 deletions show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ func TestGetSearchMetaUnmarshal(t *testing.T) {
Photo: "https://myradio.example.com/photos/shows/8675309",
}}

session, err := myradio.MockSession([]byte(getSearchMetaJson))
if err != nil {
t.Error(err)
}
session := myradio.StaticMockSession([]byte(getSearchMetaJson))

showMeta, err := session.GetSearchMeta("tutone")
if err != nil {
Expand Down
10 changes: 2 additions & 8 deletions team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ func TestGetTeamHeadPositions(t *testing.T) {
},
}

session, err := myradio.MockSession([]byte(positionJSON))
if err != nil {
t.Error(err)
}
session := myradio.StaticMockSession([]byte(positionJSON))

heads, err := session.GetTeamHeadPositions(1, nil)
if err != nil {
Expand Down Expand Up @@ -131,10 +128,7 @@ func TestGetTeamAssistantHeadPositions(t *testing.T) {
},
}

session, err := myradio.MockSession([]byte(positionJSON))
if err != nil {
t.Error(err)
}
session := myradio.StaticMockSession([]byte(positionJSON))

heads, err := session.GetTeamAssistantHeadPositions(1, nil)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions timeslot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ func TestGetWeekScheduleZero(t *testing.T) {

zeroes := [][]byte{[]byte("[]"), []byte("{}")}
for _, zero := range zeroes {
session, err := myradio.MockSession(zero)
if err != nil {
t.Error(err)
}
session := myradio.StaticMockSession(zero)

schedule, err := session.GetWeekSchedule(0, 1)
if err != nil {
Expand Down