Skip to content

Commit

Permalink
Merge branch 'master' into Brooke-fix-end-of-time
Browse files Browse the repository at this point in the history
  • Loading branch information
Brookke committed May 4, 2017
2 parents e3af600 + 64a8dc6 commit 8239acc
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 37 deletions.
17 changes: 10 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
language: go
sudo: false

go:
- 1.6
- 1.7
- 1.8
- tip

notifications:
email: false
go:
- 1.6.x
- 1.7.x
- 1.8.x
- tip

before_install:
- go get github.com/mattn/goveralls
- if [ "$TRAVIS_GO_VERSION" = "1.8" ]; then go get github.com/mattn/goveralls; fi
script:
- $HOME/gopath/bin/goveralls -service=travis-ci
- if [ "$TRAVIS_GO_VERSION" = "1.8" ]; then $HOME/gopath/bin/goveralls -service=travis-ci; fi
66 changes: 36 additions & 30 deletions team.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package myradio

import (
"encoding/json"
"errors"
"fmt"
"time"
)

// Position represents a MyRadio officer position.
type Position struct {
Team Team `json:"team"`
OfficerID uint `json:"officerid"`
Name string `json:"name"`
Alias string `json:"alias"`
Ordering uint `json:"ordering"`
Description string `json:"description"`
Status string `json:"status"`
Type string `json:"type"`
}

// Officer represents information about an officership inside a Team.
type Officer struct {
User User `json:"user"`
From time.Time
FromRaw int64 `json:"from"`
MemberOfficerID uint `json:"memberofficerid"`
Position Position `json:"position"`
FromRaw int64 `json:"from"`
MemberOfficerID uint `json:"memberofficerid"`
Position OfficerPosition `json:"position"`
}

// Team represents a station committee team.
Expand All @@ -37,14 +27,6 @@ type Team struct {
Officers []Officer `json:"officers"`
}

// HeadPosition represents the head position of a team.
type HeadPosition struct {
User User
From int
MemberOfficerID int
Position OfficerPosition
}

// GetCurrentTeams retrieves all teams inside the station committee.
// This consumes one API request.
func (s *Session) GetCurrentTeams() (teams []Team, err error) {
Expand All @@ -66,22 +48,46 @@ func (s *Session) GetTeamWithOfficers(teamName string) (team Team, err error) {
return
}

// GetTeamHeadPositions retrieves all head-of-team positions for a given team ID.
// getTeamPositions retrieves the positions for a given team ID and position type.
// The amount of detail can be controlled using MyRadio mixins.
// The position parameterType is either assistant or head
// This consumes one API request.
func (s *Session) GetTeamHeadPositions(id int, mixins []string) (head []HeadPosition, err error) {
if err = s.apiRequestInto(&head, fmt.Sprintf("/team/%d/headpositions", id), mixins); err != nil {
func getTeamPositions(positionType string, id int, mixins []string, s *Session) (position []Officer, err error) {
if positionType != "assistanthead" && positionType != "head" {
return nil, errors.New("Invalid position type provided")
}
data, err := s.apiRequest(fmt.Sprintf("/team/%d/%spositions", id, positionType), mixins)
if err != nil {
return
}

for k, v := range head {
err = json.Unmarshal(*data, &position)
if err != nil {
return
}
for k, v := range position {
position[k].From = time.Unix(v.FromRaw, 0)
if v.Position.History != nil {
for ik, iv := range v.Position.History {
head[k].Position.History[ik].From = time.Unix(iv.FromRaw, 0)
head[k].Position.History[ik].To = time.Unix(iv.ToRaw, 0)
position[k].Position.History[ik].From = time.Unix(iv.FromRaw, 0)
position[k].Position.History[ik].To = time.Unix(iv.ToRaw, 0)
}
}
}

return
}

// GetTeamHeadPositions retrieves all head-of-team positions for a given team ID.
// The amount of detail can be controlled using MyRadio mixins.
// This consumes one API request.
func (s *Session) GetTeamHeadPositions(id int, mixins []string) (head []Officer, err error) {
return getTeamPositions("head", id, mixins, s)
}

// GetTeamAssistantHeadPositions retrieves all assistant-head-of-team positions for a given team ID.
// The amount of detail can be controlled using MyRadio mixins.
// This consumes one API request.
func (s *Session) GetTeamAssistantHeadPositions(id int, mixins []string) (assHead []Officer, err error) {
return getTeamPositions("assistanthead", id, mixins, s)

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

import (
"reflect"
"testing"

"time"

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

const positionJSON = `
[{
"User": {
"memberid": 10,
"fname": "John",
"sname": "Smith",
"sex": "m",
"public_email": "john.smith@example.org.uk",
"url": "//example.org.uk/myradio/Profile/view/?memberid=10",
"receive_email": true,
"photo": "/media/image_meta/MyRadioImageMetadata/1.jpeg",
"bio": "generic bio"
},
"from": 1479081600,
"memberofficerid": 1,
"position": {
"officerid": 2,
"name": "Station Manager",
"alias": "station.manager",
"team": {
"teamid": 1,
"name": "Station Management",
"alias": "management",
"ordering": 10,
"description": "",
"status": "c"
},
"ordering": 2,
"description": "",
"status": "c",
"type": "a"
}
}]`

// TestGetTeamHeadPositions tests the getter for head positions of a team.
// It does not test the API endpoint.
func TestGetTeamHeadPositions(t *testing.T) {
expected := []myradio.Officer{
{
User: myradio.User{
Memberid: 10,
Fname: "John",
Sname: "Smith",
Sex: "m",
Email: "john.smith@example.org.uk",
Receiveemail: true,
},
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",
},
},
}

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

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

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

// TestGetTeamAssistantHeadPositions tests the getter for assistant head positions of a team.
// It does not test the API endpoint.
func TestGetTeamAssistantHeadPositions(t *testing.T) {
expected := []myradio.Officer{
{
User: myradio.User{
Memberid: 10,
Fname: "John",
Sname: "Smith",
Sex: "m",
Email: "john.smith@example.org.uk",
Receiveemail: true,
},
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",
},
},
}

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

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

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

0 comments on commit 8239acc

Please sign in to comment.