def testFsckWithInterleavingPlaylogs(self): from klangbecken.cli import main, playlog_cmd # log one one track play track1 = os.listdir(self.jingles_dir)[0] track2 = os.listdir(self.jingles_dir)[1] playlog_cmd(self.tempdir, os.path.join("jingles", track1)) # back up index.json cache shutil.copy( os.path.join(self.tempdir, "index.json"), os.path.join(self.tempdir, "index.json.bak"), ) # log two mor track plays playlog_cmd(self.tempdir, os.path.join("jingles", track1)) playlog_cmd(self.tempdir, os.path.join("jingles", track2)) # restore index.json cache shutil.copy( os.path.join(self.tempdir, "index.json.bak"), os.path.join(self.tempdir, "index.json"), ) argv, sys.argv = sys.argv, ["", "fsck", "-d", self.tempdir] try: # correct invocation (should not raise error) with self.assertRaises(SystemExit) as cm: with capture(main) as (out, err, ret): self.assertEqual(err.strip(), "") self.assertEqual(cm.exception.code, 0) finally: sys.arv = argv
def testFsckWithTooManyInterleavingPlaylogs(self): from klangbecken.cli import main, playlog_cmd # log one one track play track1 = os.listdir(self.jingles_dir)[0] track2 = os.listdir(self.jingles_dir)[1] track3 = os.listdir(self.jingles_dir)[2] track4 = os.listdir(self.jingles_dir)[3] playlog_cmd(self.tempdir, os.path.join("jingles", track1)) # back up index.json cache shutil.copy( os.path.join(self.tempdir, "index.json"), os.path.join(self.tempdir, "index.json.bak"), ) # log two mor track plays playlog_cmd(self.tempdir, os.path.join("jingles", track1)) playlog_cmd(self.tempdir, os.path.join("jingles", track2)) playlog_cmd(self.tempdir, os.path.join("jingles", track3)) playlog_cmd(self.tempdir, os.path.join("jingles", track4)) # restore index.json cache shutil.copy( os.path.join(self.tempdir, "index.json.bak"), os.path.join(self.tempdir, "index.json"), ) argv, sys.argv = sys.argv, ["", "fsck", "-d", self.tempdir] try: with self.assertRaises(SystemExit) as cm: with capture(main) as (out, err, ret): self.assertIn("ERROR", err) self.assertIn("last_play", err) self.assertEqual(cm.exception.code, 1) finally: sys.arv = argv
def testPlayLog(self): from klangbecken.cli import main, playlog_cmd now = datetime.datetime(2018, 4, 28).astimezone() filename = os.listdir(os.path.join(self.data_dir, "jingles"))[0] path = os.path.join(self.data_dir, "jingles", filename) # Make sure that the external command does not get interpreted by a shell external_command = self.playlog_script + " {id} >> {artist} | {title}" with mock.patch("klangbecken.cli.datetime") as dt: with mock.patch("klangbecken.cli.EXTERNAL_PLAY_LOGGER", external_command): dt.datetime.now = mock.Mock(return_value=now) # First call playlog_cmd(self.data_dir, path) with open(os.path.join(self.data_dir, "index.json")) as f: cache_data = json.load(f) entry = cache_data[filename.split(".")[0]] self.assertEqual(entry["last_play"], now.isoformat()) self.assertEqual(entry["play_count"], 1) mutagenFile = File(path, easy=True) self.assertEqual(mutagenFile["last_play"][0], now.isoformat()) with open(os.path.join(self.data_dir, "log", "2018-04.csv")) as f: reader = csv.DictReader(f) entries = list(reader) entry = entries[0] self.assertEqual(len(entries), 1) self.assertEqual(entry["last_play"], now.isoformat()) self.assertEqual(entry["play_count"], "1") with open(self.out_file) as f: contents = f.read().strip().split("\n") self.assertEqual(contents[0], filename.split(".")[0]) self.assertEqual(contents[1], "'Padded' `Artist`") self.assertEqual(contents[2], 'Padded | "Title"') now = now + datetime.timedelta(days=1) with mock.patch("klangbecken.cli.datetime") as dt: with mock.patch("sys.argv", ["", "playlog", "-d", self.data_dir, path]): dt.datetime.now = mock.Mock(return_value=now) # Second call with no external play logger, now via the main function main() with open(os.path.join(self.data_dir, "index.json")) as f: cache_data = json.load(f) entry = cache_data[filename.split(".")[0]] self.assertEqual(entry["last_play"], now.isoformat()) self.assertEqual(entry["play_count"], 2) with open(os.path.join(self.data_dir, "log", "2018-04.csv")) as f: reader = csv.DictReader(f) self.assertEqual(len(list(reader)), 2)