# -*- coding: utf-8 -*-################################################################################# Copyright (C) 2012 Travis Shirk <travis@pobox.com>## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program 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 General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#################################################################################importsysifsys.version_info[:2]==(2,6):importunittest2asunittestelse:importunittestimportos,shutilfromnose.toolsimport*importeyed3fromeyed3importmain,id3,corefrom.importDATA_D,RedirectStdStreamsfrom.compatimport*deftestPluginOption():forargin["--help","-h"]:# When help is requested and no plugin is specified, use defaultwithRedirectStdStreams()asout:try:args,_,config=main.parseCommandLine([arg])exceptSystemExitasex:assert_equal(ex.code,0)out.stdout.seek(0)sout=out.stdout.read()assert_not_equal(sout.find("Plugin options:\n Classic eyeD3"),-1)# When help is requested and all default plugin names are specifiedforplugin_namein["classic"]:forargsin[["--plugin=%s"%plugin_name,"--help"]]:withRedirectStdStreams()asout:try:args,_,config=main.parseCommandLine(args)exceptSystemExitasex:assert_equal(ex.code,0)out.stdout.seek(0)sout=out.stdout.read()assert_not_equal(sout.find("Plugin options:\n Classic eyeD3"),-1)@unittest.skipIf(notos.path.exists(DATA_D),"test requires data files")deftestReadEmptyMp3():withRedirectStdStreams()asout:args,_,config=main.parseCommandLine([os.path.join(DATA_D,"test.mp3")])retval=main.main(args,config)assert_equal(retval,0)assert_not_equal(out.stderr.read().find("No ID3 v1.x/v2.x tag found"),-1)classTestDefaultPlugin(unittest.TestCase):def__init__(self,name):super(TestDefaultPlugin,self).__init__(name)self.orig_test_file="%s/test.mp3"%DATA_Dself.test_file="/tmp/test.mp3"@unittest.skipIf(notos.path.exists(DATA_D),"test requires data files")defsetUp(self):shutil.copy(self.orig_test_file,self.test_file)deftearDown(self):# TODO: could remove the tag and compare audio file to originalos.remove(self.test_file)def_addVersionOpt(self,version,opts):ifversion==id3.ID3_DEFAULT_VERSION:returnifversion[0]==1:opts.append("--to-v1.1")elifversion[:2]==(2,3):opts.append("--to-v2.3")elifversion[:2]==(2,4):opts.append("--to-v2.4")else:assert_false("Unhandled version")deftestNewTagArtist(self,version=id3.ID3_DEFAULT_VERSION):foroptsin[["-a","The Cramps",self.test_file],["--artist=The Cramps",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.artist,u"The Cramps")deftestNewTagAlbum(self,version=id3.ID3_DEFAULT_VERSION):foroptsin[["-A","Psychedelic Jungle",self.test_file],["--album=Psychedelic Jungle",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.album,u"Psychedelic Jungle")deftestNewTagTitle(self,version=id3.ID3_DEFAULT_VERSION):foroptsin[["-t","Green Door",self.test_file],["--title=Green Door",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.title,u"Green Door")deftestNewTagTrackNum(self,version=id3.ID3_DEFAULT_VERSION):foroptsin[["-n","14",self.test_file],["--track=14",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.track_num[0],14)deftestNewTagTrackNumInvalid(self):foroptsin[["-n","abc",self.test_file],["--track=-14",self.test_file]]:withRedirectStdStreams()asout:try:args,_,config=main.parseCommandLine(opts)exceptSystemExitasex:assert_not_equal(ex.code,0)else:assert_false("Should not have gotten here")deftestNewTagTrackTotal(self,version=id3.ID3_DEFAULT_VERSION):ifversion[0]==1:# No support for this in v1.xreturnforoptsin[["-N","14",self.test_file],["--track-total=14",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.track_num[1],14)deftestNewTagGenre(self,version=id3.ID3_DEFAULT_VERSION):foroptsin[["-G","Rock",self.test_file],["--genre=Rock",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.genre.name,"Rock")assert_equal(af.tag.genre.id,17)deftestNewTagYear(self,version=id3.ID3_DEFAULT_VERSION):foroptsin[["-Y","1981",self.test_file],["--release-year=1981",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)ifversion==id3.ID3_V2_3:assert_equal(af.tag.original_release_date.year,1981)else:assert_equal(af.tag.release_date.year,1981)deftestNewTagReleaseDate(self,version=id3.ID3_DEFAULT_VERSION):fordatein["1981","1981-03-06","1981-03"]:orig_date=core.Date.parse(date)foroptsin[["--release-date=%s"%str(date),self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.release_date,orig_date)deftestNewTagOrigRelease(self,version=id3.ID3_DEFAULT_VERSION):foroptsin[["--orig-release-date=1981",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.original_release_date.year,1981)deftestNewTagRecordingDate(self,version=id3.ID3_DEFAULT_VERSION):foroptsin[["--recording-date=1993-10-30",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.recording_date.year,1993)assert_equal(af.tag.recording_date.month,10)assert_equal(af.tag.recording_date.day,30)deftestNewTagEncodingDate(self,version=id3.ID3_DEFAULT_VERSION):foroptsin[["--encoding-date=2012-10-23T20:22",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.encoding_date.year,2012)assert_equal(af.tag.encoding_date.month,10)assert_equal(af.tag.encoding_date.day,23)assert_equal(af.tag.encoding_date.hour,20)assert_equal(af.tag.encoding_date.minute,22)deftestNewTagTaggingDate(self,version=id3.ID3_DEFAULT_VERSION):foroptsin[["--tagging-date=2012-10-23T20:22",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.tagging_date.year,2012)assert_equal(af.tag.tagging_date.month,10)assert_equal(af.tag.tagging_date.day,23)assert_equal(af.tag.tagging_date.hour,20)assert_equal(af.tag.tagging_date.minute,22)deftestNewTagPlayCount(self):forexpected,optsin[(0,["--play-count=0",self.test_file]),(1,["--play-count=+1",self.test_file]),(6,["--play-count=+5",self.test_file]),(7,["--play-count=7",self.test_file]),(10000,["--play-count=10000",self.test_file]),]:withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.play_count,expected)deftestNewTagPlayCountInvalid(self):forexpected,optsin[(0,["--play-count=",self.test_file]),(0,["--play-count=-24",self.test_file]),(0,["--play-count=+",self.test_file]),(0,["--play-count=abc",self.test_file]),(0,["--play-count=False",self.test_file]),]:withRedirectStdStreams()asout:try:args,_,config=main.parseCommandLine(opts)exceptSystemExitasex:assert_not_equal(ex.code,0)else:assert_false("Should not have gotten here")deftestNewTagBpm(self):forexpected,optsin[(1,["--bpm=1",self.test_file]),(180,["--bpm=180",self.test_file]),(117,["--bpm","116.7",self.test_file]),(116,["--bpm","116.4",self.test_file]),]:withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.bpm,expected)deftestNewTagBpmInvalid(self):forexpected,optsin[(0,["--bpm=",self.test_file]),(0,["--bpm=-24",self.test_file]),(0,["--bpm=+",self.test_file]),(0,["--bpm=abc",self.test_file]),(0,["--bpm","=180",self.test_file]),]:withRedirectStdStreams()asout:try:args,_,config=main.parseCommandLine(opts)exceptSystemExitasex:assert_not_equal(ex.code,0)else:assert_false("Should not have gotten here")deftestNewTagPublisher(self):forexpected,optsin[("SST",["--publisher","SST",self.test_file]),("Dischord",["--publisher=Dischord",self.test_file]),]:withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.publisher,expected)# TODO:# --unique-file-id# --add-comment, --remove-comment, --remove-all-comments# --add-lyrics, --remove-lyrics, --remove-all-lyrics# --text-frame, --user-text-frame# --url-frame, --user-user-frame# --add-image, --remove-image, --remove-all-images, --write-images# etc.# --rename, --force-update, -1, -2, --excludedeftestNewTagSimpleComment(self,version=id3.ID3_DEFAULT_VERSION):ifversion[0]==1:# No support for this in v1.xreturnforoptsin[["-c","Starlette",self.test_file],["--comment=Starlette",self.test_file]]:self._addVersionOpt(version,opts)withRedirectStdStreams()asout:args,_,config=main.parseCommandLine(opts)retval=main.main(args,config)assert_equal(retval,0)af=eyed3.load(self.test_file)assert_is_not_none(af)assert_is_not_none(af.tag)assert_equal(af.tag.comments[0].text,"Starlette")assert_equal(af.tag.comments[0].description,"")deftestNewTagAll(self,version=id3.ID3_DEFAULT_VERSION):self.testNewTagArtist(version)self.testNewTagAlbum(version)self.testNewTagTitle(version)self.testNewTagTrackNum(version)self.testNewTagTrackTotal(version)self.testNewTagGenre(version)self.testNewTagYear(version)self.testNewTagSimpleComment(version)af=eyed3.load(self.test_file)assert_equal(af.tag.artist,u"The Cramps")assert_equal(af.tag.album,u"Psychedelic Jungle")assert_equal(af.tag.title,u"Green Door")assert_equal(af.tag.track_num,(14,14ifversion[0]!=1elseNone))assert_equal((af.tag.genre.name,af.tag.genre.id),("Rock",17))ifversion==id3.ID3_V2_3:assert_equal(af.tag.original_release_date.year,1981)else:assert_equal(af.tag.release_date.year,1981)ifversion[0]!=1:assert_equal(af.tag.comments[0].text,"Starlette")assert_equal(af.tag.comments[0].description,"")assert_equal(af.tag.version,version)deftestNewTagAllVersion1(self):self.testNewTagAll(version=id3.ID3_V1_1)deftestNewTagAllVersion2_3(self):self.testNewTagAll(version=id3.ID3_V2_3)deftestNewTagAllVersion2_4(self):self.testNewTagAll(version=id3.ID3_V2_4)