500-writeback.py 6.25 KB
Newer Older
1
#!/usr/bin/env python3
Ivan Frade's avatar
Ivan Frade committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

# Copyright (C) 2010, Nokia (ivan.frade@nokia.com)
#
# 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.
#
"""
Write values in tracker and check the actual values are written
on the files. Note that these tests are highly platform dependant.
"""
24
import os
25
import sys
Ivan Frade's avatar
Ivan Frade committed
26 27
import time

28 29
from common.utils.extractor import get_tracker_extract_jsonld_output
from common.utils.helpers import log
30
from common.utils.writebacktest import CommonTrackerWritebackTest as CommonTrackerWritebackTest
31
import unittest as ut
Ivan Frade's avatar
Ivan Frade committed
32

33 34
REASONABLE_TIMEOUT = 5  # Seconds we wait for tracker-writeback to do the work

Ivan Frade's avatar
Ivan Frade committed
35

36
class WritebackBasicDataTest (CommonTrackerWritebackTest):
Ivan Frade's avatar
Ivan Frade committed
37 38 39 40 41
    """
    Write in tracker store the properties witih writeback support and check
    that the new values are actually in the file
    """

42
    def __writeback_test(self, filename, mimetype, prop, expectedKey=None):
Ivan Frade's avatar
Ivan Frade committed
43 44 45 46 47 48 49 50 51 52
        """
        Set a value in @prop for the @filename. Then ask tracker-extractor
        for metadata and check in the results dictionary if the property is there.

        Note: given the special translation of some property-names in the dictionary
        with extracted metadata, there is an optional parameter @expectedKey
        to specify what property to check in the dictionary. If None, then
        the @prop is used.
        """

53 54 55 56
        # FIXME: filename is actually a URI! :(
        filename_real = filename[len('file://'):]
        initial_mtime = os.stat(filename_real).st_mtime

57
        TEST_VALUE = prop.replace(":", "") + "test"
Ivan Frade's avatar
Ivan Frade committed
58
        SPARQL_TMPL = """
59
           DELETE { ?u %s ?v } WHERE { ?u nie:url '%s' ; %s ?v }
Ivan Frade's avatar
Ivan Frade committed
60 61
           INSERT { ?u %s '%s' }
           WHERE  { ?u nie:url '%s' }
62 63
        """
        self.tracker.update(SPARQL_TMPL % (prop, filename, prop, prop, TEST_VALUE, filename))
64

65
        log("Waiting for change on %s" % filename_real)
66
        self.wait_for_file_change(filename_real, initial_mtime)
67
        log("Got the change")
68

69
        results = get_tracker_extract_jsonld_output(filename, mimetype)
Ivan Frade's avatar
Ivan Frade committed
70
        keyDict = expectedKey or prop
71
        self.assertIn(TEST_VALUE, results[keyDict])
Ivan Frade's avatar
Ivan Frade committed
72

73
    def __writeback_hasTag_test(self, filename, mimetype):
Ivan Frade's avatar
Ivan Frade committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

        SPARQL_TMPL = """
            INSERT {
              <test://writeback-hasTag-test/1> a nao:Tag ;
                        nao:prefLabel "testTag" .

              ?u nao:hasTag <test://writeback-hasTag-test/1> .
            } WHERE {
              ?u nie:url '%s' .
            }
        """

        CLEAN_VALUE = """
           DELETE {
              <test://writeback-hasTag-test/1> a rdfs:Resource.
              ?u nao:hasTag <test://writeback-hasTag-test/1> .
           } WHERE {
              ?u nao:hasTag <test://writeback-hasTag-test/1> .
           }
        """

95
        self.tracker.update(SPARQL_TMPL % (filename))
Ivan Frade's avatar
Ivan Frade committed
96

97
        time.sleep(REASONABLE_TIMEOUT)
Ivan Frade's avatar
Ivan Frade committed
98

99 100
        results = get_tracker_extract_jsonld_output(filename, mimetype)
        self.assertIn("testTag", results["nao:hasTag"])
Ivan Frade's avatar
Ivan Frade committed
101 102

    # JPEG test
103 104

    def test_001_jpeg_title(self):
105
        #FILENAME = "test-writeback-monitored/writeback-test-1.jpeg"
106
        self.__writeback_test(self.get_test_filename_jpeg(), "image/jpeg", "nie:title")
Ivan Frade's avatar
Ivan Frade committed
107

108
    def test_002_jpeg_description(self):
109
        #FILENAME = "test-writeback-monitored/writeback-test-1.jpeg"
110
        self.__writeback_test(self.get_test_filename_jpeg(), "image/jpeg", "nie:description")
Ivan Frade's avatar
Ivan Frade committed
111

112
    # def test_003_jpeg_keyword (self):
113 114 115
    #    #FILENAME = "test-writeback-monitored/writeback-test-1.jpeg"
    #    self.__writeback_test (self.get_test_filename_jpeg (), "image/jpeg",
    #                           "nie:keyword", "nao:hasTag")
Ivan Frade's avatar
Ivan Frade committed
116

117
    # def test_004_jpeg_hasTag (self):
118 119
    #    #FILENAME = "test-writeback-monitored/writeback-test-1.jpeg"
    #    self.__writeback_hasTag_test (self.get_test_filename_jpeg (), "image/jpeg")
Ivan Frade's avatar
Ivan Frade committed
120 121

    # TIFF tests
122 123

    def test_011_tiff_title(self):
124
        #FILANAME = "test-writeback-monitored/writeback-test-2.tif"
125
        self.__writeback_test(self.get_test_filename_tiff(), "image/tiff", "nie:title")
Ivan Frade's avatar
Ivan Frade committed
126

127
    def test_012_tiff_description(self):
Ivan Frade's avatar
Ivan Frade committed
128
        FILENAME = "test-writeback-monitored/writeback-test-2.tif"
129 130 131
        self.__writeback_test(self.get_test_filename_tiff(), "image/tiff", "nie:description")

    # def test_013_tiff_keyword (self):
132 133 134 135
    #    FILENAME = "test-writeback-monitored/writeback-test-2.tif"
    #    self.__writeback_test (self.get_test_filename_tiff (), "image/tiff",
    #                           "nie:keyword", "nao:hasTag")

136
    # def test_014_tiff_hasTag (self):
137 138
    #    FILENAME = "test-writeback-monitored/writeback-test-2.tif"
    #    self.__writeback_hasTag_test (self.get_test_filename_tiff (), "image/tiff")
Ivan Frade's avatar
Ivan Frade committed
139 140

    # PNG tests
141 142

    def test_021_png_title(self):
Ivan Frade's avatar
Ivan Frade committed
143
        FILENAME = "test-writeback-monitored/writeback-test-4.png"
144
        self.__writeback_test(self.get_test_filename_png(), "image/png", "nie:title")
Ivan Frade's avatar
Ivan Frade committed
145

146
    def test_022_png_description(self):
Ivan Frade's avatar
Ivan Frade committed
147
        FILENAME = "test-writeback-monitored/writeback-test-4.png"
148
        self.__writeback_test(self.get_test_filename_png(), "image/png", "nie:description")
149

150
    # def test_023_png_keyword (self):
151
    #    FILENAME = "test-writeback-monitored/writeback-test-4.png"
152
    #    self.__writeback_test (self.get_test_filename_png (), "image/png", "nie:keyword", "nao:hasTag:prefLabel")
153

154
    # def test_024_png_hasTag (self):
155
    #    FILENAME = "test-writeback-monitored/writeback-test-4.png"
156
    #    self.__writeback_hasTag_test (self.get_test_filename_png (), "image/png")
Ivan Frade's avatar
Ivan Frade committed
157

158

Ivan Frade's avatar
Ivan Frade committed
159
if __name__ == "__main__":
160 161 162
    print("FIXME: This test is skipped as it currently fails. See: https://gitlab.gnome.org/GNOME/tracker-miners/issues/55")
    sys.exit(77)

163
    ut.main(failfast=True)