-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_channels.py
More file actions
77 lines (67 loc) · 2.68 KB
/
test_channels.py
File metadata and controls
77 lines (67 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock
import pytest
from sift_client._internal.util.channels import resolve_calculated_channels
from sift_client.sift_types.calculated_channel import (
CalculatedChannel,
CalculatedChannelCreate,
ChannelReference,
)
from sift_client.sift_types.channel import Channel
class TestResolveCalculatedChannels:
@pytest.mark.asyncio
async def test_none_passthrough(self):
api = MagicMock()
api.find = AsyncMock(return_value=None)
assert await resolve_calculated_channels(None, channels_api=api) is None
@pytest.mark.asyncio
async def test_resolves_name_to_uuid(self):
mock_ch = MagicMock(spec=Channel)
mock_ch._id_or_error = "resolved-uuid"
api = MagicMock()
api.find = AsyncMock(return_value=mock_ch)
cc = MagicMock(spec=CalculatedChannel)
cc.name, cc.expression, cc.units = "calc", "$1 + 10", "m/s"
cc.asset_ids = ["asset-1"]
cc.channel_references = [
ChannelReference(channel_reference="$1", channel_identifier="sensor.vel")
]
result = await resolve_calculated_channels([cc], channels_api=api)
assert result is not None
assert len(result) == 1
refs = result[0].expression_channel_references
assert refs is not None
assert refs[0].channel_identifier == "resolved-uuid"
@pytest.mark.asyncio
async def test_skips_lookup_for_calculated_channel_version_id(self):
api = MagicMock()
api.find = AsyncMock()
cc = CalculatedChannelCreate(
name="nested",
expression="$1 + 1",
expression_channel_references=[
ChannelReference(channel_reference="$1", calculated_channel="v-nested")
],
)
result = await resolve_calculated_channels([cc], channels_api=api)
api.find.assert_not_awaited()
assert result is not None
refs = result[0].expression_channel_references
assert refs is not None
assert refs[0].calculated_channel == "v-nested"
assert refs[0].channel_identifier is None
@pytest.mark.asyncio
async def test_keeps_identifier_when_not_found(self):
api = MagicMock()
api.find = AsyncMock(return_value=None)
cc = CalculatedChannelCreate(
name="x",
expression="$1",
units="m",
expression_channel_references=[
ChannelReference(channel_reference="$1", channel_identifier="ch-1")
],
)
result = await resolve_calculated_channels([cc], channels_api=api)
assert result is not None
assert result[0] == cc