예제 #1
0
    def test_resource_query(self):
        res_objs = [
            (IonObject(RT.InstrumentDevice, name="ID1", lcstate=LCS.DEPLOYED), ),
            (IonObject(RT.InstrumentDevice, name="ID2", lcstate=LCS.INTEGRATED), ),

            (IonObject(RT.InstrumentSite, name="IS1", lcstate=LCS.INTEGRATED), ),

            (IonObject(RT.PlatformDevice, name="PD1"), ),

            (IonObject(RT.PlatformSite, name="PS1"), ),

            (IonObject(RT.PlatformSite, name="PS0"), ),

            (IonObject(RT.Observatory, name="OS1"), ),

            (IonObject(RT.DataProduct, name="DP1"), ),
            (IonObject(RT.DataProduct, name="DP2"), ),
        ]
        assocs = [
            ("ID1", PRED.hasOutputProduct, "DP1"),
            ("ID1", PRED.hasOutputProduct, "DP2"),

            ("PD1", PRED.hasDevice, "ID1"),

            ("OS1", PRED.hasSite, "PS0"),
            ("PS0", PRED.hasSite, "PS1"),
            ("PS1", PRED.hasSite, "IS1"),

            ("PS1", PRED.hasDevice, "PD1"),
            ("IS1", PRED.hasDevice, "ID1"),
        ]
        res_by_name = {}
        for res_entry in res_objs:
            res_obj = res_entry[0]
            res_name = res_obj.name
            res_obj.alt_ids.append("TEST:%s" % res_name)
            actor_id = res_by_name[res_entry[1]] if len(res_entry) > 1 else None
            rid, _ = self.rr.create(res_obj, actor_id=actor_id)
            res_by_name[res_name] = rid
        for assoc in assocs:
            sname, p, oname = assoc
            s, o = res_by_name[sname], res_by_name[oname]
            self.rr.create_association(s, p, o)

        # --- Simple resource filters

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.InstrumentDevice))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.InstrumentDevice, RT.PlatformDevice]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 3)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.InstrumentDevice, RT.PlatformDevice]),
                      rq.filter_name("ID1"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.InstrumentDevice, RT.PlatformDevice]),
                      rq.filter_name(["ID1", "PD1"]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.InstrumentDevice, RT.PlatformDevice]),
                      rq.filter_name("ID", cmpop=ResourceQuery.TXT_CONTAINS))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        # --- Association query

        rq = ResourceQuery()
        rq.set_filter(rq.filter_associated_with_object(res_by_name["ID1"]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        # --- Association queries with a target filter

        rq = ResourceQuery()
        target_filter = rq.eq(rq.RA_NAME, "DP1")
        rq.set_filter(rq.filter_type([RT.InstrumentDevice, RT.PlatformDevice]),
                      rq.filter_associated_with_subject(predicate=PRED.hasOutputProduct,
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        target_filter = rq.eq(rq.RA_NAME, "DP1")
        rq.set_filter(rq.filter_type(RT.InstrumentDevice),
                      rq.filter_associated_with_subject(predicate=[PRED.hasOutputProduct, PRED.hasSource],
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        target_filter = rq.eq(rq.RA_NAME, "DP1")
        rq.set_filter(rq.filter_type(RT.InstrumentDevice),
                      rq.filter_associated_with_subject(predicate=PRED.hasOutputProduct,
                                                        object_type=RT.DataProduct,
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        target_filter = rq.and_(rq.eq(rq.RA_NAME, "DP1"),
                                rq.eq(rq.ATT_TYPE, RT.DataProduct))
        rq.set_filter(rq.filter_type(RT.InstrumentDevice),
                      rq.filter_associated_with_subject(predicate=PRED.hasOutputProduct,
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        #rq = ResourceQuery()
        #rq.set_filter(rq.filter_by_association(res_by_name["ID1"], direction="SO"))
        #res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)

        # --- Multi-hop associations

        rq = ResourceQuery()
        # This will find InstrumentDevice child of PlatformDevice
        rq.set_filter(rq.filter_by_association(res_by_name["PS1"], predicate=[PRED.hasDevice, PRED.hasDevice], direction="OO"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        #print "\n".join("%s %s %s" % (ro._id, ro.type_, ro.name) for ro in res_obj)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        # This will find PlatformDevice for site PLUS InstrumentDevice child of PlatformDevice
        rq.set_filter(rq.filter_by_association(res_by_name["PS1"], predicate=[PRED.hasDevice, PRED.hasDevice], direction="OO"),
                      rq.filter_by_association(res_by_name["PS1"], predicate=[PRED.hasDevice], direction="O"),
                      or_filters=True)
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)
        self.assertIn(res_obj[0].name, {"ID1", "PD1"})
        self.assertIn(res_obj[1].name, {"ID1", "PD1"})

        rq = ResourceQuery()
        rq.set_filter(rq.filter_by_association(res_by_name["OS1"], predicate=[PRED.hasSite], direction="O"),
                      rq.filter_by_association(res_by_name["OS1"], predicate=[PRED.hasSite,PRED.hasSite], direction="OO"),
                      rq.filter_by_association(res_by_name["OS1"], predicate=[PRED.hasSite,PRED.hasSite,PRED.hasSite], direction="OOO"),
                      or_filters=True)
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 3)
        self.assertIn(res_obj[0].name, {"PS0", "PS1", "IS1"})
        self.assertIn(res_obj[1].name, {"PS0", "PS1", "IS1"})
        self.assertIn(res_obj[2].name, {"PS0", "PS1", "IS1"})

        # --- Association descendants (recursively)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_object_descendants(res_by_name["OS1"], predicate=[PRED.hasSite]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 3)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_object_descendants(res_by_name["OS1"], predicate=[PRED.hasSite], max_depth=2))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertGreaterEqual(len(res_obj), 9)

        # --- Parameterized queries

        query_params = dict(restype=RT.InstrumentDevice)
        rq = ResourceQuery()
        rq.set_filter(rq.filter_type("$(restype)"))
        rq.set_query_parameters(query_params)
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        # --- Association query

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject(res_by_name["ID1"]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject([res_by_name["ID1"], res_by_name["PS1"]]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 4)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object([res_by_name["DP1"], res_by_name["DP2"]]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject([res_by_name["ID1"], res_by_name["PS1"]]),
                      aq.filter_predicate(PRED.hasOutputProduct))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject(res_by_name["ID1"]),
                      aq.filter_predicate(PRED.hasOutputProduct),
                      aq.filter_object(res_by_name["DP1"]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 1)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject_type(RT.InstrumentDevice))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject_type([RT.Observatory, RT.PlatformSite]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 4)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object_type(RT.DataProduct))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object_type([RT.PlatformSite, RT.InstrumentSite]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 3)

        aq = AssociationQuery()
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 8)

        # --- Association descendants (recursively)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object_descendants(res_by_name["OS1"], predicate=[PRED.hasSite]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 3)

        #print assoc_objs

        #from pyon.util.breakpoint import breakpoint
        #breakpoint()

        # --- Clean up

        self.rr.rr_store.delete_mult(res_by_name.values())
예제 #2
0
    def test_resource_query(self):
        res_objs = [
            (IonObject(RT.TestInstrument, name="ID1", lcstate=LCS.DEPLOYED, keywords=["K1", "K2"]), ),
            (IonObject(RT.TestInstrument, name="ID2", lcstate=LCS.INTEGRATED), ),

            (IonObject(RT.TestSite, name="IS1", lcstate=LCS.INTEGRATED), ),

            (IonObject(RT.TestPlatform, name="PD1", keywords=["K", "K1", "K3"]), ),

            (IonObject(RT.TestSite, name="PS1", alt_ids=["NS1:ID11", "NS2:ID21"]), ),

            (IonObject(RT.TestSite, name="PS0", alt_ids=["NS1:ID12", "NS3:ID22"]), ),

            (IonObject(RT.TestSite, name="OS1", alt_ids=["_:IDX", "NS2:ID21"]), ),

            (IonObject(RT.TestDataset, name="DP1"), ),
            (IonObject(RT.TestDataset, name="DP2"), ),

            (IonObject(RT.ActorIdentity, name="user1", details=IonObject(OT.UserIdentityDetails,
                    contact=IonObject(OT.ContactInformation, individual_names_given="John",
                                      individual_name_family="Doe", email="*****@*****.**"))), ),
            (IonObject(RT.ActorIdentity, name="user2", details=IonObject(OT.UserIdentityDetails,
                    contact=IonObject(OT.ContactInformation, individual_names_given="Jane",
                                      individual_name_family="Foo", email="*****@*****.**"))), ),
        ]
        assocs = [
            ("ID1", PRED.hasTestDataset, "DP1"),
            ("ID1", PRED.hasTestDataset, "DP2"),

            ("PD1", PRED.hasTestDevice, "ID1"),

            ("OS1", PRED.hasTestSite, "PS0"),
            ("PS0", PRED.hasTestSite, "PS1"),
            ("PS1", PRED.hasTestSite, "IS1"),

            ("PS1", PRED.hasTestDevice, "PD1"),
            ("IS1", PRED.hasTestDevice, "ID1"),
        ]
        res_by_name = {}
        for res_entry in res_objs:
            res_obj = res_entry[0]
            res_name = res_obj.name
            res_obj.alt_ids.append("TEST:%s" % res_name)
            actor_id = res_by_name[res_entry[1]] if len(res_entry) > 1 else None
            rid, _ = self.rr.create(res_obj, actor_id=actor_id)
            res_by_name[res_name] = rid
        for assoc in assocs:
            sname, p, oname = assoc
            s, o = res_by_name[sname], res_by_name[oname]
            self.rr.create_association(s, p, o)

        # TEST: Simple resource filters

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.TestInstrument, RT.TestPlatform]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 3)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.TestInstrument, RT.TestPlatform]),
                      rq.filter_name("ID1"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.TestInstrument, RT.TestPlatform]),
                      rq.filter_name(["ID1", "PD1"]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.TestInstrument, RT.TestPlatform]),
                      rq.filter_name("ID", cmpop=ResourceQuery.TXT_CONTAINS))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        # TEST: Advanced resource filters

        rq = ResourceQuery()
        rq.set_filter(rq.filter_keyword("K1"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_keyword("K"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_keyword(""))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 0)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_keyword(["K1", "K3"]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_altid("NS1", "ID11"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_altid("NS2", "ID21"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_altid("NS2"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_altid(None))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertGreaterEqual(len(res_obj), 3)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_altid(["NS1", "NS2"], "ID21"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        # TEST: Full-text search

        rq = ResourceQuery()
        rq.set_filter(rq.filter_matchany("DP"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.ActorIdentity),
                      rq.filter_matchany("user1"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.ActorIdentity),
                      rq.filter_matchany("John"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.ActorIdentity),
                      rq.filter_matchany("JoHn", rq.TXT_CONTAINS))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 0)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.ActorIdentity),
                      rq.filter_matchany("foo.com"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.ActorIdentity),
                      rq.filter_matchany("DEPLOYED"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 0)

        # --- Query with associations

        rq = ResourceQuery()
        rq.set_filter(rq.filter_associated_from_subject(res_by_name["ID1"]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        # --- Query with associations with a target filter

        rq = ResourceQuery()
        target_filter = rq.eq(rq.RA_NAME, "DP1")
        rq.set_filter(rq.filter_type([RT.TestInstrument, RT.TestPlatform]),
                      rq.filter_associated_from_object(predicate=PRED.hasTestDataset,
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        target_filter = rq.eq(rq.RA_NAME, "DP1")
        rq.set_filter(rq.filter_type(RT.TestInstrument),
                      rq.filter_associated_from_object(predicate=[PRED.hasTestDataset, PRED.hasTestSource],
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        target_filter = rq.eq(rq.RA_NAME, "DP1")
        rq.set_filter(rq.filter_type(RT.TestInstrument),
                      rq.filter_associated_from_object(predicate=PRED.hasTestDataset,
                                                        object_type=RT.TestDataset,
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        target_filter = rq.and_(rq.eq(rq.RA_NAME, "DP1"),
                                rq.eq(rq.ATT_TYPE, RT.TestDataset))
        rq.set_filter(rq.filter_type(RT.TestInstrument),
                      rq.filter_associated_from_object(predicate=PRED.hasTestDataset,
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        #rq = ResourceQuery()
        #rq.set_filter(rq.filter_by_association(res_by_name["ID1"], direction="SO"))
        #res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)

        # --- Multi-hop associations

        rq = ResourceQuery()
        # This will find TestInstrument child of TestPlatform
        rq.set_filter(rq.filter_by_association(res_by_name["PS1"], predicate=[PRED.hasTestDevice, PRED.hasTestDevice], direction="OO"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        #print "\n".join("%s %s %s" % (ro._id, ro.type_, ro.name) for ro in res_obj)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        # This will find TestPlatform for site PLUS TestInstrument child of TestPlatform
        rq.set_filter(rq.filter_by_association(res_by_name["PS1"], predicate=[PRED.hasTestDevice, PRED.hasTestDevice], direction="OO"),
                      rq.filter_by_association(res_by_name["PS1"], predicate=[PRED.hasTestDevice], direction="O"),
                      or_filters=True)
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)
        self.assertIn(res_obj[0].name, {"ID1", "PD1"})
        self.assertIn(res_obj[1].name, {"ID1", "PD1"})

        rq = ResourceQuery()
        rq.set_filter(rq.filter_by_association(res_by_name["OS1"], predicate=[PRED.hasTestSite], direction="O"),
                      rq.filter_by_association(res_by_name["OS1"], predicate=[PRED.hasTestSite,PRED.hasTestSite], direction="OO"),
                      rq.filter_by_association(res_by_name["OS1"], predicate=[PRED.hasTestSite,PRED.hasTestSite,PRED.hasTestSite], direction="OOO"),
                      or_filters=True)
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 3)
        self.assertIn(res_obj[0].name, {"PS0", "PS1", "IS1"})
        self.assertIn(res_obj[1].name, {"PS0", "PS1", "IS1"})
        self.assertIn(res_obj[2].name, {"PS0", "PS1", "IS1"})

        # --- Association descendants (recursively)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_object_descendants(res_by_name["OS1"], predicate=[PRED.hasTestSite]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 3)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_object_descendants(res_by_name["OS1"], predicate=[PRED.hasTestSite], max_depth=2))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertGreaterEqual(len(res_obj), 9)

        # --- Parameterized queries

        query_params = dict(restype=RT.TestInstrument)
        rq = ResourceQuery()
        rq.set_filter(rq.filter_type("$(restype)"))
        rq.set_query_parameters(query_params)
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        # --- Association query

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject(res_by_name["ID1"]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject([res_by_name["ID1"], res_by_name["PS1"]]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 4)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object([res_by_name["DP1"], res_by_name["DP2"]]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject([res_by_name["ID1"], res_by_name["PS1"]]),
                      aq.filter_predicate(PRED.hasTestDataset))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject(res_by_name["ID1"]),
                      aq.filter_predicate(PRED.hasTestDataset),
                      aq.filter_object(res_by_name["DP1"]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 1)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject_type(RT.TestInstrument))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject_type([RT.TestSite, RT.TestSite]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 5)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object_type(RT.TestDataset))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object_type([RT.TestSite, RT.TestSite]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 3)

        aq = AssociationQuery()
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 8)

        # --- Association descendants (recursively)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object_descendants(res_by_name["OS1"], predicate=[PRED.hasTestSite]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 3)

        #print assoc_objs

        #from pyon.util.breakpoint import breakpoint
        #breakpoint()

        # --- Clean up

        self.rr.rr_store.delete_mult(res_by_name.values())
예제 #3
0
    def test_resource_query(self):
        res_objs = [
            (IonObject(RT.TestInstrument, name="ID1", lcstate=LCS.DEPLOYED, keywords=["K1", "K2"]), ),
            (IonObject(RT.TestInstrument, name="ID2", lcstate=LCS.INTEGRATED), ),

            (IonObject(RT.TestSite, name="IS1", lcstate=LCS.INTEGRATED), ),

            (IonObject(RT.TestPlatform, name="PD1", keywords=["K", "K1", "K3"]), ),

            (IonObject(RT.TestSite, name="PS1", alt_ids=["NS1:ID11", "NS2:ID21"]), ),

            (IonObject(RT.TestSite, name="PS0", alt_ids=["NS1:ID12", "NS3:ID22"]), ),

            (IonObject(RT.TestSite, name="OS1", alt_ids=["_:IDX", "NS2:ID21"]), ),

            (IonObject(RT.TestDataset, name="DP1"), ),
            (IonObject(RT.TestDataset, name="DP2"), ),

            (IonObject(RT.ActorIdentity, name="user1", details=IonObject(OT.UserIdentityDetails,
                    contact=IonObject(OT.ContactInformation, individual_names_given="John",
                                      individual_name_family="Doe", email="*****@*****.**"))), ),
            (IonObject(RT.ActorIdentity, name="user2", details=IonObject(OT.UserIdentityDetails,
                    contact=IonObject(OT.ContactInformation, individual_names_given="Jane",
                                      individual_name_family="Foo", email="*****@*****.**"))), ),
        ]
        assocs = [
            ("ID1", PRED.hasTestDataset, "DP1"),
            ("ID1", PRED.hasTestDataset, "DP2"),

            ("PD1", PRED.hasTestDevice, "ID1"),

            ("OS1", PRED.hasTestSite, "PS0"),
            ("PS0", PRED.hasTestSite, "PS1"),
            ("PS1", PRED.hasTestSite, "IS1"),

            ("PS1", PRED.hasTestDevice, "PD1"),
            ("IS1", PRED.hasTestDevice, "ID1"),
        ]
        res_by_name = {}
        for res_entry in res_objs:
            res_obj = res_entry[0]
            res_name = res_obj.name
            res_obj.alt_ids.append("TEST:%s" % res_name)
            actor_id = res_by_name[res_entry[1]] if len(res_entry) > 1 else None
            rid, _ = self.rr.create(res_obj, actor_id=actor_id)
            res_by_name[res_name] = rid
        for assoc in assocs:
            sname, p, oname = assoc
            s, o = res_by_name[sname], res_by_name[oname]
            self.rr.create_association(s, p, o)

        # TEST: Simple resource filters

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.TestInstrument, RT.TestPlatform]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 3)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.TestInstrument, RT.TestPlatform]),
                      rq.filter_name("ID1"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.TestInstrument, RT.TestPlatform]),
                      rq.filter_name(["ID1", "PD1"]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type([RT.TestInstrument, RT.TestPlatform]),
                      rq.filter_name("ID", cmpop=ResourceQuery.TXT_CONTAINS))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        # TEST: Advanced resource filters

        rq = ResourceQuery()
        rq.set_filter(rq.filter_keyword("K1"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_keyword("K"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_keyword(""))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 0)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_keyword(["K1", "K3"]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_altid("NS1", "ID11"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_altid("NS2", "ID21"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_altid("NS2"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_altid(None))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertGreaterEqual(len(res_obj), 3)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_altid(["NS1", "NS2"], "ID21"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        # TEST: Full-text search

        rq = ResourceQuery()
        rq.set_filter(rq.filter_matchany("DP"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.ActorIdentity),
                      rq.filter_matchany("user1"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.ActorIdentity),
                      rq.filter_matchany("John"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.ActorIdentity),
                      rq.filter_matchany("JoHn", rq.TXT_CONTAINS))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 0)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.ActorIdentity),
                      rq.filter_matchany("foo.com"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.ActorIdentity),
                      rq.filter_matchany("DEPLOYED"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 0)

        # --- Query with associations

        rq = ResourceQuery()
        rq.set_filter(rq.filter_associated_from_subject(res_by_name["ID1"]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        # --- Query with associations with a target filter

        rq = ResourceQuery()
        target_filter = rq.eq(rq.RA_NAME, "DP1")
        rq.set_filter(rq.filter_type([RT.TestInstrument, RT.TestPlatform]),
                      rq.filter_associated_from_object(predicate=PRED.hasTestDataset,
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        target_filter = rq.eq(rq.RA_NAME, "DP1")
        rq.set_filter(rq.filter_type(RT.TestInstrument),
                      rq.filter_associated_from_object(predicate=[PRED.hasTestDataset, PRED.hasTestSource],
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        target_filter = rq.eq(rq.RA_NAME, "DP1")
        rq.set_filter(rq.filter_type(RT.TestInstrument),
                      rq.filter_associated_from_object(predicate=PRED.hasTestDataset,
                                                        object_type=RT.TestDataset,
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        target_filter = rq.and_(rq.eq(rq.RA_NAME, "DP1"),
                                rq.eq(rq.ATT_TYPE, RT.TestDataset))
        rq.set_filter(rq.filter_type(RT.TestInstrument),
                      rq.filter_associated_from_object(predicate=PRED.hasTestDataset,
                                                        target_filter=target_filter))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        #rq = ResourceQuery()
        #rq.set_filter(rq.filter_by_association(res_by_name["ID1"], direction="SO"))
        #res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)

        # --- Multi-hop associations

        rq = ResourceQuery()
        # This will find TestInstrument child of TestPlatform
        rq.set_filter(rq.filter_by_association(res_by_name["PS1"], predicate=[PRED.hasTestDevice, PRED.hasTestDevice], direction="OO"))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        #print "\n".join("%s %s %s" % (ro._id, ro.type_, ro.name) for ro in res_obj)
        self.assertEquals(len(res_obj), 1)
        self.assertEquals(res_obj[0].name, "ID1")

        rq = ResourceQuery()
        # This will find TestPlatform for site PLUS TestInstrument child of TestPlatform
        rq.set_filter(rq.filter_by_association(res_by_name["PS1"], predicate=[PRED.hasTestDevice, PRED.hasTestDevice], direction="OO"),
                      rq.filter_by_association(res_by_name["PS1"], predicate=[PRED.hasTestDevice], direction="O"),
                      or_filters=True)
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)
        self.assertIn(res_obj[0].name, {"ID1", "PD1"})
        self.assertIn(res_obj[1].name, {"ID1", "PD1"})

        rq = ResourceQuery()
        rq.set_filter(rq.filter_by_association(res_by_name["OS1"], predicate=[PRED.hasTestSite], direction="O"),
                      rq.filter_by_association(res_by_name["OS1"], predicate=[PRED.hasTestSite,PRED.hasTestSite], direction="OO"),
                      rq.filter_by_association(res_by_name["OS1"], predicate=[PRED.hasTestSite,PRED.hasTestSite,PRED.hasTestSite], direction="OOO"),
                      or_filters=True)
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 3)
        self.assertIn(res_obj[0].name, {"PS0", "PS1", "IS1"})
        self.assertIn(res_obj[1].name, {"PS0", "PS1", "IS1"})
        self.assertIn(res_obj[2].name, {"PS0", "PS1", "IS1"})

        # --- Association descendants (recursively)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_object_descendants(res_by_name["OS1"], predicate=[PRED.hasTestSite]))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 3)

        rq = ResourceQuery()
        rq.set_filter(rq.filter_object_descendants(res_by_name["OS1"], predicate=[PRED.hasTestSite], max_depth=2))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        rq = ResourceQuery()
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertGreaterEqual(len(res_obj), 9)

        # --- Parameterized queries

        query_params = dict(restype=RT.TestInstrument)
        rq = ResourceQuery()
        rq.set_filter(rq.filter_type("$(restype)"))
        rq.set_query_parameters(query_params)
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        # --- Association query

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject(res_by_name["ID1"]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject([res_by_name["ID1"], res_by_name["PS1"]]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 4)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object([res_by_name["DP1"], res_by_name["DP2"]]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject([res_by_name["ID1"], res_by_name["PS1"]]),
                      aq.filter_predicate(PRED.hasTestDataset))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject(res_by_name["ID1"]),
                      aq.filter_predicate(PRED.hasTestDataset),
                      aq.filter_object(res_by_name["DP1"]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 1)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject_type(RT.TestInstrument))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject_type([RT.TestSite, RT.TestSite]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 5)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object_type(RT.TestDataset))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 2)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object_type([RT.TestSite, RT.TestSite]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 3)

        aq = AssociationQuery()
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 8)

        # --- Association descendants (recursively)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_object_descendants(res_by_name["OS1"], predicate=[PRED.hasTestSite]))
        assoc_objs = self.rr.find_associations(query=aq.get_query(), id_only=False)
        self.assertEquals(len(assoc_objs), 3)

        # --- Lifecycle state

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        self.rr.lcs_delete(res_by_name["ID2"])

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 1)

        rq.set_query_arg("with_deleted", True)
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)

        self.rr.undelete(res_by_name["ID2"])
        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument))
        res_obj = self.rr.find_resources_ext(query=rq.get_query(), id_only=False)
        self.assertEquals(len(res_obj), 2)


        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject(res_by_name["PD1"]))
        assocs = self.rr.find_associations(query=aq.get_query())
        self.assertEquals(len(assocs), 1)

        self.rr.lcs_delete(res_by_name["ID1"])

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject(res_by_name["PD1"]))
        assocs = self.rr.find_associations(query=aq.get_query())
        self.assertEquals(len(assocs), 0)

        aq.set_query_arg("with_deleted", True)
        assocs = self.rr.find_associations(query=aq.get_query())
        self.assertEquals(len(assocs), 1)

        self.rr.undelete(res_by_name["ID1"], undelete_associations=True)

        aq = AssociationQuery()
        aq.set_filter(aq.filter_subject(res_by_name["PD1"]))
        assocs = self.rr.find_associations(query=aq.get_query())
        self.assertEquals(len(assocs), 1)

        # --- Clean up

        self.rr.rr_store.delete_mult(res_by_name.values())