Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
asmfstatoil committed Oct 28, 2024
1 parent 2675172 commit fd7bb5b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
8 changes: 4 additions & 4 deletions tagreader/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ def get_units(self, tags: Union[str, List[str]]):
units[tag] = unit
except Exception:
if self.search(tag) == []: # check for nonexisting string
print("Tag not found: " + str(tag))
break
logger.warning(f"Tag not found: {tag}")
continue
return units

def get_descriptions(self, tags: Union[str, List[str]]) -> Dict[str, str]:
Expand All @@ -475,8 +475,8 @@ def get_descriptions(self, tags: Union[str, List[str]]) -> Dict[str, str]:
descriptions[tag] = desc
except Exception:
if self.search(tag) == []: # check for nonexisting string
print("Tag not found: " + str(tag))
break
logger.warning(f"Tag not found: {tag}")
continue
return descriptions

def read_tags(
Expand Down
19 changes: 11 additions & 8 deletions tagreader/web_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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":
Expand Down Expand Up @@ -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
Expand Down
64 changes: 30 additions & 34 deletions tests/test_AspenHandlerREST_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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("")
Expand All @@ -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.
Expand Down

0 comments on commit fd7bb5b

Please sign in to comment.