-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentity_test.go
144 lines (132 loc) · 3.78 KB
/
entity_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package t1
// Copyright 2016-2017 MediaMath
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"reflect"
"testing"
"time"
"github.com/MediaMath/go-t1/models"
)
func TestExecuteRequestError(t *testing.T) {
req, err := http.NewRequest("GET", "http://localhost:234567", nil)
c := &http.Client{Timeout: 1 * time.Second}
cl := NewClient(c, "", nil)
if err != nil {
t.Fatal(err)
}
meta, err := execute(req, cl, nil)
if err == nil {
t.Error("Expected err, got none")
}
if want := (Meta{}); !reflect.DeepEqual(meta, want) {
t.Errorf("request error meta: got %v, want %v", meta, want)
}
}
func TestList(t *testing.T) {
s := setupServer(200, entityPath+"advertisers", "testdata/fixtures/advertisers.json")
defer s.Close()
u, _ := url.Parse(s.URL)
cl := NewClient(nil, "", u)
var as []*models.Advertiser
_, err := cl.Advertisers.List(nil, &as)
if err != nil {
t.Errorf("EntityService list: expected no error, got %v", err)
}
if got, want := len(as), 100; got != want {
t.Errorf("EntitySservice list: got %v entities, want %v entities", got, want)
}
}
func TestGetSingleEntity(t *testing.T) {
s := setupServer(200, entityPath+"advertisers/1", "testdata/fixtures/advertiser.json")
defer s.Close()
u, _ := url.Parse(s.URL)
cl := NewClient(nil, "", u)
var a models.Advertiser
_, err := cl.Advertisers.Get(1, &a)
if err != nil {
t.Errorf("EntityService Get: expected no error, got %v", err)
}
if got, want := a.Status, true; got != want {
t.Errorf("Advertiser status: got %v, want %v", got, want)
}
if got, want := a.DMPEnabled, "disabled"; got != want {
t.Errorf("Advertiser dmp_enabled: got %v, want %v", got, want)
}
if got, want := a.AgencyID, 300; got != want {
t.Errorf("Advertiser agency_id: got %v, want %v", got, want)
}
cTime := time.Date(2016, 11, 7, 9, 7, 57, 0, time.UTC)
if got := time.Time(a.CreatedOn); !got.Equal(cTime) {
t.Errorf("Advertiser created_on: got %v, want %v", got, cTime)
}
}
// TODO more tests :/
func setupServer(statusCode int, path, filename string) *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer f.Close()
w.Header().Set("Content-Type", mediaTypeJSON)
w.WriteHeader(statusCode)
io.Copy(w, f)
})
return httptest.NewServer(mux)
}
func TestGetIDWithoutIDFieldFalses(t *testing.T) {
type a struct {
NotID int
}
b := &a{}
id, valid := getIDOfObject(b)
if valid {
t.Errorf("getID with invalid object: want %v, got %v", false, valid)
}
if want, got := 0, id; want != got {
t.Errorf("getID zero field: want %d, got %d", want, got)
}
}
func TestGetIDWithIDFieldTrue(t *testing.T) {
type a struct {
ID int
}
b := &a{}
id, valid := getIDOfObject(b)
if !valid {
t.Errorf("getID with valid object: want %v, got %v", true, valid)
}
if want, got := 0, id; want != got {
t.Errorf("getID zero field: want %d, got %d", want, got)
}
}
func TestGetIDWithIDFieldNonZero(t *testing.T) {
type a struct {
ID int
}
b := &a{ID: 1}
id, valid := getIDOfObject(b)
if !valid {
t.Errorf("getID with valid object: want %v, got %v", true, valid)
}
if want, got := 1, id; want != got {
t.Errorf("getID non-zero field: want %d, got %d", want, got)
}
}