tests: centralize script path setup in conftest.py
Add a pytest-discovered tests/conftest.py for the last30days scripts path and remove duplicate per-file sys.path.insert boilerplate from tests. Normalize affected imports to rely on the shared scripts path and remove the now-unneeded E402 suppressions.
This commit is contained in:
+7
-12
@@ -1,10 +1,6 @@
|
||||
"""Unit tests for dedupe.py: text normalization, similarity metrics, and deduplication."""
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "skills" / "last30days" / "scripts"))
|
||||
|
||||
from lib import dedupe
|
||||
from lib.schema import SourceItem
|
||||
@@ -16,11 +12,11 @@ def _item(title: str, body: str = "", source: str = "reddit", item_id: str = "t1
|
||||
url="https://example.com", engagement={}, metadata={},
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# normalize_text
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestNormalizeText(unittest.TestCase):
|
||||
|
||||
def test_lowercases(self):
|
||||
@@ -35,11 +31,11 @@ class TestNormalizeText(unittest.TestCase):
|
||||
def test_empty_string(self):
|
||||
self.assertEqual(dedupe.normalize_text(""), "")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_ngrams
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestGetNgrams(unittest.TestCase):
|
||||
|
||||
def test_simple_trigrams(self):
|
||||
@@ -58,11 +54,11 @@ class TestGetNgrams(unittest.TestCase):
|
||||
ngrams = dedupe.get_ngrams("A!B")
|
||||
self.assertEqual(ngrams, {"a b"})
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# jaccard_similarity
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestJaccardSimilarity(unittest.TestCase):
|
||||
|
||||
def test_identical_sets(self):
|
||||
@@ -81,11 +77,11 @@ class TestJaccardSimilarity(unittest.TestCase):
|
||||
def test_both_empty(self):
|
||||
self.assertAlmostEqual(dedupe.jaccard_similarity(set(), set()), 0.0)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# token_jaccard
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestTokenJaccard(unittest.TestCase):
|
||||
|
||||
def test_identical_texts(self):
|
||||
@@ -105,11 +101,11 @@ class TestTokenJaccard(unittest.TestCase):
|
||||
# "am" is len 2, "great"/"terrible" are content
|
||||
self.assertGreater(result, 0.0)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# hybrid_similarity
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestHybridSimilarity(unittest.TestCase):
|
||||
|
||||
def test_identical_texts(self):
|
||||
@@ -131,11 +127,11 @@ class TestHybridSimilarity(unittest.TestCase):
|
||||
max(ngram_sim, token_sim),
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# item_text
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestItemText(unittest.TestCase):
|
||||
|
||||
def test_combines_fields(self):
|
||||
@@ -159,11 +155,11 @@ class TestItemText(unittest.TestCase):
|
||||
self.assertIn("john", text)
|
||||
self.assertIn("r/python", text)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# dedupe_items
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestDedupeItems(unittest.TestCase):
|
||||
|
||||
def test_keeps_unique_items(self):
|
||||
@@ -212,6 +208,5 @@ class TestDedupeItems(unittest.TestCase):
|
||||
result_loose = dedupe.dedupe_items(items, threshold=0.3)
|
||||
self.assertEqual(len(result_loose), 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user