Skip to content

Commit

Permalink
Refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuscardosodeveloper committed Nov 29, 2023
1 parent 9ab54cb commit 40ed448
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
65 changes: 35 additions & 30 deletions tests/Resources/test_devices.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import os
from requests_mock.mocker import Mocker
from tagoio_sdk.modules.Resources.Devices import Devices

from tagoio_sdk.modules.Resources.Resources import Resources
from tagoio_sdk.types import ConfigurationParams, DeviceCreateResponse, DeviceListItem, DeviceTokenDataList, DeviceInfo, Data, TokenCreateResponse


os.environ["T_ANALYSIS_TOKEN"] = "your_token_value"


def mockDeviceList() -> list[DeviceListItem]:
return {
"status": True,
Expand All @@ -27,8 +32,8 @@ def testDeviceListMethod(requests_mock: Mocker) -> None:

requests_mock.get("https://api.tago.io/device", json=mockDeviceList())

tokenFake = {"token": "fake_token"}
response = Devices(params=tokenFake).listDevice()
resources = Resources()
response = resources.devices.listDevice()

assert response[0]["id"] == mockDeviceList()["result"][0]["id"]
assert isinstance(response, list)
Expand All @@ -53,15 +58,15 @@ def testDeviceCreateMethod(requests_mock: Mocker) -> None:

requests_mock.post("https://api.tago.io/device", json=mockDeviceCreate())

tokenFake = {"token": "fake_token"}
deviceObj = {
"name": "device1",
"type": "mutable",
"network": "mock_network_id",
"connector": "mock_connector_id",
"serie_number": "mock_serie_number_id",
}
response = Devices(params=tokenFake).create(deviceObj)
resources = Resources()
response = resources.devices.create(deviceObj)

assert response == mockDeviceCreate()["result"]
assert isinstance(response, dict)
Expand All @@ -75,9 +80,9 @@ def testDeviceEditMethod(requests_mock: Mocker) -> None:
deviceID = "device1"
requests_mock.put(f"https://api.tago.io/device/{deviceID}", json={"status": True, "result": "Successfully Updated"})

tokenFake = {"token": "fake_token"}
deviceObj = {"tags": [{"key": "my_tag_key", "value": "my_tag_value"}]}
response = Devices(params=tokenFake).edit(deviceID, deviceObj)
resources = Resources()
response = resources.devices.edit(deviceID, deviceObj)

assert response == "Successfully Updated"
assert isinstance(response, str)
Expand All @@ -91,8 +96,8 @@ def testDeviceDeleteMethod(requests_mock: Mocker) -> None:
deviceID = "device1"
requests_mock.delete(f"https://api.tago.io/device/{deviceID}", json={"status": True, "result": "Successfully Removed"})

tokenFake = {"token": "fake_token"}
response = Devices(params=tokenFake).delete(deviceID)
resources = Resources()
response = resources.devices.delete(deviceID)

assert response == "Successfully Removed"
assert isinstance(response, str)
Expand All @@ -119,8 +124,8 @@ def testDeviceInfoMethod(requests_mock: Mocker) -> None:
deviceID = "device1"
requests_mock.get(f"https://api.tago.io/device/{deviceID}", json=mockDeviceInfo())

tokenFake = {"token": "fake_token"}
response = Devices(params=tokenFake).info(deviceID)
resources = Resources()
response = resources.devices.info(deviceID)

assert response == mockDeviceInfo()["result"]
assert isinstance(response, dict)
Expand All @@ -134,9 +139,9 @@ def testParamSetMethodMultipleConfigurationParams(requests_mock: Mocker) -> None
deviceID = "device1"
requests_mock.post(f"https://api.tago.io/device/{deviceID}/params", json={"status": True, "result": "Params Successfully Updated"})

tokenFake = {"token": "fake_token"}
configObj = [{"key": "key1", "value": "value1"}, {"key": "key2", "value": "value2"}]
response = Devices(params=tokenFake).paramSet(deviceID, configObj)
resources = Resources()
response = resources.devices.paramSet(deviceID, configObj)

assert response == "Params Successfully Updated"
assert isinstance(response, str)
Expand All @@ -150,9 +155,9 @@ def testParamSetMethodSingleConfigurationParams(requests_mock: Mocker) -> None:
deviceID = "device1"
requests_mock.post(f"https://api.tago.io/device/{deviceID}/params", json={"status": True, "result": "Params Successfully Updated"})

tokenFake = {"token": "fake_token"}
configObj = {"key": "key1", "value": "value1"}
response = Devices(params=tokenFake).paramSet(deviceID, configObj)
resources = Resources()
response = resources.devices.paramSet(deviceID, configObj)

assert response == "Params Successfully Updated"
assert isinstance(response, str)
Expand Down Expand Up @@ -180,8 +185,8 @@ def testParamListMethod(requests_mock: Mocker) -> None:
deviceID = "device1"
requests_mock.get(f"https://api.tago.io/device/{deviceID}/params", json=mockParamList())

tokenFake = {"token": "fake_token"}
response = Devices(params=tokenFake).paramList(deviceID, sentStatus=True)
resources = Resources()
response = resources.devices.paramList(deviceID, sentStatus=True)

assert response == mockParamList()["result"]
assert isinstance(response, list)
Expand All @@ -197,8 +202,8 @@ def testParamRemoveMethod(requests_mock: Mocker) -> None:
paramID = "param1"
requests_mock.delete(f"https://api.tago.io/device/{deviceID}/params/{paramID}", json={"status": True, "result": "Successfully Removed"})

tokenFake = {"token": "fake_token"}
response = Devices(params=tokenFake).paramRemove(deviceID, paramID)
resources = Resources()
response = resources.devices.paramRemove(deviceID, paramID)

assert response == "Successfully Removed"
assert isinstance(response, str)
Expand Down Expand Up @@ -228,8 +233,8 @@ def testTokenListMethod(requests_mock: Mocker) -> None:
deviceID = "device1"
requests_mock.get(f"https://api.tago.io/device/token/{deviceID}", json=mockTokenList())

tokenFake = {"token": "fake_token"}
response = Devices(params=tokenFake).tokenList(deviceID)
resources = Resources()
response = resources.devices.tokenList(deviceID)

assert response[0]["token"] == mockTokenList()["result"][0]["token"]
assert isinstance(response, list)
Expand All @@ -253,11 +258,11 @@ def testTokenCreateMethod(requests_mock: Mocker) -> None:
"""

deviceID = "device1"
requests_mock.post(f"https://api.tago.io/device/token", json=mockTokenCreate())
requests_mock.post("https://api.tago.io/device/token", json=mockTokenCreate())

tokenFake = {"token": "fake_token"}
tokenParams = {"name": "token1", "permission": "full", "expire_time": "2023-02-21T15:17:35.880Z"}
response = Devices(params=tokenFake).tokenCreate(deviceID, tokenParams)
resources = Resources()
response = resources.devices.tokenCreate(deviceID, tokenParams)

assert response["token"] == mockTokenCreate()["result"]["token"]
assert response["permission"] == mockTokenCreate()["result"]["permission"]
Expand All @@ -272,8 +277,8 @@ def testTokenDeleteMethod(requests_mock: Mocker) -> None:
tokenToDelete = "token_value1"
requests_mock.delete(f"https://api.tago.io/device/token/{tokenToDelete}", json={"status": True, "result": "Token Successfully Removed"})

tokenFake = {"token": "fake_token"}
response = Devices(params=tokenFake).tokenDelete(tokenToDelete)
resources = Resources()
response = resources.devices.tokenDelete(tokenToDelete)

assert response == "Token Successfully Removed"
assert isinstance(response, str)
Expand Down Expand Up @@ -304,9 +309,9 @@ def testGetDeviceDataMethod(requests_mock: Mocker) -> None:
deviceID = "device1"
requests_mock.get(f"https://api.tago.io/device/{deviceID}/data", json=mockGetDeviceData())

tokenFake = {"token": "fake_token"}
queryParams = {"variables": "temperature", "qty": 1}
response = Devices(params=tokenFake).getDeviceData(deviceID, queryParams)
resources = Resources()
response = resources.devices.getDeviceData(deviceID, queryParams)

assert response[0]["group"] == mockGetDeviceData()["result"][0]["group"]
assert isinstance(response, list)
Expand All @@ -321,8 +326,8 @@ def testEmptyDeviceDataMethod(requests_mock: Mocker) -> None:
deviceID = "device1"
requests_mock.post(f"https://api.tago.io/device/{deviceID}/empty", json={"status": True, "result": "Data Successfully Removed"})

tokenFake = {"token": "fake_token"}
response = Devices(params=tokenFake).emptyDeviceData(deviceID)
resources = Resources()
response = resources.devices.emptyDeviceData(deviceID)

assert response == "Data Successfully Removed"
assert isinstance(response, str)
25 changes: 14 additions & 11 deletions tests/Resources/test_profile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
from requests_mock.mocker import Mocker

from tagoio_sdk.common.Common_Type import TokenDataList
from tagoio_sdk.modules.Resources.Profile import Profile
from tagoio_sdk.modules.Resources.Profile_Type import (ProfileInfo,
ProfileSummary)
from tagoio_sdk.modules.Resources.Profile_Type import ProfileInfo, ProfileSummary
from tagoio_sdk.modules.Resources.Resources import Resources


os.environ["T_ANALYSIS_TOKEN"] = "your_token_value"


def mockProfileInfo() -> ProfileInfo:
Expand Down Expand Up @@ -131,8 +134,8 @@ def testProfileMethodInfo(requests_mock: Mocker) -> None:

requests_mock.get("https://api.tago.io/profile/profile_id", json=mockProfileInfo())

tokenFake = {"token": "fake_token"}
response = Profile(params=tokenFake).info(profileID="profile_id")
resources = Resources()
response = resources.profile.info(profileID="profile_id")

assert response["allocation"] == mockProfileInfo()["result"]["allocation"]
assert isinstance(response, dict)
Expand All @@ -145,8 +148,8 @@ def testProfileMethodList(requests_mock: Mocker) -> None:

requests_mock.get("https://api.tago.io/profile", json=mockListProfileInfo())

tokenFake = {"token": "fake_token"}
response = Profile(params=tokenFake).list()
resources = Resources()
response = resources.profile.list()

assert response == mockListProfileInfo()["result"]
assert isinstance(response, list)
Expand All @@ -162,8 +165,8 @@ def testProfileMethodSummary(requests_mock: Mocker) -> None:
"https://api.tago.io/profile/profile_id/summary", json=mockProfileSummary()
)

tokenFake = {"token": "fake_token"}
response = Profile(params=tokenFake).summary(profileID="profile_id")
resources = Resources()
response = resources.profile.summary(profileID="profile_id")

assert response == mockProfileSummary()["result"]
assert isinstance(response, dict)
Expand All @@ -178,8 +181,8 @@ def testProfileMethodTokenList(requests_mock: Mocker) -> None:
"https://api.tago.io/profile/profile_id/token", json=mockTokenDataList()
)

tokenFake = {"token": "fake_token"}
response = Profile(params=tokenFake).tokenList(profileID="profile_id")
resources = Resources()
response = resources.profile.tokenList(profileID="profile_id")

assert response[1]["token"] == mockTokenDataList()["result"][1]["token"]
assert isinstance(response, list)
Expand Down

0 comments on commit 40ed448

Please sign in to comment.