test_pseudo_clusters.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env -S python3 -B
  2. #
  3. # Copyright (c) 2022 Project CHIP Authors
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the 'License');
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an 'AS IS' BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import unittest
  17. from matter_yamltests.pseudo_clusters.pseudo_clusters import PseudoCluster, PseudoClusters
  18. class MockStep:
  19. def __init__(self, cluster: str, command: str):
  20. self.cluster = cluster
  21. self.command = command
  22. class MyCluster(PseudoCluster):
  23. name = 'MyCluster'
  24. async def MyCommand(self, request):
  25. pass
  26. async def MyCommandWithCustomSuccess(self, request):
  27. return 'CustomSuccess'
  28. unsupported_cluster_step = MockStep('UnsupportedCluster', 'MyCommand')
  29. unsupported_command_step = MockStep('MyCluster', 'UnsupportedCommand')
  30. supported_step = MockStep('MyCluster', 'MyCommand')
  31. supported_step_with_custom_success = MockStep(
  32. 'MyCluster', 'MyCommandWithCustomSuccess')
  33. default_failure = ({'error': 'FAILURE'}, [])
  34. default_success = ({}, [])
  35. custom_success = ('CustomSuccess', [])
  36. clusters = PseudoClusters([MyCluster()])
  37. class TestPseudoClusters(unittest.IsolatedAsyncioTestCase):
  38. def test_supports(self):
  39. self.assertFalse(clusters.supports(unsupported_cluster_step))
  40. self.assertFalse(clusters.supports(unsupported_command_step))
  41. self.assertTrue(clusters.supports(supported_step))
  42. self.assertTrue(clusters.supports(supported_step_with_custom_success))
  43. async def test_execute_return_value(self):
  44. self.assertEqual(await clusters.execute(unsupported_cluster_step), default_failure)
  45. self.assertEqual(await clusters.execute(unsupported_command_step), default_failure)
  46. self.assertEqual(await clusters.execute(supported_step), default_success)
  47. self.assertEqual(await clusters.execute(supported_step_with_custom_success), custom_success)
  48. if __name__ == '__main__':
  49. unittest.main()