def __writeback_hasTag_test(self, filename, mimetype): SPARQL_TMPL = """ INSERT { <test://writeback-hasTag-test/1> a nao:Tag ; nao:prefLabel "testTag" . ?u a rdfs:Resource; nao:hasTag <test://writeback-hasTag-test/1> . } WHERE { ?u nie:url '%s' . } """ path = self.prepare_test_image(self.datadir_path(filename)) initial_mtime = path.stat().st_mtime self.tracker.update(SPARQL_TMPL % (filename)) self.wait_for_file_change(path, initial_mtime) results = fixtures.get_tracker_extract_output(self.extra_env, filename, mime_type=mimetype, output_format='json-ld') self.assertIn("testTag", results["nao:hasTag"])
def __writeback_test(self, filename, mimetype, prop, expectedKey=None): """ 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. """ path = self.prepare_test_image(self.datadir_path(filename)) initial_mtime = path.stat().st_mtime TEST_VALUE = prop.replace(":", "") + "test" self.writeback_data({ 'rdf:type': GLib.Variant('s', 'nfo:Image'), 'nie:isStoredAs': GLib.Variant('s', path.as_uri()), prop: GLib.Variant('s', TEST_VALUE), }) log.debug("Waiting for change on %s", path) self.wait_for_file_change(path, initial_mtime) log.debug("Got the change") results = fixtures.get_tracker_extract_output({}, path, mime_type=mimetype, output_format='json-ld') keyDict = expectedKey or prop self.assertIn(TEST_VALUE, results[keyDict])
def generic_test_extraction(self): abs_description = os.path.abspath(self.descfile) # Filename contains the file to extract, in a relative path to the description file desc_root, desc_file = os.path.split(abs_description) filename_to_extract = self.spec['test']['Filename'] self.file_to_extract = os.path.join(desc_root, filename_to_extract) tmpdir = tempfile.mkdtemp(prefix='tracker-extract-test-') try: extra_env = cfg.test_environment(tmpdir) jsonld = fixtures.get_tracker_extract_output( extra_env, self.file_to_extract, output_format='json-ld') self.__assert_extraction_ok(jsonld) sparql = fixtures.get_tracker_extract_output( extra_env, self.file_to_extract, output_format='sparql') self.validate_sparql_update(sparql) finally: shutil.rmtree(tmpdir, ignore_errors=True)
def test_external_cue_sheet(self): with tempfile.TemporaryDirectory() as tmpdir: datadir = pathlib.Path(__file__).parent.joinpath('test-extraction-data') shutil.copy(datadir.joinpath('audio', 'cuesheet-test.cue'), tmpdir) audio_path = pathlib.Path(tmpdir).joinpath('cuesheet-test.flac') datagenerator.create_test_flac(audio_path, duration=6*60) result = fixtures.get_tracker_extract_output( cfg.test_environment(tmpdir), audio_path, output_format='json-ld') self.assert_extract_result_matches_spec( self.spec(audio_path), result, audio_path, __file__)
def _writeback_test(self, path): prop = 'nie:title' path = self.prepare_test_audio(self.datadir_path(path)) initial_mtime = path.stat().st_mtime TEST_VALUE = prop.replace(":", "") + "test" self.writeback_data({ 'rdf:type': GLib.Variant('s', 'nfo:Audio'), 'nie:isStoredAs': GLib.Variant('s', path.as_uri()), 'nie:title': GLib.Variant('s', TEST_VALUE), }) self.wait_for_file_change(path, initial_mtime) results = fixtures.get_tracker_extract_output({}, path, output_format='json-ld') self.assertIn(TEST_VALUE, results[prop])
def test_01_NB217627_content_created_date(self): """ NB#217627 - Order if results is different when an image is marked as favorite. """ jpeg_path = self.prepare_test_image( self.datadir_path('writeback-test-1.jpeg')) tif_path = self.prepare_test_image( self.datadir_path('writeback-test-2.tif')) png_path = self.prepare_test_image( self.datadir_path('writeback-test-4.png')) query_images = """ SELECT nie:url(?u) ?contentCreated WHERE { ?u a nfo:Visual ; nfo:fileLastModified ?contentCreated } ORDER BY ?contentCreated """ results = self.tracker.query(query_images) self.assertEqual(len(results), 3, results) log.debug( "Waiting 2 seconds to ensure there is a noticiable difference in the timestamp" ) time.sleep(2) initial_mtime = jpeg_path.stat().st_mtime # This triggers the writeback mark_as_favorite = """ INSERT { ?u a rdfs:Resource ; nao:hasTag nao:predefined-tag-favorite . } WHERE { ?u nie:url <%s> . } """ % jpeg_path.as_uri() self.tracker.update(mark_as_favorite) log.debug("Setting favorite in <%s>", jpeg_path.as_uri()) self.wait_for_file_change(jpeg_path, initial_mtime) # Check the value is written in the file metadata = fixtures.get_tracker_extract_output(self.extra_env, jpeg_path, output_format='json-ld') tags = metadata.get('nao:hasTag', []) tag_names = [tag['nao:prefLabel'] for tag in tags] self.assertIn(self.favorite, tag_names, "Tag hasn't been written in the file") # Now check the modification date of the files and it should be the same :) new_results = self.tracker.query(query_images) # for (uri, date) in new_results: ## print "Checking dates of <%s>" % uri ## previous_date = convenience_dict[uri] ## print "Before: %s \nAfter : %s" % (previous_date, date) ## self.assertEquals (date, previous_date, "File <%s> has change its contentCreated date!" % uri) # Indeed the order of the results should be the same for i in range(0, len(results)): self.assertEqual(results[i][0], new_results[i][0], "Order of the files is different") self.assertEqual(results[i][1], new_results[i][1], "Date has change in file <%s>" % results[i][0])