Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Issues = "https://github.com/kanedata/ixbrl-parse/issues"
Source = "https://github.com/kanedata/ixbrl-parse"

[project.scripts]
ixbrlparse = "ixbrlparse.cli:ixbrlparse"
ixbrlparse = "ixbrlparse.cli:ixbrlparse_cli"

[tool.hatch.version]
path = "src/ixbrlparse/__about__.py"
Expand Down Expand Up @@ -170,5 +170,5 @@ tests = ["tests", "*/ixbrlparse/tests"]
[tool.coverage.report]
exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"]

[tool.hatch.build.targets.sdist]
include = ["/ixbrlparse"]
[tool.hatch.build]
packages = ["src/ixbrlparse"]
128 changes: 124 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,144 @@
import io
import json
import subprocess
import sys

import pytest
from click.testing import CliRunner

import ixbrlparse.__main__ as ixmain
from ixbrlparse.cli import ixbrlparse_cli

_ = ixmain

cli_options = [
[sys.executable, "-m", "ixbrlparse"],
["ixbrlparse"],
]


def test_cli():
buffer = io.StringIO()
runner = CliRunner()
result = runner.invoke(ixbrlparse_cli, ["--outfile", buffer, "tests/test_accounts/account_1.html"]) # type: ignore
result = runner.invoke(
ixbrlparse_cli,
["--outfile", buffer, "tests/test_accounts/account_1.html"],
) # type: ignore
assert result.exit_code == 0
assert ",CurrentAssets,2909.0," in buffer.getvalue()


@pytest.mark.parametrize("cli_command", cli_options)
def test_cli_raw(tmp_path, cli_command):
f = tmp_path / "output.csv"
result = subprocess.run( # noqa: S603
[
*cli_command,
"--outfile",
str(f),
"tests/test_accounts/account_1.html",
],
check=False,
capture_output=True,
text=True,
) # type: ignore
assert result.returncode == 0
with open(f) as file:
assert ",CurrentAssets,2909.0," in file.read()


def test_cli_json():
buffer = io.StringIO()
runner = CliRunner()
result = runner.invoke(
ixbrlparse_cli, # type: ignore
["--outfile", buffer, "--format", "json", "tests/test_accounts/account_1.html"], # type: ignore
[
"--outfile",
buffer,
"--format",
"json",
"tests/test_accounts/account_1.html",
], # type: ignore
)
assert result.exit_code == 0
data = json.loads(buffer.getvalue())
assert data["numeric"][2]["name"] == "CurrentAssets"
assert data["numeric"][2]["value"] == 2909.0


@pytest.mark.parametrize("cli_command", cli_options)
def test_cli_json_raw(tmp_path, cli_command):
f = tmp_path / "output.json"
result = subprocess.run( # noqa: S603
[
*cli_command,
"--outfile",
str(f),
"--format",
"json",
"tests/test_accounts/account_1.html",
],
check=False,
capture_output=True,
text=True,
) # type: ignore
assert result.returncode == 0
with open(f) as file:
data = json.load(file)
assert data["numeric"][2]["name"] == "CurrentAssets"
assert data["numeric"][2]["value"] == 2909.0


def test_cli_unknown_format():
buffer = io.StringIO()
runner = CliRunner()
result = runner.invoke(
ixbrlparse_cli, # type: ignore
["--outfile", buffer, "--format", "flurg", "tests/test_accounts/account_1.html"], # type: ignore
[
"--outfile",
buffer,
"--format",
"flurg",
"tests/test_accounts/account_1.html",
], # type: ignore
)
assert result.exit_code != 0
data = buffer.getvalue()
assert not data


@pytest.mark.parametrize("cli_command", cli_options)
def test_cli_unknown_format_raw(tmp_path, cli_command):
f = tmp_path / "output.txt"
result = subprocess.run( # noqa: S603
[
*cli_command,
"--outfile",
str(f),
"--format",
"flurg",
"tests/test_accounts/account_1.html",
],
check=False,
capture_output=True,
text=True,
) # type: ignore
assert result.returncode != 0
assert not f.exists()


def test_cli_jsonl():
buffer = io.StringIO()
runner = CliRunner()
result = runner.invoke(
ixbrlparse_cli, # type: ignore
["--outfile", buffer, "--format", "jsonl", "tests/test_accounts/account_1.html"], # type: ignore
[
"--outfile",
buffer,
"--format",
"jsonl",
"tests/test_accounts/account_1.html",
], # type: ignore
)
assert result.exit_code == 0
lines = buffer.getvalue().splitlines()
Expand All @@ -59,3 +150,32 @@ def test_cli_jsonl():
else:
msg = "CurrentAssets not found"
raise AssertionError(msg)


@pytest.mark.parametrize("cli_command", cli_options)
def test_cli_jsonl_raw(tmp_path, cli_command):
f = tmp_path / "output.jsonl"
result = subprocess.run( # noqa: S603
[
*cli_command,
"--outfile",
str(f),
"--format",
"jsonl",
"tests/test_accounts/account_1.html",
],
check=False,
capture_output=True,
text=True,
) # type: ignore
assert result.returncode == 0
with open(f) as file:
lines = file.readlines()
for line in lines:
data = json.loads(line)
if data["name"] == "CurrentAssets":
assert data["value"] == 2909.0
break
else:
msg = "CurrentAssets not found"
raise AssertionError(msg)