Пример #1
0
    def test_datastore_views(self):
        data_store = CouchDB_DataStore(datastore_name='ion_test_ds', profile=DataStore.DS_PROFILE.RESOURCES)

        self.data_store = data_store
        self.resources = {}
        # Just in case previous run failed without cleaning up,
        # delete data store
        try:
            data_store.delete_datastore()
        except NotFound:
            pass

        # Create should succeed and not throw exception
        data_store.create_datastore()

        res = data_store.list_objects()
        numcoredocs = len(res)

        self.assertTrue(numcoredocs > 1)
        data_store._update_views()

        # HACK: Both Predicates so that this test works
        from pyon.ion.resource import Predicates
        Predicates[OWNER_OF] = dict(domain=[RT.ActorIdentity], range=[RT.InstrumentDevice, RT.Dataset])
        Predicates[HAS_A] = dict(domain=[RT.Resource], range=[RT.Resource])
        Predicates[BASED_ON] = dict(domain=[RT.Dataset], range=[RT.Dataset])

        admin_user_id = self._create_resource(RT.ActorIdentity, 'John Doe', description='Marine Operator', lcstate=LCS.DEPLOYED, availability=AS.AVAILABLE)

        admin_profile_id = self._create_resource(RT.UserInfo, 'J.D. Profile', description='Some User',
            contact=IonObject('ContactInformation', **{"individual_names_given": "John Doe",
                                                       "email": "*****@*****.**"}))

        other_user_id = self._create_resource(RT.ActorIdentity, 'Paul Smithy', description='Other user')

        plat1_obj_id = self._create_resource(RT.PlatformDevice, 'Buoy1', description='My Platform')

        inst1_obj_id = self._create_resource(RT.InstrumentDevice, 'CTD1', description='My Instrument')

        inst2_obj_id = self._create_resource(RT.InstrumentDevice, 'CTD2', description='Other Instrument')

        ds1_obj_id = self._create_resource(RT.Dataset, 'DS_CTD_L0', description='My Dataset CTD L0', lcstate=LCS.DEPLOYED, availability=AS.AVAILABLE)

        ds2_obj_id = self._create_resource(RT.Dataset, 'DS_CTD_L1', description='My Dataset CTD L1')

        aid1, _ = data_store.create_association(admin_user_id, OWNER_OF, inst1_obj_id)

        data_store.create_association(admin_user_id, HAS_A, admin_profile_id)

        data_store.create_association(admin_user_id, OWNER_OF, ds1_obj_id)

        data_store.create_association(other_user_id, OWNER_OF, inst2_obj_id)

        data_store.create_association(plat1_obj_id, HAS_A, inst1_obj_id)

        data_store.create_association(inst1_obj_id, HAS_A, ds1_obj_id)

        data_store.create_association(ds1_obj_id, BASED_ON, ds1_obj_id)

        with self.assertRaises(BadRequest) as cm:
            data_store.create_association(ds1_obj_id, BASED_ON, ds1_obj_id)
        self.assertTrue(cm.exception.message.startswith("Association between"))

        # Subject -> Object direction
        obj_ids1, obj_assocs1 = data_store.find_objects(admin_user_id, id_only=True)
        self.assertEquals(len(obj_ids1), 3)
        self.assertEquals(len(obj_assocs1), 3)
        self.assertEquals(set(obj_ids1), set([inst1_obj_id, ds1_obj_id, admin_profile_id]))

        obj_ids1n, obj_assocs1n = data_store.find_objects("Non_Existent", id_only=True)
        self.assertEquals(len(obj_ids1n), 0)
        self.assertEquals(len(obj_assocs1n), 0)

        obj_ids1a, obj_assocs1a = data_store.find_objects(admin_user_id, id_only=False)
        self.assertEquals(len(obj_ids1a), 3)
        self.assertEquals(len(obj_assocs1a), 3)
        self.assertEquals(set([o._id for o in obj_ids1a]), set([inst1_obj_id, ds1_obj_id, admin_profile_id]))
        self.assertEquals(set([type(o).__name__ for o in obj_ids1a]), set([RT.UserInfo, RT.InstrumentDevice, RT.Dataset]))

        obj_ids1an, obj_assocs1an = data_store.find_objects("Non_Existent", id_only=False)
        self.assertEquals(len(obj_ids1an), 0)
        self.assertEquals(len(obj_assocs1an), 0)

        obj_ids2, obj_assocs2 = data_store.find_objects(admin_user_id, OWNER_OF, id_only=True)
        self.assertEquals(len(obj_ids2), 2)
        self.assertEquals(len(obj_assocs2), 2)
        self.assertEquals(set(obj_ids2), set([inst1_obj_id, ds1_obj_id]))

        obj_ids3, _ = data_store.find_objects(admin_user_id, OWNER_OF, RT.InstrumentDevice, id_only=True)
        self.assertEquals(len(obj_ids3), 1)
        self.assertEquals(obj_ids3[0], inst1_obj_id)

        # Object -> Subject direction
        sub_ids1, sub_assoc1 = data_store.find_subjects(None, None, inst1_obj_id, id_only=True)
        self.assertEquals(len(sub_ids1), 2)
        self.assertEquals(len(sub_assoc1), 2)
        self.assertEquals(set(sub_ids1), set([admin_user_id, plat1_obj_id]))

        sub_ids1a, sub_assoc1a = data_store.find_subjects(None, None, inst1_obj_id, id_only=False)
        self.assertEquals(len(sub_ids1a), 2)
        self.assertEquals(len(sub_assoc1a), 2)
        self.assertEquals(set([o._id for o in sub_ids1a]), set([admin_user_id, plat1_obj_id]))

        sub_ids1an, sub_assoc1an = data_store.find_subjects(None, None, "Non_Existent", id_only=False)
        self.assertEquals(len(sub_ids1an), 0)
        self.assertEquals(len(sub_assoc1an), 0)

        sub_ids2, sub_assoc2 = data_store.find_subjects(None, OWNER_OF, inst1_obj_id, id_only=True)
        self.assertEquals(len(sub_ids2), 1)
        self.assertEquals(len(sub_assoc2), 1)
        self.assertEquals(set(sub_ids2), set([admin_user_id]))

        sub_ids3, _ = data_store.find_subjects(RT.ActorIdentity, OWNER_OF, inst1_obj_id, id_only=True)
        self.assertEquals(len(sub_ids3), 1)
        self.assertEquals(set(sub_ids3), set([admin_user_id]))

        data_store._update_views()

        # Find all resources
        res_ids1, res_assoc1 = data_store.find_res_by_type(None, None, id_only=True)
        self.assertEquals(len(res_ids1), 8)
        self.assertEquals(len(res_assoc1), 8)

        # Find resources by type
        res_ids1, res_assoc1 = data_store.find_res_by_type(RT.ActorIdentity, id_only=True)
        self.assertEquals(len(res_ids1), 2)
        self.assertEquals(len(res_assoc1), 2)
        self.assertEquals(set(res_ids1), set([admin_user_id, other_user_id]))

        res_ids1a, res_assoc1a = data_store.find_res_by_type(RT.ActorIdentity, id_only=False)
        self.assertEquals(len(res_ids1a), 2)
        self.assertEquals(len(res_assoc1a), 2)
        self.assertEquals(set([o._id for o in res_ids1a]), set([admin_user_id, other_user_id]))

        res_ids2n, res_assoc2n = data_store.find_res_by_type("NONE##", id_only=True)
        self.assertEquals(len(res_ids2n), 0)
        self.assertEquals(len(res_assoc2n), 0)

        # Find resources by lcstate
        res_ids1, res_assoc1 = data_store.find_res_by_lcstate(LCS.DEPLOYED, id_only=True)
        self.assertEquals(len(res_ids1), 2)
        self.assertEquals(len(res_assoc1), 2)
        self.assertEquals(set(res_ids1), set([admin_user_id, ds1_obj_id]))

        res_ids1a, res_assoc1a = data_store.find_res_by_lcstate(lcstate(LCS.DEPLOYED, AS.AVAILABLE), id_only=False)
        self.assertEquals(len(res_ids1a), 2)
        self.assertEquals(len(res_assoc1a), 2)
        self.assertEquals(set([o._id for o in res_ids1a]), set([admin_user_id, ds1_obj_id]))
        self.assertEquals(set([type(o).__name__ for o in res_ids1a]), set([RT.ActorIdentity, RT.Dataset]))

        res_ids2, res_assoc2 = data_store.find_res_by_lcstate( AS.AVAILABLE, RT.ActorIdentity, id_only=True)
        self.assertEquals(len(res_ids2), 1)
        self.assertEquals(len(res_assoc2), 1)
        self.assertEquals(set(res_ids2), set([admin_user_id]))

        # Find resources by name
        res_ids1, res_assoc1 = data_store.find_res_by_name('CTD1', id_only=True)
        self.assertEquals(len(res_ids1), 1)
        self.assertEquals(len(res_assoc1), 1)
        self.assertEquals(set(res_ids1), set([inst1_obj_id]))

        res_ids1a, res_assoc1a = data_store.find_res_by_name('CTD2', id_only=False)
        self.assertEquals(len(res_ids1a), 1)
        self.assertEquals(len(res_assoc1a), 1)
        self.assertEquals(set([o._id for o in res_ids1a]), set([inst2_obj_id]))
        self.assertEquals(set([type(o).__name__ for o in res_ids1a]), set([RT.InstrumentDevice]))

        res_ids2, res_assoc2 = data_store.find_res_by_name( 'John Doe', RT.ActorIdentity, id_only=True)
        self.assertEquals(len(res_ids2), 1)
        self.assertEquals(len(res_assoc2), 1)
        self.assertEquals(set(res_ids2), set([admin_user_id]))

        res_ids2n, res_assoc2n = data_store.find_res_by_name("NONE##", "XXXXX", id_only=True)
        self.assertEquals(len(res_ids2n), 0)
        self.assertEquals(len(res_assoc2n), 0)

        # Find associations by triple
        assocs = data_store.find_associations(admin_user_id, OWNER_OF, inst1_obj_id, id_only=True)
        self.assertEquals(len(assocs), 1)
        self.assertEquals(assocs[0], aid1)

        assocs = data_store.find_associations(admin_user_id, OWNER_OF, inst1_obj_id, id_only=False)
        self.assertEquals(len(assocs), 1)
        self.assertEquals(type(assocs[0]).__name__, "Association")

        assocs = data_store.find_associations(admin_user_id, None, inst1_obj_id, id_only=True)
        self.assertEquals(len(assocs), 1)
        self.assertEquals(assocs[0], aid1)

        assocs = data_store.find_associations(subject=inst1_obj_id, id_only=True)
        self.assertEquals(len(assocs), 1)

        assocs = data_store.find_associations(obj=inst1_obj_id, id_only=True)
        self.assertEquals(len(assocs), 2)

        assocs = data_store.find_associations(None, OWNER_OF, None, id_only=True)
        self.assertEquals(len(assocs), 3)

        assocs = data_store.find_associations(anyside=inst1_obj_id, id_only=True)
        self.assertEquals(len(assocs), 3)

        assocs = data_store.find_associations(anyside=inst1_obj_id, predicate=HAS_A, id_only=True)
        self.assertEquals(len(assocs), 2)

        assocs = data_store.find_associations(anyside=[inst1_obj_id,other_user_id], id_only=True)
        self.assertEquals(len(assocs), 4)

        assocs = data_store.find_associations(anyside=[[inst1_obj_id, HAS_A], [other_user_id, OWNER_OF]], id_only=True)
        self.assertEquals(len(assocs), 3)

        assocs = data_store.find_associations(anyside=[(inst1_obj_id, HAS_A), (other_user_id, OWNER_OF)], id_only=True)
        self.assertEquals(len(assocs), 3)

        # Test regression bug: Inherited resources in associations
        idev1_obj_id = self._create_resource(RT.InstrumentDevice, 'id1', description='')

        iag1_obj_id = self._create_resource(RT.InstrumentAgentInstance, 'ia1', description='')

        data_store.create_association(idev1_obj_id, PRED.hasAgentInstance, iag1_obj_id)

        att1 = self._create_resource(RT.Attachment, 'att1', keywords=[])
        att2 = self._create_resource(RT.Attachment, 'att2', keywords=['FOO'])
        att3 = self._create_resource(RT.Attachment, 'att3', keywords=['BAR','FOO'])

        res_list,key_list = data_store.find_resources_ext(restype="NONE", keyword="FOO")
        self.assertEqual(len(res_list), 0)
        res_list,key_list = data_store.find_resources_ext(keyword="FOO")
        self.assertEqual(len(res_list), 2)
        res_list,key_list = data_store.find_resources_ext(restype=RT.Attachment, keyword="FOO")
        self.assertEqual(len(res_list), 2)
        res_list,key_list = data_store.find_resources_ext(restype=RT.Attachment, keyword="FOO", limit=1)
        self.assertEqual(len(res_list), 1)
        res_list,key_list = data_store.find_resources_ext(restype=RT.Attachment, keyword="FOO", limit=1, skip=1)
        self.assertEqual(len(res_list), 1)

        res_list,key_list = data_store.find_resources_ext(restype="NONE", nested_type="ContactInformation")
        self.assertEqual(len(res_list), 0)
        res_list,key_list = data_store.find_resources_ext(nested_type="ContactInformation")
        self.assertEqual(len(res_list), 1)
        res_list,key_list = data_store.find_resources_ext(restype=RT.UserInfo, nested_type="ContactInformation", id_only=False)
        self.assertEqual(len(res_list), 1)
        self.assertEqual(res_list[0]._get_type(), RT.UserInfo)

        # Find by attribute
        admin_user2_id = self._create_resource(RT.UserInfo, 'Other User',
            contact=IonObject('ContactInformation', **{"individual_names_given": "Frank",
                                                       "email": "*****@*****.**"}),
            alt_ids=["ALT_ID1"])

        admin_user3_id = self._create_resource(RT.UserInfo, 'Different User',
            contact=IonObject('ContactInformation', **{"individual_names_given": "Frank",
                                                       "email": "*****@*****.**"}),
            alt_ids=["NS1:ALT_ID2", "ALT_ID2"])

        res_list,key_list = data_store.find_resources(restype="UserInfo")
        self.assertEqual(len(res_list), 3)

        res_list,key_list = data_store.find_resources_ext(restype="UserInfo", attr_name="contact.email")
        self.assertEqual(len(res_list), 3)

        res_list,key_list = data_store.find_resources_ext(restype="UserInfo", attr_name="contact.email", attr_value="*****@*****.**")
        self.assertEqual(len(res_list), 1)

        res_list,key_list = data_store.find_resources_ext(restype="UserInfo", attr_name="contact.email", attr_value="DOES NOT EXIST")
        self.assertEqual(len(res_list), 0)

        # Find by alternate id
        res_list,key_list = data_store.find_resources_ext(alt_id="ALT_ID1")
        self.assertEqual(len(res_list), 1)

        res_list,key_list = data_store.find_resources_ext(alt_id="ALT_ID2")
        self.assertEqual(len(res_list), 2)

        res_list,key_list = data_store.find_resources_ext(alt_id="ALT_ID2", alt_id_ns="NS1")
        self.assertEqual(len(res_list), 1)

        res_list,key_list = data_store.find_resources_ext(alt_id="ALT_ID2", alt_id_ns="_")
        self.assertEqual(len(res_list), 1)

        res_list,key_list = data_store.find_resources_ext(alt_id="ALT_ID2", alt_id_ns="BULL")
        self.assertEqual(len(res_list), 0)

        res_list,key_list = data_store.find_resources_ext(alt_id=None, alt_id_ns="NS1")
        self.assertEqual(len(res_list), 1)

        res_list,key_list = data_store.find_resources_ext(alt_id=None, alt_id_ns="_", id_only=True)
        self.assertEqual(len(res_list), 2)

        res_list,key_list = data_store.find_resources_ext(alt_id=None, alt_id_ns="_", id_only=False)
        self.assertEqual(len(res_list), 2)