| # Copyright 2017 Google Inc. All Rights Reserved. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| from __future__ import absolute_import | |
| import unittest | |
| import step | |
| _TEST_DEVICE_VERSIONS = '1.0,2.0,3.0' | |
| _TEST_START_POINT = step.Point(.5, .5) | |
| _TEST_END_POINT = step.Point(.7, .7) | |
| # The order of this list is important for creating step objects. | |
| _STEP_KEYS = [ | |
| 'description', 'action_type', 'action', 'by', 'path', 'value', | |
| 'duration', 'start_coordinate', 'end_coordinate', 'device_type', | |
| 'device_versions', 'channel', 'saved_manual_time', 'build_type' | |
| ] | |
| _STEP_FIELDS = { | |
| 'description': 'test_description', 'action_type': 'test_type', | |
| 'action': 'test_action', 'by': 'test_by', 'path': 'test_path', | |
| 'start_coordinate': _TEST_START_POINT, 'duration': 10, | |
| 'end_coordinate': _TEST_END_POINT, 'value': 'test_value', | |
| 'device_type': 'phone', 'device_versions': _TEST_DEVICE_VERSIONS, | |
| 'channel': 'canary', 'saved_manual_time': '1.0', 'build_type': '' | |
| } | |
| def create_step(): | |
| """Creates a step test with parameters. | |
| Returns: | |
| A Step Object. | |
| """ | |
| test_step = step.Step( | |
| _STEP_FIELDS['description'], | |
| _STEP_FIELDS['action_type'], _STEP_FIELDS['action'], | |
| _STEP_FIELDS['by'], _STEP_FIELDS['path'], _STEP_FIELDS['value'], | |
| _STEP_FIELDS['duration'], _STEP_FIELDS['start_coordinate'], | |
| _STEP_FIELDS['end_coordinate'], _STEP_FIELDS['device_type'], | |
| _STEP_FIELDS['device_versions'], _STEP_FIELDS['channel'], | |
| _STEP_FIELDS['saved_manual_time'], _STEP_FIELDS['build_type'] | |
| ) | |
| return test_step | |
| class TestStep(unittest.TestCase): | |
| """Tests step module. | |
| """ | |
| def setUp(self): | |
| self.test_step = create_step() | |
| def test_default_to_dict(self): | |
| """Verifies the to_dict method for a default step object. | |
| """ | |
| test_step = step.Step() | |
| step_dict = test_step.to_dict() | |
| self.assertEqual(len(_STEP_KEYS), len(step_dict)) | |
| self.assertIsInstance(step_dict['device_versions'], str) | |
| test_step.device_versions = None | |
| step_dict = test_step.to_dict() | |
| for key in _STEP_KEYS[:-2]: | |
| self.assertIsNone(step_dict[key]) | |
| # Default value for `saved_manual_time` is `0.0` | |
| self.assertEqual('0.0', step_dict['saved_manual_time']) | |
| # Default value for `build_type` is `` | |
| self.assertEqual('', step_dict['build_type']) | |
| def test_to_dict(self): | |
| """Verifies the to_dict method for a typical step object. | |
| """ | |
| step_dict = self.test_step.to_dict() | |
| object_var_dict = vars(self.test_step) | |
| self.assertEqual(len(step_dict), len(object_var_dict)) | |
| for key in _STEP_KEYS: | |
| self.assertEqual(step_dict[key], object_var_dict[key]) | |
| def test_point_string(self): | |
| """Tests serializing a Point to a string. | |
| """ | |
| test_point = step.Point(.5, .6) | |
| target_string = '0.5,0.6' | |
| self.assertEqual(test_point.to_str(), target_string) | |
| def test_step_equals(self): | |
| """Tests the __eq__ method. | |
| """ | |
| test_step_2 = create_step() | |
| self.assertEqual(self.test_step, test_step_2) | |
| test_step_2.by = 'Not Equal' | |
| self.assertNotEqual(self.test_step, test_step_2) | |
| test_step_2.by = self.test_step.by | |
| self.assertEqual(self.test_step, test_step_2) | |
| test_step_2.start_coordinate = _TEST_END_POINT | |
| self.assertNotEqual(self.test_step, test_step_2) | |
| def test_from_row(self): | |
| """Tests from_row method. | |
| """ | |
| test_step_from_row = step.Step().from_row( | |
| _STEP_FIELDS['description'], | |
| _STEP_FIELDS['action_type'], _STEP_FIELDS['action'], | |
| _STEP_FIELDS['by'], _STEP_FIELDS['path'], _STEP_FIELDS['value'], | |
| _STEP_FIELDS['duration'], _STEP_FIELDS['start_coordinate'].to_str(), | |
| _STEP_FIELDS['end_coordinate'].to_str(), _STEP_FIELDS['device_type'], | |
| _STEP_FIELDS['device_versions'], _STEP_FIELDS['channel'], | |
| _STEP_FIELDS['saved_manual_time'], _STEP_FIELDS['build_type'] | |
| ) | |
| self.assertEqual(self.test_step, test_step_from_row) | |
| def test_to_row(self): | |
| """Tests the to_row representation. | |
| """ | |
| test_step = step.Step() | |
| target_list = [''] * len(_STEP_KEYS) | |
| # `saved_manual_time` column equals to '0.0' | |
| target_list[-2] = '0.0' | |
| self.assertEqual(len(target_list), len(test_step.to_row())) | |
| for item in test_step.to_row()[:-2] + [test_step.to_row()[-1]]: | |
| self.assertEqual('', item) | |
| self.assertEqual(test_step.to_row()[-2], target_list[-2]) | |
| test_step = step.Step() | |
| test_step.description = 'foobar' | |
| test_step.start_coordinate = _TEST_START_POINT | |
| test_step.end_coordinate = _TEST_END_POINT | |
| test_step.device_versions = _TEST_DEVICE_VERSIONS | |
| self.assertEqual(test_step.description, test_step.to_row()[0]) | |
| self.assertEqual( | |
| test_step.start_coordinate.to_str(), test_step.to_row()[7]) | |
| self.assertEqual(test_step.end_coordinate.to_str(), test_step.to_row()[8]) | |
| self.assertEqual('1.0,2.0,3.0', test_step.to_row()[10]) | |
| if __name__ == '__main__': | |
| unittest.main() |