API: create dataset (#1106)

### What problem does this PR solve?

This PR have finished 'create dataset' of both HTTP API and Python SDK.
HTTP API:
```
curl --request POST --url http://<HOST_ADDRESS>/api/v1/dataset   --header 'Content-Type: application/json' --header 'Authorization: <ACCESS_KEY>' --data-binary '{
  "name": "<DATASET_NAME>"
}'
```

Python SDK:
```
from ragflow.ragflow import RAGFLow
ragflow = RAGFLow('<ACCESS_KEY>', 'http://127.0.0.1:9380')
ragflow.create_dataset("dataset1")

```

TODO: 
- ACCESS_KEY is the login_token when user login RAGFlow, currently.
RAGFlow should have the function that user can add/delete access_key.

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
- [x] Documentation Update

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2024-06-11 11:16:37 +08:00
committed by GitHub
parent 97ced2f667
commit cf2f6592dd
11 changed files with 355 additions and 62 deletions

View File

@ -3,49 +3,46 @@ import ragflow
from ragflow.ragflow import RAGFLow
import pytest
from unittest.mock import MagicMock
from common import API_KEY, HOST_ADDRESS
class TestCase(TestSdk):
@pytest.fixture
def ragflow_instance(self):
# Here we create a mock instance of RAGFlow for testing
return ragflow.ragflow.RAGFLow('123', 'url')
class TestBasic(TestSdk):
def test_version(self):
print(ragflow.__version__)
def test_create_dataset(self):
assert ragflow.ragflow.RAGFLow('123', 'url').create_dataset('abc') == 'abc'
def test_delete_dataset(self):
assert ragflow.ragflow.RAGFLow('123', 'url').delete_dataset('abc') == 'abc'
def test_list_dataset_success(self, ragflow_instance, monkeypatch):
# Mocking the response of requests.get method
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {'datasets': [{'id': 1, 'name': 'dataset1'}, {'id': 2, 'name': 'dataset2'}]}
# Patching requests.get to return the mock_response
monkeypatch.setattr("requests.get", MagicMock(return_value=mock_response))
# Call the method under test
result = ragflow_instance.list_dataset()
# Assertion
assert result == [{'id': 1, 'name': 'dataset1'}, {'id': 2, 'name': 'dataset2'}]
def test_list_dataset_failure(self, ragflow_instance, monkeypatch):
# Mocking the response of requests.get method
mock_response = MagicMock()
mock_response.status_code = 404 # Simulating a failed request
# Patching requests.get to return the mock_response
monkeypatch.setattr("requests.get", MagicMock(return_value=mock_response))
# Call the method under test
result = ragflow_instance.list_dataset()
# Assertion
assert result is None
# def test_create_dataset(self):
# res = RAGFLow(API_KEY, HOST_ADDRESS).create_dataset('abc')
# print(res)
#
# def test_delete_dataset(self):
# assert RAGFLow('123', 'url').delete_dataset('abc') == 'abc'
#
# def test_list_dataset_success(self, ragflow_instance, monkeypatch):
# # Mocking the response of requests.get method
# mock_response = MagicMock()
# mock_response.status_code = 200
# mock_response.json.return_value = {'datasets': [{'id': 1, 'name': 'dataset1'}, {'id': 2, 'name': 'dataset2'}]}
#
# # Patching requests.get to return the mock_response
# monkeypatch.setattr("requests.get", MagicMock(return_value=mock_response))
#
# # Call the method under test
# result = ragflow_instance.list_dataset()
#
# # Assertion
# assert result == [{'id': 1, 'name': 'dataset1'}, {'id': 2, 'name': 'dataset2'}]
#
# def test_list_dataset_failure(self, ragflow_instance, monkeypatch):
# # Mocking the response of requests.get method
# mock_response = MagicMock()
# mock_response.status_code = 404 # Simulating a failed request
#
# # Patching requests.get to return the mock_response
# monkeypatch.setattr("requests.get", MagicMock(return_value=mock_response))
#
# # Call the method under test
# result = ragflow_instance.list_dataset()
#
# # Assertion
# assert result is None