test_views.py 3.2 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
from engine.controller.vector_engine import VectorEngine
from engine.settings import DATABASE_DIRECTORY
from engine import app
from flask import jsonify
import pytest
import os
import logging
import json

logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

class TestViews:
J
jinhai 已提交
14
    HEADERS = {'Content-Type': 'application/json'}
J
jinhai 已提交
15 16 17 18 19 20 21

    def loads(self, resp):
        return json.loads(resp.data.decode())

    def test_group(self, test_client):
        data = {"dimension": 10}

J
jinhai 已提交
22
        resp = test_client.get('/vector/group/6', headers = TestViews.HEADERS)
J
jinhai 已提交
23 24 25
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 1

J
jinhai 已提交
26
        resp = test_client.post('/vector/group/6', data=json.dumps(data), headers = TestViews.HEADERS)
J
jinhai 已提交
27 28 29
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0

J
jinhai 已提交
30
        resp = test_client.get('/vector/group/6', headers = TestViews.HEADERS)
J
jinhai 已提交
31 32 33 34
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0

        # GroupList
J
jinhai 已提交
35
        resp = test_client.get('/vector/group', headers = TestViews.HEADERS)
J
jinhai 已提交
36 37 38 39
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0
        assert self.loads(resp)['group_list'] == [{'file_number': 0, 'group_name': '6'}]

J
jinhai 已提交
40
        resp = test_client.delete('/vector/group/6', headers = TestViews.HEADERS)
J
jinhai 已提交
41 42 43 44 45
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0
    
    
    def test_vector(self, test_client):
X
xj.lin 已提交
46 47
        dimension = {"dimension": 8}
        resp = test_client.post('/vector/group/6', data=json.dumps(dimension), headers = TestViews.HEADERS)
J
jinhai 已提交
48 49 50 51
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0
        
        vector = {"vector": [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8]}
J
jinhai 已提交
52
        resp = test_client.post('/vector/add/6', data=json.dumps(vector), headers = TestViews.HEADERS)
J
jinhai 已提交
53 54 55
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0

X
xj.lin 已提交
56
        vector = {"vector": [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8]}
J
jinhai 已提交
57
        resp = test_client.post('/vector/add/6', data=json.dumps(vector), headers = TestViews.HEADERS)
X
xj.lin 已提交
58 59 60 61
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0

        vector = {"vector": [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8]}
J
jinhai 已提交
62
        resp = test_client.post('/vector/add/6', data=json.dumps(vector), headers = TestViews.HEADERS)
X
xj.lin 已提交
63 64 65
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0

J
jinhai 已提交
66
        resp = test_client.post('/vector/index/6', headers = TestViews.HEADERS)
J
jinhai 已提交
67 68 69
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0

X
xj.lin 已提交
70
        limit = {"vector": [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8], "limit": 1}
J
jinhai 已提交
71
        resp = test_client.get('/vector/search/6', data=json.dumps(limit), headers = TestViews.HEADERS)
J
jinhai 已提交
72 73
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0
J
jinhai 已提交
74
        assert self.loads(resp)['vector_id'] == ['6.0']
J
jinhai 已提交
75

J
jinhai 已提交
76
        resp = test_client.delete('/vector/group/6', headers = TestViews.HEADERS)
J
jinhai 已提交
77 78 79 80 81 82
        assert resp.status_code == 200
        assert self.loads(resp)['code'] == 0