summaryrefslogtreecommitdiff
path: root/tests/functional-tests/common/utils/minertest.py
blob: 7be1bd8b41e7627da4181b4af70b6aad9a2abf16 (plain)
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
#!/usr/bin/env python
#
# Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

from common.utils import configuration as cfg
from common.utils.helpers import StoreHelper
from common.utils.system import TrackerSystemAbstraction

import shutil
import os
import unittest2 as ut

MINER_TMP_DIR = cfg.TEST_MONITORED_TMP_DIR

def get_test_path (filename):
    return os.path.join (MINER_TMP_DIR, filename)

def get_test_uri (filename):
    return "file://" + os.path.join (MINER_TMP_DIR, filename)

DEFAULT_TEXT = "Some stupid content, to have a test file"

class CommonTrackerMinerTest (ut.TestCase):

    @classmethod
    def _create_test_data_simple (self):
        #
        #     ~/test-monitored/
        #                     /file1.txt
        #                     /dir1/
        #                          /file2.txt
        #                          /dir2/
        #                               /file3.txt
        #
        #
        #     ~/test-no-monitored/
        #                        /file0.txt
        #
        
        for d in ["test-monitored",
                  "test-monitored/dir1",
                  "test-monitored/dir1/dir2",
                  "test-no-monitored"]:
            directory = os.path.join (MINER_TMP_DIR, d)
            if (os.path.exists (directory)):
                shutil.rmtree (directory)
            os.makedirs (directory)

        for tf in ["test-monitored/file1.txt",
                   "test-monitored/dir1/file2.txt",
                   "test-monitored/dir1/dir2/file3.txt",
                   "test-no-monitored/file0.txt"]:
            testfile = os.path.join (MINER_TMP_DIR, tf)
            if (os.path.exists (testfile)):
                os.remove (testfile)
            f = open (testfile, 'w')
            f.write (DEFAULT_TEXT)
            f.close ()

    @classmethod
    def _create_test_data_many_files (self):
        # Create 10000 text files, so extraction takes a long time.

        directory = os.path.join (MINER_TMP_DIR, 'slow-extraction-data')
        #if (os.path.exists (directory)):
        #    shutil.rmtree (directory)
        if not os.path.exists (directory):
            os.makedirs (directory)

        # Extraction of 10,000 text files takes about 10 seconds on my system;
        # this is long enough to be able to detect bugs in extraction.
        # A more robust solution would be to create a mock subclass of
        # TrackerMinerFS in C and make it block in the process_files() callback.
        for i in range(10000):
            testfile = os.path.join (directory, "test-%i.txt" % i)
            if not os.path.exists (testfile):
                #    os.remove (testfile)
                f = open (testfile, 'w')
                f.write (DEFAULT_TEXT)
                f.close ()


    def _get_text_documents (self):
        return self.store.query ("""
          SELECT ?url WHERE {
              ?u a nfo:TextDocument ;
                 nie:url ?url.
          }
          """)

    def _get_parent_urn (self, filepath):
        result = self.store.query ("""
          SELECT nfo:belongsToContainer(?u) WHERE {
              ?u a nfo:FileDataObject ;
                 nie:url \"%s\" .
          }
          """ % (get_test_uri (filepath)))
        self.assertEquals (len (result), 1)
        return result[0][0]

    def _get_file_urn (self, filepath):
        result = self.store.query ("""
          SELECT ?u WHERE {
              ?u a nfo:FileDataObject ;
                 nie:url \"%s\" .
          }
          """ % (get_test_uri (filepath)))
        self.assertEquals (len (result), 1)
        return result[0][0]