Beispiel #1
0
 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))
Beispiel #2
0
 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))
Beispiel #3
0
 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))
Beispiel #4
0
 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)
Beispiel #5
0
 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)
Beispiel #6
0
 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)
Beispiel #7
0
 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))
Beispiel #8
0
 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')
Beispiel #9
0
 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))
Beispiel #10
0
 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))
Beispiel #11
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)
Beispiel #12
0
 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'])
Beispiel #13
0
 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')
Beispiel #14
0
 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'])
Beispiel #15
0
 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")
Beispiel #16
0
 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"])
Beispiel #17
0
 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()
Beispiel #18
0
 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'])
Beispiel #19
0
 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])
Beispiel #20
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"))
Beispiel #21
0
 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"])
Beispiel #22
0
 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
Beispiel #23
0
 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()
Beispiel #24
0
 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)
Beispiel #25
0
 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])
Beispiel #26
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)
Beispiel #27
0
 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)
Beispiel #28
0
 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)
Beispiel #29
0
 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"])
Beispiel #30
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"))