forked from datadesk/python-documentcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
523 lines (473 loc) · 19 KB
/
test.py
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Tests out the DocumentCloud API.
Most requests require authentication, which I'm not sure how deal with properly
in this circumstance. For the time being, I'm importing latimes.com credentials
and fiddling around with junk files I've placed in there. Obviously, that means
this test suite will only work on my computer, which seems like a problem.
If you know how I ought to sort this sort of thing out, please let me know.
"""
import os
import random
import string
import textwrap
import unittest
import StringIO
from copy import copy
from documentcloud import DocumentCloud
from documentcloud import CredentialsMissingError, DuplicateObjectError
from documentcloud import CredentialsFailedError, DoesNotExistError
from documentcloud import Annotation, Document, Project, Section, Entity, Mention
from private_settings import DOCUMENTCLOUD_USERNAME, DOCUMENTCLOUD_PASSWORD
#
# Odds and ends
#
def get_random_string(length=6):
"""
Generate a random string of letters and numbers
"""
return ''.join(random.choice(string.letters + string.digits) for i in xrange(length))
#
# Tests
#
class BaseTest(unittest.TestCase):
def setUp(self):
self.test_search = 'Calpers special review'
self.test_id = '74103-report-of-the-calpers-special-review'
self.public_client = DocumentCloud()
self.private_client = DocumentCloud(DOCUMENTCLOUD_USERNAME, DOCUMENTCLOUD_PASSWORD)
self.fake_client = DocumentCloud("John Doe", "TK")
class DocumentSearchTest(BaseTest):
def test_search(self):
"""
Test a search.
"""
obj_list = self.public_client.documents.search(self.test_search)
self.assertEqual(type(obj_list), type([]))
self.assertEqual(type(obj_list[0]), Document)
# def test_multipage_search(self):
# """
# Test a search that will return more than a single page of results.
# """
# obj_list = documentcloud.documents.search("johnson")
# self.assertTrue(len(obj_list) > 1000)
def test_search_attrs(self):
"""
Verify that all the Document attributes exist.
"""
obj = self.public_client.documents.search(self.test_search)[0]
attr_list = [
'access',
'annotations',
'canonical_url',
'contributor',
'contributor_organization',
'created_at',
'description',
'id',
'pages',
'resources',
'sections',
'source',
'title',
'updated_at',
'data',
]
for attr in attr_list:
self.assertTrue(hasattr(obj, attr))
def test_search_annotations(self):
"""
Test whether annotations exist.
"""
obj = self.public_client.documents.search(self.test_search)[0]
self.assertEqual(type(obj.annotations[0]), Annotation)
def test_search_sections(self):
"""
Test whether sections exist.
"""
obj = self.public_client.documents.get(self.test_id)
self.assertEqual(type(obj.sections[0]), Section)
def test_search_entities(self):
"""
Test whether entities exist.
"""
obj = self.public_client.documents.get(self.test_id)
self.assertEqual(type(obj.entities[0]), Entity)
def test_get(self):
"""
Test a get request for a particular document.
"""
obj = self.public_client.documents.get(self.test_id)
self.assertEqual(type(obj), Document)
def test_get_attrs(self):
"""
Verify that all the Document attributes exist.
"""
obj = self.public_client.documents.get(self.test_id)
attr_list = [
'access',
'annotations',
'canonical_url',
'contributor',
'contributor_organization',
'created_at',
'description',
'id',
'pages',
'resources',
'sections',
'source',
'title',
'updated_at',
'data',
]
[self.assertTrue(hasattr(obj, attr)) for attr in attr_list]
def test_get_annotations(self):
"""
Test whether annotations exist.
"""
obj = self.public_client.documents.get(self.test_id)
self.assertEqual(type(obj.annotations[0]), Annotation)
def test_get_sections(self):
"""
Test whether sections exist.
"""
obj = self.public_client.documents.get(self.test_id)
self.assertEqual(type(obj.sections[0]), Section)
def test_get_entities(self):
"""
Test whether entities exist.
"""
obj = self.public_client.documents.get(self.test_id)
self.assertEqual(type(obj.entities[0]), Entity)
def test_get_mentions(self):
"""
Test whether mentions exist.
"""
obj = self.public_client.documents.search(self.test_search)[0]
self.assertEqual(type(obj.mentions[0]), Mention)
def test_set_data_type_restrictions(self):
"""
Make sure `data` attribute will only accept a dictionary.
"""
obj = self.private_client.documents.get(self.test_id)
obj.data = dict(foo='bar')
self.assertRaises(TypeError, obj.set_data, "string")
self.assertRaises(TypeError, obj.set_data, 666)
self.assertRaises(TypeError, obj.set_data, obj)
def test_get_put(self):
"""
Test whether we can put random noise to all the editable fields.
"""
# Pull the object we'll deface
obj = self.private_client.documents.get("15144-mitchrpt")
# Create random strings we will save to the editable attributes
title = 'The Mitchell Report (%s)' % get_random_string()
source = 'DLA Piper (%s)' % get_random_string()
description = get_random_string()
data = {get_random_string(): get_random_string()}
if obj.resources.related_article == 'http://documents.latimes.com':
related_article = 'http://documentcloud.org'
else:
related_article = 'http://documents.latimes.com'
if obj.resources.published_url == 'http://documents.latimes.com':
published_url = 'http://documentcloud.org'
else:
published_url = 'http://documents.latimes.com'
# Set the random strings our local object's attributes
obj.title = title
obj.source = source
obj.description = description
obj.data = data
obj.resources.related_article = related_article
obj.resources.published_url = published_url
# Save the changes up to DocumentCloud
obj.put()
# Pull the object again and verify the changes stuck
obj = self.private_client.documents.get("15144-mitchrpt")
self.assertEqual(obj.title, title)
self.assertEqual(obj.source, source)
self.assertEqual(obj.description, description)
self.assertEqual(obj.data, data)
self.assertEqual(obj.resources.related_article, related_article)
self.assertEqual(obj.resources.published_url, published_url)
def test_save(self):
"""
Test whether the save method properly aliases `put`.
"""
# Pull the object we'll deface
obj = self.private_client.documents.get("15144-mitchrpt")
# Create random strings we will save to the editable attributes
title = 'The Mitchell Report (%s)' % get_random_string()
obj.title = title
# Save the changes up to DocumentCloud with the alias
obj.save()
# Pull the object again and verify the changes stuck
obj = self.private_client.documents.get("15144-mitchrpt")
self.assertEqual(obj.title, title)
def test_put_with_weird_encoding(self):
"""
Test whether you can save an attribute with some weird encoding
in the title.
"""
# Pull the object we'll deface
obj = self.private_client.documents.get("15144-mitchrpt")
before = copy(obj.title)
# Add something weird to the title and save it
after = copy(obj.title + u'’')
obj.title = after
obj.put()
self.assertEqual(obj.title, after)
# Switch it back
obj.title = before
obj.put()
self.assertEqual(obj.title, before)
def test_upload_and_delete(self):
"""
Makes sure you can create and delete a document.
"""
# Create it
title = '001 - Test upload (%s)' % get_random_string()
m_file = open(os.path.join(os.path.dirname(__file__), "test.pdf"), 'rb')
obj = self.private_client.documents.upload(m_file,
title,
description='Blah blah',
related_article='http://www.latimes.com',
data=dict(like='this', boom='bap'),
)
self.assertEqual(type(obj), Document)
self.assertEqual(obj.description, 'Blah blah')
self.assertEqual(obj.related_article, 'http://www.latimes.com')
self.assertEqual(obj.data, {'like': 'this', 'boom': 'bap'})
# Delete it
obj.delete()
self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
def test_secure_upload_and_delete(self):
"""
Make sure you can create and delete a document using the secure
parameter that hides your data from OpenCalais.
Currently I don't know a way to test whether the parameter is properly
applied. It seems to work in the UI, but, as far as I know, the API
doesn't return an indicator that I have figured out how to test.
"""
# Create it
title = '001 - Test upload (%s)' % get_random_string()
obj = self.private_client.documents.upload(
open(os.path.join(os.path.dirname(__file__), "test.pdf"), 'rb'),
title,
secure=True,
)
self.assertEqual(type(obj), Document)
# Delete it
obj.delete()
self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
def test_unicode_upload_and_delete(self):
"""
Ensure that documents with non-english characters can be uploaded
"""
title = 'Espanola!'
obj = self.private_client.documents.upload(
open(os.path.join(os.path.dirname(__file__), "espanol.pdf"), 'rb'),
title,
secure=True,
)
self.assertEqual(type(obj), Document)
# Delete it
obj.delete()
self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
def test_virtual_file_upload_and_delete(self):
"""
Proxy test case for files stored in memory, for instance, django-storages
these tests are difficult to create as the class used to represent a file
object is determined at runtime by the DEFAULT_FILE_STORAGE var (django)
anyway, the main point is to show the MultipartPostHandler can handle unicode
"""
real_file = open(os.path.join(os.path.dirname(__file__), "espanol.pdf"), 'rb')
virtual_file = StringIO.StringIO(file.read())
obj = self.private_client.documents.upload(pdf=file, title='Espanola!',\
secure=True)
self.assertEqual(type(obj), Document)
# Delete it
obj.delete()
self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
def test_upload_directory(self):
"""
Makes sure you can upload all the pdfs in a directory.
"""
# Upload everything in this directory.
obj_list = self.private_client.documents.upload_directory('./',
source='Los Angeles Times',
published_url='http://www.latimes.com',
)
# Which should only be one document
self.assertEqual(len(obj_list), 1)
self.assertEqual(type(obj_list[0]), Document)
self.assertEqual(obj_list[0].source, 'Los Angeles Times')
self.assertEqual(obj_list[0].published_url, 'http://www.latimes.com')
# And which we should be able to delete
obj = obj_list[0]
obj.delete()
self.assertRaises(DoesNotExistError, self.private_client.documents.get, obj.id)
def test_resources(self):
"""
Makes sure the canonical url is a top-level attribute on the Document.
"""
obj = self.public_client.documents.get(self.test_id)
# Test that they come out the same
self.assertEqual(obj.published_url, obj.resources.published_url)
self.assertEqual(obj.related_article, obj.resources.related_article)
# Then test that they setattr the same
obj.published_url = 'http://latimes.com'
obj.related_article = 'http://palewire.com'
self.assertEqual(obj.published_url, obj.resources.published_url)
self.assertEqual(obj.related_article, obj.resources.related_article)
class ProjectTest(BaseTest):
def test_all(self):
"""
Test an `all` request for a list of all projects belong to an
authorized user.
"""
obj_list = self.private_client.projects.all()
self.assertEqual(type(obj_list), type([]))
self.assertEqual(type(obj_list[0]), Project)
def test_get(self):
"""
Test a `get` methods for a particular project
"""
obj = self.private_client.projects.get('934')
self.assertEqual(type(obj), Project)
obj2 = self.private_client.projects.get_by_id('934')
self.assertEqual(obj.id, obj2.id)
obj3 = self.private_client.projects.get_by_title(obj2.title)
self.assertEqual(obj2.id, obj3.id)
def test_document_list(self):
"""
Verify that a project can pull back all if its associated documents.
"""
obj = self.private_client.projects.get('934')
doc_list = obj.document_list
self.assertEqual(type(doc_list[0]), Document)
def test_get_document(self):
"""
Verify that a project can pull a particular document by id
"""
obj = self.private_client.projects.get('934')
doc = obj.get_document(u'25798-pr-01092011-loughner')
self.assertEqual(type(doc), Document)
def test_put(self):
"""
Test whether we can put random noise to all the editable fields.
"""
# Pull the object we'll deface
obj = self.private_client.projects.get("703")
# Create random strings we will save to the editable attributes
title = 'The Klee Report (%s)' % get_random_string()
description = textwrap.dedent("""
An independent probe into Sam Zell\'s purchase of Tribune Company by
investigator Kenneth Klee. Released at the end of July 2010. (%s)
""")
description = description % get_random_string()
# Set the random strings our local object's attributes
# and zero out the document list.
obj.title = title
obj.description = description
obj.document_list = []
# Save the changes up to DocumentCloud
obj.put()
# Pull the object again and verify the changes stuck
obj = self.private_client.projects.get("703")
self.assertEqual(obj.title, title)
self.assertEqual(obj.description, description)
self.assertEqual(len(obj.document_list), 0)
# Now add all the documents back in
proj_ids = [
u'12667-the-klee-report-volume-2',
u'12666-the-klee-report-volume-1'
]
for id in proj_ids:
doc = self.private_client.documents.get(id)
obj.document_list.append(doc)
obj.put()
obj = self.private_client.projects.get("703")
self.assertEqual(len(obj.document_list), len(proj_ids))
def test_save(self):
"""
Test whether the save method properly aliases `put`.
"""
# Pull the object we'll deface
obj = self.private_client.projects.get("703")
# Create random strings we will save to the editable attributes
title = 'The Klee Report (%s)' % get_random_string()
# Save the changes up to DocumentCloud with the alias
obj.title = title
obj.save()
# Pull the object again and verify the changes stuck
obj = self.private_client.projects.get("703")
self.assertEqual(obj.title, title)
def test_document_list_type_restrictions(self):
"""
Make sure document_lists will only accept Document objects
"""
obj = self.private_client.projects.get("703")
self.assertRaises(TypeError, obj.document_list.append, "The letter C")
def test_create_and_delete(self):
"""
Test whether you can create a new project.
"""
# Create it
title = "00 - (%s) - This is only a test" % get_random_string()
doc = self.private_client.documents.get("15144-mitchrpt")
proj = self.private_client.projects.create(
title,
description='Blah blah',
document_ids=[doc.id]
)
self.assertEqual(type(proj), Project)
self.assertEqual(proj.title, title)
self.assertEqual(proj.description, 'Blah blah')
self.assertEqual(proj.document_list[0].id, doc.id)
# Delete it
proj.delete()
self.assertRaises(DoesNotExistError, self.private_client.projects.get, proj.id)
def test_get_or_create(self):
"""
Test whether get_or_create methods are working.
"""
# Create it
title = "00 - (%s) - This is only a test" % get_random_string()
proj, c = self.private_client.projects.get_or_create_by_title(title)
self.assertEqual(type(proj), Project)
self.assertEqual(c, True)
# Get it
proj, c = self.private_client.projects.get_or_create_by_title(title)
self.assertEqual(type(proj), Project)
self.assertEqual(c, False)
# Delete it
proj.delete()
self.assertRaises(DoesNotExistError, self.private_client.projects.get, proj.id)
class ErrorTest(BaseTest):
def test_missing_credentials(self):
"""
Make sure CredentialsMissingError works.
"""
self.assertRaises(CredentialsMissingError, self.public_client.projects.all)
def test_failed_credentials(self):
"""
Make sure CredentialsFailedError works.
"""
self.assertRaises(CredentialsFailedError, self.public_client.fetch, "projects.json")
def test_does_not_exist(self):
"""
Make sure DoesNotExistError works.
"""
self.assertRaises(DoesNotExistError, self.public_client.documents.get, 'TK')
def test_duplicate_object(self):
"""
Make sure DuplicateObjectError works.
"""
obj = self.private_client.projects.get("703")
doc = self.private_client.documents.get(u'12666-the-klee-report-volume-1')
self.assertRaises(DuplicateObjectError, obj.document_list.append, doc)
if __name__ == '__main__':
unittest.main()