1 | (load "../estraier-client.scm") |
---|
2 | |
---|
3 | (use test posix http-client) |
---|
4 | |
---|
5 | (import estraier-client) |
---|
6 | |
---|
7 | ;; This will start the server, we hope |
---|
8 | (system "rm -rf masterdir") ; Just in case |
---|
9 | (system "estmaster init masterdir") |
---|
10 | (printf "Waiting for estmaster to startup...") |
---|
11 | (system "estmaster start -bg masterdir > /dev/null 2>&1") |
---|
12 | (sleep 2) |
---|
13 | |
---|
14 | (define master-uri "http://admin:admin@localhost:1978/master") |
---|
15 | (define node-uri "http://admin:admin@localhost:1978/node/testnode") |
---|
16 | |
---|
17 | (test-group "node master API" |
---|
18 | (test "Empty node list on init" '() (master-nodes master-uri)) |
---|
19 | (test-error "Cannot connect with invalid credentials" |
---|
20 | (master-nodes "http://admin:invalid@localhost:1978/master")) |
---|
21 | (let ((nodes (begin (master-add-node master-uri "testnode") |
---|
22 | (master-add-node master-uri "testnode2" "testlabel") |
---|
23 | (master-nodes master-uri)))) |
---|
24 | (test "After adding two nodes, they show up" |
---|
25 | '("testnode" "testnode2") |
---|
26 | (map car nodes)) |
---|
27 | (test "Node label is accepted" |
---|
28 | '("testnode" "testlabel") |
---|
29 | (map cadr nodes))) |
---|
30 | (test "After deleting a node, it is gone" |
---|
31 | '("testnode") |
---|
32 | (begin (master-delete-node master-uri "testnode2") |
---|
33 | (map car (master-nodes master-uri)))) |
---|
34 | (let ((users (begin (master-add-user master-uri "testuser" "password" |
---|
35 | fullname: "Joe testuser" |
---|
36 | description: "This is just a test") |
---|
37 | (master-users master-uri)))) |
---|
38 | (test "After adding a user, it shows up" |
---|
39 | '("admin" "testuser") |
---|
40 | (map car users))) |
---|
41 | (test "After deleting a user, it is gone" |
---|
42 | '("admin") |
---|
43 | (begin (master-delete-user master-uri "testuser") |
---|
44 | (map car (master-users master-uri))))) |
---|
45 | |
---|
46 | ;; TODO: more in-depth tests of master result values |
---|
47 | |
---|
48 | (test-group "node API" |
---|
49 | (let ((info (node-info node-uri))) |
---|
50 | (test "Node-info reports zero documents at first" |
---|
51 | 0 (alist-ref 'document-count info)) |
---|
52 | (test "Node-info reports zero words at first" |
---|
53 | 0 (alist-ref 'word-count info)) |
---|
54 | (test "Node-info reports no guests at first" |
---|
55 | '() (alist-ref 'guest-users info)) |
---|
56 | (test "Node-info reports no admins at first" |
---|
57 | '() (alist-ref 'admin-users info))) |
---|
58 | (test "Cache usage starts out empty" |
---|
59 | 0.0 (node-cache-usage node-uri)) |
---|
60 | (test "Document list starts out empty" |
---|
61 | '() (list-documents node-uri)) |
---|
62 | |
---|
63 | (test "After registering admins and guests, they are listed in node info" |
---|
64 | '(("guest1" "guest2" "both") ("admin1" "admin2" "both")) |
---|
65 | (begin (register-guest-user node-uri "guest1") |
---|
66 | (register-guest-user node-uri "guest2") |
---|
67 | (register-guest-user node-uri "both") |
---|
68 | (register-admin-user node-uri "admin1") |
---|
69 | (register-admin-user node-uri "admin2") |
---|
70 | (register-admin-user node-uri "both") |
---|
71 | (let ((info (node-info node-uri))) |
---|
72 | (list (alist-ref 'guest-users info) |
---|
73 | (alist-ref 'admin-users info))))) |
---|
74 | (test "After unregistering users, they are not listed" |
---|
75 | '(("guest2") ("admin2")) |
---|
76 | (begin (unregister-user node-uri "guest1") |
---|
77 | (unregister-user node-uri "admin1") |
---|
78 | (unregister-user node-uri "both") |
---|
79 | (let ((info (node-info node-uri))) |
---|
80 | (list (alist-ref 'guest-users info) |
---|
81 | (alist-ref 'admin-users info))))) |
---|
82 | |
---|
83 | (test-error "Putting document without URI is an error" |
---|
84 | (put-document node-uri "This is just a test" '())) |
---|
85 | (test-assert "Putting documents with URIs succeed" |
---|
86 | (begin |
---|
87 | (put-document node-uri '("Just a test for estraier") |
---|
88 | '((@uri . "/test1") (my-tag . "something"))) |
---|
89 | (put-document node-uri '("Another test for estraier") |
---|
90 | '((@uri . "/test2") (my-tag . "foo") (my-other-tag . "whatever"))) |
---|
91 | #t)) |
---|
92 | ;; If we don't sync, the newly added docs don't (always?) show up... |
---|
93 | (test-assert "Synchronize works" (begin (node-sync node-uri) #t)) |
---|
94 | (let ((info (node-info node-uri))) |
---|
95 | (test "Node-info reports the new documents" |
---|
96 | 2 (alist-ref 'document-count info)) |
---|
97 | (test "Node-info reports the total number of words" |
---|
98 | 6 (alist-ref 'word-count info)) ;; "test for estraier" is in both |
---|
99 | ;; Why does this still report zero? |
---|
100 | #;(test-assert "Size is nonzero" (> 0 (alist-ref 'size info)))) |
---|
101 | (test "Documents are listed" |
---|
102 | '("/test1" "/test2") |
---|
103 | (map (lambda (l) (alist-ref '@uri l)) (list-documents node-uri))) |
---|
104 | (test "Skipping list results works" |
---|
105 | '("/test2") |
---|
106 | (map (lambda (l) (alist-ref '@uri l)) |
---|
107 | (list-documents node-uri prev: "/test1"))) |
---|
108 | (test "Maximum result length is used" |
---|
109 | '("/test1") |
---|
110 | (map (lambda (l) (alist-ref '@uri l)) |
---|
111 | (list-documents node-uri max: 1))) |
---|
112 | (test "Document keywords are correct" |
---|
113 | '("a" "estraier" "for" "just" "test") |
---|
114 | (sort (map car (document-keywords node-uri uri: "/test1")) string<?)) |
---|
115 | |
---|
116 | (receive (doc meta) |
---|
117 | (get-document node-uri uri: "/test1") |
---|
118 | (test "Get-document returns document" |
---|
119 | '("Just a test for estraier") |
---|
120 | (values doc)) |
---|
121 | (test "Get-document returns attributes" |
---|
122 | '("/test1" "something") |
---|
123 | (list (alist-ref '@uri meta) (alist-ref 'my-tag meta))) |
---|
124 | (test-assert "Updating gives no error" |
---|
125 | (begin |
---|
126 | (update-attributes |
---|
127 | node-uri (alist-update! 'my-tag "or other" meta)) |
---|
128 | #t)) |
---|
129 | (receive (doc meta) |
---|
130 | (get-document node-uri uri: "/test1") |
---|
131 | (test "Get-document returns updated attributes" |
---|
132 | "or other" |
---|
133 | (alist-ref 'my-tag meta)) |
---|
134 | (test-assert "Putting a modified doc gives no error" |
---|
135 | (begin |
---|
136 | (put-document node-uri |
---|
137 | '("Simply a test for estraier") |
---|
138 | meta) |
---|
139 | (node-sync node-uri) |
---|
140 | #t)) |
---|
141 | (receive (doc meta) |
---|
142 | (get-document node-uri uri: "/test1") |
---|
143 | (test "Updated document is accepted" |
---|
144 | '("Simply a test for estraier") |
---|
145 | (values doc))))) |
---|
146 | |
---|
147 | (test "Document-attribute returns correct attributes" |
---|
148 | "or other" |
---|
149 | (document-attribute node-uri 'my-tag uri: "/test1")) |
---|
150 | |
---|
151 | ;; XXX: This makes assumptions about the ordering of the docs in the |
---|
152 | ;; result set. It should not do that :) |
---|
153 | (receive (results meta-data) |
---|
154 | (find-documents node-uri phrase: "test") |
---|
155 | (test "Find-documents finds both docs" |
---|
156 | '(((#f . "Another ") ("test" . "test") (#f . " for estraier")) |
---|
157 | ((#f . "Simply a ") ("test" . "test") (#f . " for estraier"))) |
---|
158 | (map car results)) |
---|
159 | (test "URI matches put-document" |
---|
160 | "/test1" |
---|
161 | (alist-ref '@uri (cdr (cadr results)))) |
---|
162 | (test "document-uri->id gives result that matches search result details" |
---|
163 | (alist-ref '@id (cdr (cadr results))) |
---|
164 | (document-uri->id node-uri "/test1"))) |
---|
165 | |
---|
166 | (receive (results meta-data) |
---|
167 | (find-documents node-uri attr-phrases: '("my-tag STREQ foo" |
---|
168 | "my-other-tag STREQ whatever")) |
---|
169 | (test "Find-documents can search attributes" |
---|
170 | '(((#f . "Another test for estraier"))) |
---|
171 | (map car results)) |
---|
172 | (test-assert "Find-documents can search up to 10 attributes" |
---|
173 | (find-documents node-uri |
---|
174 | attr-phrases: '("0" "1" "2" "3" "4" "5" |
---|
175 | "6" "7" "8" "9"))) |
---|
176 | (test-error "Find-documents cannot search more than 10 attributes" |
---|
177 | (find-documents node-uri |
---|
178 | attr-phrases: '("0" "1" "2" "3" "4" "5" |
---|
179 | "6" "7" "8" "9" "10")))) |
---|
180 | |
---|
181 | (test "Deleted documents are not listed" |
---|
182 | '("/test1") |
---|
183 | (begin |
---|
184 | (delete-document node-uri uri: "/test2") |
---|
185 | (map (lambda (l) (alist-ref '@uri l)) (list-documents node-uri)))) |
---|
186 | |
---|
187 | (test "Clearing node results in empty document list" |
---|
188 | '() |
---|
189 | (begin (master-clear-node master-uri "testnode") |
---|
190 | (list-documents node-uri)))) |
---|
191 | |
---|
192 | ;; TODO: Test search options (especially attributes) |
---|
193 | |
---|
194 | (test-group "cleanup" |
---|
195 | (test-assert "Clean shutdown" (begin (master-shutdown master-uri) #t)) |
---|
196 | (test-error "After shutdown, server is unreachable" (master-nodes master-uri))) |
---|
197 | (system "rm -rf masterdir") |
---|