def test(self): lib = SongFileLibrary() with temp_filename() as song_fn: song = AudioFile({"~filename": song_fn}) song.sanitize() lib.add([song]) with temp_filename() as xml_fn: with open(xml_fn, "wb") as h: x = get_example_xml(song("~filename"), 1, 1371802107) h.write(x) handler = self.mod.RBDBContentHandler(lib) xml.sax.parse(xml_fn, handler) self.assertEqual(song("~#rating"), 0.2) self.assertEqual(song("~#lastplayed"), 1371802107) self.assertEqual(song("~#playcount"), 1) with open(xml_fn, "wb") as h: x = get_example_xml(song("~filename"), 2, 1371802107 - 1) h.write(x) handler = self.mod.RBDBContentHandler(lib) xml.sax.parse(xml_fn, handler) self.assertEqual(song("~#rating"), 0.4) self.assertEqual(song("~#lastplayed"), 1371802107)
def test_watched_moving_dir(self): temp_dir = self.temp_path / "old" temp_dir.mkdir(exist_ok=False) sleep(0.2) run_gtk_loop() assert temp_dir in self.library._monitors with temp_filename(dir=temp_dir, suffix=".flac", as_path=True) as path: shutil.copy(Path(get_data_path("silence-44-s.flac")), path) sleep(0.2) assert path.exists() run_gtk_loop() assert str(path) in self.library, f"New path {path!s} didn't get added" assert len(self.added) == 1 self.added.clear() assert self.library # Now move the directory... new_dir = path.parent.parent / "new" temp_dir.rename(new_dir) assert new_dir.is_dir(), "test should have moved to new dir" sleep(0.2) run_gtk_loop() new_path = new_dir / path.name assert new_path.is_file() msg = f"New path {new_path} not in library [{self.fns}]. Did move_root run?" assert str(new_path) in self.library, msg assert not self.removed, "A file was removed"
def test_glib_err_read(self): result = [] with temp_filename() as f, mock.patch.object(FIFO, "open") as m_open: fifo = FIFO(f, result.append) source = mock.Mock() assert fifo._process(source, GLib.IO_ERR) is False assert m_open.mock_calls == [mock.call(ignore_lock=True)] assert result == []
def test_successful_read(self): result = [] with temp_filename() as f, mock.patch.object(FIFO, "open") as m_open: fifo = FIFO(f, result.append) source = mock.Mock() source.read.return_value = b"foo" assert fifo._process(source, GLib.IO_IN) is True assert m_open.mock_calls == [] assert result == [b"foo"]
def test_watched_adding(self): with temp_filename(dir=self.temp_path, suffix=".mp3", as_path=True) as path: shutil.copy(Path(get_data_path("silence-44-s.mp3")), path) assert self.temp_path in path.parents, "Copied test file incorrectly" watch_dirs = self.library._monitors.keys() assert path.parent in watch_dirs, "Not monitoring directory of new file" run_gtk_loop() assert self.library, f"Nothing in library despite watches on {watch_dirs}" assert str(path) in self.library, (f"{path!s} should have been added to " f"library [{self.fns}]") assert str(path) in {af("~filename") for af in self.added}
def test_creation_destruction(self): def cb(bs, _): print_d(bs) with temp_filename() as fn: fifo = FIFO(fn, cb) self.failIf(fifo_exists(fifo._path)) fifo.open() self.failUnless(fifo_exists(fifo._path)) # Should *not* error if file is gone fifo.destroy()
def test_watched_adding_removing(self): with temp_filename(dir=self.temp_path, suffix=".mp3", as_path=True) as path: shutil.copy(Path(get_data_path("silence-44-s.mp3")), path) sleep(0.5) run_gtk_loop() assert path.exists() assert str(path) in self.library, f"{path} should be in [{self.fns}] now" assert not path.exists(), "Failed to delete test file" sleep(0.5) # Deletion now run_gtk_loop() assert self.removed, "Nothing was automatically removed" assert self.added, "Nothing was automatically added" assert {Path(af("~filename")) for af in self.added} == {path} assert {Path(af("~filename")) for af in self.removed} == {path} assert str(path) not in self.library, f"{path} shouldn't be in the library now"
def test_move(self, temp_dir: Path): monitor = BasicMonitor(temp_dir) with temp_filename(dir=temp_dir, suffix=".txt", as_path=True) as path: path.write_text("test\n") sleep(SLEEP_SECS) run_gtk_loop() assert monitor.changed, "No events after creation" monitor.changed.clear() new_name = f"new-{time()}.txt" path.rename(path.parent / new_name) sleep(SLEEP_SECS) run_gtk_loop() assert monitor.changed assert monitor.event_types >= {Event.RENAMED }, f"Got {monitor.changed}"
def test_watched_moving_song(self): with temp_filename(dir=self.temp_path, suffix=".flac", as_path=True) as path: shutil.copy(Path(get_data_path("silence-44-s.flac")), path) sleep(0.2) assert path.exists() run_gtk_loop() assert str(path) in self.library, f"New path {path!s} didn't get added" assert len(self.added) == 1 assert self.added[0]("~basename") == path.name self.added.clear() # Now move it... new_path = path.parent / f"moved-{path.name}" path.rename(new_path) sleep(0.2) assert not path.exists(), "test should have removed old file" assert new_path.exists(), "test should have renamed file" print_d(f"New test file at {new_path}") run_gtk_loop() p = normalize_path(str(new_path), True) assert p in self.library, f"New path {new_path} not in library [{self.fns}]" msg = "Inconsistent events: should be (added and removed) or nothing at all" assert not (bool(self.added) ^ bool(self.removed)), msg
def test(self): lib = SongFileLibrary() with temp_filename() as song_fn: song = AudioFile({"~filename": song_fn}) song.sanitize() lib.add([song]) # test recovery of basic song data = {"path": song("~filename"), "rating": 1, "playcount": 1, "skipcount": 2, "lastplayed": 1371802107, "added": 1260691996} db = get_example_db(data["path"], data["rating"], data["playcount"], data["skipcount"], data["lastplayed"], data["added"]) importer = self.mod.BansheeDBImporter(lib) importer.read(db) count = importer.finish() db.close() self.assertEqual(song("~#rating"), data["rating"] / 5.0) self.assertEqual(song("~#playcount"), data["playcount"]) self.assertEqual(song("~#skipcount"), data["skipcount"]) self.assertEqual(song("~#lastplayed"), data["lastplayed"]) self.assertEqual(song("~#added"), data["added"]) self.assertEqual(count, 1) # test recovery of different version of same song data_mod = {"path": song("~filename"), "rating": 2, "playcount": 4, "skipcount": 1, "lastplayed": data["lastplayed"] - 1, "added": data["added"] + 1} db = get_example_db(data_mod["path"], data_mod["rating"], data_mod["playcount"], data_mod["skipcount"], data_mod["lastplayed"], data_mod["added"]) importer = self.mod.BansheeDBImporter(lib) importer.read(db) count = importer.finish() db.close() self.assertEqual(song("~#rating"), data_mod["rating"] / 5.0) self.assertEqual(song("~#playcount"), data_mod["playcount"]) self.assertEqual(song("~#skipcount"), data_mod["skipcount"]) self.assertEqual(song("~#lastplayed"), data["lastplayed"]) self.assertEqual(song("~#added"), data["added"]) self.assertEqual(count, 1) # test that no recovery is performed when data is identical db = get_example_db(data_mod["path"], data_mod["rating"], data_mod["playcount"], data_mod["skipcount"], data_mod["lastplayed"], data_mod["added"]) importer = self.mod.BansheeDBImporter(lib) importer.read(db) count = importer.finish() db.close() self.assertEqual(count, 0)