Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Update tests

davidmezzetti03b97cbc52a3
diff --git a/test/python/testdatabase/testembeddings.py b/test/python/testdatabase/testembeddings.py
index cf7df2b4..30802fcf 100644
--- a/test/python/testdatabase/testembeddings.py
+++ b/test/python/testdatabase/testembeddings.py
@@ -129,9 +129,19 @@ class TestEmbeddings(unittest.TestCase):
Test empty index
"""
+ # Test search against empty index
embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2", "content": True})
self.assertEqual(embeddings.search("test"), [])
+ # Test index with no data
+ embeddings.index([])
+ self.assertIsNone(embeddings.ann)
+
+ # Test upsert with no data
+ embeddings.index([(0, "this is a test", None)])
+ embeddings.upsert([])
+ self.assertIsNotNone(embeddings.ann)
+
def testGenerator(self):
"""
Test index with a generator
@@ -189,6 +199,15 @@ class TestEmbeddings(unittest.TestCase):
self.assertIn("txtai", output.getvalue())
+ def testInvalidData(self):
+ """
+ Test invalid JSON data
+ """
+
+ # Test invalid JSON value
+ with self.assertRaises(ValueError):
+ self.embeddings.index([(0, {"text": "This is a test", "flag": float("NaN")}, None)])
+
def testMultiSave(self):
"""
Tests multiple successive saves
diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.py
index e9c49515..5f0c4ac4 100644
--- a/test/python/testdatabase/testsql.py
+++ b/test/python/testdatabase/testsql.py
@@ -161,6 +161,9 @@ class TestSQL(unittest.TestCase):
'func(func2(s.indexid + 1), json_extract(data, "$.a")) as "func(func2(indexid + 1), a)"',
)
self.assertSql("select", "select func(func2(indexid + 1), a) a from txtai", 'func(func2(s.indexid + 1), json_extract(data, "$.a")) a')
+ self.assertSql("select", "select 'prefix' || id from txtai", "'prefix' || s.id as \"'prefix' || id\"")
+ self.assertSql("select", "select 'prefix' || id id from txtai", "'prefix' || s.id id")
+ self.assertSql("select", "select 'prefix' || a a from txtai", "'prefix' || json_extract(data, \"$.a\") a")
def testSimilar(self):
"""
diff --git a/test/python/testembeddings.py b/test/python/testembeddings.py
index be0753f4..0c4f331d 100644
--- a/test/python/testembeddings.py
+++ b/test/python/testembeddings.py
@@ -70,9 +70,19 @@ class TestEmbeddings(unittest.TestCase):
Test empty index
"""
- embeddings = Embeddings()
+ # Test search against empty index
+ embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2"})
self.assertEqual(embeddings.search("test"), [])
+ # Test index with no data
+ embeddings.index([])
+ self.assertIsNone(embeddings.ann)
+
+ # Test upsert with no data
+ embeddings.index([(0, "this is a test", None)])
+ embeddings.upsert([])
+ self.assertIsNotNone(embeddings.ann)
+
def testExternal(self):
"""
Test embeddings backed by external vectors
diff --git a/test/python/testpipeline/testtabular.py b/test/python/testpipeline/testtabular.py
index 973e86ef..cfb5bd63 100644
--- a/test/python/testpipeline/testtabular.py
+++ b/test/python/testpipeline/testtabular.py
@@ -32,7 +32,8 @@ class TestTabular(unittest.TestCase):
row = {"id": 0, "text": "This is a test", "flag": 1}
- # When content is enabled, both (uid, text, tags) and (uid, data, tags) rows will be generated
+ # When content is enabled, both (uid, text, tags) and (uid, data, tags) rows are generated
+ # given that data doesn't necessarily include the text to index
rows = tabular([row])
uid, data, _ = rows[1]
@@ -83,6 +84,21 @@ class TestTabular(unittest.TestCase):
self.assertEqual(uid, 0)
self.assertEqual(text, "This is a test")
+ def testMissingColumns(self):
+ """
+ Tests rows with uneven or missing columns
+ """
+
+ tabular = Tabular("id", ["text"], True)
+
+ rows = tabular([{"id": 0, "text": "This is a test", "metadata": "meta"}, {"id": 1, "text": "This is a test"}])
+
+ # When content is enabled both (id, text, tag) and (id, data, tag) tuples are generated given that
+ # data doesn't necessarily include the text to index
+ _, data, _ = rows[3]
+
+ self.assertIsNone(data["metadata"])
+
def testNoColumns(self):
"""
Tests creating text without specifying columns