test_vector_engine.py 4.0 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5
from engine.controller.vector_engine import VectorEngine
from engine.settings import DATABASE_DIRECTORY
from flask import jsonify
import pytest
import os
X
xj.lin 已提交
6
import numpy as np
J
jinhai 已提交
7 8 9 10 11 12 13
import logging

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

class TestVectorEngine:
    def setup_class(self):
J
jinhai 已提交
14
        self.__vectors = [[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8] for _ in range(10) ]
J
jinhai 已提交
15
        self.__vector = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8]
X
xj.lin 已提交
16
        self.__limit = 1
J
jinhai 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29


    def teardown_class(self):
        pass

    def test_group(self):
        # Make sure there is no group
        code, group_id, file_number = VectorEngine.DeleteGroup('test_group')
        assert code == VectorEngine.SUCCESS_CODE
        assert group_id == 'test_group'
        assert file_number == 0

        # Add a group
J
jinhai 已提交
30
        code, group_id = VectorEngine.AddGroup('test_group', 8)
J
jinhai 已提交
31 32 33 34 35 36 37 38 39 40 41 42
        assert code == VectorEngine.SUCCESS_CODE
        assert group_id == 'test_group'

        # Check the group existing
        code, group_id, file_number = VectorEngine.GetGroup('test_group')
        assert code == VectorEngine.SUCCESS_CODE
        assert group_id == 'test_group'
        assert file_number == 0

        # Check the group list
        code, group_list = VectorEngine.GetGroupList()
        assert code == VectorEngine.SUCCESS_CODE
X
xj.lin 已提交
43
        print("group_list: ", group_list)
J
jinhai 已提交
44 45 46
        assert group_list == [{'group_name': 'test_group', 'file_number': 0}]

        # Add Vector for not exist group
J
jinhai 已提交
47
        code, vector_id = VectorEngine.AddVector('not_exist_group', self.__vectors)
J
jinhai 已提交
48
        assert code == VectorEngine.GROUP_NOT_EXIST
J
jinhai 已提交
49
        assert vector_id == 'invalid'
J
jinhai 已提交
50 51

        # Add vector for exist group
J
jinhai 已提交
52
        code, vector_id = VectorEngine.AddVector('test_group', self.__vectors)
X
xj.lin 已提交
53
        assert code == VectorEngine.SUCCESS_CODE
J
jinhai 已提交
54
        assert vector_id == ['test_group.0', 'test_group.1', 'test_group.2', 'test_group.3', 'test_group.4', 'test_group.5', 'test_group.6', 'test_group.7', 'test_group.8', 'test_group.9']
J
jinhai 已提交
55 56

        # Check search vector interface
X
xj.lin 已提交
57
        code, vector_id = VectorEngine.SearchVector('test_group', self.__vector, self.__limit)
J
jinhai 已提交
58
        assert code == VectorEngine.SUCCESS_CODE
J
jinhai 已提交
59
        assert vector_id == ['test_group.0']
J
jinhai 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

        # Check create index interface
        code = VectorEngine.CreateIndex('test_group')
        assert code == VectorEngine.SUCCESS_CODE

        # Remove the group
        code, group_id, file_number = VectorEngine.DeleteGroup('test_group')
        assert code == VectorEngine.SUCCESS_CODE
        assert group_id == 'test_group'
        assert file_number == 0

        # Check the group is disppeared
        code, group_id, file_number = VectorEngine.GetGroup('test_group')
        assert code == VectorEngine.FAULT_CODE
        assert group_id == 'test_group'
        assert file_number == 0

        # Check SearchVector interface
J
jinhai 已提交
78
        code, vector_ids = VectorEngine.SearchVector('test_group', self.__vector, self.__limit)
J
jinhai 已提交
79
        assert code == VectorEngine.GROUP_NOT_EXIST
J
jinhai 已提交
80
        assert vector_ids == {}
J
jinhai 已提交
81 82 83 84 85 86 87 88 89 90

        # Create Index for not exist group id
        code = VectorEngine.CreateIndex('test_group')
        assert code == VectorEngine.GROUP_NOT_EXIST

        # Clear raw file
        code = VectorEngine.ClearRawFile('test_group')
        assert code == VectorEngine.SUCCESS_CODE

    def test_raw_file(self):
J
jinhai 已提交
91
        filename = VectorEngine.InsertVectorIntoRawFile('test_group', 'test_group.raw', self.__vector, 0)
J
jinhai 已提交
92 93 94
        assert filename == 'test_group.raw'

        expected_list = [self.__vector]
J
jinhai 已提交
95
        vector_list, vector_id_list = VectorEngine.GetVectorListFromRawFile('test_group', filename)
J
jinhai 已提交
96

X
xj.lin 已提交
97

J
jinhai 已提交
98 99
        print('expected_list: ', expected_list)
        print('vector_list: ', vector_list)
J
jinhai 已提交
100
        print('vector_id_list: ', vector_id_list)
J
jinhai 已提交
101

J
jinhai 已提交
102
        expected_list = np.asarray(expected_list).astype('float32')
X
xj.lin 已提交
103
        assert np.all(vector_list == expected_list)
J
jinhai 已提交
104 105 106 107 108 109 110

        code = VectorEngine.ClearRawFile('test_group')
        assert code == VectorEngine.SUCCESS_CODE