def test_complex_queries(self):
        res_objs = [
            dict(res=IonObject(RT.ActorIdentity, name="Act1")),
            dict(res=IonObject(RT.ActorIdentity, name="Act2")),

            dict(res=IonObject(RT.Org, name="Org1"), act="Act1"),
            dict(res=IonObject(RT.TestSite, name="Obs1"), act="Act1", org="Org1"),
            dict(res=IonObject(RT.TestSite, name="PS1"), act="Act1", org="Org1"),
            dict(res=IonObject(RT.TestSite, name="PSC1"), act="Act1", org="Org1"),
            dict(res=IonObject(RT.TestSite, name="IS1"), act="Act1", org="Org1"),
            dict(res=IonObject(RT.TestDeviceModel, name="PM1", manufacturer="CGSN"), act="Act1", org="Org1"),
            dict(res=IonObject(RT.TestDeviceModel, name="PMC1", manufacturer="Bluefin"), act="Act1", org="Org1"),
            dict(res=IonObject(RT.TestDeviceModel, name="PM2", manufacturer="Webb"), act="Act1", org="Org1"),
            dict(res=IonObject(RT.TestDeviceModel, name="IM1", manufacturer="SeaBird"), act="Act1", org="Org1"),
            dict(res=IonObject(RT.TestDeviceModel, name="IM2", manufacturer="Teledyne"), act="Act1", org="Org1"),
            dict(res=IonObject(RT.TestPlatform, name="PD1"), act="Act1", org="Org1", lcstate=LCS.DEPLOYED),
            dict(res=IonObject(RT.TestPlatform, name="PDC1"), act="Act1", org="Org1", lcstate=LCS.INTEGRATED),
            dict(res=IonObject(RT.TestInstrument, name="ID1", firmware_version='A1'), act="Act1", org="Org1", lcstate=LCS.DEPLOYED),
            dict(res=IonObject(RT.TestInstrument, name="ID2", firmware_version='A2'), act="Act1", org="Org1", lcstate=LCS.INTEGRATED),

            dict(res=IonObject(RT.Org, name="Org2"), act="Act2"),
            dict(res=IonObject(RT.TestSite, name="Obs2"), act="Act2", org="Org2"),
            dict(res=IonObject(RT.TestSite, name="PS2"), act="Act2", org="Org2"),
            dict(res=IonObject(RT.TestPlatform, name="PD2"), act="Act2", org="Org2"),
            dict(res=IonObject(RT.TestInstrument, name="ID3", lcstate=LCS.DEPLOYED, firmware_version='A3'), act="Act2", org="Org2"),
            dict(res=IonObject(RT.Stream, name="Stream1")),
        ]
        assocs = [
            ("Obs1", PRED.hasTestSite, "PS1"),
            ("PS1", PRED.hasTestSite, "PSC1"),
            ("PSC1", PRED.hasTestSite, "IS1"),
            ("PS1", PRED.hasTestDevice, "PD1"),
            ("PSC1", PRED.hasTestDevice, "PDC1"),
            ("IS1", PRED.hasTestDevice, "ID1"),
            ("PD1", PRED.hasTestDevice, "PDC1"),
            ("PDC1", PRED.hasTestDevice, "ID1"),

            ("PS1", PRED.hasTestModel, "PM1"),
            ("PSC1", PRED.hasTestModel, "PMC1"),
            ("IS1", PRED.hasTestModel, "IM1"),
            ("PD1", PRED.hasTestModel, "PM1"),
            ("PDC1", PRED.hasTestModel, "PMC1"),
            ("ID1", PRED.hasTestModel, "IM1"),
            ("PD2", PRED.hasTestModel, "PM2"),
            ("ID2", PRED.hasTestModel, "IM2"),

        ]
        res_by_name = create_dummy_resources(res_objs, assocs)

        log.info("TEST: Query for all resources owned by actor Act1")
        rq = ResourceQuery()
        rq.set_filter(rq.filter_associated_from_object(res_by_name["Act1"], None, "hasOwner"))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 14)

        log.info("TEST: Query for all Site descendants of TestSite Obs1")
        rq = ResourceQuery()
        rq.set_filter(rq.filter_object_descendants(res_by_name["Obs1"], [RT.TestSite, RT.TestSite], PRED.hasTestSite))
        result = self.discovery.query(rq.get_query(), id_only=False)
        # import pprint
        # pprint.pprint(rq.get_query())
        self.assertEquals(len(result), 3)

        log.info("TEST: Query for all resources belonging to Org Org1")
        rq = ResourceQuery()
        rq.set_filter(rq.filter_associated_from_subject(res_by_name["Org1"], None, "hasResource"))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 13)

        log.info("TEST: Query for all resources belonging to Org Org1 AND of type TestInstrument")
        rq = ResourceQuery()
        rq.set_filter(rq.filter_associated_from_subject(res_by_name["Org1"], None, "hasResource"),
                      rq.filter_type(RT.TestInstrument))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 2)

        log.info("TEST: Query for instruments whose platform parent has a name of PDC1")
        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument),
                      rq.filter_associated_from_subject(subject_type=RT.TestPlatform, predicate=PRED.hasTestDevice, target_filter=rq.filter_name("PDC1")))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID1")

        log.info("TEST: Query for instruments in Org1 whose platform parent has a specific attribute set")
        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument),
                      rq.filter_associated_from_subject(res_by_name["Org1"], None, "hasResource"),
                      rq.filter_associated_from_subject(subject_type=RT.TestPlatform, predicate=PRED.hasTestDevice, target_filter=rq.filter_name("PDC1")))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID1")

        log.info("TEST: Query for instruments in Org1 that are lcstate INTEGRATED")
        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument),
                      rq.filter_lcstate(LCS.INTEGRATED),
                      rq.filter_associated_from_subject(res_by_name["Org1"], None, "hasResource"))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID2")

        log.info("TEST: Query for instruments in Org1 that are lcstate INTEGRATED OR platforms in Org1 that are lcstate DEPLOYED")
        rq = ResourceQuery()
        rq.set_filter(rq.filter_or(rq.filter_and(rq.filter_type(RT.TestInstrument),
                                                 rq.filter_lcstate(LCS.INTEGRATED)),
                                   rq.filter_and(rq.filter_type(RT.TestPlatform),
                                                 rq.filter_lcstate(LCS.DEPLOYED))),
                      rq.filter_associated_from_subject(res_by_name["Org1"], None, "hasResource"))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 2)
    def test_query_view(self):
        res_objs = [
            (IonObject(RT.ActorIdentity, name="Act1"), ),
            (IonObject(RT.ActorIdentity, name="Act2"), ),

            (IonObject(RT.TestInstrument, name="ID1", lcstate=LCS.DEPLOYED, firmware_version='A1'), "Act1"),
            (IonObject(RT.TestInstrument, name="ID2", lcstate=LCS.INTEGRATED, firmware_version='A2'), "Act2"),

            (IonObject(RT.TestPlatform, name="PD1"), ),
            (IonObject(RT.TestPlatform, name="PD2"), ),

            (IonObject(RT.TestSite, name="Site1", lcstate=LCS.DEPLOYED), ),
        ]
        assocs = [
            ("PD1", PRED.hasTestDevice, "ID1"),
            ("PD2", PRED.hasTestDevice, "ID2"),

        ]
        res_by_name = create_dummy_resources(res_objs, assocs)

        # ----------------------------------------------------
        # Resource attribute search

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument))
        view_obj = View(name="All TestInstrument resources", view_definition=rq.get_query())
        view_id = self.discovery.create_view(view_obj)

        # TEST: View by ID
        result = self.discovery.query_view(view_id, id_only=False)
        self.assertEquals(len(result), 2)
        self.assertTrue(all(True for ro in result if ro.type_ == RT.TestInstrument))

        # TEST: View by Name
        result = self.discovery.query_view(view_name="All TestInstrument resources", id_only=False)
        self.assertEquals(len(result), 2)
        self.assertTrue(all(True for ro in result if ro.type_ == RT.TestInstrument))

        # TEST: View plus ext_query
        rq = ResourceQuery()
        rq.set_filter(rq.filter_name("ID1"))
        result = self.discovery.query_view(view_id, id_only=False, ext_query=rq.get_query())
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID1")

        # TEST: View with params (anonymous)
        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument),
                      rq.filter_attribute("firmware_version", "$(firmware_version)"))
        view_obj = View(name="TestInstrument resources with a specific firmware - parameterized",
                        view_definition=rq.get_query())
        view_id = self.discovery.create_view(view_obj)

        view_params = {"firmware_version": "A2"}
        result = self.discovery.query_view(view_id, id_only=False, search_args=view_params)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID2")

        # TEST: View with params (anonymous) - no values provided
        result = self.discovery.query_view(view_id, id_only=False)
        self.assertEquals(len(result), 0)

        # View with params (with definitions and defaults)
        view_param_def = [CustomAttribute(name="firmware_version",
                                          type="str",
                                          default="A1")]
        view_obj = View(name="TestInstrument resources with a specific firmware - parameterized with defaults",
                        view_definition=rq.get_query(),
                        view_parameters=view_param_def)
        view_id = self.discovery.create_view(view_obj)

        # TEST: Definition defaults
        result = self.discovery.query_view(view_id, id_only=False)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID1")

        # TEST: Parameterized values
        result = self.discovery.query_view(view_id, id_only=False, search_args=view_params)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID2")

        # TEST: Parameterized association query for resource owner
        rq = ResourceQuery()
        rq.set_filter(rq.filter_associated_from_object("$(owner)"))
        view_obj = View(name="Resources owned by actor - parameterized", view_definition=rq.get_query())
        view_id = self.discovery.create_view(view_obj)
        view_params = {"owner": res_by_name["Act2"]}
        result = self.discovery.query_view(view_id, id_only=False, search_args=view_params)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID2")

        # TEST: Parameterized association query for resource owner with parameter value
        view_params = {"owner": res_by_name["Act2"], "query_info": True}
        result = self.discovery.query_view(view_id, id_only=False, search_args=view_params)
        self.assertEquals(len(result), 2)
        self.assertEquals(result[0].name, "ID2")
        self.assertIn("_query_info", result[1])
        self.assertIn("statement_sql", result[1])

        # TEST: Builtin views
        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestSite))
        result = self.discovery.query_view(view_name="resources_index", id_only=False, ext_query=rq.get_query())
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "Site1")


        # --- Events setup

        from interface.objects import ResourceOperatorEvent, ResourceCommandEvent
        t0 = 136304640000
        events = [
            ("RME1", ResourceCommandEvent(origin="O1", origin_type="OT1", sub_type="ST1", ts_created=str(t0))),
            ("RME2", ResourceCommandEvent(origin="O2", origin_type="OT1", sub_type="ST2", ts_created=str(t0+1))),
            ("RME3", ResourceCommandEvent(origin="O2", origin_type="OT2", sub_type="ST3", ts_created=str(t0+2))),

            ("RLE1", ResourceOperatorEvent(origin="O1", origin_type="OT3", sub_type="ST4", ts_created=str(t0+3))),
            ("RLE2", ResourceOperatorEvent(origin="O3", origin_type="OT3", sub_type="ST5", ts_created=str(t0+4))),
            ("RLE3", ResourceOperatorEvent(origin="O3", origin_type="OT2", sub_type="ST6", ts_created=str(t0+5))),

        ]
        ev_by_alias = create_dummy_events(events)

        # TEST: Event query with views
        eq = EventQuery()
        eq.set_filter(eq.filter_type(OT.ResourceCommandEvent))
        view_obj = View(name="All ResourceCommandEvent events", view_definition=eq.get_query())
        view_id = self.discovery.create_view(view_obj)
        result = self.discovery.query_view(view_id, id_only=False)
        self.assertEquals(len(result), 3)
        self.assertTrue(all(True for eo in result if eo.type_ == OT.ResourceCommandEvent))

        # TEST: Event query with views - stripped format
        result = self.discovery.query_view(view_id, id_only=False, search_args=dict(attribute_filter=["origin"]))
        self.assertEquals(len(result), 3)
        self.assertTrue(all(True for eo in result if isinstance(eo, dict)))
        self.assertTrue(all(True for eo in result if "origin" in eo))
        self.assertTrue(all(True for eo in result if len(eo) <= 4))

        # TEST: Builtin views
        eq = EventQuery()
        eq.set_filter(eq.filter_type(OT.ResourceCommandEvent))
        result = self.discovery.query_view(view_name="events_index", id_only=False, ext_query=eq.get_query())
        self.assertEquals(len(result), 3)
    def test_ui_ops(self):
        res_objs = [
            dict(res=IonObject(RT.Org, name="Org1")),
            dict(res=IonObject(RT.Org, name="Org2")),

            dict(res=IonObject(RT.InstrumentDevice, name="ID1", firmware_version='A1'), org="Org1"),
            dict(res=IonObject(RT.InstrumentDevice, name="ID2", firmware_version='A2'), org="Org1"),
            dict(res=IonObject(RT.InstrumentDevice, name="ID3", firmware_version='A2'), org="Org1"),
            dict(res=IonObject(RT.InstrumentDevice, name="ID4", firmware_version='A3'), org="Org1"),

            dict(res=IonObject(RT.PlatformDevice, name="PD1", firmware_version='P1', hardware_version="X"), org="Org1"),
            dict(res=IonObject(RT.PlatformDevice, name="PD2", firmware_version='P1', hardware_version="Y"), org="Org1"),
            dict(res=IonObject(RT.PlatformDevice, name="PD2", firmware_version='P1', hardware_version="Y"), org="Org1"),
            dict(res=IonObject(RT.PlatformDevice, name="PD2", firmware_version='P2', hardware_version="X"), org="Org1"),
            dict(res=IonObject(RT.PlatformDevice, name="PD2", firmware_version='P2', hardware_version="Z"), org="Org1"),

            dict(res=IonObject(RT.InstrumentDevice, name="ID1o2", firmware_version='A1'), org="Org2"),
            dict(res=IonObject(RT.PlatformDevice, name="PD1o2", firmware_version='P1', hardware_version="X"), org="Org2"),

            dict(res=IonObject(RT.PlatformDevice, name="PD1ono", firmware_version='P1', hardware_version="X")),

            dict(res=IonObject(RT.Attachment, name="Att1")),
            dict(res=IonObject(RT.Attachment, name="Att2")),

            dict(res=IonObject(RT.UserRole, name="Role1")),
            dict(res=IonObject(RT.UserRole, name="Role2")),
        ]
        assocs = [
            ("Org1", PRED.hasAttachment, "Att1"),
            ("Org1", PRED.hasRole, "Role1"),

        ]
        res_by_name = create_dummy_resources(res_objs, assocs)

        # Distinct value
        value_list = self.rms.get_distinct_values(RT.InstrumentDevice, attr_list=["firmware_version"])
        self.assertEquals(len(value_list), 3)
        self.assertTrue(all(type(le) is list and len(le) == 1 for le in value_list))
        self.assertEquals({le[0] for le in value_list}, {"A1", "A2", "A3"})

        value_list = self.rms.get_distinct_values(RT.PlatformDevice, attr_list=["firmware_version"])
        self.assertEquals(len(value_list), 2)

        value_list = self.rms.get_distinct_values(RT.PlatformDevice, attr_list=["firmware_version", "hardware_version"])
        self.assertEquals(len(value_list), 4)
        self.assertTrue(all(type(le) is list and len(le) == 2 for le in value_list))
        self.assertEquals({le[0] for le in value_list}, {"P1", "P2"})
        self.assertEquals({le[1] for le in value_list}, {"X", "Y", "Z"})

        # Org resources
        res_list, _ = self.rms.get_org_resource_attributes(org_id=res_by_name["Org1"])
        self.assertEquals(len(res_list), 11)
        self.assertTrue(all(type(le) is dict for le in res_list))
        self.assertTrue(all("name" in le for le in res_list))
        self.assertIn("Att1", [le["name"] for le in res_list])
        self.assertIn("Role1", [le["name"] for le in res_list])
        self.assertIn("ID1", [le["name"] for le in res_list])

        res_list, _ = self.rms.get_org_resource_attributes(org_id=res_by_name["Org2"])
        self.assertEquals(len(res_list), 2)

        res_list, _ = self.rms.get_org_resource_attributes(org_id=res_by_name["Org1"], limit=2)
        self.assertEquals(len(res_list), 2)
        self.assertIn("Att1", [le["name"] for le in res_list])
        self.assertIn("ID1", [le["name"] for le in res_list])

        res_list, _ = self.rms.get_org_resource_attributes(org_id=res_by_name["Org1"], limit=2, order_by="name")
        self.assertEquals(len(res_list), 2)
        self.assertIn("Att1", [le["name"] for le in res_list])
        self.assertIn("ID1", [le["name"] for le in res_list])
    def test_ui_ops(self):
        res_objs = [
            dict(res=IonObject(RT.Org, name="Org1")),
            dict(res=IonObject(RT.Org, name="Org2")),
            dict(res=IonObject(RT.InstrumentDevice,
                               name="ID1",
                               firmware_version='A1'),
                 org="Org1"),
            dict(res=IonObject(RT.InstrumentDevice,
                               name="ID2",
                               firmware_version='A2'),
                 org="Org1"),
            dict(res=IonObject(RT.InstrumentDevice,
                               name="ID3",
                               firmware_version='A2'),
                 org="Org1"),
            dict(res=IonObject(RT.InstrumentDevice,
                               name="ID4",
                               firmware_version='A3'),
                 org="Org1"),
            dict(res=IonObject(RT.PlatformDevice,
                               name="PD1",
                               firmware_version='P1',
                               hardware_version="X"),
                 org="Org1"),
            dict(res=IonObject(RT.PlatformDevice,
                               name="PD2",
                               firmware_version='P1',
                               hardware_version="Y"),
                 org="Org1"),
            dict(res=IonObject(RT.PlatformDevice,
                               name="PD2",
                               firmware_version='P1',
                               hardware_version="Y"),
                 org="Org1"),
            dict(res=IonObject(RT.PlatformDevice,
                               name="PD2",
                               firmware_version='P2',
                               hardware_version="X"),
                 org="Org1"),
            dict(res=IonObject(RT.PlatformDevice,
                               name="PD2",
                               firmware_version='P2',
                               hardware_version="Z"),
                 org="Org1"),
            dict(res=IonObject(RT.InstrumentDevice,
                               name="ID1o2",
                               firmware_version='A1'),
                 org="Org2"),
            dict(res=IonObject(RT.PlatformDevice,
                               name="PD1o2",
                               firmware_version='P1',
                               hardware_version="X"),
                 org="Org2"),
            dict(res=IonObject(RT.PlatformDevice,
                               name="PD1ono",
                               firmware_version='P1',
                               hardware_version="X")),
            dict(res=IonObject(RT.Attachment, name="Att1")),
            dict(res=IonObject(RT.Attachment, name="Att2")),
            dict(res=IonObject(RT.UserRole, name="Role1")),
            dict(res=IonObject(RT.UserRole, name="Role2")),
        ]
        assocs = [
            ("Org1", PRED.hasAttachment, "Att1"),
            ("Org1", PRED.hasRole, "Role1"),
        ]
        res_by_name = create_dummy_resources(res_objs, assocs)

        # Distinct value
        value_list = self.rms.get_distinct_values(
            RT.InstrumentDevice, attr_list=["firmware_version"])
        self.assertEquals(len(value_list), 3)
        self.assertTrue(
            all(type(le) is list and len(le) == 1 for le in value_list))
        self.assertEquals({le[0] for le in value_list}, {"A1", "A2", "A3"})

        value_list = self.rms.get_distinct_values(
            RT.PlatformDevice, attr_list=["firmware_version"])
        self.assertEquals(len(value_list), 2)

        value_list = self.rms.get_distinct_values(
            RT.PlatformDevice,
            attr_list=["firmware_version", "hardware_version"])
        self.assertEquals(len(value_list), 4)
        self.assertTrue(
            all(type(le) is list and len(le) == 2 for le in value_list))
        self.assertEquals({le[0] for le in value_list}, {"P1", "P2"})
        self.assertEquals({le[1] for le in value_list}, {"X", "Y", "Z"})

        # Org resources
        res_list, _ = self.rms.get_org_resource_attributes(
            org_id=res_by_name["Org1"])
        self.assertEquals(len(res_list), 11)
        self.assertTrue(all(type(le) is dict for le in res_list))
        self.assertTrue(all("name" in le for le in res_list))
        self.assertIn("Att1", [le["name"] for le in res_list])
        self.assertIn("Role1", [le["name"] for le in res_list])
        self.assertIn("ID1", [le["name"] for le in res_list])

        res_list, _ = self.rms.get_org_resource_attributes(
            org_id=res_by_name["Org2"])
        self.assertEquals(len(res_list), 2)

        res_list, _ = self.rms.get_org_resource_attributes(
            org_id=res_by_name["Org1"], limit=2)
        self.assertEquals(len(res_list), 2)
        self.assertIn("Att1", [le["name"] for le in res_list])
        self.assertIn("ID1", [le["name"] for le in res_list])

        res_list, _ = self.rms.get_org_resource_attributes(
            org_id=res_by_name["Org1"], limit=2, order_by="name")
        self.assertEquals(len(res_list), 2)
        self.assertIn("Att1", [le["name"] for le in res_list])
        self.assertIn("ID1", [le["name"] for le in res_list])
    def test_complex_queries(self):
        res_objs = [
            dict(res=IonObject(RT.ActorIdentity, name="Act1")),
            dict(res=IonObject(RT.ActorIdentity, name="Act2")),
            dict(res=IonObject(RT.Org, name="Org1"), act="Act1"),
            dict(res=IonObject(RT.TestSite, name="Obs1"),
                 act="Act1",
                 org="Org1"),
            dict(res=IonObject(RT.TestSite, name="PS1"),
                 act="Act1",
                 org="Org1"),
            dict(res=IonObject(RT.TestSite, name="PSC1"),
                 act="Act1",
                 org="Org1"),
            dict(res=IonObject(RT.TestSite, name="IS1"),
                 act="Act1",
                 org="Org1"),
            dict(res=IonObject(RT.TestDeviceModel,
                               name="PM1",
                               manufacturer="CGSN"),
                 act="Act1",
                 org="Org1"),
            dict(res=IonObject(RT.TestDeviceModel,
                               name="PMC1",
                               manufacturer="Bluefin"),
                 act="Act1",
                 org="Org1"),
            dict(res=IonObject(RT.TestDeviceModel,
                               name="PM2",
                               manufacturer="Webb"),
                 act="Act1",
                 org="Org1"),
            dict(res=IonObject(RT.TestDeviceModel,
                               name="IM1",
                               manufacturer="SeaBird"),
                 act="Act1",
                 org="Org1"),
            dict(res=IonObject(RT.TestDeviceModel,
                               name="IM2",
                               manufacturer="Teledyne"),
                 act="Act1",
                 org="Org1"),
            dict(res=IonObject(RT.TestPlatform, name="PD1"),
                 act="Act1",
                 org="Org1",
                 lcstate=LCS.DEPLOYED),
            dict(res=IonObject(RT.TestPlatform, name="PDC1"),
                 act="Act1",
                 org="Org1",
                 lcstate=LCS.INTEGRATED),
            dict(res=IonObject(RT.TestInstrument,
                               name="ID1",
                               firmware_version='A1'),
                 act="Act1",
                 org="Org1",
                 lcstate=LCS.DEPLOYED),
            dict(res=IonObject(RT.TestInstrument,
                               name="ID2",
                               firmware_version='A2'),
                 act="Act1",
                 org="Org1",
                 lcstate=LCS.INTEGRATED),
            dict(res=IonObject(RT.Org, name="Org2"), act="Act2"),
            dict(res=IonObject(RT.TestSite, name="Obs2"),
                 act="Act2",
                 org="Org2"),
            dict(res=IonObject(RT.TestSite, name="PS2"),
                 act="Act2",
                 org="Org2"),
            dict(res=IonObject(RT.TestPlatform, name="PD2"),
                 act="Act2",
                 org="Org2"),
            dict(res=IonObject(RT.TestInstrument,
                               name="ID3",
                               lcstate=LCS.DEPLOYED,
                               firmware_version='A3'),
                 act="Act2",
                 org="Org2"),
            dict(res=IonObject(RT.Stream, name="Stream1")),
        ]
        assocs = [
            ("Obs1", PRED.hasTestSite, "PS1"),
            ("PS1", PRED.hasTestSite, "PSC1"),
            ("PSC1", PRED.hasTestSite, "IS1"),
            ("PS1", PRED.hasTestDevice, "PD1"),
            ("PSC1", PRED.hasTestDevice, "PDC1"),
            ("IS1", PRED.hasTestDevice, "ID1"),
            ("PD1", PRED.hasTestDevice, "PDC1"),
            ("PDC1", PRED.hasTestDevice, "ID1"),
            ("PS1", PRED.hasTestModel, "PM1"),
            ("PSC1", PRED.hasTestModel, "PMC1"),
            ("IS1", PRED.hasTestModel, "IM1"),
            ("PD1", PRED.hasTestModel, "PM1"),
            ("PDC1", PRED.hasTestModel, "PMC1"),
            ("ID1", PRED.hasTestModel, "IM1"),
            ("PD2", PRED.hasTestModel, "PM2"),
            ("ID2", PRED.hasTestModel, "IM2"),
        ]
        res_by_name = create_dummy_resources(res_objs, assocs)

        log.info("TEST: Query for all resources owned by actor Act1")
        rq = ResourceQuery()
        rq.set_filter(
            rq.filter_associated_from_object(res_by_name["Act1"], None,
                                             "hasOwner"))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 14)

        log.info("TEST: Query for all Site descendants of TestSite Obs1")
        rq = ResourceQuery()
        rq.set_filter(
            rq.filter_object_descendants(res_by_name["Obs1"],
                                         [RT.TestSite, RT.TestSite],
                                         PRED.hasTestSite))
        result = self.discovery.query(rq.get_query(), id_only=False)
        # import pprint
        # pprint.pprint(rq.get_query())
        self.assertEquals(len(result), 3)

        log.info("TEST: Query for all resources belonging to Org Org1")
        rq = ResourceQuery()
        rq.set_filter(
            rq.filter_associated_from_subject(res_by_name["Org1"], None,
                                              "hasResource"))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 13)

        log.info(
            "TEST: Query for all resources belonging to Org Org1 AND of type TestInstrument"
        )
        rq = ResourceQuery()
        rq.set_filter(
            rq.filter_associated_from_subject(res_by_name["Org1"], None,
                                              "hasResource"),
            rq.filter_type(RT.TestInstrument))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 2)

        log.info(
            "TEST: Query for instruments whose platform parent has a name of PDC1"
        )
        rq = ResourceQuery()
        rq.set_filter(
            rq.filter_type(RT.TestInstrument),
            rq.filter_associated_from_subject(
                subject_type=RT.TestPlatform,
                predicate=PRED.hasTestDevice,
                target_filter=rq.filter_name("PDC1")))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID1")

        log.info(
            "TEST: Query for instruments in Org1 whose platform parent has a specific attribute set"
        )
        rq = ResourceQuery()
        rq.set_filter(
            rq.filter_type(RT.TestInstrument),
            rq.filter_associated_from_subject(res_by_name["Org1"], None,
                                              "hasResource"),
            rq.filter_associated_from_subject(
                subject_type=RT.TestPlatform,
                predicate=PRED.hasTestDevice,
                target_filter=rq.filter_name("PDC1")))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID1")

        log.info(
            "TEST: Query for instruments in Org1 that are lcstate INTEGRATED")
        rq = ResourceQuery()
        rq.set_filter(
            rq.filter_type(RT.TestInstrument),
            rq.filter_lcstate(LCS.INTEGRATED),
            rq.filter_associated_from_subject(res_by_name["Org1"], None,
                                              "hasResource"))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID2")

        log.info(
            "TEST: Query for instruments in Org1 that are lcstate INTEGRATED OR platforms in Org1 that are lcstate DEPLOYED"
        )
        rq = ResourceQuery()
        rq.set_filter(
            rq.filter_or(
                rq.filter_and(rq.filter_type(RT.TestInstrument),
                              rq.filter_lcstate(LCS.INTEGRATED)),
                rq.filter_and(rq.filter_type(RT.TestPlatform),
                              rq.filter_lcstate(LCS.DEPLOYED))),
            rq.filter_associated_from_subject(res_by_name["Org1"], None,
                                              "hasResource"))
        result = self.discovery.query(rq.get_query(), id_only=False)
        self.assertEquals(len(result), 2)
    def test_query_view(self):
        res_objs = [
            (IonObject(RT.ActorIdentity, name="Act1"), ),
            (IonObject(RT.ActorIdentity, name="Act2"), ),
            (IonObject(RT.TestInstrument,
                       name="ID1",
                       lcstate=LCS.DEPLOYED,
                       firmware_version='A1'), "Act1"),
            (IonObject(RT.TestInstrument,
                       name="ID2",
                       lcstate=LCS.INTEGRATED,
                       firmware_version='A2'), "Act2"),
            (IonObject(RT.TestPlatform, name="PD1"), ),
            (IonObject(RT.TestPlatform, name="PD2"), ),
            (IonObject(RT.TestSite, name="Site1", lcstate=LCS.DEPLOYED), ),
        ]
        assocs = [
            ("PD1", PRED.hasTestDevice, "ID1"),
            ("PD2", PRED.hasTestDevice, "ID2"),
        ]
        res_by_name = create_dummy_resources(res_objs, assocs)

        # ----------------------------------------------------
        # Resource attribute search

        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestInstrument))
        view_obj = View(name="All TestInstrument resources",
                        view_definition=rq.get_query())
        view_id = self.discovery.create_view(view_obj)

        # TEST: View by ID
        result = self.discovery.query_view(view_id, id_only=False)
        self.assertEquals(len(result), 2)
        self.assertTrue(
            all(True for ro in result if ro.type_ == RT.TestInstrument))

        # TEST: View by Name
        result = self.discovery.query_view(
            view_name="All TestInstrument resources", id_only=False)
        self.assertEquals(len(result), 2)
        self.assertTrue(
            all(True for ro in result if ro.type_ == RT.TestInstrument))

        # TEST: View plus ext_query
        rq = ResourceQuery()
        rq.set_filter(rq.filter_name("ID1"))
        result = self.discovery.query_view(view_id,
                                           id_only=False,
                                           ext_query=rq.get_query())
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID1")

        # TEST: View with params (anonymous)
        rq = ResourceQuery()
        rq.set_filter(
            rq.filter_type(RT.TestInstrument),
            rq.filter_attribute("firmware_version", "$(firmware_version)"))
        view_obj = View(
            name=
            "TestInstrument resources with a specific firmware - parameterized",
            view_definition=rq.get_query())
        view_id = self.discovery.create_view(view_obj)

        view_params = {"firmware_version": "A2"}
        result = self.discovery.query_view(view_id,
                                           id_only=False,
                                           search_args=view_params)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID2")

        # TEST: View with params (anonymous) - no values provided
        result = self.discovery.query_view(view_id, id_only=False)
        self.assertEquals(len(result), 0)

        # View with params (with definitions and defaults)
        view_param_def = [
            CustomAttribute(name="firmware_version", type="str", default="A1")
        ]
        view_obj = View(
            name=
            "TestInstrument resources with a specific firmware - parameterized with defaults",
            view_definition=rq.get_query(),
            view_parameters=view_param_def)
        view_id = self.discovery.create_view(view_obj)

        # TEST: Definition defaults
        result = self.discovery.query_view(view_id, id_only=False)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID1")

        # TEST: Parameterized values
        result = self.discovery.query_view(view_id,
                                           id_only=False,
                                           search_args=view_params)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID2")

        # TEST: Parameterized association query for resource owner
        rq = ResourceQuery()
        rq.set_filter(rq.filter_associated_from_object("$(owner)"))
        view_obj = View(name="Resources owned by actor - parameterized",
                        view_definition=rq.get_query())
        view_id = self.discovery.create_view(view_obj)
        view_params = {"owner": res_by_name["Act2"]}
        result = self.discovery.query_view(view_id,
                                           id_only=False,
                                           search_args=view_params)
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "ID2")

        # TEST: Parameterized association query for resource owner with parameter value
        view_params = {"owner": res_by_name["Act2"], "query_info": True}
        result = self.discovery.query_view(view_id,
                                           id_only=False,
                                           search_args=view_params)
        self.assertEquals(len(result), 2)
        self.assertEquals(result[0].name, "ID2")
        self.assertIn("_query_info", result[1])
        self.assertIn("statement_sql", result[1])

        # TEST: Builtin views
        rq = ResourceQuery()
        rq.set_filter(rq.filter_type(RT.TestSite))
        result = self.discovery.query_view(view_name="resources_index",
                                           id_only=False,
                                           ext_query=rq.get_query())
        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].name, "Site1")

        # --- Events setup

        from interface.objects import ResourceOperatorEvent, ResourceCommandEvent
        t0 = 136304640000
        events = [
            ("RME1",
             ResourceCommandEvent(origin="O1",
                                  origin_type="OT1",
                                  sub_type="ST1",
                                  ts_created=str(t0))),
            ("RME2",
             ResourceCommandEvent(origin="O2",
                                  origin_type="OT1",
                                  sub_type="ST2",
                                  ts_created=str(t0 + 1))),
            ("RME3",
             ResourceCommandEvent(origin="O2",
                                  origin_type="OT2",
                                  sub_type="ST3",
                                  ts_created=str(t0 + 2))),
            ("RLE1",
             ResourceOperatorEvent(origin="O1",
                                   origin_type="OT3",
                                   sub_type="ST4",
                                   ts_created=str(t0 + 3))),
            ("RLE2",
             ResourceOperatorEvent(origin="O3",
                                   origin_type="OT3",
                                   sub_type="ST5",
                                   ts_created=str(t0 + 4))),
            ("RLE3",
             ResourceOperatorEvent(origin="O3",
                                   origin_type="OT2",
                                   sub_type="ST6",
                                   ts_created=str(t0 + 5))),
        ]
        ev_by_alias = create_dummy_events(events)

        # TEST: Event query with views
        eq = EventQuery()
        eq.set_filter(eq.filter_type(OT.ResourceCommandEvent))
        view_obj = View(name="All ResourceCommandEvent events",
                        view_definition=eq.get_query())
        view_id = self.discovery.create_view(view_obj)
        result = self.discovery.query_view(view_id, id_only=False)
        self.assertEquals(len(result), 3)
        self.assertTrue(
            all(True for eo in result if eo.type_ == OT.ResourceCommandEvent))

        # TEST: Event query with views - stripped format
        result = self.discovery.query_view(
            view_id,
            id_only=False,
            search_args=dict(attribute_filter=["origin"]))
        self.assertEquals(len(result), 3)
        self.assertTrue(all(True for eo in result if isinstance(eo, dict)))
        self.assertTrue(all(True for eo in result if "origin" in eo))
        self.assertTrue(all(True for eo in result if len(eo) <= 4))

        # TEST: Builtin views
        eq = EventQuery()
        eq.set_filter(eq.filter_type(OT.ResourceCommandEvent))
        result = self.discovery.query_view(view_name="events_index",
                                           id_only=False,
                                           ext_query=eq.get_query())
        self.assertEquals(len(result), 3)