Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Increase test coverage

davidmezzetti4b0b45468f98
diff --git a/test/python/testapi/testextension.py b/test/python/testapi/testextension.py
index 39bb4922..33ad71b0 100644
--- a/test/python/testapi/testextension.py
+++ b/test/python/testapi/testextension.py
@@ -98,6 +98,14 @@ class TestExtension(unittest.TestCase):
cls.client = TestExtension.start()
+ def testEmpty(self):
+ """
+ Tests an empty extension.
+ """
+
+ extension = Extension()
+ self.assertIsNone(extension(None))
+
def testExtension(self):
"""
Tests a pipeline extension.
diff --git a/test/python/testtrainer.py b/test/python/testtrainer.py
index c259974a..6c8d7840 100644
--- a/test/python/testtrainer.py
+++ b/test/python/testtrainer.py
@@ -10,6 +10,7 @@ import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
+from txtai.data import Data
from txtai.pipeline import HFTrainer, Labels, Questions
@@ -131,6 +132,13 @@ class TestTrainer(unittest.TestCase):
labels = Labels((model, tokenizer), dynamic=False)
self.assertEqual(labels("cat")[0][0], 1)
+ def testEmpty(self):
+ """
+ Tests an empty training data object.
+ """
+
+ self.assertIsNone(Data(None, None, None).process(None))
+
def testQA(self):
"""
Tests training a QA model.
diff --git a/test/python/testwordvectors.py b/test/python/testwordvectors.py
index 6702b2a1..ef8d6f46 100644
--- a/test/python/testwordvectors.py
+++ b/test/python/testwordvectors.py
@@ -53,7 +53,7 @@ class TestWordVectors(unittest.TestCase):
# Generate data
documents = [(x, "This is a test", None) for x in range(1000)]
- model = VectorsFactory.create({"path": self.path}, None)
+ model = VectorsFactory.create({"path": self.path, "parallel": True}, None)
ids, dimension, stream = model.index(documents)
diff --git a/test/python/testworkflow.py b/test/python/testworkflow.py
index 05c87af0..d0663a14 100644
--- a/test/python/testworkflow.py
+++ b/test/python/testworkflow.py
@@ -11,7 +11,7 @@ import torch
from txtai.api import API
from txtai.embeddings import Documents, Embeddings
-from txtai.pipeline import Summary, Translation, Textractor
+from txtai.pipeline import Nop, Segmentation, Summary, Translation, Textractor
from txtai.workflow import Workflow, Task, FileTask, ImageTask, RetrieveTask, StorageTask, WorkflowTask
# pylint: disable = C0411
@@ -89,6 +89,25 @@ class TestWorkflow(unittest.TestCase):
self.assertEqual(index, 0)
self.assertEqual(data[0][1], "txtai builds an AI-powered index over sections")
+ def testConcurrentWorkflow(self):
+ """
+ Tests running concurrent task actions
+ """
+
+ nop = Nop()
+
+ workflow = Workflow([Task([nop, nop], concurrency="thread")])
+ results = list(workflow([2, 4]))
+ self.assertEqual(results, [(2, 2), (4, 4)])
+
+ workflow = Workflow([Task([nop, nop], concurrency="process")])
+ results = list(workflow([2, 4]))
+ self.assertEqual(results, [(2, 2), (4, 4)])
+
+ workflow = Workflow([Task([nop, nop], concurrency="unknown")])
+ results = list(workflow([2, 4]))
+ self.assertEqual(results, [(2, 2), (4, 4)])
+
def testExtractWorkflow(self):
"""
Tests column extraction tasks
@@ -116,6 +135,14 @@ class TestWorkflow(unittest.TestCase):
self.assertEqual(results[0].size, (1024, 682))
+ def testInvalidWorkflow(self):
+ """
+ Tests task with invalid parameters
+ """
+
+ with self.assertRaises(TypeError):
+ Task(invalid=True)
+
def testMergeWorkflow(self):
"""
Tests merge tasks
@@ -148,6 +175,31 @@ class TestWorkflow(unittest.TestCase):
results = list(workflow([(1, "text", "tags")]))
self.assertEqual(results[0], (0, "text", None))
+ def testMergeUnbalancedWorkflow(self):
+ """
+ Test merge tasks with unbalanced outputs (i.e. one action produce more output than another for same input).
+ """
+
+ nop = Nop()
+ segment1 = Segmentation(sentences=True)
+
+ task = Task([nop, segment1])
+
+ # Test hstack
+ workflow = Workflow([task])
+ results = list(workflow(["This is a test sentence. And another sentence to split."]))
+ self.assertEqual(
+ results, [("This is a test sentence. And another sentence to split.", ["This is a test sentence.", "And another sentence to split."])]
+ )
+
+ # Test vstack
+ task.merge = "vstack"
+ workflow = Workflow([task])
+ results = list(workflow(["This is a test sentence. And another sentence to split."]))
+ self.assertEqual(
+ results, ["This is a test sentence. And another sentence to split.", "This is a test sentence.", "And another sentence to split."]
+ )
+
def testNumpyWorkflow(self):
"""
Tests a numpy workflow
@@ -243,6 +295,12 @@ class TestWorkflow(unittest.TestCase):
# Read from string
config = """
+ # Embeddings index
+ writable: true
+ scoring: bm25
+ embeddings:
+ path: google/bert_uncased_L-2_H-128_A-2
+
# Text segmentation
segmentation:
sentences: true
@@ -252,6 +310,7 @@ class TestWorkflow(unittest.TestCase):
segment:
tasks:
- action: segmentation
+ - action: index
"""
app = API(config)