def test_show_subcommand_set_option():

    with patch(
            'todo.cmd_manager.CmdLineParser._show_action') as mock_show_action:

        parser = CmdLineParser(['show', '-c'])

        assert vars(parser.args) == {
            'all': False,
            'complete': True,
            'incomplete': False,
            'init': False,
            'execute_cmd': mock_show_action,
            'file_path': None
        }

        parser = CmdLineParser(['show', '-i'])
        assert vars(parser.args) == {
            'all': False,
            'complete': False,
            'incomplete': True,
            'init': False,
            'execute_cmd': mock_show_action,
            'file_path': None
        }

        parser = CmdLineParser(['show', '-a'])
        assert vars(parser.args) == {
            'all': True,
            'complete': False,
            'incomplete': False,
            'init': False,
            'execute_cmd': mock_show_action,
            'file_path': None
        }
def test_generate_next_id():

    with patch('todo.cmd_manager.Todo') as mock_todo:
        mock_todo.find_all.return_value = [Todo(id=8)]
        result = CmdLineParser([]).generate_next_id()
        assert mock_todo.find_all.call_args == call(order_by='id desc', size=1)
        assert result == 9
        mock_todo.find_all.return_value = None
        result = CmdLineParser([]).generate_next_id()
        assert result == 1
def test_update_subcommand_without_set_right_args():
    with pytest.raises(SystemExit):
        CmdLineParser(['update'])

    with pytest.raises(SystemExit):
        CmdLineParser(['update', '-i', '-t'])

    with pytest.raises(SystemExit):
        CmdLineParser(['update', '-i', '1'])

    with pytest.raises(SystemExit):
        CmdLineParser(['update', '-i', '1', '-t'])

    with pytest.raises(SystemExit):
        CmdLineParser(['update', '-t', 'hello'])
def test_complete_action():
    with patch('todo.cmd_manager.Todo') as mock_todo:
        with patch('todo.cmd_manager.time.time') as mock_time:
            mock_time.return_value = 1010.1010
            CmdLineParser(['complete', '10'])._complete_action()
            assert mock_todo.call_args == call(is_completed=True,
                                               id=10,
                                               update_at=1010.1010)
            assert mock_todo.return_value.update.call_count == 1
def test_add_subcommand_with_set_context():
    with patch(
            'todo.cmd_manager.CmdLineParser._add_action') as mock_add_action:
        parser = CmdLineParser(['add', 'hello'])
        assert vars(parser.args) == {
            'add-text': 'hello',
            'init': False,
            'execute_cmd': mock_add_action,
            'file_path': None
        }
def test_delete_subcommand_with_set_args():
    with patch('todo.cmd_manager.CmdLineParser._delete_action'
               ) as mock_delete_action:
        parser = CmdLineParser(['delete', '1'])
        assert vars(parser.args) == {
            'del-task-id': 1,
            'init': False,
            'execute_cmd': mock_delete_action,
            'file_path': None
        }
def test_update_action():
    with patch('todo.cmd_manager.Todo') as mock_todo:
        with patch('todo.cmd_manager.time.time') as mock_time:
            mock_time.return_value = 10.102
            CmdLineParser(['update', '-i', '1', '-t',
                           'new text'])._update_action()
            assert mock_todo.call_args == call(text='new text',
                                               id=1,
                                               update_at=10.102)
            assert mock_todo.return_value.update.call_count == 1
def test_add_action():
    with patch('todo.cmd_manager.Todo') as mock_todo:
        with patch(
                'todo.cmd_manager.CmdLineParser.generate_next_id') as mock_id:
            mock_id.return_value = 10
            CmdLineParser(['add', 'test text'])._add_action()
            assert mock_todo.call_args == call(text='test text', id=10)
            assert mock_todo.return_value.save.call_count == 1
            assert mock_todo.find_all.call_args == call(
                {'is_completed': False})
def test_update_subcommand_with_set_args():
    with patch('todo.cmd_manager.CmdLineParser._update_action'
               ) as mock_update_action:
        parser = CmdLineParser(['update', '-i', '1', '-t', 'Hello'])
        assert vars(parser.args) == {
            'init': False,
            'update_task_id': 1,
            'update_task_text': 'Hello',
            'execute_cmd': mock_update_action,
            'file_path': None
        }
def test_delete_subcommand_without_set_args():

    with pytest.raises(SystemExit):
        CmdLineParser(['delete'])
def test_add_subcommand_without_set_context():
    with pytest.raises(SystemExit):
        mock_execute_cmder = Mock()
        CmdLineParser(['add'])
def test_init_action():
    with patch('todo.cmd_manager.Todo') as mock_todo:
        parser = CmdLineParser(['--init'])
        parser._init_action()
        assert mock_todo.drop_table.call_count == 1
def test_show_action_when_choice_all():
    with patch('todo.cmd_manager.Todo') as mock_todo:
        CmdLineParser(['show', '-a'])._show_action()
        assert mock_todo.find_all.call_args == call()
def test_show_action_when_choice_incomplete():
    with patch('todo.cmd_manager.Todo') as mock_todo:
        CmdLineParser(['show', '-i'])._show_action()
        assert mock_todo.find_all.call_args == call({'is_completed': False})
def test_delete_action():
    with patch('todo.cmd_manager.Todo') as mock_todo:
        CmdLineParser(['delete', '1'])._delete_action()
        assert mock_todo.call_args == call(id=1)
        assert mock_todo.return_value.remove.call_count == 1