test_buffer.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. from unittest import mock
  3. import pytest
  4. from hapic.buffer import DecorationBuffer
  5. from hapic.description import InputPathDescription
  6. from hapic.description import InputQueryDescription
  7. from hapic.description import InputBodyDescription
  8. from hapic.description import InputHeadersDescription
  9. from hapic.description import InputFormsDescription
  10. from hapic.description import OutputBodyDescription
  11. from hapic.description import OutputHeadersDescription
  12. from hapic.description import ErrorDescription
  13. from hapic.exception import AlreadyDecoratedException
  14. from tests.base import Base
  15. fake_controller_wrapper = mock.MagicMock()
  16. class TestBuffer(Base):
  17. def test_unit__buffer_usage__ok__set_descriptions(self):
  18. buffer = DecorationBuffer()
  19. input_path_description = InputPathDescription(fake_controller_wrapper)
  20. input_query_description = InputQueryDescription(fake_controller_wrapper) # nopep8
  21. input_body_description = InputBodyDescription(fake_controller_wrapper)
  22. input_headers_description = InputHeadersDescription(fake_controller_wrapper) # nopep8
  23. input_forms_description = InputFormsDescription(fake_controller_wrapper) # nopep8
  24. output_headers_description = OutputHeadersDescription(fake_controller_wrapper) # nopep8
  25. output_body_description = OutputBodyDescription(fake_controller_wrapper) # nopep8
  26. error_description = ErrorDescription(fake_controller_wrapper)
  27. buffer.input_path = input_path_description
  28. buffer.input_query = input_query_description
  29. buffer.input_body = input_body_description
  30. buffer.input_headers = input_headers_description
  31. buffer.input_forms = input_forms_description
  32. buffer.output_headers = output_headers_description
  33. buffer.output_body = output_body_description
  34. buffer.add_error(error_description)
  35. description = buffer.get_description()
  36. assert description.input_path == input_path_description
  37. assert description.input_query == input_query_description
  38. assert description.input_body == input_body_description
  39. assert description.input_headers == input_headers_description
  40. assert description.input_forms == input_forms_description
  41. assert description.output_headers == output_headers_description
  42. assert description.output_body == output_body_description
  43. assert description.errors == [error_description]
  44. buffer.clear()
  45. description = buffer.get_description()
  46. assert description.input_path is None
  47. assert description.input_query is None
  48. assert description.input_body is None
  49. assert description.input_headers is None
  50. assert description.input_forms is None
  51. assert description.output_headers is None
  52. assert description.output_body is None
  53. assert description.errors == []
  54. def test_unit__buffer_usage__error__cant_replace(self):
  55. buffer = DecorationBuffer()
  56. input_path_description = InputPathDescription(fake_controller_wrapper)
  57. input_query_description = InputQueryDescription(fake_controller_wrapper) # nopep8
  58. input_body_description = InputBodyDescription(fake_controller_wrapper)
  59. input_headers_description = InputHeadersDescription(fake_controller_wrapper) # nopep8
  60. input_forms_description = InputFormsDescription(fake_controller_wrapper) # nopep8
  61. output_headers_description = OutputHeadersDescription(fake_controller_wrapper) # nopep8
  62. output_body_description = OutputBodyDescription(fake_controller_wrapper) # nopep8
  63. buffer.input_path = input_path_description
  64. buffer.input_query = input_query_description
  65. buffer.input_body = input_body_description
  66. buffer.input_headers = input_headers_description
  67. buffer.input_forms = input_forms_description
  68. buffer.output_headers = output_headers_description
  69. buffer.output_body = output_body_description
  70. with pytest.raises(AlreadyDecoratedException):
  71. buffer.input_path = input_path_description
  72. with pytest.raises(AlreadyDecoratedException):
  73. buffer.input_query = input_query_description
  74. with pytest.raises(AlreadyDecoratedException):
  75. buffer.input_body = input_body_description
  76. with pytest.raises(AlreadyDecoratedException):
  77. buffer.input_headers = input_headers_description
  78. with pytest.raises(AlreadyDecoratedException):
  79. buffer.input_forms = input_forms_description
  80. with pytest.raises(AlreadyDecoratedException):
  81. buffer.output_headers = output_headers_description
  82. with pytest.raises(AlreadyDecoratedException):
  83. buffer.output_body = output_body_description