File: test_fts_basic.py

package info (click to toggle)
tracker-miners 3.8.2-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 16,656 kB
  • sloc: ansic: 59,413; python: 3,774; xml: 261; perl: 106; sh: 62; makefile: 53
file content (154 lines) | stat: -rw-r--r-- 5,109 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: utf-8 -*-

# Copyright (C) 2010, Nokia (ivan.frade@nokia.com)
# Copyright (C) 2019-2020, Sam Thursfield (sam@afuera.me.uk)
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA  02110-1301, USA.

#
# TODO:
#     These tests are for files... we need to write them for folders!
#
"""
Monitor a directory, copy/move/remove/update text files and check that
the text contents are updated accordingly in the indexes.
"""

import pathlib
import unittest as ut

# Must import this for logging.
import configuration as cfg
import fixtures


class MinerFTSBasicTest(fixtures.TrackerMinerFTSTest):
    """
    Tests different contents in a single file
    """

    def test_01_single_word(self):
        TEXT = "automobile"
        self.basic_test(TEXT, TEXT)

    def test_02_multiple_words(self):
        TEXT = "automobile with unlimited power"
        self.set_text(TEXT)

        results = self.search_word("automobile")
        self.assertEqual(len(results), 1)
        self.assertIn(self.uri(self.testfile), results)

        results = self.search_word("unlimited")
        self.assertEqual(len(results), 1)
        self.assertIn(self.uri(self.testfile), results)

    def test_03_long_word(self):
        # TEXT is longer than the 200 characters specified in the fts configuration
        TEXT = "ai" * 200
        self.set_text(TEXT)

        results = self.search_word(TEXT)
        self.assertEqual(len(results), 0)

    def test_04_non_existent_word(self):
        TEXT = "This a trick"
        self.set_text(TEXT)
        results = self.search_word("trikc")
        self.assertEqual(len(results), 0)

    def test_05_word_multiple_times_in_file(self):
        TEXT = "automobile is red. automobile is big. automobile is great!"
        self.basic_test(TEXT, "automobile")

    def test_06_sentence(self):
        TEXT = "plastic is fantastic"
        self.basic_test(TEXT, TEXT)

    def test_07_partial_sentence(self):
        TEXT = "plastic is fantastic"
        self.basic_test(TEXT, "is fantastic")

    @ut.skip("Currently fails with: fts5: syntax error near '.'")
    def test_08_strange_word(self):
        # FIXME Not sure what are we testing here
        TEXT = "'summer.time'"
        self.basic_test(TEXT, "summer.time")

    # Skip the test 'search for .'

    def test_09_mixed_letters_and_numbers(self):
        TEXT = "abc123"
        self.basic_test(TEXT, "abc123")

    @ut.skip(
        "We don't ignore numbers by default since https://gitlab.gnome.org/GNOME/tracker/merge_requests/172."
    )
    def test_10_ignore_numbers(self):
        TEXT = "palabra 123123"
        self.set_text(TEXT)
        results = self.search_word("123123")
        self.assertEqual(len(results), 0)

    def test_11_utf16(self):
        TEXT = '你好'
        self.set_text(TEXT, 'utf-16')
        results = self.search_word(TEXT)
        self.assertEqual(len(results), 1)
        self.assertIn(self.uri(self.testfile), results)

    def test_12_windows1252(self):
        TEXT = 'Cañamón'
        self.set_text(TEXT, 'WINDOWS-1252')
        results = self.search_word(TEXT)
        self.assertEqual(len(results), 1)
        self.assertIn(self.uri(self.testfile), results)

    def test_13_long_text(self):
        TEXT = 'Hola caracola ' * 1000
        self.basic_test(TEXT, "hola")


class MinerFTSWAllowlistTest(fixtures.TrackerMinerFTSTest):
    """
    Test that only an allowlisted set of plaintext files get FTS indexed.

    We can create performance problems by indexing big trees of sourcecode,
    video game data, etc., so we use an extension-based allowlist to limit
    what gets FTS indexed.
    """

    def test_no_extension_file(self):
        self.testfile = "test-monitored/miner-fts-test-no-extension"

        path = pathlib.Path(self.path(self.testfile))
        url = self.uri(self.testfile)
        expected = f"a nfo:Document; nie:isStoredAs <{url}>"
        with self.tracker.await_insert(
            fixtures.DOCUMENTS_GRAPH, expected, timeout=cfg.AWAIT_TIMEOUT
        ):
            path.write_text("Definitely do not index this file.")

        # No results for full text search.
        results = self.search_word("Definitely")
        self.assertEqual(len(results), 0)

        # However, there should be an nfo:PlainTextDocument resource as normal.
        self.assertEqual(1, self.tracker.count_instances("nfo:PlainTextDocument"))


if __name__ == "__main__":
    fixtures.tracker_test_main()