From d1b922dbfed6c8c119c759d05df519070bf384b4 Mon Sep 17 00:00:00 2001 From: Remi Salmon Date: Wed, 24 Jul 2024 16:31:38 -0600 Subject: [PATCH] Add more cli tests --- tests/test_cli.py | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 1a95e82..4d519c3 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,10 +1,11 @@ import io -from lkml.tree import ContainerNode, DocumentNode, PairNode, SyntaxToken -from pathlib import Path -from unittest.mock import patch import logging import pytest import lkml +import json + +from pathlib import Path +from unittest.mock import patch @pytest.fixture @@ -25,13 +26,18 @@ def test_absence_of_debug_flag_is_parsed_to_log_level_warn(lookml_path): assert args.log_level == logging.WARN -def test_default_option(lookml_path): +def test_load_with_bad_argument_raises_type_error(): + with pytest.raises(TypeError): + lkml.load(stream=100) + + +def test_parse_default_option(lookml_path): args = lkml.parse_args([lookml_path]) assert args.json is True assert args.lookml is args.write is False -def test_options(lookml_path): +def test_parse_options(lookml_path): args = lkml.parse_args([lookml_path, "--json"]) assert args.json is True args = lkml.parse_args([lookml_path, "--lookml"]) @@ -44,15 +50,23 @@ def test_options(lookml_path): args = lkml.parse_args([lookml_path, "--json", "--lookml"]) -@patch("lkml.load") -@patch("lkml.parse_args") -def test_run_cli(mock_parse_args, mock_load, lookml_path): - mock_parse_args.return_value.file = io.StringIO() - mock_parse_args.return_value.log_level = logging.WARN - mock_load.return_value = {"a": "1"} - lkml.cli() +def test_run_cli_default_option(lookml_path, capsys): + with patch("sys.argv", ["file.py", lookml_path]): + lkml.cli() + captured = capsys.readouterr() + assert json.loads(captured.out) is not None + assert captured.err == "" -def test_load_with_bad_argument_raises_type_error(): - with pytest.raises(TypeError): - lkml.load(stream=100) +def test_run_cli_options(lookml_path, capsys): + with patch("sys.argv", ["file.py", "--json", lookml_path]): + lkml.cli() + captured = capsys.readouterr() + assert json.loads(captured.out) is not None + assert captured.err == "" + + with patch("sys.argv", ["file.py", "--lookml", lookml_path]): + lkml.cli() + captured = capsys.readouterr() + assert lkml.load(captured.out) is not None + assert captured.err == ""