From b9d6928278802408f177ea247d014af7ac690ea0 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 3 Dec 2011 17:18:51 -0800 Subject: [PATCH] consistency policy for DB mtimes (#227) --- beets/library.py | 5 ++++- test/test_db.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/beets/library.py b/beets/library.py index baf36c7c3..84ce3e154 100644 --- a/beets/library.py +++ b/beets/library.py @@ -131,6 +131,7 @@ class Item(object): 'album_id': None, }) i.read(path) + i.mtime = i.current_mtime() # Initial mtime. return i def _fill_record(self, values): @@ -177,10 +178,12 @@ class Item(object): value = str(value) if key in ITEM_KEYS: + # If the value changed, mark the field as dirty. if (not (key in self.record)) or (self.record[key] != value): - # don't dirty if value unchanged self.record[key] = value self.dirty[key] = True + if key in ITEM_KEYS_WRITABLE: + self.mtime = 0 # Reset mtime on dirty. else: super(Item, self).__setattr__(key, value) diff --git a/test/test_db.py b/test/test_db.py index 3d152d765..c6dbb2024 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -20,6 +20,7 @@ import os import sqlite3 import ntpath import posixpath +import shutil import _common from _common import item @@ -653,6 +654,38 @@ class PathStringTest(unittest.TestCase): alb = self.lib.get_album(alb.id) self.assert_(isinstance(alb.artpath, str)) +class MtimeTest(unittest.TestCase): + def setUp(self): + self.ipath = os.path.join(_common.RSRC, 'testfile.mp3') + shutil.copy(os.path.join(_common.RSRC, 'full.mp3'), self.ipath) + self.i = beets.library.Item.from_path(self.ipath) + self.lib = beets.library.Library(':memory:') + self.lib.add(self.i) + + def tearDown(self): + if os.path.exists(self.ipath): + os.remove(self.ipath) + + def _mtime(self): + return os.path.getmtime(self.ipath) + + def test_mtime_initially_up_to_date(self): + self.assertGreaterEqual(self.i.mtime, self._mtime()) + + def test_mtime_reset_on_db_modify(self): + self.i.title = 'something else' + self.assertLess(self.i.mtime, self._mtime()) + + def test_mtime_up_to_date_after_write(self): + self.i.title = 'something else' + self.i.write() + self.assertGreaterEqual(self.i.mtime, self._mtime()) + + def test_mtime_up_to_date_after_read(self): + self.i.title = 'something else' + self.i.read() + self.assertGreaterEqual(self.i.mtime, self._mtime()) + def suite(): return unittest.TestLoader().loadTestsFromName(__name__)