File: test_apiresource.py

package info (click to toggle)
python-aiopvapi 3.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: python: 3,123; xml: 850; makefile: 5
file content (195 lines) | stat: -rw-r--r-- 6,266 bytes parent folder | download
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from unittest.mock import Mock

from aiopvapi.helpers.api_base import ApiResource, ApiEntryPoint
from aiopvapi.helpers.aiorequest import AioRequest
from tests.fake_server import TestFakeServer, FAKE_BASE_URL


class TestApiResource(TestFakeServer):
    def get_resource_raw_data(self):
        return {"id": 1}

    def get_resource_uri(self):
        return "http://{}/base/1".format(FAKE_BASE_URL)

    def get_resource(self):
        """Get the resource being tested."""
        _request = Mock(spec=AioRequest)
        _request.hub_ip = FAKE_BASE_URL
        _request.api_version = 2
        _request.api_path = "api"
        return ApiResource(_request, "base", self.get_resource_raw_data())

    def setUp(self):
        super().setUp()
        self.resource = self.get_resource()

    def test_id_property(self):
        """Test id property."""
        self.assertEqual(self.get_resource_raw_data()["id"], self.resource.id)
        # Try to replace raw data, id shouldn't change!
        self.resource.raw_data = {"id": 2}
        self.assertEqual(self.get_resource_raw_data()["id"], self.resource.id)

    def test_full_path(self):
        self.assertEqual(
            self.resource.base_path, "http://{}/api/base".format(FAKE_BASE_URL)
        )

    def test_name_property(self):
        """Test name property."""
        self.assertEqual("", self.resource.name)
        self.resource.raw_data = {"name": "Name"}
        self.assertEqual("Name", self.resource.name)
        self.resource.raw_data = {"name": "Name", "name_unicode": "Name Unicode"}
        self.assertEqual("Name Unicode", self.resource.name)

    def test_raw_data_property(self):
        """Test raw_data getter/setter."""
        self.assertEqual(self.get_resource_raw_data(), self.resource.raw_data)
        # Set new raw data
        data = {"name": "name"}
        self.resource.raw_data = data
        self.assertEqual({"name": "name"}, self.resource.raw_data)
        # Try to change the original data, resource should have made a copy!
        data["name"] = "no name"
        self.assertEqual({"name": "name"}, self.resource.raw_data)


class TestApiResource_V3(TestApiResource):
    def get_resource(self):
        """Get the resource being tested."""
        _request = Mock()
        _request.hub_ip = FAKE_BASE_URL
        _request.api_version = 3
        _request.api_path = "home"
        return ApiResource(_request, "base", self.get_resource_raw_data())

    def test_full_path(self):
        self.assertEqual(
            self.resource.base_path, "http://{}/home/base".format(FAKE_BASE_URL)
        )

    # def test_delete_200(self, mocked):
    #     """Test delete resources with status 200."""
    #     mocked.delete(self.get_resource_uri(),
    #                   body="",
    #                   status=200,
    #                   headers={'content-type': 'application/json'})
    #     val = self.loop.run_until_complete(self.resource.delete())
    #     self.assertIsNone(val)
    #
    # @aioresponses()
    # def test_delete_204(self, mocked):
    #     """Test delete resources with status 204."""
    #     mocked.delete(self.get_resource_uri(),
    #                   body="",
    #                   status=204,
    #                   headers={'content-type': 'application/json'})
    #     self.assertTrue(self.loop.run_until_complete(self.resource.delete()))
    #
    # @aioresponses()
    # def test_delete_201(self, mocked):
    #     """Test delete resources with wrong status."""
    #     mocked.delete(self.get_resource_uri(),
    #                   body="",
    #                   status=201,
    #                   headers={'content-type': 'application/json'})
    #     with self.assertRaises(PvApiResponseStatusError):
    #         self.loop.run_until_complete(self.resource.delete())


test_data1 = {
    "sceneMemberIds": [60113, 27759, 63292, 18627, 59345, 32461, 63181, 45145, 64087],
    "sceneMemberData": [
        {
            "id": 60113,
            "sceneId": 64040,
            "shadeId": 18390,
            "positions": {"position1": 65535, "posKind1": 1},
        },
        {
            "id": 27759,
            "sceneId": 34037,
            "shadeId": 11155,
            "positions": {
                "position1": 7563,
                "posKind1": 1,
                "position2": 16157,
                "posKind2": 2,
            },
        },
        {
            "id": 63292,
            "sceneId": 53808,
            "shadeId": 11155,
            "positions": {"position1": 0, "posKind1": 1, "position2": 0, "posKind2": 2},
        },
        {
            "positions": {
                "posKind2": 2,
                "position2": 42144,
                "posKind1": 1,
                "position1": 23390,
            },
            "id": 18627,
            "sceneId": 43436,
            "shadeId": 11155,
            "type": 0,
        },
        {
            "id": 59345,
            "sceneId": 39635,
            "shadeId": 11155,
            "positions": {
                "position1": 45598,
                "posKind1": 1,
                "position2": 0,
                "posKind2": 2,
            },
        },
        {
            "id": 32461,
            "sceneId": 34037,
            "shadeId": 18390,
            "positions": {"position1": 65535, "posKind1": 1},
        },
        {
            "positions": {"posKind1": 1, "position1": 58134},
            "id": 63181,
            "sceneId": 43436,
            "shadeId": 18390,
            "type": 0,
        },
        {
            "id": 45145,
            "sceneId": 64040,
            "shadeId": 11155,
            "positions": {
                "position1": 8404,
                "posKind1": 1,
                "position2": 42209,
                "posKind2": 2,
            },
        },
        {
            "id": 64087,
            "sceneId": 49867,
            "shadeId": 18390,
            "positions": {"posKind1": 1, "position1": 0},
            "type": 0,
        },
    ],
}


def test_clean_names():
    req = Mock()
    req.hub_ip = "123.123.123"
    req.api_version = 2
    req.api_path = "api"
    api = ApiEntryPoint(req, "abc")
    try:
        api._sanitize_resources(test_data1)
    except NotImplementedError:
        pass