Cleanup tests

This commit is contained in:
davidmezzetti
2026-07-21 12:18:35 -04:00
parent 77965f44dd
commit fcfd340ff1
4 changed files with 58 additions and 57 deletions
+15 -16
View File
@@ -14,7 +14,8 @@ from unittest.mock import patch
from fastapi.testclient import TestClient
from txtai.api import application
from txtai.api import application, Cluster
# Configuration for an embeddings cluster
CLUSTER = """
@@ -147,21 +148,6 @@ class TestCluster(unittest.TestCase):
cls.httpd1.shutdown()
cls.httpd2.shutdown()
def testShard(self):
"""
Test sharding handles all id shapes without crashing
"""
from txtai.api.cluster import Cluster
cluster = Cluster({"shards": [{"base": "h1"}, {"base": "h2"}, {"base": "h3"}]})
# str id, tuple document, empty-string id, int id and a missing id must all be placed.
documents = [{"id": "doc1"}, (1, "text", None), {"id": ""}, {"id": 0}, {"other": "x"}]
shards = cluster.shard(documents)
self.assertEqual(sum(len(shard) for shard in shards), len(documents))
def testCount(self):
"""
Test cluster count
@@ -235,6 +221,19 @@ class TestCluster(unittest.TestCase):
uids = [result[0]["id"] for result in results]
self.assertEqual(uids, [4, 1])
def testShard(self):
"""
Test sharding handles all id shapes without crashing
"""
cluster = Cluster({"shards": [{"base": "h1"}, {"base": "h2"}, {"base": "h3"}]})
# str id, tuple document, empty-string id, int id and a missing id must all be placed.
documents = [{"id": "doc1"}, (1, "text", None), {"id": ""}, {"id": 0}, {"other": "x"}]
shards = cluster.shard(documents)
self.assertEqual(sum(len(shard) for shard in shards), len(documents))
def testSQL(self):
"""
Test cluster SQL statement
+12 -12
View File
@@ -78,18 +78,6 @@ class TestConsole(unittest.TestCase):
self.assertIn("tasks", self.command(".config"))
def testNoResults(self):
"""
Test a query that matches nothing renders a message instead of a traceback
"""
console = Console()
console.app = self.embeddings
output = self.command("select id, text from txtai where id = 'missing'", console)
self.assertNotIn("Traceback", output)
self.assertIn("No results", output)
def testEmbeddings(self):
"""
Test embeddings index
@@ -134,6 +122,18 @@ class TestConsole(unittest.TestCase):
self.assertIn("wins", self.command("feel good story"))
self.assertIn("Taiwan", self.command("asia"))
def testNoResults(self):
"""
Test a query that matches nothing renders a message instead of a traceback
"""
console = Console()
console.app = self.embeddings
output = self.command("select id, text from txtai where id = 'missing'", console)
self.assertNotIn("Traceback", output)
self.assertIn("No results", output)
def testPreloop(self):
"""
Test preloop
+16 -15
View File
@@ -25,6 +25,22 @@ class TestSQL(unittest.TestCase):
cls.sql = SQL(cls.db)
def testAggregateEmptyResults(self):
"""
Test Aggregate returns empty results for a SQL query with no results
"""
aggregate = Aggregate()
# SQL query, no results across shards - must not raise IndexError
self.assertEqual(aggregate("select id, text, score from txtai where similar('x')", []), [])
# Non-SQL query with no results stays consistent
self.assertEqual(aggregate("plain text query", []), [])
# SQL query with results still aggregates as before
self.assertEqual(aggregate("select count(*) from txtai", [{"count(*)": 1}, {"count(*)": 2}]), [{"count(*)": 3}])
def testAlias(self):
"""
Test alias clauses
@@ -313,21 +329,6 @@ class TestSQL(unittest.TestCase):
self.assertSql("where", prefix + "where f(id) = b(id)", "f(s.id) = b(s.id)")
self.assertSql("where", prefix + "WHERE f(id)", "f(s.id)")
def testAggregateEmptyResults(self):
"""
Test Aggregate returns empty results for a SQL query that matched nothing
(e.g. a sharded query with zero hits across all shards) instead of crashing.
"""
aggregate = Aggregate()
# SQL query, no results across shards - must not raise IndexError
self.assertEqual(aggregate("select id, text, score from txtai where similar('x')", []), [])
# Non-SQL query with no results stays consistent
self.assertEqual(aggregate("plain text query", []), [])
# SQL query with results still aggregates as before
self.assertEqual(aggregate("select count(*) from txtai", [{"count(*)": 1}, {"count(*)": 2}]), [{"count(*)": 3}])
def assertSql(self, clause, query, expected):
"""
Helper method to assert a query clause is as expected.
+15 -14
View File
@@ -49,20 +49,6 @@ class TestKeyword(unittest.TestCase):
self.runTests("txtai.scoring.BM25")
def testTermsEmpty(self):
"""
Test searching a terms index with nothing indexed
"""
# No documents ever inserted
scoring = ScoringFactory.create({"method": "bm25", "terms": True})
self.assertEqual(scoring.search("bear", 1), [])
# Documents present but all skipped (missing text field) - term database never initialized
scoring = ScoringFactory.create({"method": "bm25", "terms": True})
scoring.index([(0, {"other": "value"}, None)])
self.assertEqual(scoring.count(), 0)
self.assertEqual(scoring.search("bear", 1), [])
def testCustomInvalid(self):
"""
Test invalid custom method
@@ -146,6 +132,21 @@ class TestKeyword(unittest.TestCase):
self.runTests("sif")
def testTermsEmpty(self):
"""
Test searching a terms index with nothing indexed
"""
# No documents ever inserted
scoring = ScoringFactory.create({"method": "bm25", "terms": True})
self.assertEqual(scoring.search("bear", 1), [])
# Documents present but all skipped (missing text field) - term database never initialized
scoring = ScoringFactory.create({"method": "bm25", "terms": True})
scoring.index([(0, {"other": "value"}, None)])
self.assertEqual(scoring.count(), 0)
self.assertEqual(scoring.search("bear", 1), [])
def testTFIDF(self):
"""
Test tfidf