def test_get_count(self): test_obj = Request( http_session=Mock(), base="http://localhost:8001/api/dcim/devices", filters={"q": "abcd"}, ) test_obj.http_session.get.return_value.json.return_value = { "count": 42, "next": "http://localhost:8001/api/dcim/devices?limit=1&offset=1&q=abcd", "previous": False, "results": [], } call( "http://localhost:8001/api/dcim/devices/", params={ "q": "abcd", "limit": 1 }, headers={"accept": "application/json;"}, ) test_obj.http_session.get.ok = True test = test_obj.get_count() self.assertEqual(test, 42) test_obj.http_session.get.assert_called_with( "http://localhost:8001/api/dcim/devices/", params={ "q": "abcd", "limit": 1 }, headers={"accept": "application/json;"}, json=None, )
def count(self, *args, **kwargs): r"""Returns the count of objects in a query. Takes named arguments that match the usable filters on a given endpoint. If an argument is passed then it's used as a freeform search argument if the endpoint supports it. If no arguments are passed the count for all objects on an endpoint are returned. :arg str,optional \*args: Freeform search string that's accepted on given endpoint. :arg str,optional \**kwargs: Any search argument the endpoint accepts can be added as a keyword arg. :Returns: Integer with count of objects returns by query. :Examples: To return a count of objects matching a named argument filter. >>> nb.dcim.devices.count(site='tst1') 5827 >>> To return a count of objects on an entire endpoint. >>> nb.dcim.devices.count() 87382 >>> """ if args: kwargs.update({"q": args[0]}) if any(i in RESERVED_KWARGS for i in kwargs): raise ValueError( "A reserved {} kwarg was passed. Please remove it " "try again.".format(RESERVED_KWARGS)) ret = Request( filters=kwargs, base=self.url, token=self.token, http_session=self.api.http_session, ) return ret.get_count()