def testTooManyItems(self): """ Should fail because we're passing too many items """ itms = [i for i in xrange(51)] zot = z.Zotero('myuserID', 'user', 'myuserkey') with self.assertRaises(z.ze.TooManyItems): zot.create_items(itms)
def testParseAttachmentsAtomDoc(self): """" blah """ zot = z.Zotero('myuserid', 'users', 'myuserkey') my_opener = urllib2.build_opener(MyHTTPSHandler(self.attachments_doc)) z.urllib2.install_opener(my_opener) attachments_data = zot.items() self.assertEqual(u'1641 Depositions', attachments_data[0]['title'])
def testEtagsParsing(self): """ Tests item and item update response etag parsing """ zot = z.Zotero('myuserID', 'users', 'myuserkey') self.assertEqual(z.etags(self.created_response), ['1ed002db69174ae2ae0e3b90499df15e']) self.assertEqual(z.etags(self.items_doc), ['7252daf2495feb8ec89c61f391bcba24'])
def testRequestBuilder(self): """ Should add the user key, then url-encode all other added parameters """ zot = z.Zotero('myuserID', 'user', 'myuserkey') zot.add_parameters(limit=0, start=7) self.assertEqual('content=json&start=7&limit=0&key=myuserkey', zot.url_params)
def testCreateItem(self): """ Ensure that items can be created """ # first, retrieve an item template my_opener = urllib2.build_opener(MyHTTPSHandler(self.item_templt, 200)) z.urllib2.install_opener(my_opener) zot = z.Zotero('myuserID', 'users', 'myuserkey') t = zot.item_template('book') # Update the item type t['itemType'] = 'journalArticle' # Add keys which should be removed before the data is sent t['key'] = 'KEYABC123' t['etag'] = 'TAGABC123' t['group_id'] = 'GROUPABC123' t['updated'] = '14 March, 2011' tn = dict(t) tn['key'] = 'KEYABC124' ls = [] ls.append(t) ls.append(tn) # new opener which will return 403 my_opener = urllib2.build_opener(MyHTTPSHandler(self.items_doc, 403)) z.urllib2.install_opener(my_opener) with self.assertRaises(z.ze.UserNotAuthorised) as e: _ = zot.create_items(ls) exc = str(e.exception) # this test is a kludge; we're checking the POST data in the 403 response self.assertIn("journalArticle", exc) self.assertNotIn("KEYABC123", exc) self.assertNotIn("TAGABC123", exc) self.assertNotIn("GROUPABC123", exc)
def testCreateCollectionError(self): """ Ensure that collection creation fails with the wrong dict """ zot = z.Zotero('myuserID', 'user', 'myuserkey') t = {'foo': 'bar'} with self.assertRaises(z.ze.ParamNotPassed): t = zot.create_collection(t)
def testParseKeysResponse(self): """ Check that parsing plain keys returned by format = keys works """ zot = z.Zotero('myuserid', 'users', 'myuserkey') zot.url_params = 'format=keys' my_opener = urllib2.build_opener(MyHTTPSHandler(self.keys_response)) z.urllib2.install_opener(my_opener) response = zot.items() self.assertEqual('ABCDE\nFGHIJ\nKLMNO\n', response)
def testGetItems(self): """ Ensure that we can retrieve a list of all items """ my_opener = urllib2.build_opener(MyHTTPSHandler(self.item_types, 200)) z.urllib2.install_opener(my_opener) zot = z.Zotero('myuserID', 'users', 'myuserkey') t = zot.item_types() self.assertEqual(t[0]['itemType'], 'artwork') self.assertEqual(t[-1]['itemType'], 'webpage')
def testResponseMiscError(self): """ Ensure that an error is properly raised for unspecified errors """ my_opener = urllib2.build_opener(MyHTTPSHandler(self.groups_doc, 500)) z.urllib2.install_opener(my_opener) zot = z.Zotero('myuserID', 'users', 'myuserkey') with self.assertRaises(z.ze.HTTPError): zot.items()
def testResponseNotFound(self): """ Ensure that an error is properly raised for 404 """ my_opener = urllib2.build_opener(MyHTTPSHandler(self.groups_doc, 404)) z.urllib2.install_opener(my_opener) zot = z.Zotero('myuserID', 'users', 'myuserkey') with self.assertRaises(z.ze.ResourceNotFound): zot.items()
def testResponseUnsupported(self): """ Ensure that an error is properly raised for 400 """ my_opener = urllib2.build_opener(MyHTTPSHandler(self.groups_doc, 400)) z.urllib2.install_opener(my_opener) zot = z.Zotero('myuserID', 'users', 'myuserkey') with self.assertRaises(z.ze.UnsupportedParams): zot.items()
def testResponseForbidden(self): """ Ensure that an error is properly raised for 403 """ my_opener = urllib2.build_opener(MyHTTPSHandler(self.groups_doc, 403)) z.urllib2.install_opener(my_opener) zot = z.Zotero('myuserID', 'users', 'myuserkey') with self.assertRaises(z.ze.UserNotAuthorised): zot.items()
def testParseTagsAtomDoc(self): """ Should successfully return a list of tags """ my_opener = urllib2.build_opener(MyHTTPSHandler(self.tags_doc)) z.urllib2.install_opener(my_opener) zot = z.Zotero('myuserID', 'users', 'myuserkey') tags_data = zot.tags() self.assertEqual('Authority in literature', tags_data[0])
def testGetTemplate(self): """ Ensure that item templates are retrieved and converted into dicts """ my_opener = urllib2.build_opener(MyHTTPSHandler(self.item_templt, 200)) z.urllib2.install_opener(my_opener) zot = z.Zotero('myuserID', 'users', 'myuserkey') t = zot.item_template('book') self.assertEqual('book', t['itemType'])
def testGetItems(self): """ Ensure that we can retrieve a list of all items """ zot = z.Zotero('myuserID', 'user', 'myuserkey') HTTPretty.register_uri(HTTPretty.GET, 'https://api.zotero.org/itemTypes', body=self.item_types) t = zot.item_types() self.assertEqual(t[0]['itemType'], 'artwork') self.assertEqual(t[-1]['itemType'], 'webpage')
def testParamsReset(self): """ Should successfully reset URL parameters after a query string is built """ zot = z.Zotero('myuserID', 'user', 'myuserkey') zot.add_parameters(start=5, limit=10) zot._build_query('/whatever') zot.add_parameters(start=2) self.assertEqual('content=json&start=2&key=myuserkey', zot.url_params)
def testParseChildItems(self): """ Try and parse child items """ zot = z.Zotero('myuserID', 'user', 'myuserkey') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserID/items/ABC123/children?content=json&key=myuserkey', body=self.items_doc) items_data = zot.children('ABC123') self.assertEqual(u'T4AH4RZA', items_data[0]['key'])
def testParseAttachmentsAtomDoc(self): """ Ensure that attachments are being correctly parsed """ zot = z.Zotero('myuserid', 'user', 'myuserkey') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserid/items?content=json&key=myuserkey', body=self.attachments_doc) attachments_data = zot.items() self.assertEqual(u'1641 Depositions', attachments_data[0]['title'])
def testParseKeysResponse(self): """ Check that parsing plain keys returned by format = keys works """ zot = z.Zotero('myuserid', 'user', 'myuserkey') zot.url_params = 'format=keys' HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserid/items?format=keys', body=self.keys_response) response = zot.items() self.assertEqual('ABCDE\nFGHIJ\nKLMNO\n', response)
def testParseItemAtomBibDoc(self): """ Should match a DIV with class = csl-entry """ my_opener = urllib2.build_opener(MyHTTPSHandler(self.bib_doc)) z.urllib2.install_opener(my_opener) zot = z.Zotero('myuserID', 'users', 'myuserkey') zot.url_params = 'content=bib' items_data = zot.items() dec = items_data[0] self.assertTrue(dec.startswith("""<div class="csl-entry">"""))
def testGetTemplate(self): """ Ensure that item templates are retrieved and converted into dicts """ zot = z.Zotero('myuserID', 'user', 'myuserkey') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/items/new?itemType=book', body=self.item_templt) t = zot.item_template('book') self.assertEqual('book', t['itemType'])
def testParamsBlankAfterCall(self): """ self.url_params should be blank after an API call """ zot = z.Zotero('myuserID', 'user', 'myuserkey') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserID/items?content=json&key=myuserkey', body=self.items_doc) _ = zot.items() self.assertEqual(None, zot.url_params)
def testBuildQuery(self): """ Check that spaces etc. are being correctly URL-encoded and added to the URL parameters """ zot = z.Zotero('myuserID', 'user', 'myuserkey') zot.add_parameters(start=10) query_string = '/users/{u}/tags/hi there/items' query = zot._build_query(query_string) self.assertEqual( '/users/myuserID/tags/hi%20there/items?content=json&start=10&key=myuserkey', query)
def testParseGroupsAtomDoc(self): """ Should successfully return a list of group dicts, ID should match input doc's zapi:key value, and 'total_items' value should match input doc's zapi:numItems value """ my_opener = urllib2.build_opener(MyHTTPSHandler(self.groups_doc)) z.urllib2.install_opener(my_opener) zot = z.Zotero('myuserID', 'users', 'myuserkey') groups_data = zot.groups() self.assertEqual('DFW', groups_data[0]['name']) self.assertEqual('10248', groups_data[0]['group_id'])
def testResponseMiscError(self): """ Ensure that an error is properly raised for unspecified errors """ zot = z.Zotero('myuserID', 'user', 'myuserkey') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserID/items?content=json&key=myuserkey', body=self.items_doc, status=500) with self.assertRaises(z.ze.HTTPError): zot.items()
def testResponseNotFound(self): """ Ensure that an error is properly raised for 404 """ zot = z.Zotero('myuserID', 'user', 'myuserkey') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserID/items?content=json&key=myuserkey', body=self.items_doc, status=404) with self.assertRaises(z.ze.ResourceNotFound): zot.items()
def testParseTagsAtomDoc(self): """ Should successfully return a list of tags """ zot = z.Zotero('myuserID', 'user', 'myuserkey') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserID/tags?content=json&key=myuserkey', body=self.tags_doc) # /users/myuserID/tags?content=json&key=myuserkey tags_data = zot.tags() self.assertEqual('Authority in literature', tags_data[0])
def testUpdateItem(self): """ Test that we can update an item This test is a kludge; it only tests that the mechanism for internal key removal is OK, and that we haven't made any silly list/dict comprehension or genexpr errors """ import json # first, retrieve an item zot = z.Zotero('myuserID', 'users', 'myuserkey') items_data = zot.items() items_data[0]['title'] = 'flibble' json.dumps(*zot._cleanup(items_data[0]))
def testCitUTF8(self): """ ensure that unicode citations are correctly processed by Pyzotero """ zot = z.Zotero('myuserID', 'user', 'myuserkey') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserID/items/GW8V2CK7?content=citation&style=chicago-author-date&key=myuserkey', body=self.citation_doc) cit = zot.item('GW8V2CK7', content='citation', style='chicago-author-date') self.assertEqual(cit[0], u'<span>(Robert Ä. Caro 1974)</span>')
def testParseItemAtomBibDoc(self): """ Should match a DIV with class = csl-entry """ zot = z.Zotero('myuserID', 'user', 'myuserkey') zot.url_params = 'content=bib' HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserID/items?content=json&key=myuserkey', body=self.bib_doc) items_data = zot.items() dec = items_data[0] self.assertTrue(dec.startswith("""<div class="csl-entry">"""))