github.com/wit-ai/wit-go/v2@v2.0.2/apps_test.go (about) 1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 3 package witai 4 5 import ( 6 "net/http" 7 "net/http/httptest" 8 "reflect" 9 "testing" 10 "time" 11 ) 12 13 func TestGetApps(t *testing.T) { 14 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 15 res.Write([]byte(`[ 16 { 17 "id": "9890809890", 18 "name": "My_Second_App", 19 "lang": "en", 20 "private": false, 21 "created_at": "2018-01-01T00:00:01Z" 22 }, 23 { 24 "id": "9890809891", 25 "name": "My_Third_App", 26 "lang": "en", 27 "private": false, 28 "created_at": "2018-01-02T00:00:01Z" 29 } 30 ]`)) 31 })) 32 defer func() { testServer.Close() }() 33 34 c := NewClient(unitTestToken) 35 c.APIBase = testServer.URL 36 apps, err := c.GetApps(2, 0) 37 38 wantApps := []App{ 39 { 40 ID: "9890809890", 41 Name: "My_Second_App", 42 Lang: "en", 43 Private: false, 44 CreatedAt: Time{time.Date(2018, 1, 1, 0, 0, 1, 0, time.UTC)}, 45 }, 46 { 47 ID: "9890809891", 48 Name: "My_Third_App", 49 Lang: "en", 50 Private: false, 51 CreatedAt: Time{time.Date(2018, 1, 2, 0, 0, 1, 0, time.UTC)}, 52 }, 53 } 54 55 if err != nil { 56 t.Fatalf("nil error expected, got %v", err) 57 } 58 if !reflect.DeepEqual(wantApps, apps) { 59 t.Fatalf("expected\n\tapps: %v\n\tgot: %v", wantApps, apps) 60 } 61 } 62 63 func TestGetApp(t *testing.T) { 64 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 65 res.Write([]byte(`{ 66 "id": "2802177596527671", 67 "name": "alarm-clock", 68 "lang": "en", 69 "private": false, 70 "created_at": "2018-07-29T18:15:34-0700", 71 "last_training_duration_secs": 42, 72 "will_train_at": "2018-07-29T18:17:34-0700", 73 "last_trained_at": "2018-07-29T18:18:34-0700", 74 "training_status": "done" 75 }`)) 76 })) 77 defer func() { testServer.Close() }() 78 79 c := NewClient(unitTestToken) 80 c.APIBase = testServer.URL 81 app, err := c.GetApp("2802177596527671") 82 if err != nil { 83 t.Fatalf("not expected err, got: %s", err.Error()) 84 } 85 86 if app.Name != "alarm-clock" { 87 t.Fatalf("expected alarm-clock, got: %v", app) 88 } 89 if app.TrainingStatus != Done { 90 t.Fatalf("expected Done, got: %v", app) 91 } 92 93 expectedNextTrainTime, _ := time.Parse(WitTimeFormat, "2018-07-29T18:17:34-0700") 94 if !app.WillTrainAt.Time.Equal(expectedNextTrainTime) { 95 t.Fatalf("expected %v got: %v", app.WillTrainAt, expectedNextTrainTime) 96 } 97 98 if app.LastTrainingDurationSecs != 42 { 99 t.Fatalf("Expected 42 got %v", app.LastTrainingDurationSecs) 100 } 101 102 expectedLastTrainTime, _ := time.Parse(WitTimeFormat, "2018-07-29T18:18:34-0700") 103 if !app.LastTrainedAt.Time.Equal(expectedLastTrainTime) { 104 t.Fatalf("expected %v got: %v", app.WillTrainAt, expectedLastTrainTime) 105 } 106 } 107 108 func TestCreateApp(t *testing.T) { 109 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 110 res.Write([]byte(`{"app_id": "ai-dee"}`)) 111 })) 112 defer func() { testServer.Close() }() 113 114 c := NewClient(unitTestToken) 115 c.APIBase = testServer.URL 116 app, err := c.CreateApp(App{ 117 Name: "app", 118 }) 119 if err != nil { 120 t.Fatalf("nil error expected, got %v", err) 121 } 122 if app.AppID != "ai-dee" { 123 t.Fatalf("id=ai-dee expected, got: %s", app.AppID) 124 } 125 } 126 127 func TestDeleteApp(t *testing.T) { 128 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 129 res.Write([]byte(`{"success": true}`)) 130 })) 131 defer func() { testServer.Close() }() 132 133 c := NewClient(unitTestToken) 134 c.APIBase = testServer.URL 135 if err := c.DeleteApp("appid"); err != nil { 136 t.Fatalf("expected nil error, got: %v", err) 137 } 138 } 139 140 func TestUpdateApp(t *testing.T) { 141 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 142 res.Write([]byte(`{"success": true}`)) 143 })) 144 defer func() { testServer.Close() }() 145 146 c := NewClient(unitTestToken) 147 c.APIBase = testServer.URL 148 149 err := c.UpdateApp("appid", App{ 150 Lang: "fr", 151 Timezone: "Europe/Paris", 152 }) 153 if err != nil { 154 t.Fatalf("err=nil expected, got: %v", err) 155 } 156 } 157 158 func TestGetAppTags(t *testing.T) { 159 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 160 w.Write([]byte(`[ 161 [ 162 { 163 "name": "v3", 164 "created_at": "2019-09-14T20:29:53-0700", 165 "updated_at": "2019-09-14T20:29:53-0700", 166 "desc": "third version" 167 }, 168 { 169 "name": "v2", 170 "created_at": "2019-08-08T11:05:35-0700", 171 "updated_at": "2019-08-08T11:09:17-0700", 172 "desc": "second version, moved to v3" 173 } 174 ], 175 [ 176 { 177 "name": "v1", 178 "created_at": "2019-08-08T11:02:52-0700", 179 "updated_at": "2019-09-14T15:45:22-0700", 180 "desc": "legacy first version" 181 } 182 ] 183 ]`)) 184 })) 185 defer testServer.Close() 186 187 c := NewClient(unitTestToken) 188 c.APIBase = testServer.URL 189 190 wantTags := [][]AppTag{ 191 { 192 {Name: "v3", Desc: "third version"}, 193 {Name: "v2", Desc: "second version, moved to v3"}, 194 }, 195 { 196 {Name: "v1", Desc: "legacy first version"}, 197 }, 198 } 199 200 tags, err := c.GetAppTags("appid") 201 if err != nil { 202 t.Fatalf("expected nil err, got: %v", err) 203 } 204 205 if len(tags) != len(wantTags) { 206 t.Fatalf("expected\n\ttags: %v\n\tgot: %v", wantTags, tags) 207 } 208 209 for i, group := range tags { 210 wantGroup := wantTags[i] 211 212 if len(group) != len(wantGroup) { 213 t.Fatalf("expected\n\ttags[%v]: %v\n\tgot: %v", i, wantTags, tags) 214 } 215 216 for j, tag := range group { 217 wantTag := wantGroup[j] 218 if tag.Name != wantTag.Name { 219 t.Fatalf("expected\n\ttags[%v][%v].Name: %v\n\tgot: %v", i, j, wantTag.Name, tag.Name) 220 } 221 if tag.Desc != wantTag.Desc { 222 t.Fatalf("expected\n\ttags[%v][%v].Desc: %v\n\tgot: %v", i, j, wantTag.Desc, tag.Desc) 223 } 224 } 225 } 226 } 227 228 func TestGetAppTag(t *testing.T) { 229 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 230 w.Write([]byte(`{ 231 "name": "v1", 232 "created_at": "2019-08-08T11:02:52-0700", 233 "updated_at": "2019-09-14T15:45:22-0700", 234 "desc": "first version" 235 }`)) 236 })) 237 defer testServer.Close() 238 239 c := NewClient(unitTestToken) 240 c.APIBase = testServer.URL 241 242 tag, err := c.GetAppTag("appid", "tagid") 243 if err != nil { 244 t.Fatalf("expected nil err, got: %v", err) 245 } 246 247 wantTag := &AppTag{ 248 Name: "v1", 249 Desc: "first version", 250 } 251 252 if tag.Name != wantTag.Name { 253 t.Fatalf("expected\n\ttag.Name: %v, got: %v", wantTag.Name, tag.Name) 254 } 255 if tag.Desc != wantTag.Desc { 256 t.Fatalf("expected\n\ttag.Desc: %v, got: %v", wantTag.Desc, tag.Desc) 257 } 258 } 259 260 func TestCreateAppTag(t *testing.T) { 261 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 262 w.Write([]byte(`{"tag": "v_1"}`)) 263 })) 264 defer testServer.Close() 265 266 c := NewClient(unitTestToken) 267 c.APIBase = testServer.URL 268 269 tag, err := c.CreateAppTag("appid", "v$1") 270 if err != nil { 271 t.Fatalf("expected nil err, got: %v", err) 272 } 273 274 wantTag := &AppTag{Name: "v_1"} 275 if tag.Name != wantTag.Name { 276 t.Fatalf("expected\n\ttag.Name: %v, got: %v", wantTag.Name, tag.Name) 277 } 278 } 279 280 func TestUpdateAppTag(t *testing.T) { 281 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 282 w.Write([]byte(`{ 283 "tag": "v1.0", 284 "desc": "new description" 285 }`)) 286 })) 287 defer testServer.Close() 288 289 c := NewClient(unitTestToken) 290 c.APIBase = testServer.URL 291 292 updated, err := c.UpdateAppTag("appid", "v1", AppTag{Name: "v1.0", Desc: "new description"}) 293 if err != nil { 294 t.Fatalf("expected nil err, got: %v", err) 295 } 296 297 wantUpdated := &AppTag{Name: "v1.0", Desc: "new description"} 298 299 if !reflect.DeepEqual(updated, wantUpdated) { 300 t.Fatalf("expected\n\tupdated: %v\n\tgot: %v", wantUpdated, updated) 301 } 302 } 303 304 func TestMoveAppTag(t *testing.T) { 305 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 306 w.Write([]byte(`{ 307 "tag": "v1.0", 308 "desc": "1.0 version, moved to 1.1 version", 309 "moved_to": "v1.1" 310 }`)) 311 })) 312 defer testServer.Close() 313 314 c := NewClient(unitTestToken) 315 c.APIBase = testServer.URL 316 317 moved, err := c.MoveAppTag("appid", "v1", "v1.1", &AppTag{Name: "v1.0", Desc: "1.0 version, moved to 1.1 version"}) 318 if err != nil { 319 t.Fatalf("expected nil err, got: %v", err) 320 } 321 322 wantMoved := &MovedAppTag{ 323 Tag: "v1.0", 324 Desc: "1.0 version, moved to 1.1 version", 325 MovedTo: "v1.1", 326 } 327 328 if !reflect.DeepEqual(moved, wantMoved) { 329 t.Fatalf("expected\n\tmoved: %v\n\tgot: %v", wantMoved, moved) 330 } 331 } 332 333 func TestDeleteAppTag(t *testing.T) { 334 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 335 w.Write([]byte(`{"success": true}`)) 336 })) 337 defer testServer.Close() 338 339 c := NewClient(unitTestToken) 340 c.APIBase = testServer.URL 341 342 err := c.DeleteAppTag("appid", "tagid") 343 if err != nil { 344 t.Fatalf("expected nil err, got: %v", err) 345 } 346 }