From fcfd340ff1fe26051ca7153272f4d2df9ee4e0cc Mon Sep 17 00:00:00 2001 From: davidmezzetti <561939+davidmezzetti@users.noreply.github.com> Date: Tue, 21 Jul 2026 12:18:35 -0400 Subject: [PATCH] Cleanup tests --- test/python/testapi/testcluster.py | 31 +++++++++++++------------- test/python/testconsole.py | 24 ++++++++++---------- test/python/testdatabase/testsql.py | 31 +++++++++++++------------- test/python/testscoring/testkeyword.py | 29 ++++++++++++------------ 4 files changed, 58 insertions(+), 57 deletions(-) diff --git a/test/python/testapi/testcluster.py b/test/python/testapi/testcluster.py index 646d035e..2b30cb4a 100644 --- a/test/python/testapi/testcluster.py +++ b/test/python/testapi/testcluster.py @@ -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 diff --git a/test/python/testconsole.py b/test/python/testconsole.py index c2db80b4..52392d71 100644 --- a/test/python/testconsole.py +++ b/test/python/testconsole.py @@ -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 diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.py index 39085b01..dcd92a6b 100644 --- a/test/python/testdatabase/testsql.py +++ b/test/python/testdatabase/testsql.py @@ -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. diff --git a/test/python/testscoring/testkeyword.py b/test/python/testscoring/testkeyword.py index b195e3d1..540fc9c2 100644 --- a/test/python/testscoring/testkeyword.py +++ b/test/python/testscoring/testkeyword.py @@ -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