Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for frontend API #3552

Merged
merged 7 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,26 @@ jobs:
echo "RAGFLOW_IMAGE=infiniflow/ragflow:dev" >> docker/.env
sudo docker compose -f docker/docker-compose.yml up -d

- name: Run tests against Elasticsearch
- name: Run sdk tests against Elasticsearch
run: |
export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""
export HOST_ADDRESS=http://host.docker.internal:9380
until sudo docker exec ragflow-server curl -s --connect-timeout 5 ${HOST_ADDRESS} > /dev/null; do
echo "Waiting for service to be available..."
sleep 5
done
cd sdk/python && poetry install && source .venv/bin/activate && cd test && pytest --tb=short t_dataset.py t_chat.py t_session.py t_document.py t_chunk.py
cd sdk/python && poetry install && source .venv/bin/activate && cd test/test_sdk_api && pytest -s --tb=short get_email.py t_dataset.py t_chat.py t_session.py t_document.py t_chunk.py

- name: Run frontend api tests against Elasticsearch
run: |
export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""
export HOST_ADDRESS=http://host.docker.internal:9380
until sudo docker exec ragflow-server curl -s --connect-timeout 5 ${HOST_ADDRESS} > /dev/null; do
echo "Waiting for service to be available..."
sleep 5
done
cd sdk/python && poetry install && source .venv/bin/activate && cd test/test_frontend_api && pytest -s --tb=short get_email.py test_dataset.py


- name: Stop ragflow:dev
if: always() # always run this step even if previous steps failed
Expand All @@ -89,15 +100,25 @@ jobs:
run: |
sudo DOC_ENGINE=infinity docker compose -f docker/docker-compose.yml up -d

- name: Run tests against Infinity
- name: Run sdk tests against Infinity
run: |
export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""
export HOST_ADDRESS=http://host.docker.internal:9380
until sudo docker exec ragflow-server curl -s --connect-timeout 5 ${HOST_ADDRESS} > /dev/null; do
echo "Waiting for service to be available..."
sleep 5
done
cd sdk/python && poetry install && source .venv/bin/activate && cd test/test_sdk_api && pytest -s --tb=short get_email.py t_dataset.py t_chat.py t_session.py t_document.py t_chunk.py

- name: Run frontend api tests against Infinity
run: |
export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""
export HOST_ADDRESS=http://host.docker.internal:9380
until sudo docker exec ragflow-server curl -s --connect-timeout 5 ${HOST_ADDRESS} > /dev/null; do
echo "Waiting for service to be available..."
sleep 5
done
cd sdk/python && poetry install && source .venv/bin/activate && cd test && pytest --tb=short t_dataset.py t_chat.py t_session.py t_document.py t_chunk.py
cd sdk/python && poetry install && source .venv/bin/activate && cd test/test_frontend_api && pytest -s --tb=short get_email.py test_dataset.py

- name: Stop ragflow:dev
if: always() # always run this step even if previous steps failed
Expand Down
12 changes: 9 additions & 3 deletions sdk/python/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ def generate_random_email():
fN33jCHRoDUW81IH9zjij/vaw8IbVyb6vuwg6MX6inOEBRRzVbRYxXOu1wkWY6SsI8X70oF9aeLFp/PzQpjoe/YbSqpTq8qqrmHzn9vO+yvyYyvmDsphXe
X8f7fp9c7vUsfOCkM+gHY3PadG+QHa7KI7mzTKgUTZImK6BZtfRBATDTthEUbbaTewY4H0MnWiCeeDhcbeQao6cFy1To8pE3RpmxnGnS8BsBn8w=='''

def get_email():
return EMAIL

def register():
url = HOST_ADDRESS + "/v1/user/register"
name = "user"
Expand Down Expand Up @@ -50,3 +47,12 @@ def get_api_key_fixture():
raise Exception(res.get("message"))
return res["data"].get("token")

@pytest.fixture(scope="session")
def get_auth():
register()
auth = login()
return auth

@pytest.fixture(scope="session")
def get_email():
return EMAIL
File renamed without changes.
3 changes: 3 additions & 0 deletions sdk/python/test/test_frontend_api/get_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test_get_email(get_email):
print(f"\nEmail account:",flush=True)
print(f"{get_email}\n",flush=True)
10 changes: 10 additions & 0 deletions sdk/python/test/test_frontend_api/test_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from common import HOST_ADDRESS
import requests
def test_create_dataset(get_auth):
authorization={"Authorization": get_auth}
url = f"{HOST_ADDRESS}/v1/kb/create"
json = {"name":"test_create_dataset"}
res = requests.post(url=url,headers=authorization,json=json)
res = res.json()
assert res.get("code") == 0,f"{res.get('message')}"

2 changes: 2 additions & 0 deletions sdk/python/test/test_sdk_api/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import os
HOST_ADDRESS=os.getenv('HOST_ADDRESS', 'http://127.0.0.1:9380')
3 changes: 3 additions & 0 deletions sdk/python/test/test_sdk_api/get_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test_get_email(get_email):
print(f"\nEmail account:",flush=True)
print(f"{get_email}\n",flush=True)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def test_create_chat_with_name(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
kb = rag.create_dataset(name="test_create_chat")
displayed_name = "ragflow.txt"
with open("ragflow.txt", "rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name":displayed_name,"blob":blob}
documents = []
Expand All @@ -22,7 +22,7 @@ def test_update_chat_with_name(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
kb = rag.create_dataset(name="test_update_chat")
displayed_name = "ragflow.txt"
with open("ragflow.txt", "rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name": displayed_name, "blob": blob}
documents = []
Expand All @@ -39,7 +39,7 @@ def test_delete_chats_with_success(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
kb = rag.create_dataset(name="test_delete_chat")
displayed_name = "ragflow.txt"
with open("ragflow.txt", "rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name": displayed_name, "blob": blob}
documents = []
Expand All @@ -55,7 +55,7 @@ def test_list_chats_with_success(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
kb = rag.create_dataset(name="test_list_chats")
displayed_name = "ragflow.txt"
with open("ragflow.txt", "rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name": displayed_name, "blob": blob}
documents = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_parse_document_with_txt(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_parse_document")
name = 'ragflow_test.txt'
with open("test_data/ragflow_test.txt","rb") as file :
with open("test_data/ragflow_test.txt", "rb") as file :
blob = file.read()
docs = ds.upload_documents([{"displayed_name": name, "blob": blob}])
doc = docs[0]
Expand All @@ -26,7 +26,7 @@ def test_parse_and_cancel_document(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_parse_and_cancel_document")
name = 'ragflow_test.txt'
with open("test_data/ragflow_test.txt","rb") as file :
with open("test_data/ragflow_test.txt", "rb") as file :
blob = file.read()
docs=ds.upload_documents([{"displayed_name": name, "blob": blob}])
doc = docs[0]
Expand All @@ -40,7 +40,7 @@ def test_bulk_parse_documents(get_api_key_fixture):
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_bulk_parse_and_cancel_documents")
with open("ragflow.txt","rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob = file.read()
documents = [
{'displayed_name': 'test1.txt', 'blob': blob},
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_upload_document_with_success(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_upload_document")
blob = b"Sample document content for test."
with open("ragflow.txt","rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob_2=file.read()
document_infos = []
document_infos.append({"displayed_name": "test_1.txt","blob": blob})
Expand Down Expand Up @@ -63,7 +63,7 @@ def test_upload_and_parse_pdf_documents_with_general_parse_method(get_api_key_fi
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_pdf_document")
with open("test_data/test.pdf","rb") as file:
with open("test_data/test.pdf", "rb") as file:
blob=file.read()
document_infos = [{"displayed_name": "test.pdf","blob": blob}]
docs=ds.upload_documents(document_infos)
Expand All @@ -74,7 +74,7 @@ def test_upload_and_parse_docx_documents_with_general_parse_method(get_api_key_f
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_docx_document")
with open("test_data/test.docx","rb") as file:
with open("test_data/test.docx", "rb") as file:
blob=file.read()
document_infos = [{"displayed_name": "test.docx","blob": blob}]
docs=ds.upload_documents(document_infos)
Expand All @@ -84,7 +84,7 @@ def test_upload_and_parse_excel_documents_with_general_parse_method(get_api_key_
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_excel_document")
with open("test_data/test.xlsx","rb") as file:
with open("test_data/test.xlsx", "rb") as file:
blob=file.read()
document_infos = [{"displayed_name": "test.xlsx","blob": blob}]
docs=ds.upload_documents(document_infos)
Expand All @@ -94,7 +94,7 @@ def test_upload_and_parse_ppt_documents_with_general_parse_method(get_api_key_fi
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_ppt_document")
with open("test_data/test.ppt","rb") as file:
with open("test_data/test.ppt", "rb") as file:
blob=file.read()
document_infos = [{"displayed_name": "test.ppt","blob": blob}]
docs=ds.upload_documents(document_infos)
Expand All @@ -104,7 +104,7 @@ def test_upload_and_parse_image_documents_with_general_parse_method(get_api_key_
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_image_document")
with open("test_data/test.jpg","rb") as file:
with open("test_data/test.jpg", "rb") as file:
blob=file.read()
document_infos = [{"displayed_name": "test.jpg","blob": blob}]
docs=ds.upload_documents(document_infos)
Expand All @@ -114,7 +114,7 @@ def test_upload_and_parse_txt_documents_with_general_parse_method(get_api_key_fi
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_txt_document")
with open("test_data/test.txt","rb") as file:
with open("test_data/test.txt", "rb") as file:
blob=file.read()
document_infos = [{"displayed_name": "test.txt","blob": blob}]
docs=ds.upload_documents(document_infos)
Expand All @@ -124,7 +124,7 @@ def test_upload_and_parse_md_documents_with_general_parse_method(get_api_key_fix
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_md_document")
with open("test_data/test.md","rb") as file:
with open("test_data/test.md", "rb") as file:
blob=file.read()
document_infos = [{"displayed_name": "test.md","blob": blob}]
docs=ds.upload_documents(document_infos)
Expand All @@ -135,7 +135,7 @@ def test_upload_and_parse_json_documents_with_general_parse_method(get_api_key_f
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_json_document")
with open("test_data/test.json","rb") as file:
with open("test_data/test.json", "rb") as file:
blob=file.read()
document_infos = [{"displayed_name": "test.json","blob": blob}]
docs=ds.upload_documents(document_infos)
Expand All @@ -147,7 +147,7 @@ def test_upload_and_parse_eml_documents_with_general_parse_method(get_api_key_fi
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_eml_document")
with open("test_data/test.eml","rb") as file:
with open("test_data/test.eml", "rb") as file:
blob=file.read()
document_infos = [{"displayed_name": "test.eml","blob": blob}]
docs=ds.upload_documents(document_infos)
Expand All @@ -158,7 +158,7 @@ def test_upload_and_parse_html_documents_with_general_parse_method(get_api_key_f
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
ds = rag.create_dataset(name="test_html_document")
with open("test_data/test.html","rb") as file:
with open("test_data/test.html", "rb") as file:
blob=file.read()
document_infos = [{"displayed_name": "test.html","blob": blob}]
docs=ds.upload_documents(document_infos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_create_session_with_success(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
kb = rag.create_dataset(name="test_create_session")
displayed_name = "ragflow.txt"
with open("ragflow.txt", "rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name":displayed_name,"blob":blob}
documents = []
Expand All @@ -25,7 +25,7 @@ def test_create_conversation_with_success(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
kb = rag.create_dataset(name="test_create_conversation")
displayed_name = "ragflow.txt"
with open("ragflow.txt", "rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name": displayed_name, "blob": blob}
documents = []
Expand All @@ -47,7 +47,7 @@ def test_delete_sessions_with_success(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
kb = rag.create_dataset(name="test_delete_session")
displayed_name = "ragflow.txt"
with open("ragflow.txt", "rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name":displayed_name,"blob":blob}
documents = []
Expand All @@ -65,7 +65,7 @@ def test_update_session_with_name(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
kb = rag.create_dataset(name="test_update_session")
displayed_name = "ragflow.txt"
with open("ragflow.txt", "rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name": displayed_name, "blob": blob}
documents = []
Expand All @@ -83,7 +83,7 @@ def test_list_sessions_with_success(get_api_key_fixture):
rag = RAGFlow(API_KEY, HOST_ADDRESS)
kb = rag.create_dataset(name="test_list_session")
displayed_name = "ragflow.txt"
with open("ragflow.txt", "rb") as file:
with open("test_data/ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name":displayed_name,"blob":blob}
documents = []
Expand Down