-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from hv0905/itest
Add integrate test
- Loading branch information
Showing
9 changed files
with
114 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
|
||
from app import config | ||
|
||
TEST_ACCESS_TOKEN = 'test_token' | ||
TEST_ADMIN_TOKEN = 'test_admin_token' | ||
|
||
config.config.qdrant.mode = "memory" | ||
config.config.admin_api_enable = True | ||
config.config.access_protected = True | ||
config.config.access_token = TEST_ACCESS_TOKEN | ||
config.config.admin_token = TEST_ADMIN_TOKEN | ||
config.config.storage.method = config.StorageMode.LOCAL | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def test_client(tmp_path_factory) -> TestClient: | ||
# Modify the configuration for testing | ||
config.config.storage.local.path = tmp_path_factory.mktemp("static_files") | ||
|
||
from app.webapp import app | ||
# Start the application | ||
|
||
with TestClient(app) as client: | ||
yield client |
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,68 @@ | ||
import asyncio | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from .conftest import TEST_ADMIN_TOKEN, TEST_ACCESS_TOKEN | ||
|
||
assets_path = Path(__file__).parent / '..' / 'assets' | ||
|
||
test_images = {'bsn': ['bsn_0.jpg', 'bsn_1.jpg', 'bsn_2.jpg'], | ||
'cat': ['cat_0.jpg', 'cat_1.jpg'], | ||
'cg': ['cg_0.jpg', 'cg_1.png']} | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_integrate(test_client): | ||
credentials = {'x-admin-token': TEST_ADMIN_TOKEN, 'x-access-token': TEST_ACCESS_TOKEN} | ||
resp = test_client.get("/", headers=credentials) | ||
assert resp.status_code == 200 | ||
img_ids = dict() | ||
for img_cls in test_images: | ||
img_ids[img_cls] = [] | ||
for image in test_images[img_cls]: | ||
print(f'upload image {image}...') | ||
resp = test_client.post('/admin/upload', | ||
files={'image_file': open(assets_path / 'test_images' / image, 'rb')}, | ||
headers=credentials, | ||
params={'local': True}) | ||
assert resp.status_code == 200 | ||
img_ids[img_cls].append(resp.json()['image_id']) | ||
|
||
print('Waiting for images to be processed...') | ||
|
||
while True: | ||
resp = test_client.get('/admin/server_info', headers=credentials) | ||
if resp.json()['image_count'] >= 7: | ||
break | ||
await asyncio.sleep(1) | ||
|
||
resp = test_client.get('/search/text/hatsune+miku', | ||
headers=credentials) | ||
assert resp.status_code == 200 | ||
assert resp.json()['result'][0]['img']['id'] in img_ids['cg'] | ||
|
||
resp = test_client.post('/search/image', | ||
files={'image': open(assets_path / 'test_images' / test_images['cat'][0], 'rb')}, | ||
headers=credentials) | ||
|
||
assert resp.status_code == 200 | ||
assert resp.json()['result'][0]['img']['id'] in img_ids['cat'] | ||
|
||
resp = test_client.get(f"/search/similar/{img_ids['bsn'][0]}", | ||
headers=credentials) | ||
|
||
assert resp.status_code == 200 | ||
assert resp.json()['result'][0]['img']['id'] in img_ids['bsn'] | ||
|
||
resp = test_client.put(f"/admin/update_opt/{img_ids['bsn'][0]}", json={'categories': ['bsn'], 'starred': True}, | ||
headers=credentials) | ||
assert resp.status_code == 200 | ||
|
||
resp = test_client.get(f"/search/text/cat", params={'categories': 'bsn'}, headers=credentials) | ||
assert resp.status_code == 200 | ||
assert resp.json()['result'][0]['img']['id'] in img_ids['bsn'] | ||
|
||
resp = test_client.get(f"/search/text/cat", params={'starred': True}, headers=credentials) | ||
assert resp.status_code == 200 | ||
assert resp.json()['result'][0]['img']['id'] in img_ids['bsn'] |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.