test_utils.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (c) 2012-2013 LiuYC https://github.com/liuyichen/
  2. # Copyright 2012-2014 ksyun.com, Inc. or its affiliates. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"). You
  5. # may not use this file except in compliance with the License. A copy of
  6. # the License is located at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # or in the "license" file accompanying this file. This file is
  11. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. # ANY KIND, either express or implied. See the License for the specific
  13. # language governing permissions and limitations under the License.
  14. import time
  15. from tests import unittest
  16. import kscore.session
  17. from kscore.utils import ArgumentGenerator
  18. class ArgumentGeneratorError(AssertionError):
  19. def __init__(self, service_name, operation_name,
  20. generated, message):
  21. full_msg = (
  22. 'Error generating skeleton for %s:%s, %s\nActual:\n%s' % (
  23. service_name, operation_name, message, generated))
  24. super(AssertionError, self).__init__(full_msg)
  25. def test_can_generate_all_inputs():
  26. session = kscore.session.get_session()
  27. generator = ArgumentGenerator()
  28. for service_name in session.get_available_services():
  29. service_model = session.get_service_model(service_name)
  30. for operation_name in service_model.operation_names:
  31. operation_model = service_model.operation_model(operation_name)
  32. input_shape = operation_model.input_shape
  33. if input_shape is not None and getattr(input_shape, "members", None):
  34. yield (_test_can_generate_skeleton, generator,
  35. input_shape, service_name, operation_name)
  36. def _test_can_generate_skeleton(generator, shape, service_name,
  37. operation_name):
  38. generated = generator.generate_skeleton(shape)
  39. # Do some basic sanity checks to make sure the generated shape
  40. # looks right. We're mostly just ensuring that the generate_skeleton
  41. # doesn't throw an exception.
  42. if not isinstance(generated, dict):
  43. raise ArgumentGeneratorError(
  44. service_name, operation_name,
  45. generated, 'expected a dict')
  46. # The generated skeleton also shouldn't be empty (the test
  47. # generator has already filtered out input_shapes of None).
  48. if len(generated) == 0:
  49. raise ArgumentGeneratorError(
  50. service_name, operation_name,
  51. generated, "generated arguments were empty")