diff --git a/tests/all_tests.py b/tests/all_tests.py index 64b39910e4..fc0eac66c0 100644 --- a/tests/all_tests.py +++ b/tests/all_tests.py @@ -21,7 +21,7 @@ import glob import unittest - test_file_strings = [ x for x in glob.glob('*_tests.py') if x != __file__] + test_file_strings = [ x for x in glob.glob('*_tests.py') if not x in __file__] module_strings = [file_string[0:len(file_string) - 3] for file_string in test_file_strings] suites = [unittest.defaultTestLoader.loadTestsFromName(file_string) for file_string in module_strings] testSuite = unittest.TestSuite(suites) diff --git a/tests/db_tests.py b/tests/db_tests.py index b96b682927..55055fd525 100644 --- a/tests/db_tests.py +++ b/tests/db_tests.py @@ -20,11 +20,6 @@ import unittest import test_lib as test -import sys, os.path -sys.path.append(os.path.abspath('..')) -sys.path.append(os.path.abspath('../lib')) - - class DBBasicTests(test.SickbeardTestDBCase): diff --git a/tests/name_parser_tests.py b/tests/name_parser_tests.py index e0fbef52b9..81b6cc7557 100644 --- a/tests/name_parser_tests.py +++ b/tests/name_parser_tests.py @@ -10,7 +10,7 @@ import sickbeard sickbeard.SYS_ENCODING = 'UTF-8' -DEBUG = VERBOSE = True +DEBUG = VERBOSE = False simple_test_cases = { 'standard': { diff --git a/tests/pp_tests.py b/tests/pp_tests.py index a0b2b6a157..78df8e6134 100644 --- a/tests/pp_tests.py +++ b/tests/pp_tests.py @@ -23,14 +23,12 @@ import test_lib as test import sys, os.path -sys.path.append(os.path.abspath('..')) -sys.path.append(os.path.abspath('../lib')) - from sickbeard.postProcessor import PostProcessor import sickbeard from sickbeard.tv import TVEpisode, TVShow + class PPInitTests(unittest.TestCase): def setUp(self): @@ -42,6 +40,7 @@ def test_init_file_name(self): def test_init_folder_name(self): self.assertEqual(self.pp.folder_name, test.SHOWNAME) + class PPPrivateTests(test.SickbeardTestDBCase): diff --git a/tests/scene_helpers_tests.py b/tests/scene_helpers_tests.py index 36206d646b..fb7e7d200a 100644 --- a/tests/scene_helpers_tests.py +++ b/tests/scene_helpers_tests.py @@ -1,4 +1,5 @@ import unittest +import test_lib as test import sys, os.path sys.path.append(os.path.abspath('..')) @@ -8,26 +9,25 @@ import sickbeard from sickbeard import db from sickbeard.databases import cache_db +from sickbeard.tv import TVShow as Show -class Show: - def __init__(self, name, tvdbid, tvrname): - self.name = name - self.tvdbid = tvdbid - self.tvrname = tvrname -class SceneTests(unittest.TestCase): - +class SceneTests(test.SickbeardTestDBCase): + def _test_sceneToNormalShowNames(self, name, expected): result = show_name_helpers.sceneToNormalShowNames(name) self.assertTrue(len(set(expected).intersection(set(result))) == len(expected)) - dot_result = show_name_helpers.sceneToNormalShowNames(name.replace(' ','.')) - dot_expected = [x.replace(' ','.') for x in expected] + dot_result = show_name_helpers.sceneToNormalShowNames(name.replace(' ', '.')) + dot_expected = [x.replace(' ', '.') for x in expected] self.assertTrue(len(set(dot_expected).intersection(set(dot_result))) == len(dot_expected)) - + def _test_allPossibleShowNames(self, name, tvdbid=0, tvrname=None, expected=[]): - - result = show_name_helpers.allPossibleShowNames(Show(name, tvdbid, tvrname)) + s = Show(tvdbid) + s.name = name + s.tvrname = tvrname + + result = show_name_helpers.allPossibleShowNames(s) self.assertTrue(len(set(expected).intersection(set(result))) == len(expected)) def _test_filterBadReleases(self, name, expected): @@ -38,14 +38,20 @@ def _test_isGoodName(self, name, show): self.assertTrue(show_name_helpers.isGoodResult(name, show)) def test_isGoodName(self): - self._test_isGoodName('Show.Name.S01E02.Test-Test', Show('Show/Name', 0, '')) - self._test_isGoodName('Show.Name.S01E02.Test-Test', Show('Show. Name', 0, '')) - self._test_isGoodName('Show.Name.S01E02.Test-Test', Show('Show- Name', 0, '')) - self._test_isGoodName('Show.Name.Part.IV.Test-Test', Show('Show Name', 0, '')) - self._test_isGoodName('Show.Name.1x02.Test-Test', Show('Show Name', 0, '')) - self._test_isGoodName('Show.Name.S01.Test-Test', Show('Show Name', 0, '')) - self._test_isGoodName('Show.Name.E02.Test-Test', Show('Show: Name', 0, '')) - self._test_isGoodName('Show Name Season 2 Test', Show('Show: Name', 0, '')) + listOfcases = [('Show.Name.S01E02.Test-Test', 'Show/Name'), + ('Show.Name.S01E02.Test-Test', 'Show. Name'), + ('Show.Name.S01E02.Test-Test', 'Show- Name'), + ('Show.Name.Part.IV.Test-Test', 'Show Name'), + ('Show.Name.S01.Test-Test', 'Show Name'), + ('Show.Name.E02.Test-Test', 'Show: Name'), + ('Show Name Season 2 Test', 'Show: Name'), + ] + + for testCase in listOfcases: + scene_name, show_name = testCase + s = Show(0) + s.name = show_name + self._test_isGoodName(scene_name, s) def test_sceneToNormalShowNames(self): self._test_sceneToNormalShowNames('Show Name 2010', ['Show Name 2010', 'Show Name (2010)']) @@ -56,7 +62,7 @@ def test_sceneToNormalShowNames(self): self._test_sceneToNormalShowNames('Show and Name 2010', ['Show and Name 2010', 'Show & Name 2010', 'Show and Name (2010)', 'Show & Name (2010)']) self._test_sceneToNormalShowNames('show name us', ['show name us', 'show name (us)']) self._test_sceneToNormalShowNames('Show And Name', ['Show And Name', 'Show & Name']) - + # failure cases self._test_sceneToNormalShowNames('Show Name 90210', ['Show Name 90210']) self._test_sceneToNormalShowNames('Show Name YA', ['Show Name YA']) @@ -66,7 +72,7 @@ def test_allPossibleShowNames(self): myDB = db.DBConnection("cache.db") myDB.action("INSERT INTO scene_exceptions (tvdb_id, show_name) VALUES (?,?)", [-1, 'Exception Test']) common.countryList['Full Country Name'] = 'FCN' - + self._test_allPossibleShowNames('Show Name', expected=['Show Name']) self._test_allPossibleShowNames('Show Name', -1, expected=['Show Name', 'Exception Test']) self._test_allPossibleShowNames('Show Name', tvrname='TVRage Name', expected=['Show Name', 'TVRage Name']) @@ -77,7 +83,6 @@ def test_allPossibleShowNames(self): self._test_allPossibleShowNames('Show Name (FCN)', -1, 'TVRage Name', expected=['Show Name (FCN)', 'Show Name (Full Country Name)', 'Exception Test', 'TVRage Name']) def test_filterBadReleases(self): - self._test_filterBadReleases('Show.S02.German.Stuff-Grp', False) self._test_filterBadReleases('Show.S02.Some.Stuff-Core2HD', False) self._test_filterBadReleases('Show.S02.Some.German.Stuff-Grp', False) @@ -85,14 +90,11 @@ def test_filterBadReleases(self): self._test_filterBadReleases('Show.S02.This.Is.German', False) +class SceneExceptionTestCase(test.SickbeardTestDBCase): - -print 'Loading exceptions...', -db.upgradeDatabase(db.DBConnection("cache.db"), cache_db.InitialSchema) -scene_exceptions.retrieve_exceptions() -print 'done.' - -class SceneExceptionTestCase(unittest.TestCase): + def setUp(self): + super(SceneExceptionTestCase, self).setUp() + scene_exceptions.retrieve_exceptions() def test_sceneExceptionsEmpty(self): self.assertEqual(scene_exceptions.get_scene_exceptions(0), []) @@ -104,7 +106,7 @@ def test_sceneExceptionByName(self): self.assertEqual(scene_exceptions.get_scene_exception_by_name('Babylon5'), 70726) self.assertEqual(scene_exceptions.get_scene_exception_by_name('babylon 5'), 70726) self.assertEqual(scene_exceptions.get_scene_exception_by_name('Carlos 2010'), 164451) - + def test_sceneExceptionByNameEmpty(self): self.assertEqual(scene_exceptions.get_scene_exception_by_name('nothing useful'), None) @@ -112,17 +114,17 @@ def test_sceneExceptionsResetNameCache(self): # clear the exceptions myDB = db.DBConnection("cache.db") myDB.action("DELETE FROM scene_exceptions") - + # put something in the cache name_cache.addNameToCache('Cached Name', 0) - + # updating should clear the cache so our previously "Cached Name" won't be in there scene_exceptions.retrieve_exceptions() self.assertEqual(name_cache.retrieveNameFromCache('Cached Name'), None) # put something in the cache name_cache.addNameToCache('Cached Name', 0) - + # updating should not clear the cache this time since our exceptions didn't change scene_exceptions.retrieve_exceptions() self.assertEqual(name_cache.retrieveNameFromCache('Cached Name'), 0) @@ -130,7 +132,7 @@ def test_sceneExceptionsResetNameCache(self): if __name__ == '__main__': if len(sys.argv) > 1: - suite = unittest.TestLoader().loadTestsFromName('scene_helpers_tests.SceneExceptionTestCase.test_'+sys.argv[1]) + suite = unittest.TestLoader().loadTestsFromName('scene_helpers_tests.SceneExceptionTestCase.test_' + sys.argv[1]) unittest.TextTestRunner(verbosity=2).run(suite) else: suite = unittest.TestLoader().loadTestsFromTestCase(SceneTests) diff --git a/tests/snatch_tests.py b/tests/snatch_tests.py new file mode 100644 index 0000000000..ba707085c7 --- /dev/null +++ b/tests/snatch_tests.py @@ -0,0 +1,110 @@ +# coding=UTF-8 +# Author: Dennis Lutter +# URL: http://code.google.com/p/sickbeard/ +# +# This file is part of Sick Beard. +# +# Sick Beard 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 3 of the License, or +# (at your option) any later version. +# +# Sick Beard 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 Sick Beard. If not, see . + +import random +import unittest + +import test_lib as test + +import sys, os.path + +import sickbeard.search as search +import sickbeard +from sickbeard.tv import TVEpisode, TVShow +import sickbeard.common as c + +tests = {"Dexter": {"q": c.HD, "s": 5, "e": 7, "b": 'Dexter.S05E07.720p.BluRay.X264-REWARD', "i": ['Dexter.S05E07.720p.BluRay.X264-REWARD', 'Dexter.S05E07.720p.X264-REWARD']}, + "House": {"q": c.HD, "s": 4, "e": 5, "b": 'House.4x5.720p.BluRay.X264-REWARD', "i": ['Dexter.S05E04.720p.X264-REWARD', 'House.4x5.720p.BluRay.X264-REWARD']} + } + + + +def _create_fake_xml(items): + xml = '' + for item in items: + xml += '' + item + '\n' + xml += 'http://fantasy.com/' + item + '' + xml += '' + return xml + +# the real one tries to contact tvdb just stop it from getting more info on the ep +def _fake_specifyEP(self, season, episode): + pass + +TVEpisode.specifyEpisode = _fake_specifyEP + +searchItems = [] + +class SearchTest(test.SickbeardTestDBCase): + + def _fake_getURL(self, url, headers=None): + global searchItems + return _create_fake_xml(searchItems) + + def _fake_isActive(self): + return True + + def __init__(self, something): + for provider in sickbeard.providers.sortedProviderList(): + provider.getURL = self._fake_getURL + #provider.isActive = self._fake_isActive + + super(SearchTest, self).__init__(something) + +def test_generator(tvdbdid, show_name, curData, forceSearch): + + def test(self): + global searchItems + searchItems = curData["i"] + show = TVShow(tvdbdid) + show.name = show_name + show.quality = curData["q"] + show.saveToDB() + sickbeard.showList.append(show) + + episode = TVEpisode(show, curData["s"], curData["e"]) + episode.status = c.WANTED + episode.saveToDB() + + bestResult = search.findEpisode(episode, forceSearch) + if not bestResult: + self.assertEqual(curData["b"], bestResult) + self.assertEqual(curData["b"], bestResult.name) + return test + +if __name__ == '__main__': + print "==================" + print "STARTING - Snatch TESTS" + print "==================" + print "######################################################################" + # create the test methods + tvdbdid = 1 + for forceSearch in (True, False): + for name, curData in tests.items(): + if forceSearch: + test_name = 'test_manual_%s_%s' % (name, tvdbdid) + else: + test_name = 'test_%s_%s' % (name, tvdbdid) + + test = test_generator(tvdbdid, name, curData, forceSearch) + setattr(SearchTest, test_name, test) + tvdbdid += 1 + + suite = unittest.TestLoader().loadTestsFromTestCase(SearchTest) + unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/tests/test_lib.py b/tests/test_lib.py index 0bc0398431..221e8acda7 100644 --- a/tests/test_lib.py +++ b/tests/test_lib.py @@ -27,12 +27,29 @@ import sickbeard import shutil, time -from sickbeard import encodingKludge as ek +from sickbeard import encodingKludge as ek, providers, tvcache from sickbeard import db from sickbeard.databases import mainDB +from sickbeard.databases import cache_db +#================= +# test globals +#================= +TESTDIR = os.path.abspath('.') +TESTDBNAME = "sickbeard.db" +TESTCACHEDBNAME = "cache.db" +SHOWNAME = u"show name" +SEASON = 4 +EPISODE = 2 +FILENAME = u"show name - s0" + str(SEASON) + "e0" + str(EPISODE) + ".mkv" +FILEDIR = os.path.join(TESTDIR, SHOWNAME) +FILEPATH = os.path.join(FILEDIR, FILENAME) + +#sickbeard.logger.sb_log_instance = sickbeard.logger.SBRotatingLogHandler(os.path.join(TESTDIR, 'sickbeard.log'), sickbeard.logger.NUM_LOGS, sickbeard.logger.LOG_SIZE) +sickbeard.logger.SBRotatingLogHandler.log_file = os.path.join(os.path.join(TESTDIR, 'Logs'), 'test_sickbeard.log') + #================= # sickbeard globals @@ -43,22 +60,23 @@ sickbeard.SEASON_FOLDERS_DEFAULT = 1 sickbeard.SEASON_FOLDERS_FORMAT = 'Season %02d' +sickbeard.NAMING_SHOW_NAME = 1 +sickbeard.NAMING_EP_NAME = 1 +sickbeard.NAMING_EP_TYPE = 0 +sickbeard.NAMING_MULTI_EP_TYPE = 1 +sickbeard.NAMING_SEP_TYPE = 0 +sickbeard.NAMING_USE_PERIODS = 0 +sickbeard.NAMING_QUALITY = 0 +sickbeard.NAMING_DATES = 1 + +sickbeard.PROVIDER_ORDER = ["sick_beard_index"] +sickbeard.newznabProviderList = providers.getNewznabProviderList("Sick Beard Index|http://momo.sickbeard.com/||1!!!NZBs.org|http://beta.nzbs.org/||0") +sickbeard.providerList = providers.makeProviderList() sickbeard.PROG_DIR = os.path.abspath('..') sickbeard.DATA_DIR = sickbeard.PROG_DIR - -#================= -# test globals -#================= -TESTDIR = os.path.abspath('.') -TESTDBNAME = "test_sickbeard.db" - -SHOWNAME = u"show name" -SEASON = 4 -EPISODE = 2 -FILENAME = u"show name - s0" + str(SEASON) + "e0" + str(EPISODE) + ".mkv" -FILEDIR = os.path.join(TESTDIR, SHOWNAME) -FILEPATH = os.path.join(FILEDIR, FILENAME) +sickbeard.LOG_DIR = os.path.join(TESTDIR, 'Logs') +sickbeard.logger.sb_log_instance.initLogging(False) #================= # dummy functions @@ -84,13 +102,40 @@ def tearDown(self): tearDown_test_db() tearDown_test_episode_file() + class TestDBConnection(db.DBConnection, object): + def __init__(self, dbFileName=TESTDBNAME): dbFileName = os.path.join(TESTDIR, dbFileName) super(TestDBConnection, self).__init__(dbFileName) + +class TestCacheDBConnection(TestDBConnection, object): + + def __init__(self, providerName): + db.DBConnection.__init__(self, os.path.join(TESTDIR, TESTCACHEDBNAME)) + + # Create the table if it's not already there + try: + sql = "CREATE TABLE "+providerName+" (name TEXT, season NUMERIC, episodes TEXT, tvrid NUMERIC, tvdbid NUMERIC, url TEXT, time NUMERIC, quality TEXT);" + self.connection.execute(sql) + self.connection.commit() + except sqlite3.OperationalError, e: + if str(e) != "table "+providerName+" already exists": + raise + + # Create the table if it's not already there + try: + sql = "CREATE TABLE lastUpdate (provider TEXT, time NUMERIC);" + self.connection.execute(sql) + self.connection.commit() + except sqlite3.OperationalError, e: + if str(e) != "table lastUpdate already exists": + raise + # this will override the normal db connection sickbeard.db.DBConnection = TestDBConnection +sickbeard.tvcache.CacheDBConnection = TestCacheDBConnection #================= @@ -103,13 +148,20 @@ def setUp_test_db(): db.upgradeDatabase(db.DBConnection(), mainDB.InitialSchema) # fix up any db problems db.sanityCheckDatabase(db.DBConnection(), mainDB.MainSanityCheck) + + #and for cache.b too + db.upgradeDatabase(db.DBConnection("cache.db"), cache_db.InitialSchema) def tearDown_test_db(): """Deletes the test db although this seams not to work on my system it leaves me with an zero kb file """ - os.remove(ek.ek(os.path.join, TESTDIR, TESTDBNAME)) + #return False + if os.path.exists(os.path.join(TESTDIR, TESTDBNAME)): + os.remove(os.path.join(TESTDIR, TESTDBNAME)) + if os.path.exists(os.path.join(TESTDIR, TESTCACHEDBNAME)): + os.remove(os.path.join(TESTDIR, TESTCACHEDBNAME)) def setUp_test_episode_file(): if not os.path.exists(FILEDIR): @@ -119,9 +171,12 @@ def setUp_test_episode_file(): f.write("foo bar") f.close() + def tearDown_test_episode_file(): shutil.rmtree(FILEDIR) +tearDown_test_db() + if __name__ == '__main__': print "==================" print "Dont call this directly" diff --git a/tests/tv_tests.py b/tests/tv_tests.py index c66ce87cb0..40734dfc0c 100644 --- a/tests/tv_tests.py +++ b/tests/tv_tests.py @@ -19,13 +19,8 @@ import random import unittest - import test_lib as test -import sys, os.path -sys.path.append(os.path.abspath('..')) -sys.path.append(os.path.abspath('../lib')) - import sickbeard from sickbeard.tv import TVEpisode, TVShow from sickbeard import exceptions @@ -69,7 +64,6 @@ def test_set_name(self): self.assertEqual(show.name, "newName") - class TVEpisodeTests(test.SickbeardTestDBCase): def setUp(self): @@ -78,7 +72,12 @@ def setUp(self): def test_init_empty_db(self): show = TVShow(0001, "en") - self.assertRaises(exceptions.EpisodeNotFoundException, TVEpisode, show, test.SEASON, test.EPISODE) + ep = TVEpisode(show, 1, 1) + ep.name = "asdasdasdajkaj" + ep.saveToDB() + ep.loadFromDB(1, 1) + self.assertEqual(ep.name, "asdasdasdajkaj") + class TVTests(test.SickbeardTestDBCase): @@ -101,8 +100,6 @@ def test_getEpisode(self): #TODO: implement - - if __name__ == '__main__': print "==================" print "STARTING - TV TESTS"