test_cli.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding:utf-8 -*-
  2. from unittest.mock import MagicMock, patch
  3. import pytest
  4. from dw_base.datax import cli
  5. @patch('dw_base.datax.cli.JobConfigGenerator')
  6. def test_gen_json_invokes_generator(MockGen):
  7. mock_inst = MagicMock()
  8. MockGen.return_value = mock_inst
  9. cli.main(['gen-json', 'some.ini',
  10. '-start-date', '20260422',
  11. '-stop-date', '20260423'])
  12. MockGen.assert_called_once()
  13. call_args = MockGen.call_args[0]
  14. # 位置参数:(base_dir, ini, start, stop, output)
  15. assert call_args[1] == 'some.ini'
  16. assert call_args[2] == '20260422'
  17. assert call_args[3] == '20260423'
  18. assert call_args[4].endswith('some.json')
  19. mock_inst.run.assert_called_once()
  20. def test_gen_json_missing_args_exits():
  21. with pytest.raises(SystemExit):
  22. cli.main(['gen-json', 'some.ini']) # 缺 -start-date/-stop-date
  23. def test_unknown_subcommand_exits():
  24. with pytest.raises(SystemExit):
  25. cli.main(['bogus-cmd'])
  26. @patch('dw_base.datax.cli.JobConfigGenerator')
  27. def test_gen_json_speed_cli_args_flow_through(MockGen):
  28. """L3 CLI -channel/-byte/-record 传给 JobConfigGenerator cli_speed_overrides kwarg。"""
  29. mock_inst = MagicMock()
  30. MockGen.return_value = mock_inst
  31. cli.main(['gen-json', 'some.ini',
  32. '-start-date', '20260422',
  33. '-stop-date', '20260423',
  34. '-channel', '20',
  35. '-byte', '20971520',
  36. '-record', '50000'])
  37. kwargs = MockGen.call_args[1]
  38. assert kwargs['cli_speed_overrides'] == {
  39. 'channel': 20, 'byte': 20971520, 'record': 50000,
  40. }
  41. @patch('dw_base.datax.cli.JobConfigGenerator')
  42. def test_gen_json_speed_cli_default_none(MockGen):
  43. """CLI 没传 speed args 时,cli_speed_overrides 里全是 None。"""
  44. mock_inst = MagicMock()
  45. MockGen.return_value = mock_inst
  46. cli.main(['gen-json', 'some.ini',
  47. '-start-date', '20260422',
  48. '-stop-date', '20260423'])
  49. kwargs = MockGen.call_args[1]
  50. assert kwargs['cli_speed_overrides'] == {
  51. 'channel': None, 'byte': None, 'record': None,
  52. }