diff --git a/tagreader/clients.py b/tagreader/clients.py index 9b72d16..6d5adb3 100644 --- a/tagreader/clients.py +++ b/tagreader/clients.py @@ -442,15 +442,20 @@ def get_units(self, tags: Union[str, List[str]]): tags = [tags] units = {} for tag in tags: - if self.cache is not None: - r = self.cache.get_metadata(key=tag, properties="unit") - if r is not None and "unit" in r: - units[tag] = r["unit"] - if tag not in units: - unit = self.handler._get_tag_unit(tag) - if self.cache is not None and unit is not None: - self.cache.put_metadata(key=tag, value={"unit": unit}) - units[tag] = unit + try: + if self.cache is not None: + r = self.cache.get_metadata(key=tag, properties="unit") + if r is not None and "unit" in r: + units[tag] = r["unit"] + if tag not in units: + unit = self.handler._get_tag_unit(tag) + if self.cache is not None and unit is not None: + self.cache.put_metadata(key=tag, value={"unit": unit}) + units[tag] = unit + except Exception: + if self.search(tag) == []: # check for nonexisting string + logger.warning(f"Tag not found: {tag}") + continue return units def get_descriptions(self, tags: Union[str, List[str]]) -> Dict[str, str]: @@ -458,15 +463,20 @@ def get_descriptions(self, tags: Union[str, List[str]]) -> Dict[str, str]: tags = [tags] descriptions = {} for tag in tags: - if self.cache is not None: - r = self.cache.get_metadata(key=tag, properties="description") - if r is not None and "description" in r: - descriptions[tag] = r["description"] - if tag not in descriptions: - desc = self.handler._get_tag_description(tag) - if self.cache is not None and desc is not None: - self.cache.put_metadata(key=tag, value={"description": desc}) - descriptions[tag] = desc + try: + if self.cache is not None: + r = self.cache.get_metadata(key=tag, properties="description") + if r is not None and "description" in r: + descriptions[tag] = r["description"] + if tag not in descriptions: + desc = self.handler._get_tag_description(tag) + if self.cache is not None and desc is not None: + self.cache.put_metadata(key=tag, value={"description": desc}) + descriptions[tag] = desc + except Exception: + if self.search(tag) == []: # check for nonexisting string + logger.warning(f"Tag not found: {tag}") + continue return descriptions def read_tags( diff --git a/tagreader/web_handlers.py b/tagreader/web_handlers.py index 3487067..6455ade 100644 --- a/tagreader/web_handlers.py +++ b/tagreader/web_handlers.py @@ -503,7 +503,10 @@ def search( if not desc and not return_desc: ret.append(tagname) else: - description = self._get_tag_description(tagname) + try: + description = self._get_tag_description(tagname) + except KeyError: + description = "" ret.append((tagname, description)) if not desc: @@ -523,11 +526,11 @@ def _get_tag_unit(self, tag: str): query = self.generate_get_unit_query(tag) url = urljoin(self.base_url, "TagInfo") data = self.fetch(url, params=query) - try: - attr_data = data["data"]["tags"][0]["attrData"] - except KeyError as e: - logger.error(f"Error. I got this: {data}") - raise e + # try: + attr_data = data["data"]["tags"][0]["attrData"] + # except KeyError as e: + # logger.error(f"Error. I got this: {data}") + # raise e unit = "" for a in attr_data: if a["g"] == "Units": @@ -559,8 +562,8 @@ def _get_tag_description(self, tag: str): try: data = self.fetch(url, params=query) desc = data["data"]["tags"][0]["attrData"][0]["samples"][0]["v"] - except KeyError: - desc = "" + # except KeyError: + # desc = "" except JSONDecodeError: desc = "" return desc diff --git a/tests/test_AspenHandlerREST_connect.py b/tests/test_AspenHandlerREST_connect.py index a9eca7e..59c5988 100644 --- a/tests/test_AspenHandlerREST_connect.py +++ b/tests/test_AspenHandlerREST_connect.py @@ -24,8 +24,9 @@ VERIFY_SSL = False if is_AZURE_PIPELINE else get_verify_ssl() -SOURCE = "SNA" -TAG = "ATCAI" +SOURCE = "TRB" +TAG = "xxx" +FAKE_TAG = "so_random_it_cant_exist" START_TIME = datetime(2023, 5, 1, 10, 0, 0) STOP_TIME = datetime(2023, 5, 1, 11, 0, 0) SAMPLE_TIME = timedelta(seconds=60) @@ -77,44 +78,31 @@ def test_verify_connection(aspen_handler: AspenHandlerWeb) -> None: def test_search_tag(client: IMSClient) -> None: - res = client.search(tag="so_specific_it_cannot_possibly_exist", desc=None) + res = client.search(tag=FAKE_TAG, desc=None) assert 0 == len(res) - res = client.search(tag="ATCAI", desc=None) - assert res == [("ATCAI", "Sine Input")] + res = client.search(tag="AverageCPUTimeVals", desc=None) + assert res == [("AverageCPUTimeVals", "Average CPU Time")] - res = client.search(tag="ATCM*", desc=None) - assert 5 <= len(res) - - [taglist, desclist] = zip(*res) - assert "ATCMIXTIME1" in taglist - assert desclist[taglist.index("ATCMIXTIME1")] == "MIX TANK 1 TIMER" - - res = client.search(tag="ATCM*", desc=None) - assert 5 <= len(res) + res = client.search(tag="Aspen*", desc=None, return_desc=False) + assert len(res) < 5 assert isinstance(res, list) - assert isinstance(res[0], tuple) - - res = client.search("AspenCalcTrigger1", desc=None) - assert res == [("AspenCalcTrigger1", "")] - - res = client.search("ATC*", "Sine*") - assert res == [("ATCAI", "Sine Input")] - with pytest.raises(ValueError): - _ = client.search(desc="Sine Input") # noqa + assert isinstance(res[0], str) - res = client.search(tag="ATCM*", return_desc=False) - assert 5 <= len(res) + res = client.search(tag="Aspen*", desc=None) + assert len(res) < 5 assert isinstance(res, list) - assert isinstance(res[0], str) + assert isinstance(res[0], tuple) res = client.search("AspenCalcTrigger1") assert res == [("AspenCalcTrigger1", "")] res = client.search("AspenCalcTrigger1", desc=None) assert res == [("AspenCalcTrigger1", "")] - res = client.search("ATC*", "Sine*") - assert res == [("ATCAI", "Sine Input")] + res = client.search("AverageCPUTimeVals", "*CPU*") + assert res == [("AverageCPUTimeVals", "Average CPU Time")] + with pytest.raises(ValueError): + _ = client.search(desc="Sine Input") # noqa with pytest.raises(ValueError): res = client.search("") @@ -126,17 +114,25 @@ def test_search_tag(client: IMSClient) -> None: def test_read_unknown_tag(client: IMSClient) -> None: - df = client.read( - tags=["so_random_it_cant_exist"], start_time=START_TIME, end_time=STOP_TIME - ) + df = client.read(tags=[FAKE_TAG], start_time=START_TIME, end_time=STOP_TIME) assert len(df.index) == 0 - df = client.read( - tags=[TAG, "so_random_it_cant_exist"], start_time=START_TIME, end_time=STOP_TIME - ) + df = client.read(tags=[TAG, FAKE_TAG], start_time=START_TIME, end_time=STOP_TIME) assert len(df.index) > 0 assert len(df.columns == 1) +def test_get_units(client: IMSClient) -> None: + d = client.get_units(FAKE_TAG) + assert isinstance(d, dict) + assert len(d.items()) == 0 + + +def test_get_desc(client: IMSClient) -> None: + d = client.get_descriptions(FAKE_TAG) + assert isinstance(d, dict) + assert len(d.items()) == 0 + + def test_query_sql(client: IMSClient) -> None: # The % causes WC_E_SYNTAX error in result. Tried "everything" but no go. # Leaving it for now.