コード例 #1
0
class TestSearchEngine(unittest.TestCase):
    def setUp(self):
        self.engine = SearchEngine('db_name')
        self.engine.database.update(database)
        with open("test1.txt", 'w') as file:
            file.write(test1)
        with open("test2.txt", 'w') as file:
            file.write(test2)

    def test_empty(self):
        result = self.engine.search_one('')
        self.assertEqual(result, {})

    def test_search_one(self):
        result = self.engine.search_one('test')
        self.assertEqual(
            result, {
                'test1.txt': [Position_with_lines(11, 15, 0)],
                'test2.txt': [Position_with_lines(3, 7, 0)]
            })

    def test_search_many_one(self):
        result = self.engine.search_many('test')
        self.assertEqual(
            result, {
                'test1.txt': [Position_with_lines(11, 15, 0)],
                'test2.txt': [Position_with_lines(3, 7, 0)]
            })

    def test_search_many_two(self):
        result = self.engine.search_many('my test')
        self.assertEqual(
            result, {
                'test1.txt': [
                    Position_with_lines(8, 10, 0),
                    Position_with_lines(11, 15, 0)
                ],
                'test2.txt':
                [Position_with_lines(0, 2, 0),
                 Position_with_lines(3, 7, 0)]
            })

    def test_search_limit_offset_default(self):
        result = self.engine.search_limit_offset('test')
        self.assertEqual(result, {'test1.txt': [], 'test2.txt': []})

    def test_search_limit_offset_all(self):
        result = self.engine.search_limit_offset('test',
                                                 doclimit=2,
                                                 docoffset=0,
                                                 limits=[2, 2],
                                                 offsets=[0, 0])
        self.assertEqual(
            result, {
                'test1.txt': ['this is my <strong>test</strong>'],
                'test2.txt': ['my <strong>test</strong>']
            })

    def test_search_limit_offset_one(self):
        result = self.engine.search_limit_offset('test',
                                                 doclimit=1,
                                                 docoffset=0,
                                                 limits=[2, 2],
                                                 offsets=[0, 0])
        self.assertEqual(result, {
            'test1.txt': ['this is my <strong>test</strong>'],
            'test2.txt': []
        })

    def test_search_limit_offset_shift(self):
        result = self.engine.search_limit_offset('test',
                                                 doclimit=2,
                                                 docoffset=1,
                                                 limits=[2, 2],
                                                 offsets=[0, 0])
        self.assertEqual(result, {
            'test1.txt': [],
            'test2.txt': ['my <strong>test</strong>']
        })

    def test_search_many_limit_offset_one(self):
        result = self.engine.search_many_limit_offset('test',
                                                      limit=1,
                                                      offset=0,
                                                      limits=[2, 2],
                                                      offsets=[0, 0])
        self.assertEqual(result,
                         {'test1.txt': [Position_with_lines(11, 15, 0)]})

    def test_search_many_limit_offset_shift(self):
        result = self.engine.search_many_limit_offset('test',
                                                      limit=1,
                                                      offset=1,
                                                      limits=[2, 2],
                                                      offsets=[0, 0])
        self.assertEqual(result, {'test2.txt': [Position_with_lines(3, 7, 0)]})

    def test_search_many_limit_offset_all(self):
        result = self.engine.search_many_limit_offset('test',
                                                      limit=2,
                                                      offset=0,
                                                      limits=[2, 2],
                                                      offsets=[0, 0])
        self.assertEqual(
            result, {
                'test1.txt': [Position_with_lines(11, 15, 0)],
                'test2.txt': [Position_with_lines(3, 7, 0)]
            })

    def test_generator(self):
        result = self.engine.generator(
            [[Position_with_lines(12, 13, 1),
              Position_with_lines(3, 7, 0)],
             [Position_with_lines(11, 15, 0),
              Position_with_lines(3, 7, 0)], []])
        a = []
        for r in result:
            a.append(r)
        self.assertEqual(a, [
            Position_with_lines(11, 15, 0),
            Position_with_lines(3, 7, 0),
            Position_with_lines(12, 13, 1),
            Position_with_lines(3, 7, 0)
        ])

    def test_search_many_limit_offset_gen_one(self):
        result = self.engine.search_many_limit_offset_gen('test',
                                                          limit=1,
                                                          offset=0,
                                                          limits=[2, 2],
                                                          offsets=[0, 0])
        result_keys = list(result.keys())
        self.assertEqual(result_keys, ['test1.txt'])
        for key in result.keys():
            for data in result[key]:
                self.assertEqual(data, Position_with_lines(11, 15, 0))

    def test_search_many_limit_offset_gen_shift(self):
        result = self.engine.search_many_limit_offset_gen('test',
                                                          limit=1,
                                                          offset=1,
                                                          limits=[2, 2],
                                                          offsets=[0, 0])
        result_keys = list(result.keys())
        self.assertEqual(result_keys, ['test2.txt'])
        for key in result.keys():
            for data in result[key]:
                self.assertEqual(data, Position_with_lines(3, 7, 0))

    def test_search_many_limit_offset_gen_all(self):
        result = self.engine.search_many_limit_offset_gen('test',
                                                          limit=2,
                                                          offset=0,
                                                          limits=[2, 2],
                                                          offsets=[0, 0])
        result_keys = list(result.keys())
        self.assertEqual(result_keys, ['test1.txt', 'test2.txt'])
        for key in result.keys():
            for data in result[key]:
                self.assertEqual(data, database['test'][key][0])

    def tearDown(self):
        del self.engine
        for filename in os.listdir(os.getcwd()):
            if filename == 'db_name' or filename.startswith('db_name'):
                os.remove(filename)
        if 'test1.txt' in os.listdir(os.getcwd()):
            os.remove('test1.txt')
        if 'test2.txt' in os.listdir(os.getcwd()):
            os.remove('test2.txt')
コード例 #2
0
 def do_POST(self):
     """
     POST handler for query
     """
     try:
         content_length = int(self.headers['Content-Length'])
         body = str(self.rfile.read(content_length))
         print("body = " + body)
         query, limit, offset, limits, offsets, action, action_doc, action_exists = self.parse_url(
             body)
         print("query = " + query)
         print("doclimit = " + limit)
         print("docoffset = " + offset)
         print("action = " + action)
         print("actiondoc = " + action_doc)
         if action_exists:
             offsets = self.get_new_offset_limit(action, action_doc,
                                                 offsets, limits)
         print('limits = ' + str(limits))
         print('offsets = ' + str(offsets))
         search_engine = SearchEngine('database')
         r = search_engine.search_limit_offset(query, 4, limit, offset,
                                               limits, offsets)
         myresp = ''
         myresp += 'Documents Limit<br><input type="text" name="limit" value="' + str(
             limit) + '"><br>'
         myresp += 'Documents Offset<br><input type="text" name="offset" value="' + str(
             offset) + '"><br>'
         key_list = list(r.keys())
         key_list.sort()
         j = 0
         for key in key_list:
             myresp += '<ol>\n'
             myresp += '<li>' + key + '</li>\n<ul>'
             myresp += 'Limit<br><input type="text" name="doc' + str(
                 j) + 'limit" value="' + limits[j] + '"><br>'
             myresp += 'Offset<br><input type="text" name=doc' + str(
                 j) + 'offset" value="' + offsets[j] + '"><br>'
             myresp += '<input type="submit" name=action' + str(
                 j) + ' value="perv">'
             myresp += '<input type="submit" name=action' + str(
                 j) + ' value="back">'
             myresp += '<input type="submit" name=action' + str(
                 j) + ' value="next"> <br>'
             for val in r[key]:
                 myresp += '<li>' + val + '</li>'
             myresp += '</ul>'
             j = j + 1
             myresp += '</ol>'
         self.send_response(200)
         self.send_header("Content-type", "text/html; charset=utf-8")
         self.end_headers()
         self.wfile.write(
             bytes((resp + data.format(query, myresp)), "utf-8"))
     except TypeError:
         response = 'fields "limit" and "offset" can not take a negative or fractional values'
         self.wfile.write(bytes((resp + data.format('', response)),
                                "utf-8"))
     except Exception as ex:
         response = '<br>Uuups. Something went wrong. Error message: ' + str(
             ex) + '<br>'
         self.send_response(200)
         self.send_header("Content-type", "text/html; charset=utf-8")
         self.end_headers()
         files = os.listdir(".\\")
         i = 0
         response += 'Documents Limit<br><input type="text" name="limit" value="0"><br>'
         response += 'Documents Offset<br><input type="text" name="offset" value="0"><br>'
         for f in files:
             if re.match(".*\.txt", f):
                 response += (f + "<br>")
                 response += 'Limit<br><input type="text" name=doc' + str(
                     i) + 'limit value="0"><br>'
                 response += 'Offset<br><input type="text" name=doc' + str(
                     i) + 'offset value="0"><br>'
                 response += '<input type="submit" name=action' + str(
                     i) + ' value="perv">'
                 response += '<input type="submit" name=action' + str(
                     i) + ' value="back">'
                 response += '<input type="submit" name=action' + str(
                     i) + ' value="next"> <br>'
                 i = i + 1
         self.wfile.write(
             bytes((resp + data.format('', 'Not Found<br>' + response)),
                   "utf-8"))