def test_update_duplicate(self, api_factory):

        api_instance = api_factory.LocationApi()

        try:

            loc = self.get_next_location()
            loc = openapi_client.Location(None, 27.46362, 90.49542, 'country',
                                          'Trongsa, Trongsa, Bhutan', 'pv_3_locations.txt', 'BTN')
            loc.attrs = [
                openapi_client.Attr(attr_type='partner_name', attr_value='bhutan', study_name='1234-PV')
            ]
            created = api_instance.create_location(loc)
            looked_up_locs = api_instance.download_partner_location(loc.attrs[0].attr_value)
            looked_up = looked_up_locs.locations[0]
            newloc = openapi_client.Location(None, 28.46362, 91.49542, 'location',
                                        'new_Trongsa, Trongsa, Bhutan', 'new_pv_3_locations.txt', 'IND')
            new_created = api_instance.create_location(newloc)
            with pytest.raises(ApiException, status=422):
                created.location_id = new_created.location_id
                updated = api_instance.update_location(new_created.location_id, created)


            api_instance.delete_location(looked_up.location_id)
            api_instance.delete_location(new_created.location_id)

        except ApiException as error:
            self.check_api_exception(api_factory, "LocationApi->create_location", error)
Example #2
0
    def test_location_study(self, api_factory):

        api_instance = api_factory.LocationApi()
        study_api = api_factory.StudyApi()

        loc = openapi_client.Location(None, 27.46362, 90.49542, 'country',
                                      'Trongsa, Trongsa, Bhutan',
                                      'pv_3_locations.txt', 'BTN')
        loc.attrs = [
            openapi_client.Attr(attr_type='partner_name',
                                attr_value='bhutan',
                                study_name='2001-MD-UP')
        ]
        try:
            created = api_instance.create_location(loc)
            studies = study_api.download_studies()

            found = False
            for study in studies.studies:
                if study.name == '2001-MD-UP' and study.code == '2001':
                    found = True

            assert found, 'Study does not exist'

            api_instance.delete_location(created.location_id)
        except ApiException as error:
            self.check_api_exception(api_factory,
                                     "LocationApi->create_location", error)
    def test_duplicate_partner_name(self, api_factory):

        api_instance = api_factory.LocationApi()

        try:

            loc = self.get_next_location()
            loc.attrs = [
                openapi_client.Attr(attr_type='partner_name', attr_value='Kobeni', study_name='5002-PF-MR-ANON'),
            ]

            loc1 = openapi_client.Location(None, 28.46362, 91.49542, 'country',
                                          'Trongsa, Trongsa, Bhutan', 'pv_3_locations.txt', 'BTN')
            loc1.attrs = [
                openapi_client.Attr(attr_type='partner_name', attr_value='Kobeni', study_name='5002-PF-MR-ANON'),
            ]

            created = api_instance.create_location(loc)

            created1 = api_instance.create_location(loc1)

            assert created.location_id != created1.location_id

            api_instance.delete_location(created.location_id)
            api_instance.delete_location(created1.location_id)

        except ApiException as error:
            self.check_api_exception(api_factory, "LocationApi->create_location", error)
    def create_location_from_values(self, values, prefix):

        if prefix + 'location_name' not in values:
            #print("No {}location name: {}".format(prefix, values))
            return None, None

        partner_name = values[prefix + 'location_name']

        if not partner_name:
            if prefix + 'latitude' in values and values[prefix + 'latitude']:
                self.report("No location name: ", values)
            return None, None

        #Will have been set to 0000 if not present
        if 'study_id' in values:
            study_id = values['study_id'][:4]

        loc = openapi_client.Location(None)

        loc.attrs = [
            openapi_client.Attr(attr_type='partner_name',
                                attr_value=partner_name,
                                attr_source=self._event_set,
                                study_name=study_id)
        ]
        if prefix + "src_location_id" in values:
            src_lid = openapi_client.Attr(attr_type='src_location_id',
                                          attr_value=values[prefix +
                                                            'src_location_id'],
                                          attr_source=self._event_set,
                                          study_name=study_id)
            loc.attrs.append(src_lid)

        try:
            if prefix + 'latitude' in values and values[prefix + 'latitude']:
                loc.latitude = round(
                    float(Decimal(values[prefix + 'latitude'])), 7)
            if prefix + 'longitude' in values and values[prefix + 'longitude']:
                loc.longitude = round(
                    float(Decimal(values[prefix + 'longitude'])), 7)
        except Exception as excp:
            print(excp)
            pass

        if prefix + 'resolution' in values:
            loc.accuracy = values[prefix + 'resolution']

        if prefix + 'country' in values:
            loc.country = values[prefix + 'country']

        if 'description' in values:
            loc.notes = self._event_set + ' ' + values['description']
        else:
            loc.notes = self._event_set

        #print(values)
        #print(loc)

        return partner_name, loc
    def get_next_location(self):

        loc = openapi_client.Location(None)

        loc.latitude = self._location_number
        loc.longitude = self._location_number
        loc.curated_name = 'Test Location{}'.format(self._location_number)
        loc.country = 'BTN'
        loc.notes = 'Generated location'

        self._location_number = self._location_number + 1

        return loc
    def test_update_missing(self, api_factory):

        api_instance = api_factory.LocationApi()

        try:

            loc = self.get_next_location()
            newloc = openapi_client.Location(None, 28.46362, 91.49542, 'location',
                                        'new_Trongsa, Trongsa, Bhutan', 'new_pv_3_locations.txt', 'IND')
            fake_id = uuid.uuid4()
            newloc.location_id = str(fake_id)

            if api_factory.is_authorized(None):
                with pytest.raises(ApiException, status=404):
                    updated = api_instance.update_location(newloc.location_id, newloc)
            else:
                with pytest.raises(ApiException, status=403):
                    updated = api_instance.update_location(newloc.location_id, newloc)

        except ApiException as error:
            self.check_api_exception(api_factory, "LocationApi->update_location", error)
    def test_get_location_attrs(self, api_factory):

        metadata_api_instance = api_factory.MetadataApi()
        api_instance = api_factory.LocationApi()

        try:
            loc = self.get_next_location()
            loc = openapi_client.Location(None, 27.46362, 90.49542, 'country',
                                          'Trongsa, Trongsa, Bhutan', 'pv_3_locations.txt', 'BTN')
            loc.attrs = [
                openapi_client.Attr(attr_type='partner_name', attr_value='bhutan', study_name='1234-PV')
            ]
            created = api_instance.create_location(loc)

            idents = metadata_api_instance.get_location_attr_types()

            assert 'partner_name' in idents

            api_instance.delete_location(created.location_id)

        except ApiException as error:
            self.check_api_exception(api_factory, "LocationApi->create_location", error)
    def test_gps_lookup_negative(self, api_factory):

        api_instance = api_factory.LocationApi()

        try:

            loc = openapi_client.Location(None, 15.82083, -9.4145, None, None, None, None)
            loc.attrs = [
                openapi_client.Attr(attr_type='partner_name', attr_value='Kobeni', study_name='5002-PF-MR-ANON')
            ]
            created = api_instance.create_location(loc)

            looked_up = api_instance.download_gps_location(15.82083, -9.4145)

            fetched = looked_up.locations[0]

            assert created == fetched, "create response != download response"
            fetched.location_id = None
            assert loc == fetched, "upload != download response"
            api_instance.delete_location(created.location_id)

        except ApiException as error:
            self.check_api_exception(api_factory, "LocationApi->create_location", error)
Example #9
0
    def find_country_for_study(self, country_value, study, country_ident):

        if study[:4] not in self._country_study_location_cache:
            self._country_study_location_cache[study[:4]] = {}
        elif country_value in self._country_study_location_cache[study[:4]]:
            return self._country_study_location_cache[study[:4]][country_value]

        try:
            named_locations = self._dao.download_partner_location(
                self._country_cache[country_value].english)

            #print('Locations for {}'.format(self._country_cache[country_value].english))
            #print(named_locations)

            for named_loc in named_locations.locations:
                for ident in named_loc.attrs:
                    if ident.study_name:
                        if ident.study_name[:4] == study[:4]:
                            self._country_study_location_cache[
                                study[:4]][country_value] = named_loc
                            #print('Found location')
                            return named_loc
        except ApiException as e:
            pass

        #print('location not found for study {}'.format(study))
        location = None

        if country_value in self._country_location_cache:
            cached_country = self._country_location_cache[country_value]

            if 'location' in cached_country:
                location = cached_country['location']
            else:
                try:
                    location = self._dao.download_gps_location(
                        cached_country['latitude'],
                        cached_country['longitude'])
                    location = location.locations[0]
                except ApiException as exp:
                    lat = round(float(Decimal(cached_country['latitude'])), 7)
                    lng = round(float(Decimal(cached_country['longitude'])), 7)
                    loc = openapi_client.Location(
                        None,
                        lat,
                        lng,
                        accuracy='country',
                        country=self._country_cache[country_value].alpha3)
                    loc.attrs = [country_ident]
                    location = self._dao.create_location(loc)
                cached_country['location'] = location
                self._country_location_cache[country_value][
                    'location'] = location

            if location:
                self._country_study_location_cache[
                    study[:4]][country_value] = location

        else:
            self.os_processor.report(
                "Unknown country {} in study {}".format(country_value, study),
                None)

        return location
Example #10
0
    def create_test_samples(self, api_factory):

        created_es = None
        created_se = None
        created = None
        created1 = None
        created2 = None

        try:

            api_instance = api_factory.OriginalSampleApi()
            loc_api_instance = api_factory.LocationApi()
            se_api_instance = api_factory.SamplingEventApi()
            es_api_instance = api_factory.EventSetApi()
            ds_api_instance = api_factory.DerivativeSampleApi()
            study_api = api_factory.StudyApi()

            event_set_name = 'test_hist_event_set_lookup'
            study_name = '8004-MD-UP'

            created_es = es_api_instance.create_event_set(event_set_name)

            loc = openapi_client.Location(None, 28.463, 91.495, 'country',
                                          'Trongsa, Trongsa, Bhutan',
                                          'history_locations.txt', 'BTN')

            location = loc_api_instance.create_location(loc)

            sampling_event = openapi_client.SamplingEvent(None,
                                                          date(2017, 10, 10),
                                                          doc_accuracy='month')

            sampling_event.location_id = location.location_id

            created_se = se_api_instance.create_sampling_event(sampling_event)

            es_api_instance.create_event_set_item(event_set_name,
                                                  created_se.sampling_event_id)

            samp = openapi_client.OriginalSample(None,
                                                 study_name=study_name,
                                                 partner_species='PF')
            samp.attrs = [
                openapi_client.Attr(attr_type='ds_os_attr',
                                    attr_value='123456')
            ]
            created = api_instance.create_original_sample(samp)
            study_detail = study_api.download_study(study_name)
            study_detail.partner_species[0].taxa = [
                openapi_client.Taxonomy(taxonomy_id=5833)
            ]
            study_api.update_study(study_name, study_detail)

            samp1 = openapi_client.DerivativeSample(None)
            samp2 = openapi_client.DerivativeSample(None)

            samp1.attrs = [
                openapi_client.Attr(attr_type='test1',
                                    attr_value='test1',
                                    attr_source='ds_os_attr')
            ]
            samp2.attrs = [
                openapi_client.Attr(attr_type='test2',
                                    attr_value='test2',
                                    attr_source='ds_os_attr')
            ]
            samp1.original_sample_id = created.original_sample_id
            samp2.original_sample_id = created.original_sample_id

            created1 = ds_api_instance.create_derivative_sample(samp1)
            created2 = ds_api_instance.create_derivative_sample(samp2)

            created.sampling_event_id = created_se.sampling_event_id

            api_instance.update_original_sample(created.original_sample_id,
                                                created)

        except ApiException as error:
            self.check_api_exception(
                api_factory, "OriginalSampleApi->create_original_sample",
                error)

        return created_es, created_se, created, created1, created2