чуть подредактировал

This commit is contained in:
2026-09-07 22:33:51 +03:00
parent 320d88b0b8
commit 843a6a1c12
9 changed files with 245 additions and 121 deletions

View File

@@ -1,64 +1,78 @@
from pathlib import Path
"""Tests for core numeric analysis."""
from pathlib import Path
import pytest
from text_analyzer.core import analyze_file
from numstats.core import analyze_numbers, read_numbers
def test_basic_analysis(tmp_path: Path) -> None:
test_file = tmp_path / "test.txt"
test_file.write_text("hello world\nhello python")
res = analyze_file(test_file)
assert res["lines"] == 2
assert res["words"] == 4
assert res["top_words"] == [("hello", 2), ("world", 1), ("python", 1)]
def test_read_numbers_basic(tmp_path: Path) -> None:
f = tmp_path / "data.txt"
f.write_text("1\n2.5\n3\n")
nums = read_numbers(f)
assert nums == [1.0, 2.5, 3.0]
def test_file_not_found() -> None:
def test_read_numbers_skips_non_numeric(tmp_path: Path) -> None:
f = tmp_path / "bad.txt"
f.write_text("1\na\n2\n")
nums = read_numbers(f)
assert nums == [1.0, 2.0]
def test_read_numbers_min_filter(tmp_path: Path) -> None:
f = tmp_path / "filter.txt"
f.write_text("1\n5\n10\n")
nums = read_numbers(f, min_value=5)
assert nums == [5.0, 10.0]
def test_read_numbers_file_not_found() -> None:
with pytest.raises(FileNotFoundError):
analyze_file(Path("non_existent_file_123.txt"))
read_numbers(Path("nonexistent.txt"))
def test_empty_file(tmp_path: Path) -> None:
test_file = tmp_path / "empty.txt"
test_file.write_text("")
res = analyze_file(test_file)
assert res["lines"] == 0
assert res["words"] == 0
assert res["top_words"] == []
def test_analyze_numbers_empty(tmp_path: Path) -> None:
f = tmp_path / "empty.txt"
f.write_text("")
res = analyze_numbers(f)
assert res["count"] == 0
assert res["mean"] == 0.0
assert res["min"] is None
assert res["max"] is None
assert res["top"] == []
@pytest.mark.parametrize(
"min_len,expected_words",
"content, top_n, min_len, expected_count, expected_sum, expected_mean, expected_top",
[
(1, 4),
(3, 2),
(4, 1),
("1\n2\n3\n", 2, 0, 3, 6.0, 2.0, [3.0, 2.0]),
("-5\n0\n10\n", 1, 0, 3, 5.0, 5.0/3, [10.0]),
("1\n2\n3\n4\n", 3, 2, 3, 9.0, 3.0, [4.0, 3.0, 2.0]),
("", 5, 0, 0, 0.0, 0.0, []),
],
)
def test_min_length_filter(tmp_path: Path, min_len: int, expected_words: int) -> None:
test_file = tmp_path / "filter.txt"
test_file.write_text("a ab abc abcd")
res = analyze_file(test_file, min_len=min_len)
assert res["words"] == expected_words
def test_analyze_numbers_parametrized(
tmp_path: Path,
content: str,
top_n: int,
min_len: int,
expected_count: int,
expected_sum: float,
expected_mean: float,
expected_top: list,
) -> None:
f = tmp_path / "param.txt"
f.write_text(content)
res = analyze_numbers(f, top_n=top_n, min_len=min_len)
assert res["count"] == expected_count
assert abs(res["sum"] - expected_sum) < 1e-9
assert abs(res["mean"] - expected_mean) < 1e-9
assert res["top"] == expected_top
def test_top_n_param(tmp_path: Path) -> None:
test_file = tmp_path / "top.txt"
test_file.write_text("one two two three three three")
res = analyze_file(test_file, top_n=2)
top_words = res["top_words"]
assert isinstance(top_words, list)
assert len(top_words) == 2
def test_punctuation_stripping(tmp_path: Path) -> None:
test_file = tmp_path / "punct.txt"
test_file.write_text("hello, world! hello...")
res = analyze_file(test_file)
assert res["top_words"] == [("hello", 2), ("world", 1)]
def test_analyze_numbers_encoding(tmp_path: Path) -> None:
f = tmp_path / "utf16.txt"
f.write_text("1\n2\n3\n", encoding="utf-16")
res = analyze_numbers(f, encoding="utf-16")
assert res["count"] == 3