def testRequestBuilder(self): """ Should url-encode all added parameters """ zot = z.Zotero("myuserID", "user", "myuserkey") zot.add_parameters(limit=0, start=7) self.assertEqual(parse_qs("start=7&limit=100&format=json"), parse_qs(zot.url_params))
def testRequestBuilderLimitNegativeOne(self): """ Should skip limit = 100 param if limit is set to -1 """ zot = z.Zotero("myuserID", "user", "myuserkey") zot.add_parameters(limit=-1, start=7) self.assertEqual(parse_qs("start=7&format=json"), parse_qs(zot.url_params))
def testRequestBuilder(self): """ Should url-encode all added parameters """ zot = z.Zotero('myuserID', 'user', 'myuserkey') zot.add_parameters(limit=0, start=7) self.assertEqual(parse_qs('start=7&limit=0&format=json'), parse_qs(zot.url_params))
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_collections(t)
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_collections(t)
def testTooManyItems(self): """ Should fail because we're passing too many items """ itms = [i for i in range(51)] zot = z.Zotero('myuserID', 'user', 'myuserkey') with self.assertRaises(z.ze.TooManyItems): zot.create_items(itms)
def testRequestBuilderLimitNone(self): """ Should skip limit = 100 param if limit is set to None """ zot = z.Zotero('myuserID', 'user', 'myuserkey') zot.add_parameters(limit=None, start=7) self.assertEqual( parse_qs('start=7&format=json'), parse_qs(zot.url_params))
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 = json.loads(zot.item_types()) self.assertEqual(t[0]['itemType'], 'artwork')
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(parse_qs("start=2&format=json&limit=100"), parse_qs(zot.url_params))
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(parse_qs('start=2&format=json'), parse_qs(zot.url_params))
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_type='application/json', body=self.items_doc) _ = zot.items() self.assertEqual(None, 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_type='application/json', body=self.items_doc) items_data = zot.children('ABC123') self.assertEqual(u'NM66T6EF', items_data[0]['key'])
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', content_type='application/json', body=self.item_types) resp = zot.item_types() self.assertEqual(resp[0]['itemType'], 'artwork')
def testParseAttachmentsJSONDoc(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_type='application/json', body=self.attachments_doc) attachments_data = zot.items() self.assertEqual(u'1641 Depositions', attachments_data['data']['title'])
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", content_type="application/json", body=self.item_types, ) resp = zot.item_types() self.assertEqual(resp[0]["itemType"], "artwork")
def testParseAttachmentsJSONDoc(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_type="application/json", body=self.attachments_doc, ) attachments_data = zot.items() self.assertEqual(u"1641 Depositions", attachments_data["data"]["title"])
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', body=self.items_doc, content_type='application/json', status=404) with self.assertRaises(z.ze.ResourceNotFound): zot.items()
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', content_type='application/json', body=self.item_templt) t = zot.item_template('book') self.assertEqual('book', t['itemType'])
def testParseTagsJSON(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?limit=1', content_type='application/json', body=self.tags_doc) tags_data = zot.tags() self.assertEqual(u'Community / Economic Development', tags_data[0])
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', content_type='text/plain', body=self.keys_response) response = zot.items() self.assertEqual('JIFWQ4AN', response[:8].decode("utf-8"))
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_type="application/json", body=self.items_doc, ) items_data = zot.children("ABC123") self.assertEqual(u"NM66T6EF", items_data[0]["key"])
def testNoApiKey(self): """ Ensure that pyzotero works when api_key is not set """ zot = z.Zotero('myuserID', 'user') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserID/items', content_type='application/json', body=self.item_doc) items = zot.items() self.assertEqual(len(items), 6) # this isn't a very good assertion
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_type='application/json', body=self.items_doc, status=500) with self.assertRaises(z.ze.HTTPError): zot.items()
def testGetItemFile(self): """ Should successfully return a binary string with a PDF content """ zot = z.Zotero('myuserid', 'user', 'myuserkey') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserid/items/MYITEMID/file', content_type='application/pdf', body=self.item_file) items_data = zot.file('myitemid') self.assertEqual(b'One very strange PDF\n', items_data)
def testParseTagsJSON(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?limit=1", content_type="application/json", body=self.tags_doc, ) tags_data = zot.tags() self.assertEqual(u"Community / Economic Development", tags_data[0])
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_type="application/json", body=self.items_doc, ) zot.items() self.assertEqual(None, zot.url_params)
def testParseCollectionVersionsResponse(self): """ Check that parsing version dict returned by format = versions works """ zot = z.Zotero('myuserid', 'user', 'myuserkey') HTTPretty.register_uri( HTTPretty.GET, 'https://api.zotero.org/users/myuserid/collections?format=versions', content_type='application/json', body=self.collection_versions) iversions = zot.collection_versions() self.assertEqual(iversions['RRK27C5F'], 4000) self.assertEqual(iversions['EAWCSKSF'], 4087) self.assertEqual(len(iversions), 2)
def testRateLimitWithBackoff(self): """ Test 429 response handling when a backoff header is received """ zot = z.Zotero("myuserID", "user", "myuserkey") HTTPretty.register_uri( HTTPretty.GET, "https://api.zotero.org/users/myuserID/items", status=429, adding_headers={"backoff": 10}, ) zot.items() self.assertTrue(zot.backoff)
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", content_type="application/json", body=self.item_templt, ) t = zot.item_template("book") self.assertEqual("book", t["itemType"])
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", content_type="text/plain", body=self.keys_response, ) response = zot.items() self.assertEqual("JIFWQ4AN", response[:8].decode("utf-8"))