Skip to content

Commit

Permalink
refactor: update config in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jameszyao authored and SimsonW committed Mar 11, 2024
1 parent 3bf371b commit 59c4af1
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 32 deletions.
18 changes: 9 additions & 9 deletions backend/tests/common/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import requests
from typing import Dict

from tests.settings import HOST, WEB_SERVICE_PORT, API_SERVICE_PORT
from config import CONFIG
from tests.settings import HOST, WEB_SERVICE_PORT
from app.config import CONFIG


class ResponseWrapper:
Expand All @@ -22,20 +22,20 @@ def get_token():
app_base_url = f"{HOST}:{WEB_SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
login_data = {"username": CONFIG.DEFAULT_ADMIN_USERNAME, "password": CONFIG.DEFAULT_ADMIN_PASSWORD}
response = requests.post(f"{app_base_url}/admins/login", json=login_data)
return response.json()['data']['token']
return response.json()["data"]["token"]


Token = get_token()


def get_apikey():
app_base_url = f"{HOST}:{WEB_SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
create_apikey_res = requests.post(f"{app_base_url}/apikeys", headers=get_headers(Token),
json={"name": "test_apikey"})
apikey_id = create_apikey_res.json()['data']['apikey_id']
apikey_res = requests.get(f"{app_base_url}/apikeys/{apikey_id}", headers=get_headers(Token),
params={"plain": True})
return apikey_res.json()['data']['apikey']
create_apikey_res = requests.post(
f"{app_base_url}/apikeys", headers=get_headers(Token), json={"name": "test_apikey"}
)
apikey_id = create_apikey_res.json()["data"]["apikey_id"]
apikey_res = requests.get(f"{app_base_url}/apikeys/{apikey_id}", headers=get_headers(Token), params={"plain": True})
return apikey_res.json()["data"]["apikey"]


if CONFIG.API:
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/services_api/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Dict
from tests.common.utils import ResponseWrapper, get_headers
from tests.settings import HOST, WEB_SERVICE_PORT
from config import CONFIG
from app.config import CONFIG

BASE_URL = f"{HOST}:{WEB_SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"

Expand Down
2 changes: 1 addition & 1 deletion backend/tests/services_api/apikey/apikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Dict
from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST, WEB_SERVICE_PORT
from config import CONFIG
from app.config import CONFIG

BASE_URL = f"{HOST}:{WEB_SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"

Expand Down
3 changes: 2 additions & 1 deletion backend/tests/services_api/assistant/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST
from config import CONFIG
from app.config import CONFIG

if CONFIG.WEB:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
elif CONFIG.API:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.API_ROUTE_PREFIX}"
from tests.common.utils import APIKEY

Token = APIKEY


Expand Down
3 changes: 2 additions & 1 deletion backend/tests/services_api/assistant/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST
from config import CONFIG
from app.config import CONFIG

if CONFIG.WEB:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
elif CONFIG.API:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.API_ROUTE_PREFIX}"
from tests.common.utils import APIKEY

Token = APIKEY


Expand Down
7 changes: 4 additions & 3 deletions backend/tests/services_api/assistant/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST
from config import CONFIG
from app.config import CONFIG

if CONFIG.WEB:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
elif CONFIG.API:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.API_ROUTE_PREFIX}"
from tests.common.utils import APIKEY

Token = APIKEY


async def create_message(assistant_id: str, chat_id: str, payload: Dict):
async def create_message(assistant_id: str, chat_id: str, payload: Dict):
headers = get_headers(Token)
async with aiohttp.ClientSession(headers=headers) as session:
request_url = f"{BASE_URL}/assistants/{assistant_id}/chats/{chat_id}/messages"
Expand All @@ -25,7 +26,7 @@ async def list_messages(assistant_id: str, chat_id: str, payload: Dict = None):
headers = get_headers(Token)
async with aiohttp.ClientSession(headers=headers) as session:
request_url = f"{BASE_URL}/assistants/{assistant_id}/chats/{chat_id}/messages"
response = await session.get(url=request_url, params=payload)
response = await session.get(url=request_url, params=payload)
return ResponseWrapper(response.status, await response.json())


Expand Down
3 changes: 2 additions & 1 deletion backend/tests/services_api/inference/chat_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST
from config import CONFIG
from app.config import CONFIG

if CONFIG.WEB:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
elif CONFIG.API:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.API_ROUTE_PREFIX}"
from tests.common.utils import APIKEY

Token = APIKEY


Expand Down
3 changes: 2 additions & 1 deletion backend/tests/services_api/inference/text_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST
from config import CONFIG
from app.config import CONFIG

if CONFIG.WEB:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
elif CONFIG.API:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.API_ROUTE_PREFIX}"
from tests.common.utils import APIKEY

Token = APIKEY


Expand Down
2 changes: 1 addition & 1 deletion backend/tests/services_api/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST, WEB_SERVICE_PORT
from config import CONFIG
from app.config import CONFIG

BASE_URL = f"{HOST}:{WEB_SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"

Expand Down
2 changes: 1 addition & 1 deletion backend/tests/services_api/model/model_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST, WEB_SERVICE_PORT
from config import CONFIG
from app.config import CONFIG

BASE_URL = f"{HOST}:{WEB_SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"

Expand Down
2 changes: 1 addition & 1 deletion backend/tests/services_api/model/provider.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import aiohttp
from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST, WEB_SERVICE_PORT
from config import CONFIG
from app.config import CONFIG

BASE_URL = f"{HOST}:{WEB_SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"

Expand Down
3 changes: 2 additions & 1 deletion backend/tests/services_api/retrieval/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from typing import Dict
from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST
from config import CONFIG
from app.config import CONFIG

if CONFIG.WEB:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
elif CONFIG.API:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.API_ROUTE_PREFIX}"
from tests.common.utils import APIKEY

Token = APIKEY


Expand Down
3 changes: 2 additions & 1 deletion backend/tests/services_api/retrieval/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from typing import Dict
from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST
from config import CONFIG
from app.config import CONFIG

if CONFIG.WEB:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
elif CONFIG.API:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.API_ROUTE_PREFIX}"
from tests.common.utils import APIKEY

Token = APIKEY


Expand Down
3 changes: 2 additions & 1 deletion backend/tests/services_api/retrieval/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from typing import Dict
from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST
from config import CONFIG
from app.config import CONFIG

if CONFIG.WEB:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
elif CONFIG.API:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.API_ROUTE_PREFIX}"
from tests.common.utils import APIKEY

Token = APIKEY


Expand Down
3 changes: 2 additions & 1 deletion backend/tests/services_api/tool/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

from tests.common.utils import ResponseWrapper, get_headers, Token
from tests.settings import HOST
from config import CONFIG
from app.config import CONFIG

if CONFIG.WEB:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.WEB_ROUTE_PREFIX}"
elif CONFIG.API:
BASE_URL = f"{HOST}:{CONFIG.SERVICE_PORT}{CONFIG.API_ROUTE_PREFIX}"
from tests.common.utils import APIKEY

Token = APIKEY


Expand Down
7 changes: 1 addition & 6 deletions backend/tests/services_tests/admin/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
import pytest

from tests.services_api.admin.admin import login, verify_token, refresh_token, logout
from config import CONFIG
from app.config import CONFIG


class TestAdmin:

admin_list = ["object", "admin_id", "username", "token", "created_timestamp", "updated_timestamp"]
admin_keys = set(admin_list)
token = None

@pytest.mark.asyncio
@pytest.mark.run(order=101)
async def test_login(self):

login_data = {"username": CONFIG.DEFAULT_ADMIN_USERNAME, "password": CONFIG.DEFAULT_ADMIN_PASSWORD}
res = await login(login_data)
res_json = res.json()
Expand All @@ -27,7 +25,6 @@ async def test_login(self):
@pytest.mark.run(order=102)
@pytest.mark.asyncio
async def test_verify_token(self):

res = await verify_token(TestAdmin.token)
res_json = res.json()
assert res.status_code == 200
Expand All @@ -37,7 +34,6 @@ async def test_verify_token(self):
@pytest.mark.run(order=103)
@pytest.mark.asyncio
async def test_refresh_token(self):

res = await refresh_token(TestAdmin.token)
res_json = res.json()
assert res.status_code == 200
Expand All @@ -63,7 +59,6 @@ async def test_refresh_token(self):
@pytest.mark.run(order=104)
@pytest.mark.asyncio
async def test_logout(self):

res = await logout(TestAdmin.token)
res_json = res.json()
assert res.status_code == 200
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from config import CONFIG, load_str_env
from app.config import CONFIG, load_str_env

HOST = "http://127.0.0.1"
WEB_SERVICE_PORT = os.environ.get("WEB_SERVICE_PORT", CONFIG.SERVICE_PORT)
Expand Down

0 comments on commit 59c4af1

Please sign in to comment.