-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlingotek_test.go
588 lines (465 loc) · 13.2 KB
/
lingotek_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package lingotek
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"testing"
"time"
)
// RewriteTransport is an http.RoundTripper that rewrites requests
// using the provided URL's Scheme and Host, and its Path as a prefix.
// The Opaque field is untouched.
// If Transport is nil, http.DefaultTransport is used
// http://stackoverflow.com/a/27894872/61980
type RewriteTransport struct {
Transport http.RoundTripper
URL *url.URL
}
func (t RewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// note that url.URL.ResolveReference doesn't work here
// since t.u is an absolute url
req.URL.Scheme = t.URL.Scheme
req.URL.Host = t.URL.Host
req.URL.Path = path.Join(t.URL.Path, req.URL.Path)
rt := t.Transport
if rt == nil {
rt = http.DefaultTransport
}
return rt.RoundTrip(req)
}
func createTestServer(requestChan chan *http.Request, getFileName func(*http.Request) string) (*httptest.Server, http.Client) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var testData io.Reader
var err error
testData, err = os.Open(getFileName(r))
if err != nil {
http.NotFound(w, r)
return
}
io.Copy(w, testData)
requestChan <- r
})
server := httptest.NewServer(handler)
testUrl, _ := url.Parse(server.URL)
client := http.Client{
Transport: RewriteTransport{
URL: testUrl,
},
}
return server, client
}
func TestResponseGetNext(t *testing.T) {
r := Response{}
selfLink := Link{
Rel: []string{"self"},
Href: "test?limit=10&offset=0",
}
nextLink := Link{
Rel: []string{"next"},
Href: "test?limit=10&offset=10",
}
r.Links = append(r.Links, selfLink)
r.Links = append(r.Links, nextLink)
path, params, err := r.GetNext()
if err != nil {
t.Errorf("Get error %s", err)
}
if path != "test" {
t.Errorf("Expected test, got %s", path)
}
if params.Get("limit") != "10" {
t.Errorf("Expected limit 10, got %s", params.Get("limit"))
}
if params.Get("offset") != "10" {
t.Errorf("Expected offset 10, got %s", params.Get("offset"))
}
}
func TestGetNextPage(t *testing.T) {
f := func(r *http.Request) string {
return "test_data/test_communitys.json"
}
rCh := make(chan *http.Request, 1)
server, client := createTestServer(rCh, f)
defer server.Close()
defer close(rCh)
api := NewApi("dummyToken", &client)
r := Response{}
selfLink := Link{
Rel: []string{"self"},
Href: "test?limit=10&offset=0",
}
nextLink := Link{
Rel: []string{"next"},
Href: "test?limit=10&offset=20",
}
r.Links = append(r.Links, selfLink)
r.Links = append(r.Links, nextLink)
api.getNextPage(&r)
req := <-rCh
offset := req.URL.Query().Get("offset")
if offset != "20" {
t.Errorf("Expected 20, got: %s", offset)
}
nextLink.Href = "test?limit=10&offset=30"
r.Links[1] = nextLink
api.getNextPage(&r)
req = <-rCh
offset = req.URL.Query().Get("offset")
if offset != "30" {
t.Errorf("Expected 30, got: %s", offset)
}
}
func TestGetCommunities(t *testing.T) {
f := func(r *http.Request) string {
return "test_data/test_communitys.json"
}
rCh := make(chan *http.Request, 1)
server, client := createTestServer(rCh, f)
defer server.Close()
defer close(rCh)
api := NewApi("dummyToken", &client)
resp, err := api.GetCommunitiesPage(0, 10)
if err != nil {
t.Error(err)
}
if len(resp) != 10 {
t.Errorf("Expected 10 communities, got %d\n", len(resp))
}
if resp[7].Property.Title != "Blah blah community" {
t.Errorf("Expected \"Blah blah community\", got %s", resp[8].Property.Title)
}
if resp[2].Actions[0].Href != "community/25e1d7ad-40be-45b8-83b0-90d62447b865" {
t.Errorf("Expected \"community/25e1d7ad-40be-45b8-83b0-90d62447b865\" got %s", resp[2].Actions[0].Href)
}
}
func TestListCommunities(t *testing.T) {
p := func(r *http.Request) (fileName string) {
offset := r.URL.Query().Get("offset")
if offset == "10" {
fileName = "test_data/test_communities_justone.json"
} else {
fileName = "test_data/test_communitys.json"
}
return
}
rCh := make(chan *http.Request, 3)
server, client := createTestServer(rCh, p)
defer server.Close()
defer close(rCh)
api := NewApi("dummyToken", &client)
doneChan := make(chan bool)
communityChan, _ := api.ListCommunities(doneChan)
cNum := 0
for _ = range communityChan {
cNum += 1
}
if cNum != 11 {
t.Errorf("Expected 11 communities, got %d", cNum)
}
communityChan, errs := api.ListCommunities(doneChan)
cNum = 0
for c := range communityChan {
cNum += 1
if cNum == 8 {
doneChan <- true
}
if cNum == 7 {
if c.Property.Title != "asdf" {
t.Errorf("Expected \"asdf\", got %s", c.Property.Title)
}
}
}
// We always range one more time, because we don't see the done message
// until the next value has already been written to the channel
if cNum != 8 {
t.Errorf("Expected 8 communities, got %d", cNum)
}
// If we exited early due to an error, we can check here
if err, ok := <-errs; ok && err != nil {
t.Errorf("Given error %s", err)
}
}
func TestGetProjects(t *testing.T) {
p := func(r *http.Request) (fileName string) {
return "test_data/test_projects.json"
}
rCh := make(chan *http.Request, 1)
server, client := createTestServer(rCh, p)
defer server.Close()
defer close(rCh)
api := NewApi("dummyToken", &client)
resp, err := api.GetProjects("dummyCommunity")
if err != nil {
t.Error(err)
}
if len(resp) != 10 {
t.Errorf("Expected 10 projects, got %d\n", len(resp))
}
if resp[0].Property.Title != "jobName" {
t.Errorf("Expected \"jobName\", got %s", resp[0].Property.Title)
}
if resp[1].Property.Id != "72106daf-69f9-4366-8ad8-2c52af9ca3ee" {
t.Errorf("Expected \"72106daf-69f9-4366-8ad8-2c52af9ca3ee\", got %s", resp[1].Property.Id)
}
creationDate := time.Unix(1433116800, 0)
if resp[2].Property.CreationDate.Time != creationDate {
t.Errorf("Expected \"%s\", got %s", creationDate, resp[2].Property.CreationDate)
}
}
func TestListProjects(t *testing.T) {
p := func(r *http.Request) (fileName string) {
offset := r.URL.Query().Get("offset")
if offset == "10" {
fileName = "test_data/test_projects_three.json"
} else {
fileName = "test_data/test_projects.json"
}
return
}
rCh := make(chan *http.Request, 3)
server, client := createTestServer(rCh, p)
defer server.Close()
defer close(rCh)
api := NewApi("dummyToken", &client)
community := Community{}
community.Property.Id = "dummycommunity"
doneChan := make(chan bool)
projectChan, _ := api.ListProjects(&community, doneChan)
cNum := 0
for _ = range projectChan {
cNum += 1
}
if cNum != 13 {
t.Errorf("Expected 13 projects, got %d", cNum)
}
projectChan, _ = api.ListProjects(&community, doneChan)
cNum = 0
for c := range projectChan {
cNum += 1
if cNum == 8 {
doneChan <- true
}
if cNum == 7 {
if c.Property.Title != "devzone.lingotek.com" {
t.Errorf("Expected \"devzone.lingotek.com\", got %s", c.Property.Title)
}
}
}
// We always range one more time, because we don't see the done message
// until the next value has already been written to the channel
if cNum != 8 {
t.Errorf("Expected 8 projects, got %d", cNum)
}
}
func TestListDocuments(t *testing.T) {
p := func(r *http.Request) (fileName string) {
offset := r.URL.Query().Get("offset")
if offset == "10" {
fileName = "test_data/test_documents_two.json"
} else {
fileName = "test_data/test_documents.json"
}
return
}
rCh := make(chan *http.Request, 3)
server, client := createTestServer(rCh, p)
defer server.Close()
defer close(rCh)
api := NewApi("dummyToken", &client)
doneChan := make(chan bool)
documentChan, _ := api.ListDocuments(doneChan)
cNum := 0
for _ = range documentChan {
cNum += 1
}
if cNum != 12 {
t.Errorf("Expected 12 documents, got %d", cNum)
}
documentChan, _ = api.ListDocuments(doneChan)
cNum = 0
for c := range documentChan {
cNum += 1
if cNum == 8 {
doneChan <- true
}
if cNum == 7 {
if c.Property.Title != "My New Document 1401064842" {
t.Errorf("Expected \"My New Document 1401064842\", got %s", c.Property.Title)
}
}
}
// We always range one more time, because we don't see the done message
// until the next value has already been written to the channel
if cNum != 8 {
t.Errorf("Expected 8 projects, got %d", cNum)
}
}
func TestUploadString(t *testing.T) {
p := func(r *http.Request) (fileName string) {
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded;charset=utf-8" {
t.Error("Expected Content-Type x-www-form-urlencoded, got %s", r.Header.Get("Content-Type"))
}
err := r.ParseForm()
if err != nil {
t.Error(err)
}
if r.Form.Get("locale_code") != "en-US" {
t.Errorf("Expected en-US, got %s", r.PostForm.Get("locale_code"))
}
if r.Form.Get("project_id") != "12345" {
t.Errorf("Expected 12345, got %s", r.PostForm.Get("project_id"))
}
return "test_data/status.json"
}
rCh := make(chan *http.Request, 1)
server, client := createTestServer(rCh, p)
defer server.Close()
defer close(rCh)
api := NewApi("dummyToken", &client)
prop := ProjectProperty{}
prop.Id = "12345"
project := Project{}
project.Property = prop
resp, err := api.UploadString("My API Test", "Let's go to the shoe store", "en-US", project)
if err != nil {
t.Fatal(err)
}
if resp.Property.Title != "Status of My Test" {
t.Fatal("Expected \"Status of My Test\", got %s", resp.Property.Title)
}
if resp.Property.Id != "59d28ae8-25bd-4f99-85fc-9fd4fbc2af87" {
t.Fatal("Expected \"59d28ae8-25bd-4f99-85fc-9fd4fbc2af87\", got %s", resp.Property.Id)
}
}
func TestAddTranslation(t *testing.T) {
p := func(r *http.Request) (fileName string) {
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded;charset=utf-8" {
t.Error("Expected Content-Type x-www-form-urlencoded, got %s", r.Header.Get("Content-Type"))
}
err := r.ParseForm()
if err != nil {
t.Error(err)
}
if r.PostForm.Get("locale_code") != "es-ES" {
t.Errorf("Expected es-ES, got %s", r.PostForm.Get("locale_code"))
}
return "test_data/document_translate_post.json"
}
rCh := make(chan *http.Request, 1)
server, client := createTestServer(rCh, p)
defer server.Close()
defer close(rCh)
api := NewApi("dummyToken", &client)
prop := DocumentProperty{}
document := Document{}
document.Property = prop
translation, err := api.AddTranslation(&document, "es-ES")
if err != IdRequired {
t.Error("Looking for IdRequired error")
}
prop.Id = "12345"
document.Property = prop
translation, err = api.AddTranslation(&document, "es-ES")
if err != nil {
t.Error(err)
}
if translation.Property.PercentComplete != 0 {
t.Errorf("Expected 0, got %d", translation.Property.PercentComplete)
}
}
func TestListTranslations(t *testing.T) {
p := func(r *http.Request) (fileName string) {
offset := r.URL.Query().Get("offset")
if offset == "10" {
fileName = "test_data/document_translate_get_empty.json"
} else {
fileName = "test_data/document_translate_get.json"
}
return
}
rCh := make(chan *http.Request, 3)
server, client := createTestServer(rCh, p)
defer server.Close()
defer close(rCh)
api := NewApi("dummyToken", &client)
doneChan := make(chan bool)
prop := DocumentProperty{}
prop.Id = "12345"
document := Document{}
document.Property = prop
translationChan, _ := api.ListTranslations(&document, doneChan)
cNum := 0
for _ = range translationChan {
cNum += 1
}
if cNum != 4 {
t.Errorf("Expected 4 projects, got %d", cNum)
}
}
func TestCheckStatus(t *testing.T) {
p := func(r *http.Request) (fileName string) {
return "test_data/document.json"
}
rCh := make(chan *http.Request, 1)
server, client := createTestServer(rCh, p)
defer server.Close()
defer close(rCh)
api := NewApi("dummyToken", &client)
prop := DocumentProperty{}
prop.Id = "12345"
document := Document{}
document.Property = prop
resp, err := api.CheckStatus(document)
if err != nil {
t.Error(err)
}
if resp.Property.Title != "My Test" {
t.Errorf("Expected \"My Test\", got %s", resp.Property.Title)
}
if resp.Property.Id != "59d28ae8-25bd-4f99-85fc-9fd4fbc2af87" {
t.Errorf("Expected \"59d28ae8-25bd-4f99-85fc-9fd4fbc2af87\", got %s", resp.Property.Id)
}
if resp.Status.Property.Title != "Status of My Test" {
t.Errorf("Expected \"Status of My Test\", got %s", resp.Status.Property.Title)
}
if resp.Status.Property.Progress != 0 {
t.Errorf("Expected 0, got %d", resp.Status.Property.Progress)
}
if resp.Status.Property.Count.Word.Total != 5 {
t.Errorf("Expected 5, got %d", resp.Status.Property.Count.Word.Total)
}
}
func TestGetTranslatedDocument(t *testing.T) {
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "test_data/big_document.bin")
})
// Setup our test server
ts := httptest.NewServer(testHandler)
defer ts.Close()
testUrl, _ := url.Parse(ts.URL)
client := &http.Client{
Transport: RewriteTransport{
URL: testUrl,
},
}
api := NewApi("dummyToken", client)
prop := DocumentProperty{}
prop.Id = "12345"
document := Document{}
document.Property = prop
var buf bytes.Buffer
n, err := api.GetTranslatedDocument(&document, "es-ES", &buf)
if err != nil {
t.Error(err)
}
if len(buf.Bytes()) != 102400 {
t.Errorf("Expected len 102400, got %d", len(buf.Bytes()))
}
if n != int64(len(buf.Bytes())) {
t.Errorf("Expected len(buf)(%d) to equal n(%d)", len(buf.Bytes()), n)
}
}