PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/test/peachyprinter_test/infrastructure_test/zaxis_test.py

https://gitlab.com/goolic/peachyprintertools
Python | 185 lines | 150 code | 35 blank | 0 comment | 1 complexity | aa5cd69af161407d40167e6a5bce03ca MD5 | raw file
  1. import unittest
  2. import os
  3. import sys
  4. import time
  5. import logging
  6. from mock import MagicMock
  7. sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
  8. sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..', 'src'))
  9. from peachyprinter.infrastructure.zaxis import SerialDripZAxis
  10. from peachyprinter.infrastructure.messages import DripRecordedMessage, SetDripCountMessage, MoveToDripCountMessage
  11. class SerialDripZAxisTests(unittest.TestCase):
  12. def test_init_calls_registers_handler(self):
  13. mock_communicatior = MagicMock()
  14. sdza = SerialDripZAxis(mock_communicatior, 1.0, 0.0)
  15. mock_communicatior.register_handler.assert_called_with(DripRecordedMessage, sdza.drip_reported_handler)
  16. def test_init_sets_up_starting_height(self):
  17. mock_communicatior = MagicMock()
  18. starting_height = 10.0
  19. sdza = SerialDripZAxis(mock_communicatior, 1.0, starting_height)
  20. self.assertEqual(starting_height, sdza.current_z_location_mm())
  21. def test_init_resets_micro_drip_counter(self):
  22. mock_communicatior = MagicMock()
  23. starting_height = 10.0
  24. sdza = SerialDripZAxis(mock_communicatior, 1.0, starting_height)
  25. mock_communicatior.send.assert_called_with(SetDripCountMessage(0))
  26. def test_drip_recorded_handler_adds_drip(self):
  27. mock_communicatior = MagicMock()
  28. starting_height = 0.0
  29. drips_per_mm = 1.0
  30. sdza = SerialDripZAxis(mock_communicatior, drips_per_mm, starting_height)
  31. drip_message = DripRecordedMessage(1)
  32. sdza.drip_reported_handler(drip_message)
  33. self.assertEqual(1.0, sdza.current_z_location_mm())
  34. def test_drip_recorded_handler_adds_drips_if_missing_message(self):
  35. mock_communicatior = MagicMock()
  36. starting_height = 0.0
  37. drips_per_mm = 1.0
  38. sdza = SerialDripZAxis(mock_communicatior, drips_per_mm, starting_height)
  39. drip_message_1 = DripRecordedMessage(1)
  40. drip_message_2 = DripRecordedMessage(3)
  41. sdza.drip_reported_handler(drip_message_1)
  42. sdza.drip_reported_handler(drip_message_2)
  43. self.assertEqual(3.0, sdza.current_z_location_mm())
  44. def test_drip_recorded_handler_adds_correct_height_per_drip(self):
  45. mock_communicatior = MagicMock()
  46. starting_height = 0.0
  47. drips_per_mm = 2.5
  48. sdza = SerialDripZAxis(mock_communicatior, drips_per_mm, starting_height)
  49. drip_message = DripRecordedMessage(1)
  50. sdza.drip_reported_handler(drip_message)
  51. self.assertEqual(0.4, sdza.current_z_location_mm())
  52. def test_drip_recorded_handler_should_call_back_per_drip(self):
  53. mock_communicatior = MagicMock()
  54. starting_height = 0.0
  55. drips_per_mm = 1.0
  56. mock_call_back = MagicMock()
  57. expected_drips = 1
  58. expected_height = 1.0
  59. expected_average = 0.0
  60. sdza = SerialDripZAxis(mock_communicatior, drips_per_mm, starting_height, mock_call_back)
  61. drip_message = DripRecordedMessage(1)
  62. start = time.time()
  63. sdza.drip_reported_handler(drip_message)
  64. end = time.time()
  65. self.assertTrue(mock_call_back.called)
  66. self.assertEqual(expected_drips, mock_call_back.call_args_list[0][0][0])
  67. self.assertEqual(expected_height, mock_call_back.call_args_list[0][0][1])
  68. self.assertEqual(expected_average, mock_call_back.call_args_list[0][0][2])
  69. self.assertTrue(mock_call_back.call_args_list[0][0][3][0] >= start)
  70. self.assertTrue(mock_call_back.call_args_list[0][0][3][0] <= end)
  71. def test_drip_recorded_handler_should_adjust_history_for_missing_drips(self):
  72. mock_communicatior = MagicMock()
  73. starting_height = 0.0
  74. drips_per_mm = 1.0
  75. mock_call_back = MagicMock()
  76. sdza = SerialDripZAxis(mock_communicatior, drips_per_mm, starting_height, mock_call_back)
  77. drip_message_1 = DripRecordedMessage(1)
  78. drip_message_2 = DripRecordedMessage(10)
  79. sdza.drip_reported_handler(drip_message_1)
  80. sdza.drip_reported_handler(drip_message_2)
  81. self.assertEquals(10, len(mock_call_back.call_args_list[1][0][3]))
  82. def test_move_to_sends_drips(self):
  83. mock_communicatior = MagicMock()
  84. starting_height = 0.0
  85. drips_per_mm = 1.0
  86. mock_call_back = MagicMock()
  87. sdza = SerialDripZAxis(mock_communicatior, drips_per_mm, starting_height, mock_call_back)
  88. sdza.move_to(3.0)
  89. mock_communicatior.send.assert_called_with(MoveToDripCountMessage(3))
  90. def test_move_to_sends_drips_as_ints(self):
  91. mock_communicatior = MagicMock()
  92. starting_height = 0.0
  93. drips_per_mm = 1.0
  94. mock_call_back = MagicMock()
  95. sdza = SerialDripZAxis(mock_communicatior, drips_per_mm, starting_height, mock_call_back)
  96. sdza.move_to(3.5)
  97. mock_communicatior.send.assert_called_with(MoveToDripCountMessage(4))
  98. def test_reset_removes_drips_count(self):
  99. mock_communicatior = MagicMock()
  100. starting_height = 0.0
  101. drips_per_mm = 2.5
  102. sdza = SerialDripZAxis(mock_communicatior, drips_per_mm, starting_height)
  103. drip_message = DripRecordedMessage(1)
  104. sdza.drip_reported_handler(drip_message)
  105. sdza.reset()
  106. actual_height = sdza.current_z_location_mm()
  107. history = sdza.drip_history
  108. self.assertEqual(0.0, actual_height)
  109. self.assertEqual([], history)
  110. def test_reset_removes_drips_count_accounting_for_hardware(self):
  111. mock_communicatior = MagicMock()
  112. starting_height = 0.0
  113. drips_per_mm = 1.0
  114. sdza = SerialDripZAxis(mock_communicatior, drips_per_mm, starting_height)
  115. drip_message_1 = DripRecordedMessage(1)
  116. drip_message_2 = DripRecordedMessage(2)
  117. drip_message_3 = DripRecordedMessage(1)
  118. sdza.drip_reported_handler(drip_message_1)
  119. self.assertEqual(1.0, sdza.current_z_location_mm())
  120. sdza.drip_reported_handler(drip_message_2)
  121. self.assertEqual(2.0, sdza.current_z_location_mm())
  122. sdza.reset()
  123. self.assertEqual(0.0, sdza.current_z_location_mm())
  124. self.assertEqual(2, mock_communicatior.send.call_count)
  125. mock_communicatior.send.assert_called_with(SetDripCountMessage(0))
  126. sdza.drip_reported_handler(drip_message_3)
  127. self.assertEqual(1.0, sdza.current_z_location_mm())
  128. def test_set_call_back_should(self):
  129. mock_communicatior = MagicMock()
  130. starting_height = 0.0
  131. drips_per_mm = 1.0
  132. mock_call_back = MagicMock()
  133. expected_drips = 1
  134. expected_height = 1.0
  135. expected_average = 0.0
  136. sdza = SerialDripZAxis(mock_communicatior, drips_per_mm, starting_height, None)
  137. sdza.set_call_back(mock_call_back)
  138. drip_message = DripRecordedMessage(1)
  139. start = time.time()
  140. sdza.drip_reported_handler(drip_message)
  141. end = time.time()
  142. self.assertTrue(mock_call_back.called)
  143. self.assertEqual(expected_drips, mock_call_back.call_args_list[0][0][0])
  144. self.assertEqual(expected_height, mock_call_back.call_args_list[0][0][1])
  145. self.assertEqual(expected_average, mock_call_back.call_args_list[0][0][2])
  146. self.assertTrue(mock_call_back.call_args_list[0][0][3][0] >= start)
  147. self.assertTrue(mock_call_back.call_args_list[0][0][3][0] <= end)
  148. if __name__ == '__main__':
  149. logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level='DEBUG')
  150. unittest.main()