-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### What problem does this PR solve? Test Cases ### Type of change - [x] Refactoring Co-authored-by: liuhua <10215101452@stu.ecun.edu.cn>
- Loading branch information
Showing
11 changed files
with
611 additions
and
473 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pytest | ||
import requests | ||
import string | ||
import random | ||
|
||
|
||
|
||
HOST_ADDRESS = 'http://127.0.0.1:9380' | ||
|
||
def generate_random_email(): | ||
return 'user_' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))+'@1.com' | ||
|
||
EMAIL = generate_random_email() | ||
# password is "123" | ||
PASSWORD='''ctAseGvejiaSWWZ88T/m4FQVOpQyUvP+x7sXtdv3feqZACiQleuewkUi35E16wSd5C5QcnkkcV9cYc8TKPTRZlxappDuirxghxoOvFcJxFU4ixLsD | ||
fN33jCHRoDUW81IH9zjij/vaw8IbVyb6vuwg6MX6inOEBRRzVbRYxXOu1wkWY6SsI8X70oF9aeLFp/PzQpjoe/YbSqpTq8qqrmHzn9vO+yvyYyvmDsphXe | ||
X8f7fp9c7vUsfOCkM+gHY3PadG+QHa7KI7mzTKgUTZImK6BZtfRBATDTthEUbbaTewY4H0MnWiCeeDhcbeQao6cFy1To8pE3RpmxnGnS8BsBn8w==''' | ||
|
||
def get_email(): | ||
return EMAIL | ||
|
||
def register(): | ||
url = HOST_ADDRESS + "/v1/user/register" | ||
name = "user" | ||
register_data = {"email":EMAIL,"nickname":name,"password":PASSWORD} | ||
res = requests.post(url=url,json=register_data) | ||
res = res.json() | ||
if res.get("retcode") != 0: | ||
raise Exception(res.get("retmsg")) | ||
|
||
def login(): | ||
url = HOST_ADDRESS + "/v1/user/login" | ||
login_data = {"email":EMAIL,"password":PASSWORD} | ||
response=requests.post(url=url,json=login_data) | ||
res = response.json() | ||
if res.get("retcode")!=0: | ||
raise Exception(res.get("retmsg")) | ||
auth = response.headers["Authorization"] | ||
return auth | ||
|
||
@pytest.fixture(scope="session") | ||
def get_api_key_fixture(): | ||
register() | ||
auth = login() | ||
url = HOST_ADDRESS + "/v1/system/new_token" | ||
auth = {"Authorization": auth} | ||
response = requests.post(url=url,headers=auth) | ||
res = response.json() | ||
if res.get("retcode") != 0: | ||
raise Exception(res.get("retmsg")) | ||
return res["data"].get("token") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,67 @@ | ||
from ragflow import RAGFlow, Chat | ||
from xgboost.testing import datasets | ||
|
||
from common import API_KEY, HOST_ADDRESS | ||
from test_sdkbase import TestSdk | ||
|
||
|
||
class TestChat(TestSdk): | ||
def test_create_chat_with_success(self): | ||
""" | ||
Test creating an chat with success | ||
""" | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
kb = rag.create_dataset(name="test_create_chat") | ||
chat = rag.create_chat("test_create", datasets=[kb]) | ||
if isinstance(chat, Chat): | ||
assert chat.name == "test_create", "Name does not match." | ||
else: | ||
assert False, f"Failed to create chat, error: {chat}" | ||
|
||
def test_update_chat_with_success(self): | ||
""" | ||
Test updating an chat with success. | ||
""" | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
kb = rag.create_dataset(name="test_update_chat") | ||
chat = rag.create_chat("test_update", datasets=[kb]) | ||
if isinstance(chat, Chat): | ||
assert chat.name == "test_update", "Name does not match." | ||
res=chat.update({"name":"new_chat"}) | ||
assert res is None, f"Failed to update chat, error: {res}" | ||
else: | ||
assert False, f"Failed to create chat, error: {chat}" | ||
|
||
def test_delete_chats_with_success(self): | ||
""" | ||
Test deleting an chat with success | ||
""" | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
kb = rag.create_dataset(name="test_delete_chat") | ||
chat = rag.create_chat("test_delete", datasets=[kb]) | ||
if isinstance(chat, Chat): | ||
assert chat.name == "test_delete", "Name does not match." | ||
res = rag.delete_chats(ids=[chat.id]) | ||
assert res is None, f"Failed to delete chat, error: {res}" | ||
else: | ||
assert False, f"Failed to create chat, error: {chat}" | ||
|
||
def test_list_chats_with_success(self): | ||
""" | ||
Test listing chats with success | ||
""" | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
list_chats = rag.list_chats() | ||
assert len(list_chats) > 0, "Do not exist any chat" | ||
for chat in list_chats: | ||
assert isinstance(chat, Chat), "Existence type is not chat." | ||
import time | ||
HOST_ADDRESS = 'http://127.0.0.1:9380' | ||
|
||
def test_create_chat_with_name(get_api_key_fixture): | ||
API_KEY = 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: | ||
blob = file.read() | ||
document = {"displayed_name":displayed_name,"blob":blob} | ||
documents = [] | ||
documents.append(document) | ||
doc_ids = [] | ||
docs= kb.upload_documents(documents) | ||
for doc in docs: | ||
doc_ids.append(doc.id) | ||
kb.async_parse_documents(doc_ids) | ||
time.sleep(60) | ||
rag.create_chat("test_create", datasets=[kb]) | ||
|
||
|
||
def test_update_chat_with_name(get_api_key_fixture): | ||
API_KEY = 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: | ||
blob = file.read() | ||
document = {"displayed_name": displayed_name, "blob": blob} | ||
documents = [] | ||
documents.append(document) | ||
doc_ids = [] | ||
docs = kb.upload_documents(documents) | ||
for doc in docs: | ||
doc_ids.append(doc.id) | ||
kb.async_parse_documents(doc_ids) | ||
time.sleep(60) | ||
chat = rag.create_chat("test_update", datasets=[kb]) | ||
chat.update({"name": "new_chat"}) | ||
|
||
|
||
def test_delete_chats_with_success(get_api_key_fixture): | ||
API_KEY = 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: | ||
blob = file.read() | ||
document = {"displayed_name": displayed_name, "blob": blob} | ||
documents = [] | ||
documents.append(document) | ||
doc_ids = [] | ||
docs = kb.upload_documents(documents) | ||
for doc in docs: | ||
doc_ids.append(doc.id) | ||
kb.async_parse_documents(doc_ids) | ||
time.sleep(60) | ||
chat = rag.create_chat("test_delete", datasets=[kb]) | ||
rag.delete_chats(ids=[chat.id]) | ||
|
||
API_KEY = get_api_key_fixture | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
rag.list_chats() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,54 @@ | ||
from ragflow import RAGFlow, DataSet | ||
|
||
from common import API_KEY, HOST_ADDRESS | ||
from test_sdkbase import TestSdk | ||
|
||
|
||
class TestDataset(TestSdk): | ||
def test_create_dataset_with_success(self): | ||
""" | ||
Test creating a dataset with success | ||
""" | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
ds = rag.create_dataset("God") | ||
if isinstance(ds, DataSet): | ||
assert ds.name == "God", "Name does not match." | ||
else: | ||
assert False, f"Failed to create dataset, error: {ds}" | ||
|
||
def test_update_dataset_with_success(self): | ||
""" | ||
Test updating a dataset with success. | ||
""" | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
ds = rag.create_dataset("ABC") | ||
if isinstance(ds, DataSet): | ||
assert ds.name == "ABC", "Name does not match." | ||
res = ds.update({"name":"DEF"}) | ||
assert res is None, f"Failed to update dataset, error: {res}" | ||
else: | ||
assert False, f"Failed to create dataset, error: {ds}" | ||
|
||
def test_delete_datasets_with_success(self): | ||
""" | ||
Test deleting a dataset with success | ||
""" | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
ds = rag.create_dataset("MA") | ||
if isinstance(ds, DataSet): | ||
assert ds.name == "MA", "Name does not match." | ||
res = rag.delete_datasets(ids=[ds.id]) | ||
assert res is None, f"Failed to delete dataset, error: {res}" | ||
else: | ||
assert False, f"Failed to create dataset, error: {ds}" | ||
|
||
def test_list_datasets_with_success(self): | ||
""" | ||
Test listing datasets with success | ||
""" | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
list_datasets = rag.list_datasets() | ||
assert len(list_datasets) > 0, "Do not exist any dataset" | ||
for ds in list_datasets: | ||
assert isinstance(ds, DataSet), "Existence type is not dataset." | ||
from ragflow import RAGFlow | ||
import random | ||
import pytest | ||
|
||
HOST_ADDRESS = 'http://127.0.0.1:9380' | ||
|
||
def test_create_dataset_with_name(get_api_key_fixture): | ||
API_KEY = get_api_key_fixture | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
rag.create_dataset("test_create_dataset_with_name") | ||
|
||
def test_create_dataset_with_duplicated_name(get_api_key_fixture): | ||
API_KEY = get_api_key_fixture | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
with pytest.raises(Exception) as exc_info: | ||
rag.create_dataset("test_create_dataset_with_name") | ||
assert str(exc_info.value) == "Duplicated dataset name in creating dataset." | ||
|
||
def test_create_dataset_with_random_chunk_method(get_api_key_fixture): | ||
API_KEY = get_api_key_fixture | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
valid_chunk_methods = ["naive","manual","qa","table","paper","book","laws","presentation","picture","one","knowledge_graph","email"] | ||
random_chunk_method = random.choice(valid_chunk_methods) | ||
rag.create_dataset("test_create_dataset_with_random_chunk_method",chunk_method=random_chunk_method) | ||
|
||
def test_create_dataset_with_invalid_parameter(get_api_key_fixture): | ||
API_KEY = get_api_key_fixture | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
valid_chunk_methods = ["naive", "manual", "qa", "table", "paper", "book", "laws", "presentation", "picture", "one", | ||
"knowledge_graph", "email"] | ||
chunk_method = "invalid_chunk_method" | ||
with pytest.raises(Exception) as exc_info: | ||
rag.create_dataset("test_create_dataset_with_name",chunk_method=chunk_method) | ||
assert str(exc_info.value) == f"{chunk_method} is not in {valid_chunk_methods}" | ||
|
||
|
||
def test_update_dataset_with_name(get_api_key_fixture): | ||
API_KEY = get_api_key_fixture | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
ds = rag.create_dataset("test_update_dataset") | ||
ds.update({"name": "updated_dataset"}) | ||
|
||
|
||
def test_delete_datasets_with_success(get_api_key_fixture): | ||
API_KEY = get_api_key_fixture | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
ds = rag.create_dataset("MA") | ||
rag.delete_datasets(ids=[ds.id]) | ||
|
||
|
||
def test_list_datasets_with_success(get_api_key_fixture): | ||
API_KEY = get_api_key_fixture | ||
rag = RAGFlow(API_KEY, HOST_ADDRESS) | ||
rag.list_datasets() |
Oops, something went wrong.