Passed
Push — master ( 373907...3fcd0c )
by Stanislav
01:37
created

client_test.NewMockedServer   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
eloc 6
nop 1
1
package client_test
2
3
import (
4
	"testing"
5
	"github.com/viasite/planfix-toggl-server/app/client"
6
	"log"
7
	"os"
8
	"github.com/viasite/planfix-toggl-server/app/config"
9
	"github.com/popstas/planfix-go/planfix"
10
	"net/http/httptest"
11
	"net/http"
12
	"io/ioutil"
13
	"encoding/xml"
14
	"github.com/popstas/go-toggl"
15
	"reflect"
16
)
17
18
func assert(t *testing.T, data interface{}, expected interface{}) {
19
	if data != expected {
20
		t.Errorf("Expected %v, got, %v", expected, data)
21
	}
22
}
23
24
type planfixRequestStruct struct {
25
	XMLName xml.Name `xml:"request"`
26
	Method  string   `xml:"method,attr"`
27
	Account string   `xml:"account"`
28
	Sid     string   `xml:"sid"`
29
}
30
31
type MockedServer struct {
32
	*httptest.Server
33
	Requests  [][]byte
34
	Responses []string // fifo queue of answers
35
}
36
37
func NewMockedServer(responses []string) *MockedServer {
38
	s := &MockedServer{
39
		Requests:  [][]byte{},
40
		Responses: responses,
41
	}
42
43
	s.Server = httptest.NewServer(s)
44
	return s
45
}
46
47
func (s *MockedServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
48
	lastRequest, err := ioutil.ReadAll(req.Body)
49
	if err != nil {
50
		panic(err)
51
	}
52
53
	rs := planfixRequestStruct{}
54
	err = xml.Unmarshal(lastRequest, &rs)
55
	if err != nil {
56
		panic(err)
57
	}
58
	s.Requests = append(s.Requests, lastRequest)
59
	answer := s.Responses[0]
60
61
	s.Responses = s.Responses[1:]
62
	resp.Write([]byte(answer))
63
}
64
65
func newClient() client.TogglClient {
66
	cfg := config.Config{}
67
	ms := NewMockedServer([]string{""})
68
	api := planfix.New(ms.URL, "apiKey", "account", "user", "password")
69
	api.Sid = "123"
70
71
	return client.TogglClient{
72
		Session:    toggl.OpenSession(cfg.TogglAPIToken),
73
		Config:     &cfg,
74
		PlanfixAPI: api,
75
		Logger:     log.New(os.Stderr, "", log.LstdFlags),
76
	}
77
}
78
79
func getGroupedEntries() map[int][]client.TogglPlanfixEntry {
80
	return map[int][]client.TogglPlanfixEntry{
81
		1: {
82
			{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
83
				toggl.DetailedTimeEntry{Duration: 1},
84
				client.PlanfixEntryData{TaskID: 1, GroupCount: 1},
85
			},
86
			{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
87
				toggl.DetailedTimeEntry{Duration: 2},
88
				client.PlanfixEntryData{TaskID: 1, GroupCount: 1},
89
			},
90
		},
91
		2: {
92
			{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
93
				toggl.DetailedTimeEntry{Duration: 3},
94
				client.PlanfixEntryData{TaskID: 2, GroupCount: 1},
95
			},
96
			{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
97
				toggl.DetailedTimeEntry{Duration: 4},
98
				client.PlanfixEntryData{TaskID: 2, GroupCount: 1},
99
			},
100
			{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
101
				toggl.DetailedTimeEntry{Duration: 5},
102
				client.PlanfixEntryData{TaskID: 2, GroupCount: 1},
103
			},
104
		},
105
		3: {
106
			{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
107
				toggl.DetailedTimeEntry{Duration: 6},
108
				client.PlanfixEntryData{TaskID: 3, GroupCount: 1},
109
			},
110
		},
111
	}
112
}
113
114
func TestTogglClient_SumEntriesGroup(t *testing.T) {
115
	c := newClient()
116
	groupedEntries := getGroupedEntries()
117
	expected := []client.TogglPlanfixEntry{
118
		{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
119
			toggl.DetailedTimeEntry{Duration: 3},
120
			client.PlanfixEntryData{TaskID: 1, GroupCount: 2},
121
		},
122
		{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
123
			toggl.DetailedTimeEntry{Duration: 12},
124
			client.PlanfixEntryData{TaskID: 2, GroupCount: 3},
125
		},
126
		{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
127
			toggl.DetailedTimeEntry{Duration: 6},
128
			client.PlanfixEntryData{TaskID: 3, GroupCount: 1},
129
		},
130
	}
131
132
	summed := c.SumEntriesGroup(groupedEntries)
133
	equals := reflect.DeepEqual(summed, expected)
134
	assert(t, equals, true)
135
}
136
137
func TestTogglClient_GroupEntriesByTask(t *testing.T) {
138
	c := newClient()
139
	entries := []client.TogglPlanfixEntry{
140
		{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
141
			toggl.DetailedTimeEntry{Duration: 1},
142
			client.PlanfixEntryData{TaskID: 1, GroupCount: 1},
143
		},
144
		{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
145
			toggl.DetailedTimeEntry{Duration: 2},
146
			client.PlanfixEntryData{TaskID: 1, GroupCount: 1},
147
		},
148
		{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
149
			toggl.DetailedTimeEntry{Duration: 3},
150
			client.PlanfixEntryData{TaskID: 2, GroupCount: 1},
151
		},
152
		{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
153
			toggl.DetailedTimeEntry{Duration: 4},
154
			client.PlanfixEntryData{TaskID: 2, GroupCount: 1},
155
		},
156
		{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
157
			toggl.DetailedTimeEntry{Duration: 5},
158
			client.PlanfixEntryData{TaskID: 2, GroupCount: 1},
159
		},
160
		{
0 ignored issues
show
introduced by
github.com/viasite/planfix-toggl-server/app/client.TogglPlanfixEntry composite literal uses unkeyed fields
Loading history...
161
			toggl.DetailedTimeEntry{Duration: 6},
162
			client.PlanfixEntryData{TaskID: 3, GroupCount: 1},
163
		},
164
	}
165
	expected := getGroupedEntries()
166
167
	grouped := c.GroupEntriesByTask(entries)
168
	equals := reflect.DeepEqual(grouped, expected)
169
	assert(t, equals, true)
170
}
171