You need to sign in or sign up before continuing.
test_vector_engine.py 4.5 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]]
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43


    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
        code, group_id, file_number = VectorEngine.AddGroup('test_group', 8)
        assert code == VectorEngine.SUCCESS_CODE
        assert group_id == 'test_group'
        assert file_number == 0

        # 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 已提交
44
        print("group_list: ", group_list)
J
jinhai 已提交
45 46 47
        assert group_list == [{'group_name': 'test_group', 'file_number': 0}]

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

        # Add vector for exist group
J
jinhai 已提交
53
        code, vector_id = VectorEngine.AddVector('test_group', self.__vectors)
X
xj.lin 已提交
54
        assert code == VectorEngine.SUCCESS_CODE
J
jinhai 已提交
55
        assert vector_id == ['test_group.0']
X
xj.lin 已提交
56 57

        # Add vector for exist group
J
jinhai 已提交
58
        code, vector_id = VectorEngine.AddVector('test_group', self.__vectors)
X
xj.lin 已提交
59
        assert code == VectorEngine.SUCCESS_CODE
J
jinhai 已提交
60
        assert vector_id == ['test_group.1']
X
xj.lin 已提交
61 62

        # Add vector for exist group
J
jinhai 已提交
63
        code, vector_id = VectorEngine.AddVector('test_group', self.__vectors)
X
xj.lin 已提交
64
        assert code == VectorEngine.SUCCESS_CODE
J
jinhai 已提交
65
        assert vector_id == ['test_group.2']
X
xj.lin 已提交
66 67

        # Add vector for exist group
J
jinhai 已提交
68
        code, vector_id = VectorEngine.AddVector('test_group', self.__vectors)
J
jinhai 已提交
69
        assert code == VectorEngine.SUCCESS_CODE
J
jinhai 已提交
70
        assert vector_id == ['test_group.3']
J
jinhai 已提交
71 72

        # Check search vector interface
X
xj.lin 已提交
73
        code, vector_id = VectorEngine.SearchVector('test_group', self.__vector, self.__limit)
J
jinhai 已提交
74
        assert code == VectorEngine.SUCCESS_CODE
J
jinhai 已提交
75
        assert vector_id == ['test_group.0']
J
jinhai 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

        # 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 已提交
94
        code, vector_ids = VectorEngine.SearchVector('test_group', self.__vector, self.__limit)
J
jinhai 已提交
95
        assert code == VectorEngine.GROUP_NOT_EXIST
J
jinhai 已提交
96
        assert vector_ids == {}
J
jinhai 已提交
97 98 99 100 101 102 103 104 105 106

        # 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 已提交
107
        filename = VectorEngine.InsertVectorIntoRawFile('test_group', 'test_group.raw', self.__vector, 0)
J
jinhai 已提交
108 109 110
        assert filename == 'test_group.raw'

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

X
xj.lin 已提交
113

J
jinhai 已提交
114 115
        print('expected_list: ', expected_list)
        print('vector_list: ', vector_list)
J
jinhai 已提交
116
        print('vector_id_list: ', vector_id_list)
J
jinhai 已提交
117

J
jinhai 已提交
118
        expected_list = np.asarray(expected_list).astype('float32')
X
xj.lin 已提交
119
        assert np.all(vector_list == expected_list)
J
jinhai 已提交
120 121 122 123 124 125 126

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