Example #1
0
 def getItems(self, result: dict, at: str = "items", pager: Pager = None):
     """
     Converts a list of Maps to a List of ParaObjects, at a given path within the JSON tree structure.
     @param result: the response body for an API request
     @param at: the path (field) where the array of objects is located (defaults to 'items')
     @param pager: a pager
     @return: a list of ParaObjects
     """
     if result and at and at in result:
         if pager and "totalHits" in result:
             pager.count = result["totalHits"]
         if pager and "lastKey" in result:
             pager.lastKey = result["lastKey"]
         return self.getItemsFromList(result[at])
     return []
    def testList(self):
        cats = []
        for i in range(0, 3):
            s = ParaObject(self.catsType + str(i))
            s.type = self.catsType
            cats.append(s)

        self.pc.createAll(cats)
        sleep(1)

        self.assertIs(len(self.pc.list(None)), 0)
        self.assertIs(len(self.pc.list("")), 0)

        list1 = self.pc.list(self.catsType)
        self.assertIsNot(len(list1), 0)
        self.assertEqual(3, len(list1))
        self.assertEqual(isinstance(list1[0], ParaObject), True)

        list2 = self.pc.list(self.catsType, Pager(limit=2))
        self.assertIsNot(len(list2), 0)
        self.assertEqual(2, len(list2))

        nl = [cats[0].id, cats[1].id, cats[2].id]
        self.pc.deleteAll(nl)

        self.assertIs(self.catsType in self.pc.getApp()["datatypes"].values(),
                      True)
 def countLinks(self, obj: ParaObject, type2: str):
     """
     Count the total number of links between this object and another type of object.
     @param obj: the object to execute this method on
     @param type2: the other type of object
     @return: the number of links for the given object
     """
     if not obj or not obj.id or not type2:
         return 0
     pager = Pager()
     url = obj.getObjectURI() + "/links/" + self.urlenc(type2)
     self.getItems(self.getEntity(self.invokeGet(url, {"count": "true"})), pager=pager)
     return pager.count
Example #4
0
 def countChildren(self, obj: ParaObject, type2: str):
     """
     Count the total number of child objects for this object.
     @param obj: the object to execute this method on
     @param type2: the type of the other object
     @return: the number of links
     """
     if not obj or not obj.id or not type2:
         return 0
     params = {"count": "true", "childrenonly": "true"}
     pager = Pager()
     url = obj.getObjectURI() + "/links/" + self.urlenc(type2)
     self.getItems(self.getEntity(self.invokeGet(url, params)), pager=pager)
     return pager.count
 def getCount(self, type_: str, terms={}):
     """
     Counts indexed objects matching a set of terms/values.
     @param type_: the type of object to search for. @see ParaObject::type
     @param terms: a list of terms (property values)
     @return: the number of results found
     """
     if terms is None:
         return 0
     params = {}
     pager = Pager()
     if not terms:
         params["type"] = type_
         self.getItems(self.find("count", params), pager=pager)
         return pager.count
     else:
         termz = [(key + self.SEPARATOR + value) for key, value in terms.items() if value]
         params["terms"] = termz
     params["type"] = type_
     params["count"] = "true"
     self.getItems(self.find("terms", params), pager=pager)
     return pager.count
    def testSearch(self):
        self.assertIsNone(self.pc.findById(None))
        self.assertIsNone(self.pc.findById(""))
        self.assertIsNotNone(self.pc.findById(self.u.id))
        self.assertIsNotNone(self.pc.findById(self.t.id))

        self.assertIs(len(self.pc.findByIds(None)), 0)
        self.assertEqual(
            3, len(self.pc.findByIds([self.u.id, self.u1.id, self.u2.id])))

        self.assertIs(len(self.pc.findNearby(None, None, 100, 1, 1)), 0)
        l1 = self.pc.findNearby(self.u.type, "*", 10, 40.60, -73.90)
        self.assertIsNot(len(l1), 0)

        self.assertIs(len(self.pc.findNearby(None, None, 100, 1, 1)), 0)
        l1 = self.pc.findNearby(self.u.type, "*", 10, 40.60, -73.90)
        self.assertIsNot(len(l1), 0)

        self.assertIs(len(self.pc.findPrefix(None, None, "")), 0)
        self.assertIs(len(self.pc.findPrefix("", "None", "xx")), 0)
        self.assertIsNot(len(self.pc.findPrefix(self.u.type, "name", "Ann")),
                         0)

        self.assertIsNot(len(self.pc.findQuery(None, None)), 0)
        self.assertIsNot(len(self.pc.findQuery("", "*")), 0)
        self.assertEqual(2, len(self.pc.findQuery(self.a1.type, "country:US")))
        self.assertIsNot(len(self.pc.findQuery(self.u.type, "Ann*")), 0)
        self.assertIsNot(len(self.pc.findQuery(self.u.type, "Ann*")), 0)
        self.assertGreater(len(self.pc.findQuery(None, "*")), 4)

        p = Pager()
        self.assertEqual(0, p.count)
        res = self.pc.findQuery(self.u.type, "*", p)
        self.assertEqual(len(res), p.count)
        self.assertGreater(p.count, 0)

        self.assertIs(len(self.pc.findSimilar(self.t.type, "", None, None)), 0)
        self.assertIs(len(self.pc.findSimilar(self.t.type, "", [], "")), 0)
        res = self.pc.findSimilar(self.s1.type, self.s1.id,
                                  ["properties.text"], self.s1["text"])
        self.assertIsNot(len(res), 0)
        self.assertEqual(self.s2.id, res[0].id)

        i0 = len(self.pc.findTagged(self.u.type, None))
        i1 = len(self.pc.findTagged(self.u.type, ["two"]))
        i2 = len(self.pc.findTagged(self.u.type, ["one", "two"]))
        i3 = len(self.pc.findTagged(self.u.type, ["three"]))
        i4 = len(self.pc.findTagged(self.u.type, ["four", "three"]))
        i5 = len(self.pc.findTagged(self.u.type, ["five", "three"]))
        i6 = len(self.pc.findTagged(self.t.type, ["four", "three"]))

        self.assertEqual(0, i0)
        self.assertEqual(2, i1)
        self.assertEqual(1, i2)
        self.assertEqual(3, i3)
        self.assertEqual(2, i4)
        self.assertEqual(1, i5)
        self.assertEqual(0, i6)

        self.assertIsNot(len(self.pc.findTags(None)), 0)
        self.assertIsNot(len(self.pc.findTags("")), 0)
        self.assertIs(len(self.pc.findTags("unknown")), 0)
        self.assertGreaterEqual(len(self.pc.findTags(self.t["tag"])), 1)

        self.assertEqual(
            3,
            len(
                self.pc.findTermInList(
                    self.u.type, "id",
                    [self.u.id, self.u1.id, self.u2.id, "xxx", "yyy"])))

        # many terms
        terms = {"id": self.u.id}
        # terms.put("type", u.type)

        terms1 = {"type": None, "id": " "}

        terms2 = {" ": "bad", "": ""}

        self.assertEqual(1, len(self.pc.findTerms(self.u.type, terms, True)))
        self.assertIs(len(self.pc.findTerms(self.u.type, terms1, True)), 0)
        self.assertIs(len(self.pc.findTerms(self.u.type, terms2, True)), 0)

        # single term
        self.assertIs(len(self.pc.findTerms(None, None, True)), 0)
        self.assertIs(len(self.pc.findTerms(self.u.type, {"": None}, True)), 0)
        self.assertIs(len(self.pc.findTerms(self.u.type, {"": ""}, True)), 0)
        self.assertIs(
            len(self.pc.findTerms(self.u.type, {"term": None}, True)), 0)
        self.assertGreaterEqual(
            len(self.pc.findTerms(self.u.type, {"type": self.u.type}, True)),
            2)

        self.assertIs(len(self.pc.findWildcard(self.u.type, None, None)), 0)
        self.assertIs(len(self.pc.findWildcard(self.u.type, "", "")), 0)
        self.assertIsNot(len(self.pc.findWildcard(self.u.type, "name", "An*")),
                         0)

        self.assertGreater(self.pc.getCount(None), 4)
        self.assertNotEqual(0, self.pc.getCount(""))
        self.assertEqual(0, self.pc.getCount("test"))
        self.assertGreaterEqual(self.pc.getCount(self.u.type), 3)

        self.assertEqual(0, self.pc.getCount(None, None))
        self.assertEqual(0, self.pc.getCount(self.u.type, {"id": " "}))
        self.assertEqual(1, self.pc.getCount(self.u.type, {"id": self.u.id}))
        self.assertGreater(self.pc.getCount(None, {"type": self.u.type}), 1)