1+ import base64
12import os
3+ from pathlib import Path
4+
25import pytest
3- import base64
4- from colivara_py import ColiVara , AsyncColiVara
5- from colivara_py .models import (
6- CollectionOut ,
7- DocumentOut ,
8- QueryOut ,
9- PageOutQuery ,
10- FileOut ,
11- EmbeddingsOut ,
12- PatchCollectionIn ,
13- DocumentIn ,
14- DocumentInPatch ,
15- QueryFilter ,
16- GenericMessage ,
17- )
186import responses
19- from requests .exceptions import HTTPError
207from pydantic import ValidationError
21- from pathlib import Path
8+ from requests .exceptions import HTTPError
9+
10+ from colivara_py import AsyncColiVara , ColiVara
11+ from colivara_py .models import (CollectionOut , DocumentIn , DocumentInPatch ,
12+ DocumentOut , EmbeddingsOut , FileOut ,
13+ GenericMessage , PageOutQuery ,
14+ PatchCollectionIn , QueryFilter , QueryOut )
2215
2316
2417def test_colivara_init_no_api_key ():
@@ -988,6 +981,113 @@ def test_search_with_filter(api_key):
988981 assert result .results [0 ].document_metadata ["category" ] == "AI"
989982
990983
984+ @responses .activate
985+ def test_filter_documents (api_key ):
986+ os .environ ["COLIVARA_API_KEY" ] = api_key
987+ base_url = "https://api.test.com"
988+ client = ColiVara (base_url = base_url )
989+
990+ expected_out = [
991+ {
992+ "id" : 1 ,
993+ "name" : "Test Document Fixture" ,
994+ "metadata" : {"important" : True },
995+ "url" : "https://www.example.com" ,
996+ "num_pages" : 1 ,
997+ "collection_name" : "Test Collection Fixture" ,
998+ }
999+ ]
1000+
1001+ responses .add (
1002+ responses .POST , f"{ client .base_url } /v1/filter/" , json = expected_out , status = 200
1003+ )
1004+
1005+ query_filter = {
1006+ "on" : "document" ,
1007+ "key" : "important" ,
1008+ "value" : True ,
1009+ }
1010+ result = client .filter (query_filter = query_filter )
1011+ assert isinstance (result , list )
1012+ assert len (result ) == 1
1013+
1014+
1015+ @responses .activate
1016+ def test_filter_documents_expand (api_key ):
1017+ os .environ ["COLIVARA_API_KEY" ] = api_key
1018+ base_url = "https://api.test.com"
1019+ client = ColiVara (base_url = base_url )
1020+
1021+ expected_out = [
1022+ {
1023+ "id" : 1 ,
1024+ "name" : "Test Document Fixture" ,
1025+ "metadata" : {"important" : True },
1026+ "url" : "https://www.example.com" ,
1027+ "num_pages" : 1 ,
1028+ "collection_name" : "Test Collection Fixture" ,
1029+ "pages" : [
1030+ {
1031+ "document_name" : "Test Document Fixture" ,
1032+ "img_base64" : "base64_string" ,
1033+ "page_number" : 1 ,
1034+ }
1035+ ],
1036+ }
1037+ ]
1038+
1039+ responses .add (
1040+ responses .POST ,
1041+ f"{ client .base_url } /v1/filter/?expand=pages" ,
1042+ json = expected_out ,
1043+ status = 200 ,
1044+ )
1045+
1046+ query_filter = {
1047+ "on" : "document" ,
1048+ "key" : "important" ,
1049+ "value" : True ,
1050+ }
1051+ result = client .filter (query_filter = query_filter , expand = "pages" )
1052+ assert isinstance (result , list )
1053+ assert len (result ) == 1
1054+
1055+
1056+ @responses .activate
1057+ def test_filter_collections (api_key ):
1058+ os .environ ["COLIVARA_API_KEY" ] = api_key
1059+ base_url = "https://api.test.com"
1060+ client = ColiVara (base_url = base_url )
1061+
1062+ expected_out = [
1063+ {
1064+ "id" : 1 ,
1065+ "name" : "test_collection" ,
1066+ "metadata" : {"description" : "A test collection" },
1067+ "num_documents" : 2 ,
1068+ },
1069+ {
1070+ "id" : 2 ,
1071+ "name" : "another_test_collection" ,
1072+ "metadata" : {"description" : "Another test collection" },
1073+ "num_documents" : 3 ,
1074+ },
1075+ ]
1076+
1077+ responses .add (
1078+ responses .POST , f"{ client .base_url } /v1/filter/" , json = expected_out , status = 200
1079+ )
1080+
1081+ query_filter = {
1082+ "on" : "collection" ,
1083+ "key" : "important" ,
1084+ "value" : True ,
1085+ }
1086+ result = client .filter (query_filter = query_filter , expand = "pages" )
1087+ assert isinstance (result , list )
1088+ assert len (result ) == 2
1089+
1090+
9911091@responses .activate
9921092def test_search_service_unavailable (api_key ):
9931093 os .environ ["COLIVARA_API_KEY" ] = api_key
@@ -1006,6 +1106,31 @@ def test_search_service_unavailable(api_key):
10061106 assert "Service unavailable" in str (exc_info .value )
10071107
10081108
1109+ @responses .activate
1110+ def test_filter_service_unavailable (api_key ):
1111+ os .environ ["COLIVARA_API_KEY" ] = api_key
1112+ base_url = "https://api.test.com"
1113+ client = ColiVara (base_url = base_url )
1114+
1115+ error_response = {"detail" : "Service is temporarily unavailable" }
1116+
1117+ responses .add (
1118+ responses .POST , f"{ client .base_url } /v1/filter/" , json = error_response , status = 503
1119+ )
1120+
1121+ with pytest .raises (ValueError ) as exc_info :
1122+ client .filter (
1123+ query_filter = {
1124+ "on" : "document" ,
1125+ "key" : "category" ,
1126+ "value" : "AI" ,
1127+ "lookup" : "contains" ,
1128+ }
1129+ )
1130+
1131+ assert "Service unavailable" in str (exc_info .value )
1132+
1133+
10091134@responses .activate
10101135def test_search_invalid_filter (api_key ):
10111136 os .environ ["COLIVARA_API_KEY" ] = api_key
@@ -1018,6 +1143,18 @@ def test_search_invalid_filter(api_key):
10181143 assert "Invalid query_filter" in str (exc_info .value )
10191144
10201145
1146+ @responses .activate
1147+ def test_filter_invalid_filter (api_key ):
1148+ os .environ ["COLIVARA_API_KEY" ] = api_key
1149+ base_url = "https://api.test.com"
1150+ client = ColiVara (base_url = base_url )
1151+
1152+ with pytest .raises (ValueError ) as exc_info :
1153+ client .filter (query_filter = {"invalid" : "filter" })
1154+
1155+ assert "Invalid query_filter" in str (exc_info .value )
1156+
1157+
10211158@responses .activate
10221159def test_search_http_error (api_key ):
10231160 os .environ ["COLIVARA_API_KEY" ] = api_key
@@ -1033,6 +1170,28 @@ def test_search_http_error(api_key):
10331170 client .search ("what is 1+1?" )
10341171
10351172
1173+ @responses .activate
1174+ def test_filter_http_error (api_key ):
1175+ os .environ ["COLIVARA_API_KEY" ] = api_key
1176+ base_url = "https://api.test.com"
1177+ client = ColiVara (base_url = base_url )
1178+ responses .add (
1179+ responses .POST ,
1180+ f"{ client .base_url } /v1/filter/" ,
1181+ json = {"error" : "Internal Server Error" },
1182+ status = 500 ,
1183+ )
1184+ with pytest .raises (HTTPError ):
1185+ client .filter (
1186+ query_filter = {
1187+ "on" : "document" ,
1188+ "key" : "category" ,
1189+ "value" : "AI" ,
1190+ "lookup" : "contains" ,
1191+ }
1192+ )
1193+
1194+
10361195@pytest .fixture
10371196def test_file_path (tmp_path ):
10381197 file_content = b"Test file content"
0 commit comments