-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi_test.go
971 lines (925 loc) · 26.3 KB
/
api_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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
package dataset
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"sort"
"testing"
"time"
// Caltech Library packages
"github.com/caltechlibrary/dotpath"
)
var (
contentType = "application/json"
dName = "testout"
testCollectionsSrc = []byte(`{
"attachment_test.ds": {
"one": {
"one": 1,
"two": "too",
"three": true
}
},
"objects_test.ds": {
"Miller-A": {
"id": "Miller-A",
"given": "Arthor",
"family": "Miller",
"genre": ["plays", "film"]
},
"Lopez-T": {
"id": "Lopez-T",
"given": "Tom",
"family": "Lopez",
"genre": ["plays", "radio-theater"]
}
},
"frames_test.ds": {
"Miller-A": {
"id": "Miller-A",
"given": "Arthor",
"family": "Miller",
"character": false,
"vocations": ["playright", "writer", "author"]
},
"Lopez-T": {
"id": "Lopez-T",
"given": "Tom",
"family": "Lopez",
"character": false,
"vocations": ["playright", "producer", "director", "sound-engineer", "voice actor", "disc jockey"]
},
"Flanders-J": {
"id": "Flanders-J",
"given": "Jack",
"family": "Flanders",
"played-by": "Robert Lorick",
"character": true,
"description": "Metaphysical Detective"
},
"Freda-L": {
"id": "Freda-L",
"given": "Little",
"family": "Freda",
"played-by": "P.J. O'Rorke",
"character": true,
"description": "A wise Venusian"
},
"Sam-M": {
"id": "Sam-M",
"given": "Mojo",
"family": "Sam",
"played-by": "Dave Adams",
"character": true,
"description": "The wise You-do man"
}
}
}`)
)
// sameStrings compares one slice of strings to another by converting
// them to a JSON represention and compariing the representation.
func sameStrings(expected []string, got []string) bool {
src1, _ := JSONMarshalIndent(expected, "", " ")
src2, _ := JSONMarshalIndent(got, "", " ")
return bytes.Compare(src1, src2) == 0
}
// sameMapping compares one map to another by converting
// them to a JSON representation and comparing the representation.
func sameMapping(expected map[string]interface{}, got map[string]interface{}) bool {
src1, _ := JSONMarshalIndent(expected, "", " ")
src2, _ := JSONMarshalIndent(got, "", " ")
return bytes.Compare(src1, src2) == 0
}
// sameObjects compares one slice of objects to an another
// by converting them to JSON and comparing the representation.
func sameObjects(expected []map[string]interface{}, got []map[string]interface{}) bool {
if len(expected) != len(got) {
return false
}
for i := 0; i < len(expected); i++ {
if !sameMapping(expected[i], got[i]) {
return false
}
}
return true
}
func makePayload(src []byte) (io.Reader, error) {
return bytes.NewBuffer(src), nil
}
func makeObjectPayload(o map[string]interface{}) (io.Reader, error) {
src, err := JSONMarshalIndent(o, "", " ")
if err != nil {
return nil, err
}
return makePayload(src)
}
func makeRequest(u string, method string, payload io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, u, payload)
if err != nil {
return nil, err
}
req.Header.Set("content-type", contentType)
client := new(http.Client)
return client.Do(req)
}
func assertHTTPStatus(expected int, got int) error {
if got == http.StatusNotImplemented {
return fmt.Errorf("expected status %s, got %s <- %s",
http.StatusText(expected), http.StatusText(got), http.StatusText(http.StatusNotImplemented))
}
if expected != got {
return fmt.Errorf("expected http status %q, got %q", http.StatusText(expected), http.StatusText(got))
}
return nil
}
func sameObjectSrc(expected []byte, got []byte) bool {
expectedO := map[string]interface{}{}
gotO := map[string]interface{}{}
if err := json.Unmarshal(expected, &expectedO); err != nil {
return false
}
if err := json.Unmarshal(got, &gotO); err != nil {
return false
}
if len(expectedO) != len(gotO) {
return false
}
for k, v := range expectedO {
gotV, ok := gotO[k]
if !ok {
return false
}
//FIXME: assumes simple type, i.e. not object or array
eT := fmt.Sprintf("%T", v)
gT := fmt.Sprintf("%T", gotV)
if eT != gT {
return false
}
if v != gotV {
return false
}
}
return true
}
func clientTestVersion(t *testing.T, settings *Settings) {
fmt.Printf("starting client test verisons\n")
// Run through a set of the
u := fmt.Sprintf("http://%s/api/version", settings.Host)
res, err := http.Get(u)
if err != nil {
t.Errorf("failed to get version of api, %s", err)
t.FailNow()
}
defer res.Body.Close()
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("%s", err)
t.FailNow()
}
if len(body) == 0 {
t.Errorf("expected a response body, got %q", body)
t.FailNow()
}
}
func clientTestKeys(t *testing.T, settings *Settings) {
fmt.Printf("starting client test keys\n")
for _, cfg := range settings.Collections {
c, err := Open(cfg.CName)
if err != nil {
t.Errorf("Open(%q) failed, %s", cfg.CName, err)
t.FailNow()
}
expectedKeys, err := c.Keys()
if err != nil {
t.Errorf("c.Key() %s", err)
t.FailNow()
}
cName := path.Base(cfg.CName)
// Run through a set of the
u := fmt.Sprintf("http://%s/api/%s/keys", settings.Host, cName)
res, err := http.Get(u)
if err != nil {
t.Errorf("failed to get keys from api, %s", err)
t.FailNow()
}
defer res.Body.Close()
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("%s", err)
t.FailNow()
}
if len(body) == 0 {
t.Errorf("expected a response body, got %q", body)
t.FailNow()
}
keys := []string{}
err = json.Unmarshal(body, &keys)
if err != nil {
t.Errorf("failed to unmarshal\n%s\n%s", body, err)
t.FailNow()
}
if len(expectedKeys) != len(keys) {
t.Errorf("expected %d keys, got %d", len(expectedKeys), len(keys))
t.FailNow()
}
sort.Strings(expectedKeys)
sort.Strings(keys)
for i := 0; i < len(expectedKeys); i++ {
if expectedKeys[i] != keys[i] {
t.Errorf("expected key %q, got %q", expectedKeys[i], keys[i])
t.FailNow()
}
}
}
}
func clientTestObjects(t *testing.T, settings *Settings) {
fmt.Printf("starting client test add objects\n")
newRecords := map[string]map[string]interface{}{
"Flanders-J": {
"name": "Jack Flanders",
"played-by": "Robert Lorick",
"character": true,
"description": "Metaphysical Detective",
},
"Freda-L": {
"name": "Little Freda",
"played-by": "P.J. O'Rorke",
"character": true,
"description": "A wise Venusian",
},
"Sam-M": {
"name": "Mojo Sam",
"played-by": "Dave Adams",
"character": true,
"description": "The wise You-do man",
},
}
newPost := make(map[string][]byte)
for k, o := range newRecords {
src, _ := JSONMarshalIndent(o, "", " ")
newPost[k] = src
}
for _, cfg := range settings.Collections {
cName := path.Base(cfg.CName)
if cName == "objects_test.ds" {
//FIXME: only work with object_tests.ds
for k, v := range newPost {
// Add an Object
u := fmt.Sprintf("http://%s/api/%s/object/%s", settings.Host, cName, k)
body := bytes.NewBuffer(v)
res, err := makeRequest(u, http.MethodPost, body)
if err != nil {
t.Errorf("http.Post(%q, %q, %s) error %s", u, contentType, body, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusCreated, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
// Read Back Object
res, err = makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
src, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if !sameObjectSrc(v, src) {
t.Errorf("expected %s, got %s", v, src)
}
// We lost Robert Lorick and Dave Adams.
if k == "Flanders-J" || k == "Sam-M" {
o := map[string]interface{}{}
json.Unmarshal(v, &o)
o["active"] = false
src, _ := JSONMarshal(o)
body = bytes.NewBuffer(src)
res, err := makeRequest(u, http.MethodPut, body)
if err != nil {
t.Errorf("makeRequest(%q, %q, %s) %s", u, http.MethodPut, body, err)
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
} else if k == "Freda-L" {
// Little Freda went on to a different plain of existence
res, _ := makeRequest(u, http.MethodDelete, nil)
if err != nil {
t.Errorf("delete failed, %s", err)
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
}
}
}
}
}
func clientTestAttachments(t *testing.T, settings *Settings) {
cPath := path.Join(dName, "attachment_test.ds")
cName := path.Base(cPath)
/*
c, err := Open(cPath)
if err != nil {
t.Errorf("Failed to open test collection %q, %s", cPath, err)
}
defer c.Close()
*/
// Write out a file to attach, attach it and then test "attachments"
// route.
key := "123"
src := []byte(`one,two,three
1,3,2
4,7,6
10,100,50
131,313,113
`)
aName := path.Join(dName, "numbers.csv")
if _, err := os.Stat(aName); err == nil {
os.RemoveAll(aName)
}
if err := ioutil.WriteFile(aName, src, 0664); err != nil {
t.Errorf("failed to write %q, %s", aName, err)
t.FailNow()
}
// Retrieve the attachments (should be no attachments first time)
u := fmt.Sprintf("http://%s/api/%s/attachments/%s", settings.Host, cName, key)
res, err := makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
t.FailNow()
}
defer res.Body.Close()
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("%s", err)
t.FailNow()
}
if len(body) == 0 {
t.Errorf("expected a response body, got %q", body)
t.FailNow()
}
l := []string{}
if err := json.Unmarshal(body, &l); err != nil {
t.Errorf("expected a list of attachments %q, %s", len(l), err)
t.FailNow()
}
if len(l) > 0 {
t.Errorf("expected a no attachments, got %+v", l)
t.FailNow()
}
// Adding a new document
fName := "numbers.csv"
u = fmt.Sprintf("http://%s/api/%s/attachment/%s/%s", settings.Host, cName, key, fName)
payload, err := makePayload(src)
res, err = makeRequest(u, http.MethodPost, payload)
if err != nil {
t.Errorf("makeRequest(%q, %q, %q) -> %s", u, http.MethodPost, payload, err)
t.FailNow()
}
defer res.Body.Close()
if err := assertHTTPStatus(http.StatusCreated, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
body, err = io.ReadAll(res.Body)
if err != nil {
t.Errorf("%s", err)
t.FailNow()
}
if len(body) == 0 {
t.Errorf("expected a response body, got %q", body)
t.FailNow()
}
// Retrieve the attachments (should be one attachment second time)
u = fmt.Sprintf("http://%s/api/%s/attachments/%s", settings.Host, cName, key)
res, err = makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
t.FailNow()
}
defer res.Body.Close()
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
body, err = io.ReadAll(res.Body)
if err != nil {
t.Errorf("%s", err)
t.FailNow()
}
if len(body) == 0 {
t.Errorf("expected a response body, got %q", body)
t.FailNow()
}
l = []string{}
if err := json.Unmarshal(body, &l); err != nil {
t.Errorf("expected a list of attachments %q, %s", len(l), err)
t.FailNow()
}
if len(l) != 1 {
t.Errorf("expected a no attachments, got %+v", l)
t.FailNow()
}
if l[0] != path.Base(aName) {
t.Errorf("expected %q, got, %q", path.Base(aName), l[0])
t.FailNow()
}
// Get the file we added and make sure it looks OK
for _, filename := range l {
u = fmt.Sprintf("http://%s/api/%s/attachment/%s/%s", settings.Host, cName, key, filename)
res, err = makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
t.FailNow()
}
defer res.Body.Close()
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
body, err = io.ReadAll(res.Body)
if err != nil {
t.Errorf("%s", err)
t.FailNow()
}
if len(body) == 0 {
t.Errorf("expected a response body, got %q", body)
t.FailNow()
}
outName := path.Join(dName, "download-"+filename)
if err := ioutil.WriteFile(outName, body, 0664); err != nil {
t.Errorf("unable to write requested file %q, %s", filename, err)
}
}
for _, filename := range l {
u = fmt.Sprintf("http://%s/api/%s/attachment/%s/%s", settings.Host, cName, key, filename)
res, err = makeRequest(u, http.MethodDelete, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
t.FailNow()
}
defer res.Body.Close()
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
}
// Now make sure attachments were all removed.
u = fmt.Sprintf("http://%s/api/%s/attachments/%s", settings.Host, cName, key)
res, err = makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
t.FailNow()
}
defer res.Body.Close()
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
body, err = io.ReadAll(res.Body)
if err != nil {
t.Errorf("%s", err)
t.FailNow()
}
if len(body) == 0 {
t.Errorf("expected a response body, got %q", body)
t.FailNow()
}
l = []string{}
if err := json.Unmarshal(body, &l); err != nil {
t.Errorf("expected a list of attachments %q, %s", len(l), err)
t.FailNow()
}
if len(l) > 0 {
t.Errorf("expected a no attachments, got %+v", l)
t.FailNow()
}
}
func clientTestFrames(t *testing.T, settings *Settings) {
cPath := path.Join(dName, "frames_test.ds")
cName := path.Base(cPath)
c, err := Open(cPath)
if err != nil {
t.Errorf("Failed to open test collection %q, %s", cPath, err)
}
defer c.Close()
// Double check loaded test data
frmKeys := []string{"Sam-M", "Freda-L", "Flanders-J"}
attributes := []string{"family", "given"}
dotPaths := []string{".family", ".given"}
labels := []string{"Family Name", "Given Name"}
for _, key := range frmKeys {
m := map[string]interface{}{}
if err := c.Read(key, m); err != nil {
t.Errorf("could not open %q, %s", key, err)
t.FailNow()
}
for _, attr := range attributes {
if given, ok := m[attr]; !ok {
t.Errorf("failed to find .%s in %s, %+v", attr, key, m)
} else if given == "" {
t.Errorf("failed to find .%s is empty string %s, %+v", attr, key, m)
}
expr := fmt.Sprintf(".%s", attr)
val, err := dotpath.Eval(expr, m)
if err != nil {
t.Errorf("dotpath.Eval(%q, %+v) failed, %s", expr, m, err)
}
if val == nil {
t.Errorf("dotpath.Eval(%q, %+v) should not return nil value", expr, m)
t.FailNow()
}
}
}
exFrameName := "expected"
expectedFrm, err := c.FrameCreate(exFrameName, frmKeys, dotPaths, labels, true)
if err != nil {
t.Errorf("should be able to created the %q frame, %s", exFrameName, err)
}
if expectedFrm == nil {
t.Errorf("something went really wrong, should have an %q frame", exFrameName)
}
// Check to make sure frame does not exist
frameName := "test_names"
u := fmt.Sprintf("http://%s/api/%s/has-frame/%s", settings.Host, cName, frameName)
res, err := makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusNotFound, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
if _, err := ioutil.ReadAll(res.Body); err != nil {
t.Errorf("expected to read the body of reqest, %s", err)
t.FailNow()
}
res.Body.Close()
// Create the frame "test_names"
frameDef := map[string]interface{}{
"keys": frmKeys,
"dot_paths": dotPaths,
"labels": labels,
}
payload, err := makeObjectPayload(frameDef)
u = fmt.Sprintf("http://%s/api/%s/frame/%s", settings.Host, cName, frameName)
res, err = makeRequest(u, http.MethodPost, payload)
if err != nil {
t.Errorf("makeRequest(%q, %q, %s) -> %s", u, http.MethodPost, payload, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusCreated, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
// Attempt to read the frame that was just created.
frm, err := c.FrameRead(frameName)
if err != nil {
t.Errorf("frame failed to be created %q, %s", frameName, err)
t.FailNow()
}
if len(frm.ObjectMap) == 0 {
t.Errorf("something went wrong, frm.ObjectMap should have objects, %+v", frm)
t.FailNow()
}
hasError := false
for k, v := range frm.ObjectMap {
if v == nil || len(v.(map[string]interface{})) == 0 {
t.Errorf("something went wrong, frm.ObjectMap[%q] should have a populated map[string]interface{}, %+v", k, frm.ObjectMap)
hasError = true
}
}
if hasError {
t.FailNow()
}
// Check if we have the "names" frame.
u = fmt.Sprintf("http://%s/api/%s/has-frame/%s", settings.Host, cName, frameName)
res, err = makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
if _, err := ioutil.ReadAll(res.Body); err != nil {
t.Errorf("expected to read the body of request, %s", err)
}
res.Body.Close()
// List the frames in a collection
u = fmt.Sprintf("http://%s/api/%s/frames/", settings.Host, cName)
res, err = makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
src, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Errorf("expected to read the body of the request, %s", err)
}
res.Body.Close()
frameList := []string{}
if err := json.Unmarshal(src, &frameList); err != nil {
t.Errorf("Unmarshal(%s, %+v) -> %s", src, frameList, err)
}
if len(frameList) != 2 {
t.Errorf("expected two frames defined, got %d", len(frameList))
t.FailNow()
}
foundFrame := false
for _, name := range frameList {
if name == frameName {
foundFrame = true
break
}
}
if !foundFrame {
t.Errorf("expected frame %q, it was missing from %+v", frameName, frameList)
t.FailNow()
}
// Get the frame's def
expectedDef, err := c.FrameDef(frameName)
if err != nil {
t.Errorf("expected to find frame def for %q, got %s", frameName, err)
t.FailNow()
}
u = fmt.Sprintf("http://%s/api/%s/frame/%s", settings.Host, cName, frameName)
res, err = makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
src, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Errorf("expected to read the body of the request, %s", err)
}
res.Body.Close()
def := map[string]interface{}{}
if err := json.Unmarshal(src, &def); err != nil {
t.Errorf("json.Unmarshal(%s, def) failed, %s", src, err)
t.FailNow()
}
if !sameMapping(expectedDef, def) {
exSrc, _ := JSONMarshalIndent(expectedDef, "", " ")
t.Errorf("expected map %s, got %s", exSrc, src)
}
expectedKeys := c.FrameKeys(frameName)
u = fmt.Sprintf("http://%s/api/%s/frame-keys/%s", settings.Host, cName, frameName)
res, err = makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
src, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Errorf("expected to read the body of the request, %s", err)
}
res.Body.Close()
gotKeys := []string{}
if err := json.Unmarshal(src, &gotKeys); err != nil {
t.Errorf("json.Unmarshal(%s, gotKeys) failed, %s", src, err)
t.FailNow()
}
if !sameStrings(expectedKeys, gotKeys) {
exSrc, _ := JSONMarshalIndent(expectedKeys, "", " ")
t.Errorf("expected map %s, got %s", exSrc, src)
}
expectedObjects, err := c.FrameObjects(frameName)
if err != nil {
t.Errorf("Could get get %q objects, %s", frameName, err)
t.FailNow()
}
u = fmt.Sprintf("http://%s/api/%s/frame-objects/%s", settings.Host, cName, frameName)
res, err = makeRequest(u, http.MethodGet, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
src, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Errorf("expected to read the body of the request, %s", err)
}
res.Body.Close()
gotObjects := []map[string]interface{}{}
if err := json.Unmarshal(src, &gotObjects); err != nil {
t.Errorf("json.Unmarshal(%s, gotObjects) failed, %s", src, err)
t.FailNow()
}
if !sameObjects(expectedObjects, gotObjects) {
exSrc, _ := JSONMarshalIndent(expectedObjects, "", " ")
t.Errorf("expected map %s, got %s", exSrc, src)
}
expectedObjects, err = c.FrameObjects(frameName)
if err != nil {
t.Errorf("Could get get %q objects, %s", frameName, err)
t.FailNow()
}
// save old object list
oldObjects := gotObjects[:]
// Update a record so we can then try frame-refresh
key := "Flanders-J"
buf := bytes.NewBuffer([]byte(`{
"id": "Flanders-J",
"given": "Capt. Marcelle",
"family": "Du Champ",
"character": true,
"description": "Sky Pirate Captain"
}`))
u = fmt.Sprintf("http://%s/api/%s/object/%s", settings.Host, cName, key)
res, err = makeRequest(u, http.MethodPut, buf)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodPut, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
// Make a refresh call
u = fmt.Sprintf("http://%s/api/%s/frame/%s", settings.Host, cName, frameName)
res, err = makeRequest(u, http.MethodPut, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodPut, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
// Verify records changed.
gotObjects, err = c.FrameObjects(frameName)
if err != nil {
t.Errorf("Could get get %q objects, %s", frameName, err)
t.FailNow()
}
if sameObjects(oldObjects, gotObjects) {
t.Errorf("FrameRefresh failed to update objects")
t.FailNow()
}
//FIXME: need tests for reframe
newKeys, err := c.Keys()
if err != nil {
t.Errorf("failed to get keys from %q, %s", cName, err)
t.FailNow()
}
src, err = JSONMarshalIndent(newKeys, "", " ")
if err != nil {
t.Errorf("failed to marshal key list, %s", err)
t.FailNow()
}
u = fmt.Sprintf("http://%s/api/%s/frame/%s", settings.Host, cName, frameName)
buf = bytes.NewBuffer(src)
res, err = makeRequest(u, http.MethodPut, buf)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodPut, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
updatedKeys := c.FrameKeys(frameName)
if !sameStrings(newKeys, updatedKeys) {
t.Errorf("expected keys %+v, got %+v", newKeys, updatedKeys)
t.FailNow()
}
objects, err := c.FrameObjects(frameName)
if len(objects) != len(newKeys) {
t.Errorf("expected %d objects, got %d", len(newKeys), len(objects))
t.FailNow()
}
frameName = "expected"
u = fmt.Sprintf("http://%s/api/%s/frame/%s", settings.Host, cName, frameName)
res, err = makeRequest(u, http.MethodDelete, nil)
if err != nil {
t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodDelete, err)
t.FailNow()
}
if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
t.Error(err)
t.FailNow()
}
if c.HasFrame(frameName) {
t.Errorf("expected frame %q to be deleted, found it", frameName)
t.FailNow()
}
}
func TestRunAPI(t *testing.T) {
if _, err := os.Stat(dName); os.IsNotExist(err) {
os.MkdirAll(dName, 0775)
}
// Setup up a test collection
fName := path.Join(dName, "settings.json")
if _, err := os.Stat(fName); err == nil {
os.RemoveAll(fName)
}
settings := new(Settings)
settings.Host = "localhost:8585"
testCollections := map[string]interface{}{}
if err := json.Unmarshal(testCollectionsSrc, &testCollections); err != nil {
t.Errorf("Failed to unpack text data to populate collections, %s", err)
t.FailNow()
}
for name, testRecords := range testCollections {
records := map[string]map[string]interface{}{}
for k, v := range testRecords.(map[string]interface{}) {
records[k] = v.(map[string]interface{})
}
pName := path.Join(dName, name)
if _, err := os.Stat(pName); err == nil {
os.RemoveAll(pName)
}
dbName := path.Join(pName, "collection.db")
dsnURI := "sqlite://" + dbName
if err := SetupApiTestCollection(pName, dsnURI, records); err != nil {
t.Errorf("Failed to setup test collection %q, %s", pName, err)
t.FailNow()
}
// Setup a test configuration
cfg := new(Config)
cfg.CName = pName
cfg.DsnURI = dsnURI
cfg.Keys = true
cfg.Create = true
cfg.Read = true
cfg.Update = true
cfg.Delete = true
cfg.Attachments = true
cfg.Attach = true
cfg.Retrieve = true
cfg.Prune = true
cfg.FrameRead = true
cfg.FrameWrite = true
settings.Collections = append(settings.Collections, cfg)
}
if err := settings.WriteFile(fName, 0664); err != nil {
t.Errorf("failed to save config %q, %s", fName, err)
t.FailNow()
}
setupWait := "5s"
wait, _ := time.ParseDuration(setupWait)
fmt.Printf(`
Launching API Tests at http://%s
Press Ctr-C if tests hang, you should see requests log output
Client testings starts in %s (s = seconds)
`, settings.Host, setupWait)
appName := os.Args[0]
// Add some records to our test collection.
// Send our web service into the back ground so we can run
// a client test.
debug := false
go func() {
if err := RunAPI(appName, fName, debug); err != nil {
t.Errorf("Expected API to setup and run, %s", err)
}
}()
time.Sleep(wait)
clientTestVersion(t, settings)
clientTestKeys(t, settings)
clientTestObjects(t, settings)
clientTestKeys(t, settings)
clientTestFrames(t, settings)
clientTestAttachments(t, settings)
}