Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers detect_targets_test.py Source File

detect_targets_test.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2017 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009 http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 
00018 import unittest
00019 from mock import patch
00020 from tools.detect_targets import get_interface_version
00021 
00022 
00023 class MbedLsToolsMock ():
00024     """
00025     Mock of mbedls tools
00026     """
00027     
00028     def __init__(self, test_type):
00029         self.interface_test_type  = test_type
00030     
00031     def list_mbeds(self, unique_names=False, read_details_txt=False):
00032         return self.mbed_types [self.interface_test_type ];
00033     
00034     # Static details.txt types.
00035     mbed_types = {    
00036         'details_valid_interface_version' : [{
00037             'daplink_hic_id': u'97969900', 
00038             'daplink_version': u'0244', 
00039             'target_id': u'0240000034544e45001500048e41001b8321000097969900', 
00040             'serial_port': u'COM3', 
00041             'target_id_usb_id': u'0240000034544e45001500048e41001b8321000097969900', 
00042             'daplink_auto_reset': u'0', 
00043             'daplink_git_sha': u'392f85aa88a41125dec0b963ce73c6795b8bdd0d', 
00044             'daplink_interface_version': u'0244', 
00045             'daplink_overflow_detection': u'0', 
00046             'daplink_daplink_mode': u'Interface', 
00047             'target_id_mbed_htm': u'0240000034544e45001500048e41001b8321000097969900', 
00048             'mount_point': 'D:', 
00049             'daplink_automation_allowed': u'0', 
00050             'daplink_interface_crc': u'0xc525d856', 
00051             'daplink_local_mods': u'0', 
00052             'daplink_unique_id': u'0240000034544e45001500048e41001b8321000097969900', 
00053             'daplink_remount_count': u'0', 
00054             'daplink_bootloader_crc': u'0xc4ebf65c', 
00055             'platform_name': u'K64F', 
00056             'platform_name_unique': u'K64F[0]', 
00057             'daplink_bootloader_version': u'0244', 
00058             'daplink_usb_interfaces': u'MSD, CDC, HID'
00059 
00060         }],
00061         'details_missing_interface_version' : [{
00062             'daplink_hic_id': u'97969900', 
00063             'target_id': u'0240000034544e45001500048e41001b8321000097969900', 
00064             'serial_port': u'COM3', 
00065             'target_id_usb_id': u'0240000034544e45001500048e41001b8321000097969900', 
00066             'daplink_auto_reset': u'0', 
00067             'daplink_git_sha': u'392f85aa88a41125dec0b963ce73c6795b8bdd0d', 
00068             'daplink_interface_version': u'0244', 
00069             'daplink_overflow_detection': u'0', 
00070             'daplink_daplink_mode': u'Interface', 
00071             'target_id_mbed_htm': u'0240000034544e45001500048e41001b8321000097969900', 
00072             'mount_point': 'D:', 
00073             'daplink_automation_allowed': u'0', 
00074             'daplink_interface_crc': u'0xc525d856', 
00075             'daplink_local_mods': u'0', 
00076             'daplink_unique_id': u'0240000034544e45001500048e41001b8321000097969900', 
00077             'daplink_remount_count': u'0', 
00078             'daplink_bootloader_crc': u'0xc4ebf65c', 
00079             'platform_name': u'K64F', 
00080             'platform_name_unique': u'K64F[0]', 
00081             'daplink_bootloader_version': u'0244', 
00082             'daplink_usb_interfaces': u'MSD, CDC, HID'
00083 
00084         }],
00085         'details_invalid_none' : None
00086     }
00087 
00088 """
00089 Tests for detect_targets.py
00090 """
00091 
00092 class DetectTargetsTest (unittest.TestCase):
00093     """
00094     Test cases for Detect Target functionality
00095     """
00096 
00097     def setUp (self):
00098         """
00099         Called before each test case
00100 
00101         :return:
00102         """      
00103         self.missing_mount_point  = None
00104         self.mount_point  = "D:"
00105         self.invalid_mount_point  = "F:"
00106 
00107     def tearDown (self):
00108         """
00109         Nothing to tear down.
00110         Called after each test case
00111 
00112         :return:
00113         """
00114         pass
00115     
00116     @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_interface_version'))
00117     def test_interface_version_valid (self, mbed_lstools_mock):
00118         """
00119         Test that checks function returns correctly when given a valid Interface Version
00120         
00121         :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
00122         :return 
00123         """
00124         
00125         interface_version = get_interface_version(self.mount_point )
00126         assert interface_version == '0244'
00127         
00128     @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_missing_interface_version'))
00129     def test_interface_version_missing_interface_version (self, mbed_lstools_mock):
00130         """
00131         Test that checks function returns correctly when DETAILS.txt is present
00132         but an interface version is not listed.
00133         
00134         :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
00135         :return 
00136         """
00137 
00138         interface_version = get_interface_version(self.mount_point )
00139         assert interface_version == 'unknown'
00140         
00141     @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none'))
00142     def test_version_none (self, mbed_lstools_mock):
00143         """
00144         Test that checks function returns correctly when a valid mount point is supplied
00145         but DETAILS.txt is not present.
00146         
00147         :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
00148         :return 
00149         """
00150         
00151         interface_version = get_interface_version(self.mount_point )
00152         assert interface_version == 'unknown'
00153         
00154     @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_interface_version'))
00155     def test_interface_version_wrong_mount_point (self, mbed_lstools_mock):
00156         """
00157         Test that checks function returns correctly when there is no board attached to supplied mount point.
00158         
00159         :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
00160         :return 
00161         """
00162         
00163         interface_version = get_interface_version(self.invalid_mount_point )
00164         assert interface_version == 'unknown'
00165         
00166     @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none'))
00167     def test_interface_version_missing_mount_point (self, mbed_lstools_mock):
00168         """
00169         Test that checks function returns correctly when no mount point is supplied.
00170         
00171         :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
00172         :return 
00173         """
00174         
00175         interface_version = get_interface_version(self.missing_mount_point )
00176         assert interface_version == 'unknown'
00177 
00178 if __name__ == '__main__':
00179     unittest.main()