Skip to content

Commit

Permalink
Add more cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remisalmon committed Jul 24, 2024
1 parent 79fc503 commit d1b922d
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"])
Expand All @@ -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 == ""

0 comments on commit d1b922d

Please sign in to comment.