Mercurial > hg > toolbox
comparison test/test_json.txt @ 0:b0942f44413f
import from git://github.com/mozilla/toolbox.git
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 11 May 2014 09:15:35 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b0942f44413f |
---|---|
1 Test toolbox JSON handling | |
2 ========================== | |
3 | |
4 Ensure we have no projects:: | |
5 | |
6 >>> app.get('/') | |
7 [] | |
8 | |
9 Also no authors:: | |
10 | |
11 >>> app.get('/author') | |
12 {} | |
13 | |
14 Make a project:: | |
15 | |
16 >>> project = {'name': 'foo', 'description': 'foo description', 'url': 'http://example.com'} | |
17 >>> response = app.new(**project) | |
18 >>> response.status | |
19 303 | |
20 >>> newproject = app.get('/')[0] | |
21 >>> modified = newproject.pop('modified') | |
22 >>> project == newproject | |
23 True | |
24 | |
25 Get the project by name:: | |
26 | |
27 >>> foo = app.get('/foo') | |
28 >>> modified == foo.pop('modified') | |
29 True | |
30 >>> foo == project | |
31 True | |
32 | |
33 Add some fields to it:: | |
34 | |
35 >>> fields = {'author': 'jhammel'} | |
36 >>> url = '/' + project['name'] | |
37 >>> response = app.post(url, params=fields) | |
38 >>> app.get('/')[0]['author'] | |
39 [u'jhammel'] | |
40 >>> app.get('/author') | |
41 {u'jhammel': [u'foo']} | |
42 >>> fields = {'author': 'turing'} | |
43 >>> response = app.post(url, params=fields) | |
44 >>> sorted(app.get(url)['author']) | |
45 [u'jhammel', u'turing'] | |
46 >>> sorted(app.get('/')[0]['author']) | |
47 [u'jhammel', u'turing'] | |
48 | |
49 Now let's search for the project!:: | |
50 | |
51 >>> authors = app.get('/author') | |
52 >>> len(authors) | |
53 2 | |
54 >>> authors['jhammel'] | |
55 [u'foo'] | |
56 >>> authors['turing'] | |
57 [u'foo'] | |
58 | |
59 Let's search for it a different way:: | |
60 | |
61 >>> project = app.get('/')[0] | |
62 >>> projects = app.get('/', params={'author': 'jhammel'}) | |
63 >>> newproject = projects[0] | |
64 >>> newproject == project | |
65 True | |
66 | |
67 Just to show that the search is doing something:: | |
68 | |
69 >>> app.get('/', params={'author': 'sauron'}) | |
70 [] | |
71 | |
72 Now lets add another project:: | |
73 | |
74 >>> project2 = {'name': 'bar', 'description': 'a bar downtown', 'url': 'http://www.example.com'} | |
75 >>> response = app.new(**project2) | |
76 >>> projects = app.get('/') | |
77 >>> len(projects) | |
78 2 | |
79 >>> projects[0]['name'] | |
80 u'bar' | |
81 >>> projects[1]['name'] | |
82 u'foo' | |
83 >>> jhammels_projects = app.get('/', params={'author': 'jhammel'}) | |
84 >>> len(jhammels_projects) | |
85 1 | |
86 >>> jhammels_projects[0]['name'] | |
87 u'foo' | |
88 | |
89 Test search:: | |
90 | |
91 >>> projects = app.get('/', params={'q': 'jhammel'}) | |
92 >>> len(projects) | |
93 1 | |
94 >>> projects[0]['name'] | |
95 u'foo' | |
96 >>> projects = app.get('/', params={'q': 'downtown'}) | |
97 >>> len(projects) | |
98 1 | |
99 >>> projects[0]['name'] | |
100 u'bar' | |
101 | |
102 Add some metadata. Make sure we see it:: | |
103 | |
104 >>> url = '/bar' | |
105 >>> response = app.post(url, {'author': 'turing'}) | |
106 >>> len(app.get()) | |
107 2 | |
108 >>> len(app.get('/', params={'author': 'jhammel'})) | |
109 1 | |
110 >>> len(app.get('/', params={'q': 'jhammel'})) | |
111 1 | |
112 >>> len(app.get('/', params={'author': 'turing'})) | |
113 2 | |
114 >>> len(app.get('/', params={'q': 'turing'})) | |
115 2 | |
116 >>> projects = app.get('/', params={'author': 'turing', 'q': 'downtown'}) | |
117 >>> len(projects) | |
118 1 | |
119 >>> projects[0]['name'] | |
120 u'bar' | |
121 | |
122 Add a third project, just for variety:: | |
123 | |
124 >>> response = app.new(name='fleem', description='fleem in a building downtown', url='http://example.net') | |
125 >>> projects = app.get('/') | |
126 >>> len(projects) | |
127 3 | |
128 >>> sorted([i['name'] for i in app.get('/', params=dict(q='downtown'))]) | |
129 [u'bar', u'fleem'] | |
130 >>> [i['name'] for i in app.get('/', params=dict(q='building'))] | |
131 [u'fleem'] | |
132 | |
133 Delete some metadata:: | |
134 | |
135 >>> response = app.post('/bar', params={'action': 'delete', 'author': 'turing'}) | |
136 >>> projects = app.get('/', params={'author': 'turing'}) | |
137 >>> len(projects) | |
138 1 | |
139 >>> projects[0]['name'] | |
140 u'foo' | |
141 >>> projects = app.get('/', params={'q': 'turing'}) | |
142 >>> len(projects) | |
143 1 | |
144 >>> projects[0]['name'] | |
145 u'foo' | |
146 | |
147 Delete a project:: | |
148 | |
149 >>> response = app.post('/delete', params={'project': 'foo'}) | |
150 >>> len(app.get('/')) | |
151 2 | |
152 >>> len(app.get('/', params={'author': 'jhammel'})) | |
153 0 | |
154 >>> results = app.get('/', params={'q': 'jhammel'}) | |
155 >>> len(results) | |
156 0 | |
157 | |
158 You're back to two basic projects without much metadata. Let's give them some!:: | |
159 | |
160 >>> projects = app.get('/') | |
161 >>> [sorted(project.keys()) for project in projects] | |
162 [[u'description', u'modified', u'name', u'url'], [u'description', u'modified', u'name', u'url']] | |
163 >>> bar_modified_last = projects[0]['modified'] | |
164 >>> fleem_modified_earlier = projects[1]['modified'] | |
165 >>> bar_modified_last > fleem_modified_earlier | |
166 True | |
167 >>> [project['name'] for project in projects] | |
168 [u'bar', u'fleem'] | |
169 >>> description = 'You could be swining on a star' | |
170 >>> response = app.post('/bar', params=dict(description=description)) | |
171 >>> projects = app.get('/', params={'q': 'star'}) | |
172 >>> len(projects) | |
173 1 | |
174 >>> projects[0]['description'] == description | |
175 True | |
176 >>> response = app.post('/bar', params={'type': 'song', 'usage': 'music', 'author': 'Sinatra'}) | |
177 >>> songs = app.get('/', params={'type': 'song'}) | |
178 >>> len(songs) | |
179 1 | |
180 >>> songs[0]['name'] == 'bar' | |
181 True | |
182 >>> songs = app.get('/', params={'q': 'song'}) | |
183 >>> len(songs) | |
184 1 | |
185 >>> songs[0]['name'] == 'bar' | |
186 True | |
187 >>> response = app.post('/fleem', params={'type': 'song', 'description': 'Cotton Eye Joe', 'author': 'Rednex'}) | |
188 >>> songs = app.get('/', params={'type': 'song'}) | |
189 >>> len(songs) | |
190 2 | |
191 >>> songs = app.get('/', params={'q': 'song'}) | |
192 >>> len(songs) | |
193 2 | |
194 >>> songs = app.get('/', params={'type': 'song', 'q': 'star'}) | |
195 >>> len(songs) | |
196 1 | |
197 >>> songs[0]['name'] | |
198 u'bar' | |
199 >>> songs = app.get('/', params={'type': 'song', 'author': 'Sinatra'}) | |
200 >>> len(songs) | |
201 1 | |
202 >>> songs[0]['name'] | |
203 u'bar' | |
204 | |
205 Now try renaming a tool:: | |
206 >>> [i['name'] for i in app.get('/')] | |
207 [u'fleem', u'bar'] | |
208 >>> response = app.post('/bar', params={'name': 'star'}) | |
209 >>> songs = app.get('/') | |
210 >>> len(songs) | |
211 2 | |
212 >>> projects = app.get('/', params={'q': 'star'}) | |
213 >>> len(projects) | |
214 1 | |
215 >>> star = projects[0] | |
216 >>> star['name'] | |
217 u'star' | |
218 >>> star['type'] | |
219 [u'song'] | |
220 | |
221 You should not be allowed to rename a tool if another tool has the | |
222 same name:: | |
223 | |
224 >>> sorted([i['name'] for i in app.get('/')]) | |
225 [u'fleem', u'star'] | |
226 >>> response = app.post('/star', params={'name': 'fleem'}, status=403) # Forbidden | |
227 >>> sorted([i['name'] for i in app.get('/')]) | |
228 [u'fleem', u'star'] | |
229 | |
230 You should not be allowed to have multiple identical item in the same | |
231 field:: | |
232 | |
233 >>> app.get('/star')['author'] | |
234 [u'Sinatra'] | |
235 >>> response = app.post('/star', params={'action': 'replace', 'author': 'Sinatra,Sinatra'}) | |
236 >>> app.get('/star')['author'] | |
237 [u'Sinatra'] | |
238 | |
239 You can rename an entire set of fields:: | |
240 | |
241 >>> [project['type'] for project in app.get('/')] | |
242 [[u'song'], [u'song']] | |
243 >>> response = app.post('/type', params={'song': 'number one hit'}) | |
244 >>> [project['type'] for project in app.get('/')] | |
245 [[u'number one hit'], [u'number one hit']] | |
246 | |
247 Fields in the request should be comma-separated and stripped of whitespace:: | |
248 | |
249 >>> project = {'name': 'A New Project', 'description': 'new description', 'url': 'http://example.com'} | |
250 >>> project_url = '/' + urlescape(project['name']) | |
251 >>> response = app.new(**project) | |
252 >>> fields = {'type': 'song, project, something special'} | |
253 >>> response = app.post(project_url, params=fields) | |
254 >>> sorted(app.get(project_url)['type']) | |
255 [u'project', u'something special', u'song'] | |
256 | |
257 You won't be able to have multiple identical field values or empty values:: | |
258 | |
259 >>> response = app.post(project_url, params=dict(author=' john, , , fielding, the third,,')) | |
260 >>> sorted(app.get(project_url)['author']) | |
261 [u'fielding', u'john', u'the third'] | |
262 | |
263 You should not be able to rename a project:: | |
264 | |
265 >>> sorted([project['name'] for project in app.get('/')]) | |
266 [u'A New Project', u'fleem', u'star'] | |
267 >>> response = app.post('/star', params=dict(name=''), status=403) | |
268 >>> response.status | |
269 403 | |
270 >>> response = app.post('/fleem', params=dict(name=' '), status=403) | |
271 >>> response.status | |
272 403 | |
273 >>> sorted([project['name'] for project in app.get('/')]) | |
274 [u'A New Project', u'fleem', u'star'] |