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 test method docs for consistency

davidmezzettib14d8769dd32
diff --git a/test/python/testann.py b/test/python/testann.py
index 6540253e..71f42fd7 100644
--- a/test/python/testann.py
+++ b/test/python/testann.py
@@ -13,7 +13,7 @@ from txtai.ann import ANNFactory, ANN
class TestANN(unittest.TestCase):
"""
- ANN tests
+ ANN tests.
"""
def testAnnoy(self):
@@ -124,7 +124,7 @@ class TestANN(unittest.TestCase):
def append(self, name, params=None, length=500):
"""
- Appends new data to index
+ Appends new data to index.
Args:
name: backend name
diff --git a/test/python/testapi/testcluster.py b/test/python/testapi/testcluster.py
index 1151c247..9fc3f7bc 100644
--- a/test/python/testapi/testcluster.py
+++ b/test/python/testapi/testcluster.py
@@ -26,7 +26,7 @@ cluster:
class RequestHandler(BaseHTTPRequestHandler):
"""
- Test HTTP handler
+ Test HTTP handler.
"""
def do_GET(self):
diff --git a/test/python/testapi/testembeddings.py b/test/python/testapi/testembeddings.py
index 038dd717..426a5d70 100644
--- a/test/python/testapi/testembeddings.py
+++ b/test/python/testapi/testembeddings.py
@@ -41,7 +41,7 @@ writable: False
class TestEmbeddings(unittest.TestCase):
"""
- API tests for embeddings indices
+ API tests for embeddings indices.
"""
@staticmethod
diff --git a/test/python/testapi/testextension.py b/test/python/testapi/testextension.py
index 33ad71b0..04f3f906 100644
--- a/test/python/testapi/testextension.py
+++ b/test/python/testapi/testextension.py
@@ -22,7 +22,7 @@ testapi.testextension.SamplePipeline:
class SampleRouter:
"""
- Sample API router
+ Sample API router.
"""
router = APIRouter()
@@ -45,7 +45,7 @@ class SampleRouter:
class SampleExtension(Extension):
"""
- Sample API extension
+ Sample API extension.
"""
def __call__(self, app):
@@ -54,7 +54,7 @@ class SampleExtension(Extension):
class SamplePipeline(Pipeline):
"""
- Sample pipeline
+ Sample pipeline.
"""
def __call__(self, text):
@@ -63,7 +63,7 @@ class SamplePipeline(Pipeline):
class TestExtension(unittest.TestCase):
"""
- API tests for extensions
+ API tests for extensions.
"""
@staticmethod
@@ -100,7 +100,7 @@ class TestExtension(unittest.TestCase):
def testEmpty(self):
"""
- Tests an empty extension.
+ Tests an empty extension
"""
extension = Extension()
@@ -108,7 +108,7 @@ class TestExtension(unittest.TestCase):
def testExtension(self):
"""
- Tests a pipeline extension.
+ Tests a pipeline extension
"""
text = self.client.get("sample?text=Test%20String").json()
diff --git a/test/python/testapi/testpipelines.py b/test/python/testapi/testpipelines.py
index 8b6e85d4..01bfc6fe 100644
--- a/test/python/testapi/testpipelines.py
+++ b/test/python/testapi/testpipelines.py
@@ -56,7 +56,7 @@ translation:
# pylint: disable=R0904
class TestPipelines(unittest.TestCase):
"""
- API tests for pipelines
+ API tests for pipelines.
"""
@staticmethod
diff --git a/test/python/testapi/testworkflow.py b/test/python/testapi/testworkflow.py
index 897305ba..3cef7a44 100644
--- a/test/python/testapi/testworkflow.py
+++ b/test/python/testapi/testworkflow.py
@@ -81,7 +81,7 @@ workflow:
class RequestHandler(BaseHTTPRequestHandler):
"""
- Test HTTP handler
+ Test HTTP handler.
"""
def do_GET(self):
@@ -126,7 +126,7 @@ class RequestHandler(BaseHTTPRequestHandler):
class TestWorkflow(unittest.TestCase):
"""
- API tests for workflows
+ API tests for workflows.
"""
@staticmethod
@@ -170,7 +170,7 @@ class TestWorkflow(unittest.TestCase):
def testServiceGet(self):
"""
- Test workflow with ServiceTask GET via API.
+ Test workflow with ServiceTask GET via API
"""
text = "This is a test sentence. And another sentence to split."
@@ -181,7 +181,7 @@ class TestWorkflow(unittest.TestCase):
def testServicePost(self):
"""
- Test workflow with ServiceTask POST via API.
+ Test workflow with ServiceTask POST via API
"""
text = "This is a test sentence. And another sentence to split."
@@ -192,7 +192,7 @@ class TestWorkflow(unittest.TestCase):
def testServiceXml(self):
"""
- Test workflow with ServiceTask GET via API and XML response.
+ Test workflow with ServiceTask GET via API and XML response
"""
text = "This is a test sentence. And another sentence to split."
@@ -203,7 +203,7 @@ class TestWorkflow(unittest.TestCase):
def testWorkflowLabels(self):
"""
- Test workflow with labels via API.
+ Test workflow with labels via API
"""
text = "This is the best"
@@ -216,7 +216,7 @@ class TestWorkflow(unittest.TestCase):
def testWorkflowSegment(self):
"""
- Test workflow with segmentation via API.
+ Test workflow with segmentation via API
"""
text = "This is a test sentence. And another sentence to split."
diff --git a/test/python/testdatabase/testembeddings.py b/test/python/testdatabase/testembeddings.py
index b0e46485..bdf425f4 100644
--- a/test/python/testdatabase/testembeddings.py
+++ b/test/python/testdatabase/testembeddings.py
@@ -2,6 +2,8 @@
Embeddings+database module tests
"""
+import contextlib
+import io
import os
import tempfile
import unittest
@@ -12,7 +14,7 @@ from txtai.database import Database, SQLException
class TestEmbeddings(unittest.TestCase):
"""
- Embeddings with a database tests
+ Embeddings with a database tests.
"""
@classmethod
@@ -139,6 +141,20 @@ class TestEmbeddings(unittest.TestCase):
self.assertEqual(result["text"], self.data[4])
+ def testInfo(self):
+ """
+ Test info
+ """
+
+ # Create an index for the list of text
+ self.embeddings.index([(uid, text, None) for uid, text in enumerate(self.data)])
+
+ output = io.StringIO()
+ with contextlib.redirect_stdout(output):
+ self.embeddings.info()
+
+ self.assertTrue("txtai" in output.getvalue())
+
def testMultiSave(self):
"""
Tests multiple successive saves
diff --git a/test/python/testdatabase/testencoder.py b/test/python/testdatabase/testencoder.py
new file mode 100644
index 00000000..f28a575e
--- /dev/null
+++ b/test/python/testdatabase/testencoder.py
@@ -0,0 +1,101 @@
+"""
+Tests encoding/decoding database objects
+"""
+
+import glob
+import unittest
+
+from PIL import Image
+
+from txtai.embeddings import Embeddings
+
+# pylint: disable = C0411
+from utils import Utils
+
+
+class TestEncoder(unittest.TestCase):
+ """
+ Encoder tests.
+ """
+
+ @classmethod
+ def setUpClass(cls):
+ """
+ Initialize test data.
+ """
+
+ cls.data = []
+ for path in glob.glob(Utils.PATH + "/*jpg"):
+ cls.data.append((path, {"object": Image.open(path)}, None))
+
+ # Create embeddings model, backed by sentence-transformers & transformers
+ cls.embeddings = Embeddings(
+ {"method": "sentence-transformers", "path": "sentence-transformers/clip-ViT-B-32", "content": True, "objects": "image"}
+ )
+
+ def testDefault(self):
+ """
+ Tests an index with default encoder
+ """
+
+ try:
+ # Change to use default encoder
+ self.embeddings.config["objects"] = True
+ data = [(0, {"object": bytearray([1, 2, 3]), "text": "default test"}, None)]
+
+ # Create an index for the list of images
+ self.embeddings.index(data)
+
+ result = self.embeddings.search("select object from txtai limit 1")[0]
+
+ self.assertEqual(result["object"].getvalue(), bytearray([1, 2, 3]))
+ finally:
+ self.embeddings.config["objects"] = "image"
+
+ def testImages(self):
+ """
+ Tests an index with image encoder
+ """
+
+ # Create an index for the list of images
+ self.embeddings.index(self.data)
+
+ result = self.embeddings.search("select id, object from txtai where similar('universe') limit 1")[0]
+
+ self.assertTrue(result["id"].endswith("stars.jpg"))
+ self.assertTrue(isinstance(result["object"], Image.Image))
+
+ def testPickle(self):
+ """
+ Tests an index with pickle encoder
+ """
+
+ try:
+ # Change to use default encoder
+ self.embeddings.config["objects"] = "pickle"
+ data = [(0, {"object": [1, 2, 3, 4, 5], "text": "default test"}, None)]
+
+ # Create an index for the list of images
+ self.embeddings.index(data)
+
+ result = self.embeddings.search("select object from txtai limit 1")[0]
+
+ self.assertEqual(result["object"], [1, 2, 3, 4, 5])
+ finally:
+ self.embeddings.config["objects"] = "image"
+
+ def testReindex(self):
+ """
+ Tests reindex with objects
+ """
+
+ # Create an index for the list of images
+ self.embeddings.index(self.data)
+
+ # Reindex images
+ self.embeddings.reindex({"method": "sentence-transformers", "path": "sentence-transformers/clip-ViT-B-32"})
+
+ result = self.embeddings.search("select id, object from txtai where similar('universe') limit 1")[0]
+
+ self.assertTrue(result["id"].endswith("stars.jpg"))
+ self.assertTrue(isinstance(result["object"], Image.Image))
diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.py
index 19a18dc0..94596b8f 100644
--- a/test/python/testdatabase/testsql.py
+++ b/test/python/testdatabase/testsql.py
@@ -169,7 +169,7 @@ class TestSQL(unittest.TestCase):
def assertSql(self, clause, query, expected):
"""
- Helper method to assert a query clause is as expected
+ Helper method to assert a query clause is as expected.
Args:
clause: clause to select
diff --git a/test/python/testembeddings.py b/test/python/testembeddings.py
index 0982425b..4be52409 100644
--- a/test/python/testembeddings.py
+++ b/test/python/testembeddings.py
@@ -12,7 +12,7 @@ from txtai.vectors import WordVectors
class TestEmbeddings(unittest.TestCase):
"""
- Embeddings tests
+ Embeddings tests.
"""
@classmethod
diff --git a/test/python/testoptional.py b/test/python/testoptional.py
index 426bb592..76df4a39 100644
--- a/test/python/testoptional.py
+++ b/test/python/testoptional.py
@@ -58,7 +58,7 @@ class TestOptional(unittest.TestCase):
@classmethod
def tearDownClass(cls):
"""
- Resets modules environment back to initial state
+ Resets modules environment back to initial state.
"""
# Reset replaced modules in setup
@@ -68,28 +68,25 @@ class TestOptional(unittest.TestCase):
else:
del sys.modules[key]
- def testAnn(self):
+ def testApi(self):
"""
- Test missing ann dependencies
+ Test missing api dependencies
"""
- from txtai.ann import ANNFactory
-
- with self.assertRaises(ImportError):
- ANNFactory.create({"backend": "annoy"})
-
with self.assertRaises(ImportError):
- ANNFactory.create({"backend": "hnsw"})
+ import txtai.api
- def testApi(self):
+ def testDatabase(self):
"""
- Test missing api dependencies
+ Test missing database dependencies
"""
+ from txtai.database import ImageEncoder
+
with self.assertRaises(ImportError):
- import txtai.api
+ ImageEncoder()
- def testModels(self):
+ def testModel(self):
"""
Test missing model dependencies
"""
@@ -137,13 +134,20 @@ class TestOptional(unittest.TestCase):
with self.assertRaises(ImportError):
Translation().detect(["test"])
- def testVectors(self):
+ def testSimilarity(self):
"""
- Test missing vectors dependencies
+ Test missing similarity dependencies
"""
+ from txtai.ann import ANNFactory
from txtai.vectors import VectorsFactory
+ with self.assertRaises(ImportError):
+ ANNFactory.create({"backend": "annoy"})
+
+ with self.assertRaises(ImportError):
+ ANNFactory.create({"backend": "hnsw"})
+
with self.assertRaises(ImportError):
VectorsFactory.create({"method": "words"}, None)
diff --git a/test/python/testpipeline/__init__.py b/test/python/testpipeline/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/test/python/testcaption.py b/test/python/testpipeline/testcaption.py
similarity index 95%
rename from test/python/testcaption.py
rename to test/python/testpipeline/testcaption.py
index 6cacb9e9..a75f44bc 100644
--- a/test/python/testcaption.py
+++ b/test/python/testpipeline/testcaption.py
@@ -14,7 +14,7 @@ from utils import Utils
class TestCaption(unittest.TestCase):
"""
- Caption tests
+ Caption tests.
"""
def testCaption(self):
diff --git a/test/python/testextractor.py b/test/python/testpipeline/testextractor.py
similarity index 98%
rename from test/python/testextractor.py
rename to test/python/testpipeline/testextractor.py
index 6f81e34f..448f5814 100644
--- a/test/python/testextractor.py
+++ b/test/python/testpipeline/testextractor.py
@@ -11,7 +11,7 @@ from txtai.pipeline import Extractor, Similarity
class TestExtractor(unittest.TestCase):
"""
- Extractor tests
+ Extractor tests.
"""
@classmethod
@@ -62,7 +62,7 @@ class TestExtractor(unittest.TestCase):
def testEmptyQuery(self):
"""
- Tests an empty extractor queries list.
+ Tests an empty extractor queries list
"""
self.assertEqual(self.extractor.query(None, None), [])
diff --git a/test/python/testlabels.py b/test/python/testpipeline/testlabels.py
similarity index 99%
rename from test/python/testlabels.py
rename to test/python/testpipeline/testlabels.py
index 7dc77332..cb46599b 100644
--- a/test/python/testlabels.py
+++ b/test/python/testpipeline/testlabels.py
@@ -9,7 +9,7 @@ from txtai.pipeline import Labels, Similarity
class TestLabels(unittest.TestCase):
"""
- Labels tests
+ Labels tests.
"""
@classmethod
diff --git a/test/python/testobjects.py b/test/python/testpipeline/testobjects.py
similarity index 97%
rename from test/python/testobjects.py
rename to test/python/testpipeline/testobjects.py
index b91321a0..3d78ba75 100644
--- a/test/python/testobjects.py
+++ b/test/python/testpipeline/testobjects.py
@@ -12,7 +12,7 @@ from utils import Utils
class TestObjects(unittest.TestCase):
"""
- Object detection tests
+ Object detection tests.
"""
def testClassification(self):
diff --git a/test/python/testonnx.py b/test/python/testpipeline/testonnx.py
similarity index 98%
rename from test/python/testonnx.py
rename to test/python/testpipeline/testonnx.py
index bf5c5a9c..c72f5410 100644
--- a/test/python/testonnx.py
+++ b/test/python/testpipeline/testonnx.py
@@ -16,13 +16,13 @@ from txtai.pipeline import HFOnnx, HFTrainer, Labels, MLOnnx, Questions
class TestOnnx(unittest.TestCase):
"""
- ONNX tests
+ ONNX tests.
"""
@classmethod
def setUpClass(cls):
"""
- Create default datasets
+ Create default datasets.
"""
cls.data = [{"text": "Dogs", "label": 0}, {"text": "dog", "label": 0}, {"text": "Cats", "label": 1}, {"text": "cat", "label": 1}] * 100
diff --git a/test/python/testquantization.py b/test/python/testpipeline/testquantization.py
similarity index 97%
rename from test/python/testquantization.py
rename to test/python/testpipeline/testquantization.py
index 7289ee50..c65c1fc3 100644
--- a/test/python/testquantization.py
+++ b/test/python/testpipeline/testquantization.py
@@ -12,7 +12,7 @@ from txtai.pipeline import HFModel, HFPipeline
class TestQuantization(unittest.TestCase):
"""
- Quantization tests
+ Quantization tests.
"""
@unittest.skipIf(platform.system() == "Darwin", "Quantized models not supported on macOS")
diff --git a/test/python/testsummary.py b/test/python/testpipeline/testsummary.py
similarity index 98%
rename from test/python/testsummary.py
rename to test/python/testpipeline/testsummary.py
index b2d08f97..8d51f88d 100644
--- a/test/python/testsummary.py
+++ b/test/python/testpipeline/testsummary.py
@@ -9,7 +9,7 @@ from txtai.pipeline import Summary
class TestSummary(unittest.TestCase):
"""
- Summary tests
+ Summary tests.
"""
@classmethod
diff --git a/test/python/testtabular.py b/test/python/testpipeline/testtabular.py
similarity index 99%
rename from test/python/testtabular.py
rename to test/python/testpipeline/testtabular.py
index 0d9d88e8..973e86ef 100644
--- a/test/python/testtabular.py
+++ b/test/python/testpipeline/testtabular.py
@@ -12,7 +12,7 @@ from utils import Utils
class TestTabular(unittest.TestCase):
"""
- Tabular tests
+ Tabular tests.
"""
@classmethod
diff --git a/test/python/testtextractor.py b/test/python/testpipeline/testtextractor.py
similarity index 98%
rename from test/python/testtextractor.py
rename to test/python/testpipeline/testtextractor.py
index f283ee9a..c6569056 100644
--- a/test/python/testtextractor.py
+++ b/test/python/testpipeline/testtextractor.py
@@ -12,7 +12,7 @@ from utils import Utils
class TestTextractor(unittest.TestCase):
"""
- Textractor tests
+ Textractor tests.
"""
def testBeautifulSoup(self):
diff --git a/test/python/testtokenizer.py b/test/python/testpipeline/testtokenizer.py
similarity index 94%
rename from test/python/testtokenizer.py
rename to test/python/testpipeline/testtokenizer.py
index 9924a635..bfb10093 100644
--- a/test/python/testtokenizer.py
+++ b/test/python/testpipeline/testtokenizer.py
@@ -9,7 +9,7 @@ from txtai.pipeline import Tokenizer
class TestTokenizer(unittest.TestCase):
"""
- Tokenizer tests
+ Tokenizer tests.
"""
def testTokenize(self):
diff --git a/test/python/testtrainer.py b/test/python/testpipeline/testtrainer.py
similarity index 97%
rename from test/python/testtrainer.py
rename to test/python/testpipeline/testtrainer.py
index 6c8d7840..ee106575 100644
--- a/test/python/testtrainer.py
+++ b/test/python/testpipeline/testtrainer.py
@@ -16,13 +16,13 @@ from txtai.pipeline import HFTrainer, Labels, Questions
class TestTrainer(unittest.TestCase):
"""
- Trainer tests
+ Trainer tests.
"""
@classmethod
def setUpClass(cls):
"""
- Create default datasets
+ Create default datasets.
"""
cls.data = [{"text": "Dogs", "label": 0}, {"text": "dog", "label": 0}, {"text": "Cats", "label": 1}, {"text": "cat", "label": 1}] * 100
@@ -134,14 +134,14 @@ class TestTrainer(unittest.TestCase):
def testEmpty(self):
"""
- Tests an empty training data object.
+ Tests an empty training data object
"""
self.assertIsNone(Data(None, None, None).process(None))
def testQA(self):
"""
- Tests training a QA model.
+ Tests training a QA model
"""
# Training data
@@ -163,7 +163,7 @@ class TestTrainer(unittest.TestCase):
def testRegression(self):
"""
- Tests training a model with a regression (continuous) output.
+ Tests training a model with a regression (continuous) output
"""
data = []
diff --git a/test/python/testtranscription.py b/test/python/testpipeline/testtranscription.py
similarity index 95%
rename from test/python/testtranscription.py
rename to test/python/testpipeline/testtranscription.py
index 3efb0ba4..641f12a1 100644
--- a/test/python/testtranscription.py
+++ b/test/python/testpipeline/testtranscription.py
@@ -12,7 +12,7 @@ from utils import Utils
class TestTranscription(unittest.TestCase):
"""
- Transcription tests
+ Transcription tests.
"""
def testTranscription(self):
diff --git a/test/python/testtranslation.py b/test/python/testpipeline/testtranslation.py
similarity index 92%
rename from test/python/testtranslation.py
rename to test/python/testpipeline/testtranslation.py
index f8da8b29..f301e8e7 100644
--- a/test/python/testtranslation.py
+++ b/test/python/testpipeline/testtranslation.py
@@ -10,12 +10,12 @@ from txtai.pipeline import Translation
class TestTranslation(unittest.TestCase):
"""
- Translation tests
+ Translation tests.
"""
def testLongTranslation(self):
"""
- Tests a translation longer than max tokenization length.
+ Tests a translation longer than max tokenization length
"""
translate = Translation()
@@ -58,7 +58,7 @@ class TestTranslation(unittest.TestCase):
def testNoLang(self):
"""
- Test no matching language id.
+ Test no matching language id
"""
translate = Translation()
@@ -66,7 +66,7 @@ class TestTranslation(unittest.TestCase):
def testNoModel(self):
"""
- Tests no known available model found.
+ Tests no known available model found
"""
translate = Translation()
diff --git a/test/python/testscoring.py b/test/python/testscoring.py
index 2185947a..c1cadc8e 100644
--- a/test/python/testscoring.py
+++ b/test/python/testscoring.py
@@ -11,7 +11,7 @@ from txtai.scoring import ScoringFactory
class TestScoring(unittest.TestCase):
"""
- Scoring tests
+ Scoring tests.
"""
@classmethod
diff --git a/test/python/testvectors/__init__.py b/test/python/testvectors/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/test/python/testtransformers.py b/test/python/testvectors/testtransformers.py
similarity index 87%
rename from test/python/testtransformers.py
rename to test/python/testvectors/testtransformers.py
index 9400f71c..05223ef7 100644
--- a/test/python/testtransformers.py
+++ b/test/python/testvectors/testtransformers.py
@@ -51,12 +51,12 @@ class TestTransformersVectors(unittest.TestCase):
"""
self.model.tokenize = True
- self.assertEqual(self.model.text("Y 123 This is a test!"), "test")
- self.assertEqual(self.model.text(["This", "is", "a", "test"]), "This is a test")
+ self.assertEqual(self.model.prepare("Y 123 This is a test!"), "test")
+ self.assertEqual(self.model.prepare(["This", "is", "a", "test"]), "This is a test")
self.model.tokenize = False
- self.assertEqual(self.model.text("Y 123 This is a test!"), "Y 123 This is a test!")
- self.assertEqual(self.model.text(["This", "is", "a", "test"]), "This is a test")
+ self.assertEqual(self.model.prepare("Y 123 This is a test!"), "Y 123 This is a test!")
+ self.assertEqual(self.model.prepare(["This", "is", "a", "test"]), "This is a test")
def testTransform(self):
"""
diff --git a/test/python/testvectors.py b/test/python/testvectors/testvectors.py
similarity index 96%
rename from test/python/testvectors.py
rename to test/python/testvectors/testvectors.py
index d754463c..6b62d8aa 100644
--- a/test/python/testvectors.py
+++ b/test/python/testvectors/testvectors.py
@@ -9,7 +9,7 @@ from txtai.vectors import Vectors
class TestVectors(unittest.TestCase):
"""
- Vectors tests
+ Vectors tests.
"""
def testNotImplemented(self):
diff --git a/test/python/testwordvectors.py b/test/python/testvectors/testwordvectors.py
similarity index 99%
rename from test/python/testwordvectors.py
rename to test/python/testvectors/testwordvectors.py
index ef8d6f46..2255ffd2 100644
--- a/test/python/testwordvectors.py
+++ b/test/python/testvectors/testwordvectors.py
@@ -11,7 +11,7 @@ from txtai.vectors import WordVectors, VectorsFactory
class TestWordVectors(unittest.TestCase):
"""
- Vectors tests
+ Vectors tests.
"""
@classmethod
diff --git a/test/python/testworkflow.py b/test/python/testworkflow.py
index d0663a14..661c80af 100644
--- a/test/python/testworkflow.py
+++ b/test/python/testworkflow.py
@@ -20,7 +20,7 @@ from utils import Utils
class TestWorkflow(unittest.TestCase):
"""
- Workflow tests
+ Workflow tests.
"""
def testBaseWorkflow(self):