Skip to content

Commit e0cb05b

Browse files
[WFP-900]: Fix issues in Multimodal Pipeline Tests
1 parent af7fbab commit e0cb05b

File tree

3 files changed

+136
-3
lines changed

3 files changed

+136
-3
lines changed

.github/workflows/run_tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ jobs:
4040
shell: bash
4141
run: |
4242
export PYTHONPATH=.
43+
export CLARIFAI_USER_ID="$(python scripts/key_for_tests.py --get-userid)"
44+
export CLARIFAI_PAT="$(python scripts/key_for_tests.py --create-pat)"
4345
pytest tests/pipelines/

scripts/key_for_tests.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import json
5+
import os
6+
import sys
7+
8+
try:
9+
from urllib.error import HTTPError
10+
from urllib.request import HTTPHandler, Request, build_opener
11+
except ImportError:
12+
from urllib2 import HTTPError, HTTPHandler, Request, build_opener
13+
14+
EMAIL = os.environ["CLARIFAI_USER_EMAIL"]
15+
PASSWORD = os.environ["CLARIFAI_USER_PASSWORD"]
16+
17+
18+
def _assert_response_success(response):
19+
assert "status" in response, f"Invalid response {response}"
20+
assert "code" in response["status"], f"Invalid response {response}"
21+
assert response["status"]["code"] == 10000, f"Invalid response {response}"
22+
23+
24+
def _request(method, url, payload={}, headers={}):
25+
base_url = os.environ.get("CLARIFAI_GRPC_BASE", "api.clarifai.com")
26+
27+
opener = build_opener(HTTPHandler)
28+
full_url = f"https://{base_url}/v2{url}"
29+
request = Request(full_url, data=json.dumps(payload).encode())
30+
for k in headers.keys():
31+
request.add_header(k, headers[k])
32+
request.get_method = lambda: method
33+
try:
34+
response = opener.open(request).read().decode()
35+
except HTTPError as e:
36+
error_body = e.read().decode()
37+
try:
38+
error_body = json.dumps(json.loads(error_body), indent=4)
39+
except Exception:
40+
pass
41+
raise Exception("ERROR after a HTTP request to: %s %s" % (method, full_url) +
42+
". Response: %d %s:\n%s" % (e.code, e.reason, error_body))
43+
return json.loads(response)
44+
45+
46+
def login():
47+
url = "/login"
48+
payload = {"email": EMAIL, "password": PASSWORD}
49+
data = _request(method="POST", url=url, payload=payload)
50+
_assert_response_success(data)
51+
52+
assert "v2_user_id" in data, f"Invalid response {data}"
53+
user_id = data["v2_user_id"]
54+
assert user_id, f"Invalid response {data}"
55+
56+
assert "session_token" in data, f"Invalid response {data}"
57+
session_token = data["session_token"]
58+
assert session_token, f"Invalid response {data}"
59+
60+
return session_token, user_id
61+
62+
63+
def _auth_headers(session_token):
64+
headers = {"Content-Type": "application/json", "X-Clarifai-Session-Token": session_token}
65+
return headers
66+
67+
68+
def create_pat():
69+
session_token, user_id = login()
70+
os.environ["CLARIFAI_USER_ID"] = user_id
71+
72+
url = "/users/%s/keys" % user_id
73+
payload = {
74+
"keys": [{
75+
"description": "Auto-created in a CI test run",
76+
"scopes": ["All"],
77+
"type": "personal_access_token",
78+
"apps": [],
79+
}]
80+
}
81+
data = _request(method="POST", url=url, payload=payload, headers=_auth_headers(session_token))
82+
_assert_response_success(data)
83+
84+
assert "keys" in data, f"Invalid response {data}"
85+
assert len(data["keys"]) == 1, f"Invalid response {data}"
86+
assert "id" in data["keys"][0], f"Invalid response {data}"
87+
pat_id = data["keys"][0]["id"]
88+
assert pat_id, f"Invalid response {data}"
89+
90+
# This print needs to be present so we can read the value in CI.
91+
print(pat_id)
92+
93+
94+
def run(arguments):
95+
if arguments.email:
96+
global EMAIL
97+
EMAIL = arguments.email # override the default testing email
98+
if arguments.password:
99+
global PASSWORD
100+
PASSWORD = arguments.password # override the default testing password
101+
# these options are mutually exclusive
102+
if arguments.create_pat:
103+
create_pat()
104+
elif arguments.get_userid:
105+
_, user_id = login()
106+
# This print needs to be present so we can read the value in CI.
107+
print(user_id)
108+
else:
109+
print(f"No relevant arguments specified. Run {sys.argv[0]} --help to see available options")
110+
exit(1)
111+
112+
113+
if __name__ == "__main__":
114+
parser = argparse.ArgumentParser(
115+
description="Create Applications, Keys, and Workflows for testing.")
116+
parser.add_argument(
117+
"--user-email",
118+
dest="email",
119+
help=
120+
"The email of the account for which the command will run. (Defaults to ${CLARIFAI_USER_EMAIL})",
121+
)
122+
parser.add_argument(
123+
"--user-password",
124+
dest="password",
125+
help=
126+
"The password of the account for which the command will run. (Defaults to ${CLARIFAI_USER_PASSWORD})",
127+
)
128+
group = parser.add_mutually_exclusive_group()
129+
group.add_argument("--create-pat", action="store_true", help=" Creates a new PAT key.")
130+
group.add_argument("--get-userid", action="store_true", help=" Gets the user id.")
131+
132+
args = parser.parse_args()
133+
run(args)

tests/pipelines/test_multimodal_pipelines.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os.path as osp
2-
import pytest
32

43
PDF_FILE_PATH = osp.abspath(
54
osp.join(osp.dirname(__file__), "assets", "Multimodal_sample_file.pdf"))
@@ -65,7 +64,6 @@ def test_pipeline_run_loader(self,):
6564
assert len(elements) == 14
6665
assert elements.elements[0].metadata.to_dict()['filename'] == 'Multimodal_sample_file.pdf'
6766

68-
@pytest.mark.skip(reason="Need additional setup")
6967
def test_pipeline_summarize(self,):
7068
"""Tests for pipeline run with summarizer"""
7169
import os
@@ -84,7 +82,7 @@ def test_pipeline_summarize(self,):
8482
])
8583
elements = pipeline.run(files=PDF_FILE_PATH, loader=False)
8684

87-
assert len(elements) == 17
85+
assert len(elements) == 16
8886
assert isinstance(elements, list)
8987
assert elements[0].metadata.to_dict()['filename'] == 'Multimodal_sample_file.pdf'
9088
assert elements[0].metadata.to_dict()['page_number'] == 1

0 commit comments

Comments
 (0)