Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 590 lines (533 sloc) 22.795 kb
051b62f @palewire Started up a basic test.py file
palewire authored
1 #! /usr/bin/env python
496ffe0 @palewire Merged in Joe Germuska's unicode-ish tweak to urlencode. Added a test…
palewire authored
2 # -*- coding: utf-8 -*-
456f73c @palewire Cleaned up my first draft of the put method a little bit. Fixes #16.
palewire authored
3 """
4 Tests out the DocumentCloud API.
5
6 Most requests require authentication, which I'm not sure how deal with properly
7 in this circumstance. For the time being, I'm importing latimes.com credentials
8 and fiddling around with junk files I've placed in there. Obviously, that means
9 this test suite will only work on my computer, which seems like a problem.
10
11 If you know how I ought to sort this sort of thing out, please let me know.
12 """
3801369 @palewire Added a test for the upload that passes. Fixes #29
palewire authored
13 import os
87d2919 @palewire Wrote a test for PUT Document updates that seems to work, except rela…
palewire authored
14 import random
15 import string
cd0acf2 @palewire Figured out how to set document_list on the Project class and wrote a…
palewire authored
16 import textwrap
051b62f @palewire Started up a basic test.py file
palewire authored
17 import unittest
d7889e3 @palewire Worked in the unicode support hashed out by @shaneshifflet in the Bay…
palewire authored
18 import StringIO
496ffe0 @palewire Merged in Joe Germuska's unicode-ish tweak to urlencode. Added a test…
palewire authored
19 from copy import copy
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
20 from documentcloud import DocumentCloud
b095fe5 @palewire Added some catches that I hope will help ensure dupes and non-Documen…
palewire authored
21 from documentcloud import CredentialsMissingError, DuplicateObjectError
abcb0e7 @palewire Added test for CredentialsMissingError and fixed some bugs with the o…
palewire authored
22 from documentcloud import CredentialsFailedError, DoesNotExistError
2d656c0 @palewire Added support for retreiving 'mentions' in search results that show w…
palewire authored
23 from documentcloud import Annotation, Document, Project, Section, Entity, Mention
97dc208 @palewire Added a method for pulling all the projects belonging to an authorize…
palewire authored
24 from private_settings import DOCUMENTCLOUD_USERNAME, DOCUMENTCLOUD_PASSWORD
051b62f @palewire Started up a basic test.py file
palewire authored
25
87d2919 @palewire Wrote a test for PUT Document updates that seems to work, except rela…
palewire authored
26 #
27 # Odds and ends
28 #
29
30 def get_random_string(length=6):
31 """
32 Generate a random string of letters and numbers
33 """
34 return ''.join(random.choice(string.letters + string.digits) for i in xrange(length))
35
1b609b0 @palewire Added some more complicated non-English characters to the testing
palewire authored
36 PANGRAMS = {
37 'en': 'The quick brown fox jumps over the lazy dog.',
38 'da': 'Quizdeltagerne spiste jordbær med fløde, mens cirkusklovnen Wolther spillede på xylofon.',
39 'de': 'Falsches Üben von Xylophonmusik quält jeden größeren Zwerg.',
40 'el': 'Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο.',
41 'es': 'El pingüino Wenceslao hizo kilómetros bajo exhaustiva lluvia y frío, añoraba a su querido cachorro.',
42 'fr': "Portez ce vieux whisky au juge blond qui fume sur son île intérieure, à côté de l'alcôve ovoïde, où les bûches se consument dans l'âtre, ce qui lui permet de penser à la cænogenèse de l'être dont il est question dans la cause ambiguë entendue à Moÿ, dans un capharnaüm qui, pense-t-il, diminue çà et là la qualité de son œuvre.",
43 'ga': "D'fhuascail Íosa, Úrmhac na hÓighe Beannaithe, pór Éava agus Ádhaimh.",
44 'hu': 'Árvíztűrő tükörfúrógép.',
45 'is': 'Kæmi ný öxi hér ykist þjófum nú bæði víl og ádrepa.',
46 'jp': """'いろはにほへとちりぬるを
47 わかよたれそつねならむ
48 うゐのおくやまけふこえて
49 あさきゆめみしゑひもせす""",
50 'iw': '? דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה.',
51 'pl': 'Pchnąć w tę łódź jeża lub ośm skrzyń fig.',
52 'ru': 'чащах юга жил бы цитрус? Да, но фальшивый экземпляр!'
53 }
54
87d2919 @palewire Wrote a test for PUT Document updates that seems to work, except rela…
palewire authored
55 #
56 # Tests
57 #
051b62f @palewire Started up a basic test.py file
palewire authored
58
59 class BaseTest(unittest.TestCase):
60
61 def setUp(self):
7cbec0a @palewire Got an annotations test passing for lazy loaded search objects
palewire authored
62 self.test_search = 'Calpers special review'
63 self.test_id = '74103-report-of-the-calpers-special-review'
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
64 self.public_client = DocumentCloud()
97dc208 @palewire Added a method for pulling all the projects belonging to an authorize…
palewire authored
65 self.private_client = DocumentCloud(DOCUMENTCLOUD_USERNAME, DOCUMENTCLOUD_PASSWORD)
abcb0e7 @palewire Added test for CredentialsMissingError and fixed some bugs with the o…
palewire authored
66 self.fake_client = DocumentCloud("John Doe", "TK")
051b62f @palewire Started up a basic test.py file
palewire authored
67
68
ec960bf @palewire Added a test get test
palewire authored
69 class DocumentSearchTest(BaseTest):
3a5de2f @palewire Added a test to verify that all the expected document attributes exis…
palewire authored
70
051b62f @palewire Started up a basic test.py file
palewire authored
71 def test_search(self):
72 """
73 Test a search.
74 """
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
75 obj_list = self.public_client.documents.search(self.test_search)
051b62f @palewire Started up a basic test.py file
palewire authored
76 self.assertEqual(type(obj_list), type([]))
97dc208 @palewire Added a method for pulling all the projects belonging to an authorize…
palewire authored
77 self.assertEqual(type(obj_list[0]), Document)
6b9ac1b @palewire Added a multipage search test
palewire authored
78
3a5de2f @palewire Added a test to verify that all the expected document attributes exis…
palewire authored
79 # def test_multipage_search(self):
80 # """
81 # Test a search that will return more than a single page of results.
82 # """
83 # obj_list = documentcloud.documents.search("johnson")
84 # self.assertTrue(len(obj_list) > 1000)
c6212c5 @palewire Added a test to verify attributes on the search result Document that …
palewire authored
85
69bc7ef @palewire Consolidated Document related tests
palewire authored
86 def test_search_attrs(self):
c6212c5 @palewire Added a test to verify attributes on the search result Document that …
palewire authored
87 """
88 Verify that all the Document attributes exist.
89 """
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
90 obj = self.public_client.documents.search(self.test_search)[0]
c6212c5 @palewire Added a test to verify attributes on the search result Document that …
palewire authored
91 attr_list = [
92 'access',
93 'annotations',
94 'canonical_url',
95 'contributor',
96 'contributor_organization',
97 'created_at',
98 'description',
99 'id',
100 'pages',
101 'resources',
102 'sections',
103 'source',
104 'title',
105 'updated_at',
6ec0074 @palewire Added support for the new 'data' document attribute, a dictionary of …
palewire authored
106 'data',
c6212c5 @palewire Added a test to verify attributes on the search result Document that …
palewire authored
107 ]
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
108 for attr in attr_list:
109 self.assertTrue(hasattr(obj, attr))
abcb0e7 @palewire Added test for CredentialsMissingError and fixed some bugs with the o…
palewire authored
110
69bc7ef @palewire Consolidated Document related tests
palewire authored
111 def test_search_annotations(self):
7cbec0a @palewire Got an annotations test passing for lazy loaded search objects
palewire authored
112 """
113 Test whether annotations exist.
114 """
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
115 obj = self.public_client.documents.search(self.test_search)[0]
7cbec0a @palewire Got an annotations test passing for lazy loaded search objects
palewire authored
116 self.assertEqual(type(obj.annotations[0]), Annotation)
92e9343 @palewire Added tests for the sections
palewire authored
117
69bc7ef @palewire Consolidated Document related tests
palewire authored
118 def test_search_sections(self):
92e9343 @palewire Added tests for the sections
palewire authored
119 """
120 Test whether sections exist.
121 """
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
122 obj = self.public_client.documents.get(self.test_id)
92e9343 @palewire Added tests for the sections
palewire authored
123 self.assertEqual(type(obj.sections[0]), Section)
f3cb4e4 @palewire Added methods for pulling Entity objects extracted from a document. A…
palewire authored
124
69bc7ef @palewire Consolidated Document related tests
palewire authored
125 def test_search_entities(self):
f3cb4e4 @palewire Added methods for pulling Entity objects extracted from a document. A…
palewire authored
126 """
127 Test whether entities exist.
128 """
129 obj = self.public_client.documents.get(self.test_id)
130 self.assertEqual(type(obj.entities[0]), Entity)
3a5de2f @palewire Added a test to verify that all the expected document attributes exis…
palewire authored
131
ec960bf @palewire Added a test get test
palewire authored
132 def test_get(self):
133 """
134 Test a get request for a particular document.
135 """
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
136 obj = self.public_client.documents.get(self.test_id)
ec960bf @palewire Added a test get test
palewire authored
137 self.assertEqual(type(obj), Document)
3a5de2f @palewire Added a test to verify that all the expected document attributes exis…
palewire authored
138
69bc7ef @palewire Consolidated Document related tests
palewire authored
139 def test_get_attrs(self):
3a5de2f @palewire Added a test to verify that all the expected document attributes exis…
palewire authored
140 """
141 Verify that all the Document attributes exist.
142 """
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
143 obj = self.public_client.documents.get(self.test_id)
3a5de2f @palewire Added a test to verify that all the expected document attributes exis…
palewire authored
144 attr_list = [
145 'access',
146 'annotations',
147 'canonical_url',
148 'contributor',
149 'contributor_organization',
150 'created_at',
151 'description',
152 'id',
153 'pages',
154 'resources',
155 'sections',
156 'source',
157 'title',
158 'updated_at',
6ec0074 @palewire Added support for the new 'data' document attribute, a dictionary of …
palewire authored
159 'data',
3a5de2f @palewire Added a test to verify that all the expected document attributes exis…
palewire authored
160 ]
161 [self.assertTrue(hasattr(obj, attr)) for attr in attr_list]
7cbec0a @palewire Got an annotations test passing for lazy loaded search objects
palewire authored
162
ee538f4 @palewire Consolidated some of the changes made to address ticket #77 so there …
palewire authored
163 def test_get_pdf(self):
164 """
165 Test if you can pull the PDF
166 """
167 obj = self.public_client.documents.get(self.test_id)
168 self.assertTrue(len(obj.pdf) > 0, True)
169
170 # def test_get_full_text(self):
171 # """
172 # Test if you can pull the full text
173 # """
174 # obj = self.public_client.documents.get(self.test_id)
175 # try:
176 # self.assertTrue(len(obj.full_text) > 0, True)
177 # except:
178 # self.assertRaises(obj.full_text, NotImplementedError)
179
180 def test_get_images(self):
181 """
182 Test if you can pull the images
183 """
184 obj = self.public_client.documents.get(self.test_id)
185 self.assertTrue(len(obj.small_image) > 0, True)
186 self.assertTrue(len(obj.thumbnail_image) > 0, True)
187 self.assertTrue(len(obj.normal_image) > 0, True)
188 self.assertTrue(len(obj.large_image) > 0, True)
189
69bc7ef @palewire Consolidated Document related tests
palewire authored
190 def test_get_annotations(self):
7cbec0a @palewire Got an annotations test passing for lazy loaded search objects
palewire authored
191 """
192 Test whether annotations exist.
193 """
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
194 obj = self.public_client.documents.get(self.test_id)
7cbec0a @palewire Got an annotations test passing for lazy loaded search objects
palewire authored
195 self.assertEqual(type(obj.annotations[0]), Annotation)
92e9343 @palewire Added tests for the sections
palewire authored
196
69bc7ef @palewire Consolidated Document related tests
palewire authored
197 def test_get_sections(self):
92e9343 @palewire Added tests for the sections
palewire authored
198 """
199 Test whether sections exist.
200 """
6e08bac @palewire Refactored the client as a callable so future improvements can start …
palewire authored
201 obj = self.public_client.documents.get(self.test_id)
92e9343 @palewire Added tests for the sections
palewire authored
202 self.assertEqual(type(obj.sections[0]), Section)
f3cb4e4 @palewire Added methods for pulling Entity objects extracted from a document. A…
palewire authored
203
69bc7ef @palewire Consolidated Document related tests
palewire authored
204 def test_get_entities(self):
f3cb4e4 @palewire Added methods for pulling Entity objects extracted from a document. A…
palewire authored
205 """
206 Test whether entities exist.
207 """
208 obj = self.public_client.documents.get(self.test_id)
209 self.assertEqual(type(obj.entities[0]), Entity)
87d2919 @palewire Wrote a test for PUT Document updates that seems to work, except rela…
palewire authored
210
2d656c0 @palewire Added support for retreiving 'mentions' in search results that show w…
palewire authored
211 def test_get_mentions(self):
212 """
213 Test whether mentions exist.
214 """
215 obj = self.public_client.documents.search(self.test_search)[0]
216 self.assertEqual(type(obj.mentions[0]), Mention)
217
7be552d @palewire A touch of documentation, and a test, supporting the dictionary restr…
palewire authored
218 def test_set_data_type_restrictions(self):
219 """
220 Make sure `data` attribute will only accept a dictionary.
221 """
222 obj = self.private_client.documents.get(self.test_id)
223 obj.data = dict(foo='bar')
224 self.assertRaises(TypeError, obj.set_data, "string")
225 self.assertRaises(TypeError, obj.set_data, 666)
226 self.assertRaises(TypeError, obj.set_data, obj)
227
69bc7ef @palewire Consolidated Document related tests
palewire authored
228 def test_get_put(self):
87d2919 @palewire Wrote a test for PUT Document updates that seems to work, except rela…
palewire authored
229 """
230 Test whether we can put random noise to all the editable fields.
231 """
232 # Pull the object we'll deface
233 obj = self.private_client.documents.get("15144-mitchrpt")
234 # Create random strings we will save to the editable attributes
235 title = 'The Mitchell Report (%s)' % get_random_string()
236 source = 'DLA Piper (%s)' % get_random_string()
237 description = get_random_string()
6ec0074 @palewire Added support for the new 'data' document attribute, a dictionary of …
palewire authored
238 data = {get_random_string(): get_random_string()}
87d2919 @palewire Wrote a test for PUT Document updates that seems to work, except rela…
palewire authored
239 if obj.resources.related_article == 'http://documents.latimes.com':
240 related_article = 'http://documentcloud.org'
241 else:
242 related_article = 'http://documents.latimes.com'
243 if obj.resources.published_url == 'http://documents.latimes.com':
244 published_url = 'http://documentcloud.org'
245 else:
246 published_url = 'http://documents.latimes.com'
247 # Set the random strings our local object's attributes
248 obj.title = title
249 obj.source = source
250 obj.description = description
6ec0074 @palewire Added support for the new 'data' document attribute, a dictionary of …
palewire authored
251 obj.data = data
87d2919 @palewire Wrote a test for PUT Document updates that seems to work, except rela…
palewire authored
252 obj.resources.related_article = related_article
253 obj.resources.published_url = published_url
254 # Save the changes up to DocumentCloud
255 obj.put()
256 # Pull the object again and verify the changes stuck
257 obj = self.private_client.documents.get("15144-mitchrpt")
258 self.assertEqual(obj.title, title)
259 self.assertEqual(obj.source, source)
260 self.assertEqual(obj.description, description)
6ec0074 @palewire Added support for the new 'data' document attribute, a dictionary of …
palewire authored
261 self.assertEqual(obj.data, data)
87d2919 @palewire Wrote a test for PUT Document updates that seems to work, except rela…
palewire authored
262 self.assertEqual(obj.resources.related_article, related_article)
263 self.assertEqual(obj.resources.published_url, published_url)
3801369 @palewire Added a test for the upload that passes. Fixes #29
palewire authored
264
d60ec75 @palewire Created an alias for save that uses put, and tests. Fixes #58.
palewire authored
265 def test_save(self):
266 """
267 Test whether the save method properly aliases `put`.
268 """
269 # Pull the object we'll deface
270 obj = self.private_client.documents.get("15144-mitchrpt")
271 # Create random strings we will save to the editable attributes
272 title = 'The Mitchell Report (%s)' % get_random_string()
273 obj.title = title
274 # Save the changes up to DocumentCloud with the alias
275 obj.save()
276 # Pull the object again and verify the changes stuck
277 obj = self.private_client.documents.get("15144-mitchrpt")
278 self.assertEqual(obj.title, title)
279
496ffe0 @palewire Merged in Joe Germuska's unicode-ish tweak to urlencode. Added a test…
palewire authored
280 def test_put_with_weird_encoding(self):
281 """
282 Test whether you can save an attribute with some weird encoding
283 in the title.
284 """
285 # Pull the object we'll deface
286 obj = self.private_client.documents.get("15144-mitchrpt")
1b609b0 @palewire Added some more complicated non-English characters to the testing
palewire authored
287 before_title = copy(obj.title)
288 before_description = copy(obj.description)
496ffe0 @palewire Merged in Joe Germuska's unicode-ish tweak to urlencode. Added a test…
palewire authored
289 # Add something weird to the title and save it
1b609b0 @palewire Added some more complicated non-English characters to the testing
palewire authored
290 after_title = copy(PANGRAMS['iw'])
291 after_description = copy(PANGRAMS['jp'])
292 obj.title = after_title
293 obj.description = after_description
496ffe0 @palewire Merged in Joe Germuska's unicode-ish tweak to urlencode. Added a test…
palewire authored
294 obj.put()
1b609b0 @palewire Added some more complicated non-English characters to the testing
palewire authored
295 self.assertEqual(obj.title, after_title)
296 self.assertEqual(obj.description, after_description)
496ffe0 @palewire Merged in Joe Germuska's unicode-ish tweak to urlencode. Added a test…
palewire authored
297 # Switch it back
1b609b0 @palewire Added some more complicated non-English characters to the testing
palewire authored
298 obj.title = before_title
299 obj.description = before_description
496ffe0 @palewire Merged in Joe Germuska's unicode-ish tweak to urlencode. Added a test…
palewire authored
300 obj.put()
1b609b0 @palewire Added some more complicated non-English characters to the testing
palewire authored
301 self.assertEqual(obj.title, before_title)
302 self.assertEqual(obj.description, before_description)
496ffe0 @palewire Merged in Joe Germuska's unicode-ish tweak to urlencode. Added a test…
palewire authored
303
42f0359 @palewire Added a DELETE method to get rid of documents and a test that passes.…
palewire authored
304 def test_upload_and_delete(self):
305 """
306 Makes sure you can create and delete a document.
307 """
308 # Create it
309 title = '001 - Test upload (%s)' % get_random_string()
9b6628b @palewire documents.upload now returns the document object, not just the id. Fi…
palewire authored
310 obj = self.private_client.documents.upload(
3801369 @palewire Added a test for the upload that passes. Fixes #29
palewire authored
311 os.path.join(os.path.dirname(__file__), "test.pdf"),
312 title,
27889a6 @palewire Added upload kwargs for other attributes to upload_directory. Fixes #…
palewire authored
313 description='Blah blah',
314 related_article='http://www.latimes.com',
6ec0074 @palewire Added support for the new 'data' document attribute, a dictionary of …
palewire authored
315 data=dict(like='this', boom='bap'),
3801369 @palewire Added a test for the upload that passes. Fixes #29
palewire authored
316 )
317 self.assertEqual(type(obj), Document)
27889a6 @palewire Added upload kwargs for other attributes to upload_directory. Fixes #…
palewire authored
318 self.assertEqual(obj.description, 'Blah blah')
319 self.assertEqual(obj.related_article, 'http://www.latimes.com')
6ec0074 @palewire Added support for the new 'data' document attribute, a dictionary of …
palewire authored
320 self.assertEqual(obj.data, {'like': 'this', 'boom': 'bap'})
42f0359 @palewire Added a DELETE method to get rid of documents and a test that passes.…
palewire authored
321 # Delete it
322 obj.delete()
9b6628b @palewire documents.upload now returns the document object, not just the id. Fi…
palewire authored
323 self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
6d6e621 @palewire The 'related_article' and 'published_url' attributes, hidden away in …
palewire authored
324
9e6dcbe @palewire Upload should no accept file objects in addition to paths, ala Django…
palewire authored
325 def test_file_obj_upload_and_delete(self):
326 """
327 Test that uploading works when you provide a file object instead of a
328 path.
329 """
330 # Create it
331 title = '001 - Test upload (%s)' % get_random_string()
332 obj = self.private_client.documents.upload(
333 open(os.path.join(os.path.dirname(__file__), "test.pdf"), "rb"),
334 title,
335 )
336 self.assertEqual(type(obj), Document)
337 self.assertEqual(obj.title, title)
338 # Delete it
339 obj.delete()
340 self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
341
d7889e3 @palewire Worked in the unicode support hashed out by @shaneshifflet in the Bay…
palewire authored
342 def test_unicode_upload_and_delete(self):
343 """
344 Ensure that documents with non-english characters can be uploaded
345 """
a593377 @palewire Fixed a bug in how titles are assigned to a file object
palewire authored
346 pdf = os.path.join(os.path.dirname(__file__), "español.pdf")
347 obj = self.private_client.documents.upload(open(pdf, 'rb'))
d7889e3 @palewire Worked in the unicode support hashed out by @shaneshifflet in the Bay…
palewire authored
348 self.assertEqual(type(obj), Document)
349 # Delete it
350 obj.delete()
351 self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
352
353 def test_virtual_file_upload_and_delete(self):
354 """
355 Proxy test case for files stored in memory, for instance, django-storages
356 these tests are difficult to create as the class used to represent a file
357 object is determined at runtime by the DEFAULT_FILE_STORAGE var (django)
358 anyway, the main point is to show the MultipartPostHandler can handle unicode
359 """
a593377 @palewire Fixed a bug in how titles are assigned to a file object
palewire authored
360 path = os.path.join(os.path.dirname(__file__), "español.pdf")
d7889e3 @palewire Worked in the unicode support hashed out by @shaneshifflet in the Bay…
palewire authored
361 real_file = open(path, 'rb')
362 virtual_file = StringIO.StringIO(real_file.read())
363 obj = self.private_client.documents.upload(virtual_file, title='Espanola!')
364 self.assertEqual(type(obj), Document)
365 # Delete it
366 obj.delete()
367 self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
368
3a65720 @palewire Added the option to document uploads so you can prevent your data fr…
palewire authored
369 def test_secure_upload_and_delete(self):
370 """
371 Make sure you can create and delete a document using the secure
372 parameter that hides your data from OpenCalais.
373
374 Currently I don't know a way to test whether the parameter is properly
375 applied. It seems to work in the UI, but, as far as I know, the API
376 doesn't return an indicator that I have figured out how to test.
377 """
378 # Create it
379 title = '001 - Test upload (%s)' % get_random_string()
380 obj = self.private_client.documents.upload(
381 os.path.join(os.path.dirname(__file__), "test.pdf"),
382 title,
383 secure=True,
384 )
385 self.assertEqual(type(obj), Document)
386 # Delete it
387 obj.delete()
388 self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
389
a5e37d5 @palewire Wrote a test for upload_directory that passes. Fixes ##52
palewire authored
390 def test_upload_directory(self):
391 """
392 Makes sure you can upload all the pdfs in a directory.
393 """
394 # Upload everything in this directory.
27889a6 @palewire Added upload kwargs for other attributes to upload_directory. Fixes #…
palewire authored
395 obj_list = self.private_client.documents.upload_directory('./',
6ec0074 @palewire Added support for the new 'data' document attribute, a dictionary of …
palewire authored
396 source='Los Angeles Times',
397 published_url='http://www.latimes.com',
398 )
a5e37d5 @palewire Wrote a test for upload_directory that passes. Fixes ##52
palewire authored
399 # Which should only be one document
d7889e3 @palewire Worked in the unicode support hashed out by @shaneshifflet in the Bay…
palewire authored
400 self.assertEqual(len(obj_list), 2)
a5e37d5 @palewire Wrote a test for upload_directory that passes. Fixes ##52
palewire authored
401 self.assertEqual(type(obj_list[0]), Document)
27889a6 @palewire Added upload kwargs for other attributes to upload_directory. Fixes #…
palewire authored
402 self.assertEqual(obj_list[0].source, 'Los Angeles Times')
403 self.assertEqual(obj_list[0].published_url, 'http://www.latimes.com')
a5e37d5 @palewire Wrote a test for upload_directory that passes. Fixes ##52
palewire authored
404 # And which we should be able to delete
d7889e3 @palewire Worked in the unicode support hashed out by @shaneshifflet in the Bay…
palewire authored
405 [i.delete() for i in obj_list]
406 [self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
407 for obj in obj_list]
a5e37d5 @palewire Wrote a test for upload_directory that passes. Fixes ##52
palewire authored
408
6d6e621 @palewire The 'related_article' and 'published_url' attributes, hidden away in …
palewire authored
409 def test_resources(self):
410 """
411 Makes sure the canonical url is a top-level attribute on the Document.
412 """
413 obj = self.public_client.documents.get(self.test_id)
414 # Test that they come out the same
415 self.assertEqual(obj.published_url, obj.resources.published_url)
416 self.assertEqual(obj.related_article, obj.resources.related_article)
417 # Then test that they setattr the same
418 obj.published_url = 'http://latimes.com'
419 obj.related_article = 'http://palewire.com'
420 self.assertEqual(obj.published_url, obj.resources.published_url)
421 self.assertEqual(obj.related_article, obj.resources.related_article)
ec960bf @palewire Added a test get test
palewire authored
422
051b62f @palewire Started up a basic test.py file
palewire authored
423
97dc208 @palewire Added a method for pulling all the projects belonging to an authorize…
palewire authored
424 class ProjectTest(BaseTest):
425
e9da123 @palewire Added a method that will retrieve a single project when provided its …
palewire authored
426 def test_all(self):
97dc208 @palewire Added a method for pulling all the projects belonging to an authorize…
palewire authored
427 """
e9da123 @palewire Added a method that will retrieve a single project when provided its …
palewire authored
428 Test an `all` request for a list of all projects belong to an
97dc208 @palewire Added a method for pulling all the projects belonging to an authorize…
palewire authored
429 authorized user.
430 """
431 obj_list = self.private_client.projects.all()
432 self.assertEqual(type(obj_list), type([]))
433 self.assertEqual(type(obj_list[0]), Project)
e9da123 @palewire Added a method that will retrieve a single project when provided its …
palewire authored
434
435 def test_get(self):
436 """
a4fef64 @palewire Projects can now be pulled by id or by title. And when you create a p…
palewire authored
437 Test a `get` methods for a particular project
e9da123 @palewire Added a method that will retrieve a single project when provided its …
palewire authored
438 """
439 obj = self.private_client.projects.get('934')
440 self.assertEqual(type(obj), Project)
a4fef64 @palewire Projects can now be pulled by id or by title. And when you create a p…
palewire authored
441 obj2 = self.private_client.projects.get_by_id('934')
442 self.assertEqual(obj.id, obj2.id)
443 obj3 = self.private_client.projects.get_by_title(obj2.title)
444 self.assertEqual(obj2.id, obj3.id)
445
4d1990b @palewire Added methods to the Project class that will pull and store associate…
palewire authored
446 def test_document_list(self):
447 """
448 Verify that a project can pull back all if its associated documents.
449 """
450 obj = self.private_client.projects.get('934')
451 doc_list = obj.document_list
452 self.assertEqual(type(doc_list[0]), Document)
453
454 def test_get_document(self):
455 """
456 Verify that a project can pull a particular document by id
457 """
458 obj = self.private_client.projects.get('934')
459 doc = obj.get_document(u'25798-pr-01092011-loughner')
460 self.assertEqual(type(doc), Document)
cd0acf2 @palewire Figured out how to set document_list on the Project class and wrote a…
palewire authored
461
462 def test_put(self):
463 """
464 Test whether we can put random noise to all the editable fields.
465 """
466 # Pull the object we'll deface
467 obj = self.private_client.projects.get("703")
468 # Create random strings we will save to the editable attributes
469 title = 'The Klee Report (%s)' % get_random_string()
470 description = textwrap.dedent("""
471 An independent probe into Sam Zell\'s purchase of Tribune Company by
472 investigator Kenneth Klee. Released at the end of July 2010. (%s)
473 """)
474 description = description % get_random_string()
475 # Set the random strings our local object's attributes
476 # and zero out the document list.
477 obj.title = title
478 obj.description = description
479 obj.document_list = []
480 # Save the changes up to DocumentCloud
481 obj.put()
482 # Pull the object again and verify the changes stuck
483 obj = self.private_client.projects.get("703")
484 self.assertEqual(obj.title, title)
485 self.assertEqual(obj.description, description)
486 self.assertEqual(len(obj.document_list), 0)
487 # Now add all the documents back in
488 proj_ids = [
489 u'12667-the-klee-report-volume-2',
490 u'12666-the-klee-report-volume-1'
491 ]
492 for id in proj_ids:
493 doc = self.private_client.documents.get(id)
494 obj.document_list.append(doc)
495 obj.put()
496 obj = self.private_client.projects.get("703")
497 self.assertEqual(len(obj.document_list), len(proj_ids))
b095fe5 @palewire Added some catches that I hope will help ensure dupes and non-Documen…
palewire authored
498
d60ec75 @palewire Created an alias for save that uses put, and tests. Fixes #58.
palewire authored
499 def test_save(self):
500 """
501 Test whether the save method properly aliases `put`.
502 """
503 # Pull the object we'll deface
504 obj = self.private_client.projects.get("703")
505 # Create random strings we will save to the editable attributes
506 title = 'The Klee Report (%s)' % get_random_string()
507 # Save the changes up to DocumentCloud with the alias
508 obj.title = title
509 obj.save()
510 # Pull the object again and verify the changes stuck
511 obj = self.private_client.projects.get("703")
512 self.assertEqual(obj.title, title)
513
b095fe5 @palewire Added some catches that I hope will help ensure dupes and non-Documen…
palewire authored
514 def test_document_list_type_restrictions(self):
515 """
516 Make sure document_lists will only accept Document objects
517 """
518 obj = self.private_client.projects.get("703")
519 self.assertRaises(TypeError, obj.document_list.append, "The letter C")
0070b99 @palewire Added a CREATE method for Projects and a test that passes. Fixes #30
palewire authored
520
155ee61 @palewire Wrote a DELETE method for Projects and a test that passes. Fixes #19
palewire authored
521 def test_create_and_delete(self):
0070b99 @palewire Added a CREATE method for Projects and a test that passes. Fixes #30
palewire authored
522 """
523 Test whether you can create a new project.
524 """
155ee61 @palewire Wrote a DELETE method for Projects and a test that passes. Fixes #19
palewire authored
525 # Create it
526 title = "00 - (%s) - This is only a test" % get_random_string()
f79ba76 @palewire Added some unittests that verify you can successfully add documents t…
palewire authored
527 doc = self.private_client.documents.get("15144-mitchrpt")
528 proj = self.private_client.projects.create(
529 title,
530 description='Blah blah',
531 document_ids=[doc.id]
532 )
0070b99 @palewire Added a CREATE method for Projects and a test that passes. Fixes #30
palewire authored
533 self.assertEqual(type(proj), Project)
f79ba76 @palewire Added some unittests that verify you can successfully add documents t…
palewire authored
534 self.assertEqual(proj.title, title)
535 self.assertEqual(proj.description, 'Blah blah')
536 self.assertEqual(proj.document_list[0].id, doc.id)
155ee61 @palewire Wrote a DELETE method for Projects and a test that passes. Fixes #19
palewire authored
537 # Delete it
538 proj.delete()
a4fef64 @palewire Projects can now be pulled by id or by title. And when you create a p…
palewire authored
539 self.assertRaises(DoesNotExistError, self.private_client.projects.get, proj.id)
005280a @palewire added a get_or_create_by_title method to the Project class that makes…
palewire authored
540
541 def test_get_or_create(self):
542 """
543 Test whether get_or_create methods are working.
544 """
545 # Create it
546 title = "00 - (%s) - This is only a test" % get_random_string()
547 proj, c = self.private_client.projects.get_or_create_by_title(title)
548 self.assertEqual(type(proj), Project)
549 self.assertEqual(c, True)
550 # Get it
551 proj, c = self.private_client.projects.get_or_create_by_title(title)
552 self.assertEqual(type(proj), Project)
553 self.assertEqual(c, False)
554 # Delete it
555 proj.delete()
556 self.assertRaises(DoesNotExistError, self.private_client.projects.get, proj.id)
97dc208 @palewire Added a method for pulling all the projects belonging to an authorize…
palewire authored
557
558
abcb0e7 @palewire Added test for CredentialsMissingError and fixed some bugs with the o…
palewire authored
559 class ErrorTest(BaseTest):
560
561 def test_missing_credentials(self):
562 """
563 Make sure CredentialsMissingError works.
564 """
565 self.assertRaises(CredentialsMissingError, self.public_client.projects.all)
566
3a98091 @palewire Added test for CredentialsFailedError. Closes #13.
palewire authored
567 def test_failed_credentials(self):
568 """
569 Make sure CredentialsFailedError works.
570 """
571 self.assertRaises(CredentialsFailedError, self.public_client.fetch, "projects.json")
572
847cd29 @palewire Added test for DoesNotExist error. Fixes #12.
palewire authored
573 def test_does_not_exist(self):
574 """
575 Make sure DoesNotExistError works.
576 """
577 self.assertRaises(DoesNotExistError, self.public_client.documents.get, 'TK')
b095fe5 @palewire Added some catches that I hope will help ensure dupes and non-Documen…
palewire authored
578
579 def test_duplicate_object(self):
580 """
581 Make sure DuplicateObjectError works.
582 """
583 obj = self.private_client.projects.get("703")
3801369 @palewire Added a test for the upload that passes. Fixes #29
palewire authored
584 doc = self.private_client.documents.get(u'12666-the-klee-report-volume-1')
b095fe5 @palewire Added some catches that I hope will help ensure dupes and non-Documen…
palewire authored
585 self.assertRaises(DuplicateObjectError, obj.document_list.append, doc)
abcb0e7 @palewire Added test for CredentialsMissingError and fixed some bugs with the o…
palewire authored
586
2fab3fb @palewire Whitespace fascism
palewire authored
587
051b62f @palewire Started up a basic test.py file
palewire authored
588 if __name__ == '__main__':
589 unittest.main()
Something went wrong with that request. Please try again.