Skip to content

Commit

Permalink
fix unittest
Browse files Browse the repository at this point in the history
fix unittest
  • Loading branch information
stanley31huang committed Aug 6, 2024
1 parent 7c3360b commit 3d826b1
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 68 deletions.
54 changes: 0 additions & 54 deletions providers/base/tests/test_wwan_resource_with_iterations.py

This file was deleted.

123 changes: 109 additions & 14 deletions providers/base/tests/test_wwan_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,47 @@ def test_invoked_with_mmcli(self, mock_mmcli):
mmcli_instance.get_modem_ids.return_value = ["test"]
mock_mmcli.return_value = mmcli_instance

sys.argv = ["wwan_tests.py", "resources", "--use-cli"]
sys.argv = [
"wwan_tests.py",
"resources",
"--use-cli",
"--iteration",
"1",
]

with redirect_stdout(StringIO()):
wwan_tests.Resources().invoked()
self.assertTrue(mock_mmcli.called)
self.assertTrue(mmcli_instance.get_equipment_id.called)
self.assertTrue(mmcli_instance.get_manufacturer.called)
self.assertTrue(mmcli_instance.get_model_name.called)
self.assertTrue(mmcli_instance.get_firmware_revision.called)
self.assertTrue(mmcli_instance.get_hardware_revision.called)
self.assertEqual(mock_mmcli.call_count, 1)
self.assertEqual(mmcli_instance.get_equipment_id.call_count, 1)
self.assertEqual(mmcli_instance.get_manufacturer.call_count, 1)
self.assertEqual(mmcli_instance.get_model_name.call_count, 1)
self.assertEqual(
mmcli_instance.get_firmware_revision.call_count, 1
)
self.assertEqual(
mmcli_instance.get_hardware_revision.call_count, 1
)

@patch("wwan_tests.MMDbus")
def test_invoked_with_mmdbus(self, mock_mmdbus):
mmdbus_instance = Mock()
mmdbus_instance.get_modem_ids.return_value = ["test"]
mock_mmdbus.return_value = mmdbus_instance

sys.argv = ["wwan_tests.py", "resources"]
sys.argv = ["wwan_tests.py", "resources", "--iteration", "2"]

with redirect_stdout(StringIO()):
wwan_tests.Resources().invoked()
self.assertTrue(mock_mmdbus.called)
self.assertTrue(mmdbus_instance.get_equipment_id.called)
self.assertTrue(mmdbus_instance.get_manufacturer.called)
self.assertTrue(mmdbus_instance.get_model_name.called)
self.assertTrue(mmdbus_instance.get_firmware_revision.called)
self.assertTrue(mmdbus_instance.get_hardware_revision.called)
self.assertEqual(mock_mmdbus.call_count, 1)
self.assertEqual(mmdbus_instance.get_equipment_id.call_count, 2)
self.assertEqual(mmdbus_instance.get_manufacturer.call_count, 2)
self.assertEqual(mmdbus_instance.get_model_name.call_count, 2)
self.assertEqual(
mmdbus_instance.get_firmware_revision.call_count, 2
)
self.assertEqual(
mmdbus_instance.get_hardware_revision.call_count, 2
)


class TestCommonFunctions(unittest.TestCase):
Expand Down Expand Up @@ -264,3 +278,84 @@ def test_invoked_call_error(self, mock_arg, mock_mmctx, mock_run):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)


class TestThreeGppConnectionTest(unittest.TestCase):

def test_register_argument(self):

sys.argv = [
"wwan_tests.py",
"3gpp-connection",
"hw_id",
"wwan_net_if",
"apn",
"30",
]
obj_3gppscan = wwan_tests.ThreeGppConnection()
ret_args = obj_3gppscan.register_argument()
self.assertEqual(ret_args.hw_id, "hw_id")
self.assertEqual(ret_args.wwan_net_if, "wwan_net_if")
self.assertEqual(ret_args.apn, "apn")
self.assertEqual(ret_args.wwan_setup_time, 30)

@patch("wwan_tests._ping_test")
@patch("wwan_tests._destroy_3gpp_connection")
@patch("wwan_tests._create_3gpp_connection")
@patch("wwan_tests.WWANTestCtx")
@patch("wwan_tests.ThreeGppConnection.register_argument")
def test_invoked_successfully(
self, mock_arg, mock_mmctx, mock_create_conn, mock_rm_conn, mock_ping
):
mock_arg.return_value = argparse.Namespace(
hw_id="2", wwan_net_if="wwan0", apn="internet", wwan_setup_time=0.1
)
mock_get_primary_port = Mock()
mmcli_instance = Mock()
mmcli_instance.modem_idx = "0"
mmcli_instance.mm_obj = Mock(get_primary_port=mock_get_primary_port)
mock_mmctx.return_value.__enter__.return_value = mmcli_instance
mock_ping.return_value = 0

with redirect_stdout(StringIO()):
with self.assertRaises(SystemExit) as context:
obj_3gppscan = wwan_tests.ThreeGppConnection()
obj_3gppscan.invoked()

mock_mmctx.assert_called_with("2", True, True)
self.assertEqual(mock_arg.call_count, 1)
self.assertEqual(mock_get_primary_port.call_count, 1)
self.assertEqual(mock_ping.call_count, 1)
self.assertEqual(mock_create_conn.call_count, 1)
self.assertEqual(mock_rm_conn.call_count, 1)
self.assertEqual(context.exception.code, 0)

@patch("wwan_tests._ping_test")
@patch("wwan_tests._destroy_3gpp_connection")
@patch("wwan_tests._create_3gpp_connection")
@patch("wwan_tests.WWANTestCtx")
@patch("wwan_tests.ThreeGppConnection.register_argument")
def test_invoked_failed_exit_code(
self, mock_arg, mock_mmctx, mock_create_conn, mock_rm_conn, mock_ping
):
mock_arg.return_value = argparse.Namespace(
hw_id="2", wwan_net_if="wwan0", apn="internet", wwan_setup_time=0.1
)
mock_get_primary_port = Mock()
mmcli_instance = Mock()
mmcli_instance.modem_idx = "0"
mmcli_instance.mm_obj = Mock(get_primary_port=mock_get_primary_port)
mock_mmctx.return_value.__enter__.return_value = mmcli_instance
mock_ping.return_value = 1

with redirect_stdout(StringIO()):
with self.assertRaises(SystemExit) as context:
obj_3gppscan = wwan_tests.ThreeGppConnection()
obj_3gppscan.invoked()

mock_mmctx.assert_called_with("2", True, True)
self.assertEqual(mock_arg.call_count, 1)
self.assertEqual(mock_ping.call_count, 1)
self.assertEqual(mock_create_conn.call_count, 1)
self.assertEqual(mock_rm_conn.call_count, 1)
self.assertEqual(context.exception.code, 1)

0 comments on commit 3d826b1

Please sign in to comment.