diff --git a/cdci_data_analysis/analysis/instrument.py b/cdci_data_analysis/analysis/instrument.py index 03e7e7e67..5217f1a34 100644 --- a/cdci_data_analysis/analysis/instrument.py +++ b/cdci_data_analysis/analysis/instrument.py @@ -504,24 +504,31 @@ def check_instrument_query_role(self, query_obj, product_type, roles, par_dic): else: return True - def get_html_draw(self, prod_name, image,image_header,catalog=None,**kwargs): - return self.get_query_by_name(prod_name).get_html_draw( image,image_header,catalog=catalog,**kwargs) + def get_html_draw(self, prod_name, image, image_header, catalog=None, **kwargs): + return self.get_query_by_name(prod_name).get_html_draw(image, image_header, catalog=catalog, **kwargs) - #def get_par_by_name(self,par_name, validate=False): - def get_par_by_name(self,par_name): + def get_par_by_name(self, par_name, add_src_query=True, add_instr_query=True, prod_name=None): p=None - + for _query in self._queries_list: + if isinstance(_query, SourceQuery) and not add_src_query: + continue + + if isinstance(_query, InstrumentQuery) and not add_instr_query: + continue + + if isinstance(_query, ProductQuery) and prod_name is not None and _query.name!=self.query_dictionary[prod_name]: + continue + if par_name in _query.par_names: - # TODO: this picks the last one if there are many?.. + if p is not None: + self.logger.warning('Same parameter name %s in several queries. ' + 'Will return parameter from the last query') p = _query.get_par_by_name(par_name) if p is None: raise Warning('parameter', par_name, 'not found') - # if validate and hasattr(p, 'check_value'): - # p.check_value(p.value) - return p def show_parameters_list(self): @@ -532,32 +539,22 @@ def show_parameters_list(self): _query.show_parameters_list() print("-------------") - def get_parameters_list_as_json(self,add_src_query=True,add_instr_query=True,prod_name=None): + def get_parameters_list_as_json(self, add_src_query=True, add_instr_query=True, prod_name=None): l=[{'instrumet':self.name}] l.append({'prod_dict':self.query_dictionary}) - #print('--> dict',self.query_dictionary) - for _query in self._queries_list: - _add_query = True - if isinstance(_query,SourceQuery) and add_src_query==False: - _add_query=False - #print('src',_query.name) + if isinstance(_query,SourceQuery) and not add_src_query: + continue - if isinstance(_query,InstrumentQuery) and add_instr_query==False: - _add_query=False - #print('isntr', _query.name) + if isinstance(_query,InstrumentQuery) and not add_instr_query: + continue - if isinstance(_query, ProductQuery) and prod_name is not None and _query.name==self.query_dictionary[prod_name]: - _add_query = True - #print('prd', _query.name,prod_name) - elif isinstance(_query, ProductQuery) and prod_name is not None and _query.name!=self.query_dictionary[prod_name]: - #print('prd', _query.name, prod_name) - _add_query = False + if isinstance(_query, ProductQuery) and prod_name is not None and _query.name!=self.query_dictionary[prod_name]: + continue - if _add_query == True: - l.append(_query.get_parameters_list_as_json(prod_dict=self.query_dictionary)) + l.append(_query.get_parameters_list_as_json(prod_dict=self.query_dictionary)) return l diff --git a/cdci_data_analysis/analysis/queries.py b/cdci_data_analysis/analysis/queries.py index 4908870a6..e42bedea0 100644 --- a/cdci_data_analysis/analysis/queries.py +++ b/cdci_data_analysis/analysis/queries.py @@ -114,15 +114,28 @@ def set_par_value(self,name,value): def _build_parameters_list(self,_list): _l = [] + _names = [] if _list is None: pass else: - for p in _list: if isinstance(p, Parameter): + if p.name in _names: + raise RuntimeError('Parameter type %s have duplicate name %s in the query %s', + p.__class__.__name__, p.name, self) _l.append(p) + if p.name is not None: + _names.append(p.name) else: - _l.extend(p.to_list()) + # parametertuple + pars = p.to_list() + for x in pars: + if x.name in _names: + raise RuntimeError('Parameter type %s have duplicate name %s in the query %s', + p.__class__.__name__, p.name, self) + + _l.extend(pars) + _names.extend([x.name for x in pars if x.name is not None]) return _l def show_parameters_list(self): diff --git a/tests/test_parameters.py b/tests/test_parameters.py index 13db06886..0327bb0e2 100644 --- a/tests/test_parameters.py +++ b/tests/test_parameters.py @@ -30,8 +30,8 @@ @pytest.mark.fast -@pytest.mark.parametrize("add_duplicate", [True, False]) -def test_repeating_parameters(add_duplicate): +@pytest.mark.parametrize("same_query", [True, False]) +def test_repeating_parameters(caplog, same_query): src_query = SourceQuery("src_query") instr_query = InstrumentQuery( @@ -46,34 +46,31 @@ def test_repeating_parameters(add_duplicate): ) p2 = Name(value="default-name", name="duplicate-name") - if add_duplicate: + if same_query: parameters_list = [p1, p2] + with pytest.raises(RuntimeError): + product_query = ProductQuery("test_product_query", parameters_list=parameters_list) else: - parameters_list = [p1] - - product_query = ProductQuery("test_product_query", parameters_list=parameters_list) - - query_dictionary = {"numerical": "numerical_parameters_dummy_query"} - - instrument = Instrument( - "empty-async", - src_query=src_query, - instrumet_query=instr_query, - product_queries_list=[product_query], - query_dictionary=query_dictionary, - data_server_query_class=None, - ) - - # TODO: this is current behavior. This is hardly desirable. It should be fixed eventually. - if add_duplicate: + product_query1 = ProductQuery("test_product_query1", parameters_list=[p1]) + product_query2 = ProductQuery("test_product_query2", parameters_list=[p2]) + + query_dictionary = {"prod1": "test_product_query1", + "prod2": "test_product_query2",} + + instrument = Instrument( + "empty-async", + src_query=src_query, + instrumet_query=instr_query, + product_queries_list=[product_query1, product_query2], + query_dictionary=query_dictionary, + data_server_query_class=None, + ) + + assert instrument.get_par_by_name("duplicate-name", prod_name='prod1') == p1 + assert instrument.get_par_by_name("duplicate-name", prod_name='prod2') == p2 + assert instrument.get_par_by_name("duplicate-name") == p2 - assert instrument.get_par_by_name("duplicate-name") != p1 - assert [p["field name"] for p in product_query.par_dictionary_list] == [ - "duplicate-name", - "duplicate-name", - ] - else: - assert instrument.get_par_by_name("duplicate-name") == p1 + assert 'Same parameter name' in caplog.text @pytest.mark.fast def test_input_prod_list():