|
12 | 12 | import uuid
|
13 | 13 | import os
|
14 | 14 | import wiotp.sdk
|
| 15 | +import time |
| 16 | +from wiotp.sdk.device import ManagedDeviceClient |
| 17 | +from wiotp.sdk import Utf8Codec |
15 | 18 |
|
16 | 19 |
|
17 | 20 | class TestDeviceMgd(testUtils.AbstractTest):
|
@@ -45,3 +48,224 @@ def testManagedDeviceConnect(self, device):
|
45 | 48 | assert managedDevice.isConnected() == True
|
46 | 49 | managedDevice.disconnect()
|
47 | 50 | assert managedDevice.isConnected() == False
|
| 51 | + |
| 52 | + def testManagedDeviceSetPropertyNameNone(self): |
| 53 | + with pytest.raises(Exception) as e: |
| 54 | + config = { |
| 55 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 56 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 57 | + } |
| 58 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 59 | + managedDeviceClientValue.setProperty(value=1) |
| 60 | + assert "Unsupported property name: " in str(e.value) |
| 61 | + |
| 62 | + def testManagedDeviceSetPropertyValue(self): |
| 63 | + try: |
| 64 | + config = { |
| 65 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 66 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 67 | + } |
| 68 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 69 | + testName = "model" |
| 70 | + testValue = 2 |
| 71 | + test = managedDeviceClientValue.setProperty(name=testName, value=testValue) |
| 72 | + assert managedDeviceClientValue._deviceInfo[testName] == testValue |
| 73 | + except: |
| 74 | + assert False == True |
| 75 | + |
| 76 | + # TO DO Rest of SetProperty and Notifyfieldchange (onSubscribe put variables) |
| 77 | + # Code in comments hangs when running but improves percentage |
| 78 | + # Look into later |
| 79 | + # def testManagedDeviceManageOnSubscribe(self): |
| 80 | + # try: |
| 81 | + # config = { |
| 82 | + # "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 83 | + # "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 84 | + # } |
| 85 | + # managedDeviceClientValue = ManagedDeviceClient(config) |
| 86 | + # test = managedDeviceClientValue._onSubscribe(mqttc=1, userdata=2, mid=3, granted_qos=4) |
| 87 | + # assert True |
| 88 | + # except: |
| 89 | + # assert False == True |
| 90 | + |
| 91 | + def testManagedDeviceManageLifetimeValueZero(self): |
| 92 | + try: |
| 93 | + config = { |
| 94 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 95 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 96 | + } |
| 97 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 98 | + test = managedDeviceClientValue.manage(lifetime=3000) |
| 99 | + assert True |
| 100 | + except: |
| 101 | + assert False == True |
| 102 | + |
| 103 | + def testManagedDeviceUnManage(self): |
| 104 | + try: |
| 105 | + config = { |
| 106 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 107 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 108 | + } |
| 109 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 110 | + test = managedDeviceClientValue.unmanage() |
| 111 | + assert True |
| 112 | + except: |
| 113 | + assert False == True |
| 114 | + |
| 115 | + def testManagedDeviceSetLocationLongitude(self): |
| 116 | + try: |
| 117 | + config = { |
| 118 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 119 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 120 | + } |
| 121 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 122 | + test = managedDeviceClientValue.setLocation(longitude=1, latitude=2) |
| 123 | + assert managedDeviceClientValue._location["longitude"] == 1 |
| 124 | + except: |
| 125 | + assert False == True |
| 126 | + |
| 127 | + def testManagedDeviceSetLocationLatitude(self): |
| 128 | + try: |
| 129 | + config = { |
| 130 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 131 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 132 | + } |
| 133 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 134 | + test = managedDeviceClientValue.setLocation(longitude=1, latitude=2) |
| 135 | + assert managedDeviceClientValue._location["latitude"] == 2 |
| 136 | + except: |
| 137 | + assert False == True |
| 138 | + |
| 139 | + def testManagedDeviceSetLocationElevation(self): |
| 140 | + try: |
| 141 | + config = { |
| 142 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 143 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 144 | + } |
| 145 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 146 | + test = managedDeviceClientValue.setLocation(longitude=1, latitude=2, elevation=3) |
| 147 | + assert managedDeviceClientValue._location["elevation"] == 3 |
| 148 | + except: |
| 149 | + assert False == True |
| 150 | + |
| 151 | + def testManagedDeviceSetLocationAccuracy(self): |
| 152 | + try: |
| 153 | + config = { |
| 154 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 155 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 156 | + } |
| 157 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 158 | + test = managedDeviceClientValue.setLocation(longitude=1, latitude=2, elevation=3, accuracy=4) |
| 159 | + assert managedDeviceClientValue._location["accuracy"] == 4 |
| 160 | + except: |
| 161 | + assert False == True |
| 162 | + |
| 163 | + def testManagedDeviceSetErrorCodeNone(self): |
| 164 | + try: |
| 165 | + config = { |
| 166 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 167 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 168 | + } |
| 169 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 170 | + test = managedDeviceClientValue.setErrorCode(errorCode=None) |
| 171 | + assert managedDeviceClientValue._errorCode == 0 |
| 172 | + except: |
| 173 | + assert False == True |
| 174 | + |
| 175 | + def testManagedDeviceSetErrorCode(self): |
| 176 | + try: |
| 177 | + config = { |
| 178 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 179 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 180 | + } |
| 181 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 182 | + test = managedDeviceClientValue.setErrorCode(errorCode=15) |
| 183 | + assert True |
| 184 | + except: |
| 185 | + assert False == True |
| 186 | + |
| 187 | + def testManagedDeviceClearErrorCodes(self): |
| 188 | + try: |
| 189 | + config = { |
| 190 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 191 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 192 | + } |
| 193 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 194 | + test = managedDeviceClientValue.clearErrorCodes() |
| 195 | + assert managedDeviceClientValue._errorCode == None |
| 196 | + except: |
| 197 | + assert False == True |
| 198 | + |
| 199 | + def testManagedDeviceAddLog(self): |
| 200 | + try: |
| 201 | + config = { |
| 202 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 203 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 204 | + } |
| 205 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 206 | + test = managedDeviceClientValue.addLog(msg="h", data="e") |
| 207 | + assert True |
| 208 | + except: |
| 209 | + assert False == True |
| 210 | + |
| 211 | + def testManagedDeviceClearLog(self): |
| 212 | + try: |
| 213 | + config = { |
| 214 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 215 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 216 | + } |
| 217 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 218 | + test = managedDeviceClientValue.clearLog() |
| 219 | + assert True |
| 220 | + except: |
| 221 | + assert False == True |
| 222 | + |
| 223 | + def testManagedDeviceRespondDeviceAction(self): |
| 224 | + try: |
| 225 | + config = { |
| 226 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 227 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 228 | + } |
| 229 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 230 | + test = managedDeviceClientValue.respondDeviceAction(reqId=1) |
| 231 | + assert True |
| 232 | + except: |
| 233 | + assert False == True |
| 234 | + |
| 235 | + # Do line 337 - 571 |
| 236 | + def testManagedDeviceSetState(self): |
| 237 | + try: |
| 238 | + config = { |
| 239 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 240 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 241 | + } |
| 242 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 243 | + test = managedDeviceClientValue.setState(status=1) |
| 244 | + assert True |
| 245 | + except: |
| 246 | + assert False == True |
| 247 | + |
| 248 | + def testManagedDeviceSetUpdateStatus(self): |
| 249 | + try: |
| 250 | + config = { |
| 251 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 252 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 253 | + } |
| 254 | + managedDeviceClientValue = ManagedDeviceClient(config) |
| 255 | + test = managedDeviceClientValue.setUpdateStatus(status=1) |
| 256 | + except: |
| 257 | + assert False == True |
| 258 | + |
| 259 | + # Use template for rest __functions |
| 260 | + |
| 261 | + def testManagedDeviceMgmtResponseError(self): |
| 262 | + with pytest.raises(Exception) as e: |
| 263 | + config = { |
| 264 | + "identity": {"orgId": "1", "typeId": "xxx", "deviceId": "xxx"}, |
| 265 | + "auth": {"token": "xxxxxxxxxxxxxxxxxx"}, |
| 266 | + } |
| 267 | + managedDevice = ManagedDeviceClient(config) |
| 268 | + testValue = "Test" |
| 269 | + encodedPayload = Utf8Codec.encode(testValue) |
| 270 | + managedDevice._ManagedDeviceClient__onDeviceMgmtResponse(client=1, userdata=2, pahoMessage=encodedPayload) |
| 271 | + assert "Unable to parse JSON. payload=" " error" in str(e.value) |
0 commit comments