@@ -121,7 +121,6 @@ def setUp(self):
121121 )
122122 self .fake_client = DocumentCloud ("John Doe" , "TK" )
123123 self .version = self .get_version ()
124- # self.editable_project = self.get_editable_project(self.version)
125124
126125
127126class SearchTest (BaseTest ):
@@ -186,7 +185,7 @@ def test_search(self):
186185
187186
188187class DocumentTest (BaseTest ):
189- """"
188+ """
190189 Document object related tests.
191190 """
192191 def test_public_actions (self ):
@@ -328,8 +327,8 @@ def test_private_actions(self):
328327 # Test whether you can save an attribute with some weird encoding
329328 before_title = copy (obj .title )
330329 before_description = copy (obj .description )
331- obj .title = random .choice (PANGRAMS .keys ())
332- obj .description = random .choice (PANGRAMS .keys ())
330+ obj .title = random .choice (list ( PANGRAMS .keys () ))
331+ obj .description = random .choice (list ( PANGRAMS .keys () ))
333332 obj .put ()
334333 obj .title = before_title
335334 obj .description = before_description
@@ -423,170 +422,181 @@ def test_private_actions(self):
423422 [i .delete () for i in obj_list ]
424423
425424
426- #class ProjectTest(BaseTest):
427-
428- # def test_all(self):
429- # """
430- # Test an `all` request for a list of all projects belong to an
431- # authorized user.
432- # """
433- # obj_list = self.private_client.projects.all()
434- # self.assertEqual(type(obj_list), type([]))
435- # self.assertEqual(type(obj_list[0]), Project)
436-
437- # def test_get(self):
438- # """
439- # Test a `get` methods for a particular project
440- # """
441- # obj = self.private_client.projects.get('934')
442- # self.assertEqual(type(obj), Project)
443- # obj2 = self.private_client.projects.get_by_id('934')
444- # self.assertEqual(obj.id, obj2.id)
445- # obj3 = self.private_client.projects.get_by_title(obj2.title)
446- # self.assertEqual(obj2.id, obj3.id)
447- # obj.__str__()
448- # obj.__unicode__()
449-
450- # def test_document_list(self):
451- # """
452- # Verify that a project can pull back all if its associated documents.
453- # """
454- # obj = self.private_client.projects.get('934')
455- # doc_list = obj.document_list
456- # self.assertEqual(type(doc_list[0]), Document)
457-
458- # def test_get_document(self):
459- # """
460- # Verify that a project can pull a particular document by id
461- # """
462- # obj = self.private_client.projects.get('934')
463- # doc = obj.get_document('25798-pr-01092011-loughner')
464- # self.assertEqual(type(doc), Document)
465-
466- # def test_put(self):
467- # """
468- # Test whether we can put random noise to all the editable fields.
469- # """
470- # # Pull the object we'll deface
471- # obj = self.private_client.projects.get(self.editable_project)
472- # # Create random strings we will save to the editable attributes
473- # title = 'The Klee Report (%s)' % get_random_string()
474- # description = textwrap.dedent("""
475- # An independent probe into Sam Zell\'s purchase of Tribune Company by
476- # investigator Kenneth Klee. Released at the end of July 2010. (%s)
477- # """)
478- # description = description % get_random_string()
479- # # Set the random strings our local object's attributes
480- # # and zero out the document list.
481- # obj.title = title
482- # obj.description = description
483- # obj.document_list = []
484- # # Save the changes up to DocumentCloud
485- # obj.put()
486- # # Pull the object again and verify the changes stuck
487- # obj = self.private_client.projects.get(self.editable_project)
488- # self.assertEqual(obj.title, title)
489- # self.assertEqual(obj.description, description)
490- # self.assertEqual(len(obj.document_list), 0)
491- # # Now add all the documents back in
492- # proj_ids = [
493- # '12667-the-klee-report-volume-2',
494- # '12666-the-klee-report-volume-1'
495- # ]
496- # for id in proj_ids:
497- # doc = self.private_client.documents.get(id)
498- # obj.document_list.append(doc)
499- # obj.put()
500- # obj = self.private_client.projects.get(self.editable_project)
501- # self.assertEqual(len(obj.document_list), len(proj_ids))
502-
503- # def test_save(self):
504- # """
505- # Test whether the save method properly aliases `put`.
506- # """
507- # # Pull the object we'll deface
508- # obj = self.private_client.projects.get(self.editable_project)
509- # # Create random strings we will save to the editable attributes
510- # title = get_random_string()
511- # # Save the changes up to DocumentCloud with the alias
512- # obj.title = title
513- # obj.save()
514- # # Pull the object again and verify the changes stuck
515- # obj = self.private_client.projects.get(self.editable_project)
516- # self.assertEqual(obj.title, title)
517-
518- # def test_document_list_type_restrictions(self):
519- # """
520- # Make sure document_lists will only accept Document objects
521- # """
522- # obj = self.private_client.projects.get(self.editable_project)
523- # self.assertRaises(TypeError, obj.document_list.append, "The letter C")
524-
525- # def test_create_and_delete(self):
526- # """
527- # Test whether you can create a new project.
528- # """
529- # # Create it
530- # title = get_random_string()
531- # doc = self.private_client.documents.get(self.editable_document)
532- # proj = self.private_client.projects.create(
533- # title,
534- # description='Blah blah',
535- # document_ids=[doc.id]
536- # )
537- # self.assertEqual(type(proj), Project)
538- # self.assertEqual(proj.title, title)
539- # self.assertEqual(proj.description, 'Blah blah')
540- # self.assertEqual(proj.document_list[0].id, doc.id)
541- # # Delete it
542- # proj.delete()
543- # self.assertRaises(DoesNotExistError, self.private_client.projects.get, proj.id)
544-
545- # def test_get_or_create(self):
546- # """
547- # Test whether get_or_create methods are working.
548- # """
549- # # Create it
550- # title = get_random_string()
551- # proj, c = self.private_client.projects.get_or_create_by_title(title)
552- # self.assertEqual(type(proj), Project)
553- # self.assertEqual(c, True)
554- # # Get it
555- # proj, c = self.private_client.projects.get_or_create_by_title(title)
556- # self.assertEqual(type(proj), Project)
557- # self.assertEqual(c, False)
558- # # Delete it
559- # proj.delete()
560- # self.assertRaises(DoesNotExistError, self.private_client.projects.get, proj.id)
561-
562-
563- #class ErrorTest(BaseTest):
564-
565- # def test_missing_credentials(self):
566- # """
567- # Make sure CredentialsMissingError works.
568- # """
569- # self.assertRaises(CredentialsMissingError, self.public_client.projects.all)
570-
571- # def test_failed_credentials(self):
572- # """
573- # Make sure CredentialsFailedError works.
574- # """
575- # self.assertRaises(CredentialsFailedError, self.public_client.fetch, "projects.json")
576-
577- # def test_does_not_exist(self):
578- # """
579- # Make sure DoesNotExistError works.
580- # """
581- # self.assertRaises(DoesNotExistError, self.public_client.documents.get, 'TK')
582-
583- # def test_duplicate_object(self):
584- # """
585- # Make sure DuplicateObjectError works.
586- # """
587- # obj = self.private_client.projects.get("703")
588- # doc = self.private_client.documents.get('12666-the-klee-report-volume-1')
589- # self.assertRaises(DuplicateObjectError, obj.document_list.append, doc)
425+ class ProjectTest (BaseTest ):
426+ """
427+ Project object related tests.
428+ """
429+ def test_all (self ):
430+ """
431+ Test an `all` request for a list of all projects belong to an
432+ authorized user.
433+ """
434+ obj_list = self .private_client .projects .all ()
435+ self .assertEqual (type (obj_list ), type ([]))
436+ self .assertEqual (type (obj_list [0 ]), Project )
437+
438+ def test_get (self ):
439+ """
440+ Test ability to get a project and look at it.
441+ """
442+ # Make sure all the different pull commands return the same thing
443+ obj = self .private_client .projects .get ('934' )
444+ self .assertTrue (isinstance (obj , Project ))
445+
446+ obj2 = self .private_client .projects .get_by_id ('934' )
447+ self .assertEqual (obj .id , obj2 .id )
448+
449+ obj3 = self .private_client .projects .get_by_title (obj2 .title )
450+ self .assertEqual (obj2 .id , obj3 .id )
451+
452+ # Test other attributes
453+ obj .__str__ ()
454+ obj .__unicode__ ()
455+
456+ # Document list
457+ doc_list = obj .document_list
458+ self .assertTrue (isinstance (doc_list [0 ], Document ))
459+
460+ # Pull a document
461+ doc = obj .get_document ('25798-pr-01092011-loughner' )
462+ self .assertTrue (isinstance (doc , Document ))
463+
464+ def test_put (self ):
465+ """
466+ Test whether we can put random noise to all the editable fields.
467+ """
468+ # Pull the project
469+ self .editable_project = self .get_editable_project (self .version )
470+ obj = self .private_client .projects .get (self .editable_project )
471+
472+ # Create random strings we will save to the editable attributes
473+ title = 'The Klee Report (%s)' % get_random_string ()
474+ description = textwrap .dedent ("""
475+ An independent probe into Sam Zell\' s purchase of Tribune Company by
476+ investigator Kenneth Klee. Released at the end of July 2010. (%s)
477+ """ )
478+ description = description % get_random_string ()
479+ # Set the random strings our local object's attributes
480+ # and zero out the document list.
481+ obj .title = title
482+ obj .description = description
483+ obj .document_list = []
484+ # Save the changes up to DocumentCloud
485+ obj .put ()
486+
487+ # Pull the object again and verify the changes stuck
488+ obj = self .private_client .projects .get (self .editable_project )
489+ self .assertEqual (obj .title , title )
490+ self .assertEqual (obj .description , description )
491+ self .assertEqual (len (obj .document_list ), 0 )
492+ # Now add all the documents back in
493+ proj_ids = [
494+ '12667-the-klee-report-volume-2' ,
495+ '12666-the-klee-report-volume-1'
496+ ]
497+ for id in proj_ids :
498+ doc = self .private_client .documents .get (id )
499+ obj .document_list .append (doc )
500+ obj .put ()
501+ obj = self .private_client .projects .get (self .editable_project )
502+ self .assertEqual (len (obj .document_list ), len (proj_ids ))
503+
504+ # Verify that the save alias to put works
505+ title = get_random_string ()
506+ obj .title = title
507+ obj .save ()
508+ obj = self .private_client .projects .get (self .editable_project )
509+ self .assertEqual (obj .title , title )
510+
511+ # Make sure document_lists will only accept Document objects
512+ obj = self .private_client .projects .get (self .editable_project )
513+ self .assertRaises (TypeError , obj .document_list .append , "The letter C" )
514+
515+ def test_create_and_delete (self ):
516+ """
517+ Test whether you can create a new project.
518+ """
519+ # Create it
520+ title = get_random_string ()
521+ doc_id = self .get_editable_document (self .version )
522+ doc = self .private_client .documents .get (doc_id )
523+ proj = self .private_client .projects .create (
524+ title ,
525+ description = 'Blah blah' ,
526+ document_ids = [doc .id ]
527+ )
528+ self .assertTrue (isinstance (proj , Project ))
529+ self .assertEqual (proj .title , title )
530+ self .assertEqual (proj .description , 'Blah blah' )
531+ self .assertEqual (proj .document_list [0 ].id , doc .id )
532+
533+ # Delete it
534+ proj .delete ()
535+ self .assertRaises (
536+ DoesNotExistError ,
537+ self .private_client .projects .get ,\
538+ proj .id
539+ )
540+
541+ def test_get_or_create (self ):
542+ """
543+ Test whether get_or_create methods are working.
544+ """
545+ # Create it
546+ title = get_random_string ()
547+ proj , c = self .private_client .projects .get_or_create_by_title (title )
548+ self .assertTrue (isinstance (proj , Project ))
549+ self .assertTrue (c )
550+ # Get it
551+ proj , c = self .private_client .projects .get_or_create_by_title (title )
552+ self .assertTrue (isinstance (proj , Project ))
553+ self .assertFalse (c )
554+ # Delete it
555+ proj .delete ()
556+
557+
558+ class ErrorTest (BaseTest ):
559+ """
560+ Test a lot of the errors.
561+ """
562+ def test_missing_credentials (self ):
563+ """
564+ Make sure CredentialsMissingError works.
565+ """
566+ self .assertRaises (
567+ CredentialsMissingError ,
568+ self .public_client .projects .all
569+ )
570+
571+ def test_failed_credentials (self ):
572+ """
573+ Make sure CredentialsFailedError works.
574+ """
575+ self .assertRaises (
576+ CredentialsFailedError ,
577+ self .public_client .fetch ,
578+ "projects.json"
579+ )
580+
581+ def test_does_not_exist (self ):
582+ """
583+ Make sure DoesNotExistError works.
584+ """
585+ self .assertRaises (
586+ DoesNotExistError ,
587+ self .public_client .documents .get ,
588+ 'TK'
589+ )
590+
591+ def test_duplicate_object (self ):
592+ """
593+ Make sure DuplicateObjectError works.
594+ """
595+ obj = self .private_client .projects .get ("703" )
596+ doc = self .private_client .documents .get (
597+ '12666-the-klee-report-volume-1'
598+ )
599+ self .assertRaises (DuplicateObjectError , obj .document_list .append , doc )
590600
591601
592602if __name__ == '__main__' :
0 commit comments