2014-03-10 05:18:05 +00:00
|
|
|
# coding=UTF-8
|
|
|
|
# Author: Dennis Lutter <lad1337@gmail.com>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2014-03-10 05:18:05 +00:00
|
|
|
# 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
|
2014-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-09 11:13:00 +00:00
|
|
|
from __future__ import print_function
|
2014-03-10 05:18:05 +00:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import sqlite3
|
2014-12-09 12:30:00 +00:00
|
|
|
import glob
|
2014-03-10 05:18:05 +00:00
|
|
|
import sys
|
|
|
|
import os.path
|
2014-12-09 12:30:00 +00:00
|
|
|
|
2015-06-04 13:36:38 +00:00
|
|
|
sys.path.insert(1, os.path.abspath('..'))
|
|
|
|
sys.path.insert(1, os.path.abspath('../lib'))
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
from sickbeard import encodingKludge as ek, providers, tvcache
|
|
|
|
from sickbeard import db
|
|
|
|
from sickbeard.databases import mainDB
|
2014-11-22 14:15:27 +00:00
|
|
|
from sickbeard.databases import cache_db, failed_db
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
#=================
|
|
|
|
# test globals
|
|
|
|
#=================
|
|
|
|
TESTDIR = os.path.abspath('.')
|
2015-08-11 11:51:26 +00:00
|
|
|
TESTDBNAME = 'sickbeard.db'
|
|
|
|
TESTCACHEDBNAME = 'cache.db'
|
|
|
|
TESTFAILEDDBNAME = 'failed.db'
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-08-11 11:51:26 +00:00
|
|
|
SHOWNAME = u'show name'
|
2014-03-10 05:18:05 +00:00
|
|
|
SEASON = 4
|
|
|
|
EPISODE = 2
|
2015-08-11 11:51:26 +00:00
|
|
|
FILENAME = u'show name - s0' + str(SEASON) + 'e0' + str(EPISODE) + '.mkv'
|
2014-03-10 05:18:05 +00:00
|
|
|
FILEDIR = os.path.join(TESTDIR, SHOWNAME)
|
|
|
|
FILEPATH = os.path.join(FILEDIR, FILENAME)
|
|
|
|
|
2015-08-11 11:51:26 +00:00
|
|
|
SHOWDIR = os.path.join(TESTDIR, SHOWNAME + ' final')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-12-09 15:41:49 +00:00
|
|
|
#sickbeard.logger.sb_log_instance = sickbeard.logger.SBRotatingLogHandler(os.path.join(TESTDIR, 'sickgear.log'), sickbeard.logger.NUM_LOGS, sickbeard.logger.LOG_SIZE)
|
|
|
|
sickbeard.logger.SBRotatingLogHandler.log_file = os.path.join(os.path.join(TESTDIR, 'Logs'), 'test_sickgear.log')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
#=================
|
|
|
|
# prepare env functions
|
|
|
|
#=================
|
|
|
|
def createTestLogFolder():
|
|
|
|
if not os.path.isdir(sickbeard.LOG_DIR):
|
|
|
|
os.mkdir(sickbeard.LOG_DIR)
|
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
def createTestCacheFolder():
|
|
|
|
if not os.path.isdir(sickbeard.CACHE_DIR):
|
|
|
|
os.mkdir(sickbeard.CACHE_DIR)
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
# call env functions at appropriate time during sickbeard var setup
|
|
|
|
|
|
|
|
#=================
|
|
|
|
# sickbeard globals
|
|
|
|
#=================
|
|
|
|
sickbeard.SYS_ENCODING = 'UTF-8'
|
|
|
|
sickbeard.showList = []
|
|
|
|
sickbeard.QUALITY_DEFAULT = 4 # hdtv
|
|
|
|
sickbeard.FLATTEN_FOLDERS_DEFAULT = 0
|
|
|
|
|
|
|
|
sickbeard.NAMING_PATTERN = ''
|
|
|
|
sickbeard.NAMING_ABD_PATTERN = ''
|
2014-04-28 09:15:29 +00:00
|
|
|
sickbeard.NAMING_SPORTS_PATTERN = ''
|
2014-03-10 05:18:05 +00:00
|
|
|
sickbeard.NAMING_MULTI_EP = 1
|
|
|
|
|
|
|
|
|
2015-08-11 11:51:26 +00:00
|
|
|
sickbeard.PROVIDER_ORDER = ['sick_beard_index']
|
2014-05-26 06:29:22 +00:00
|
|
|
sickbeard.newznabProviderList = providers.getNewznabProviderList("'Sick Beard Index|http://lolo.sickbeard.com/|0|5030,5040,5060|0|eponly|0!!!NZBs.org|https://nzbs.org/||5030,5040,5060,5070,5090|0|eponly|0!!!Usenet-Crawler|https://www.usenet-crawler.com/||5030,5040,5060|0|eponly|0'")
|
2014-03-10 05:18:05 +00:00
|
|
|
sickbeard.providerList = providers.makeProviderList()
|
|
|
|
|
|
|
|
sickbeard.PROG_DIR = os.path.abspath('..')
|
|
|
|
sickbeard.DATA_DIR = sickbeard.PROG_DIR
|
|
|
|
sickbeard.LOG_DIR = os.path.join(TESTDIR, 'Logs')
|
|
|
|
createTestLogFolder()
|
2016-11-14 21:33:15 +00:00
|
|
|
sickbeard.logger.sb_log_instance.init_logging(False)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
sickbeard.CACHE_DIR = os.path.join(TESTDIR, 'cache')
|
|
|
|
createTestCacheFolder()
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
#=================
|
|
|
|
# dummy functions
|
|
|
|
#=================
|
|
|
|
def _dummy_saveConfig():
|
|
|
|
return True
|
2014-11-22 14:15:27 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
# this overrides the sickbeard save_config which gets called during a db upgrade
|
|
|
|
# this might be considered a hack
|
|
|
|
mainDB.sickbeard.save_config = _dummy_saveConfig
|
|
|
|
|
|
|
|
|
|
|
|
# the real one tries to contact tvdb just stop it from getting more info on the ep
|
2017-12-27 03:14:20 +00:00
|
|
|
def _fake_specifyEP(self, season, episode, show_sql=None):
|
2014-03-10 05:18:05 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
sickbeard.tv.TVEpisode.specifyEpisode = _fake_specifyEP
|
|
|
|
|
|
|
|
|
|
|
|
#=================
|
|
|
|
# test classes
|
|
|
|
#=================
|
|
|
|
class SickbeardTestDBCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
sickbeard.showList = []
|
|
|
|
setUp_test_db()
|
|
|
|
setUp_test_episode_file()
|
|
|
|
setUp_test_show_dir()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sickbeard.showList = []
|
|
|
|
tearDown_test_db()
|
|
|
|
tearDown_test_episode_file()
|
|
|
|
tearDown_test_show_dir()
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
2015-08-11 11:51:26 +00:00
|
|
|
sql = 'CREATE TABLE ' + providerName + ' (name TEXT, season NUMERIC, episodes TEXT, indexerid NUMERIC, url TEXT, time NUMERIC, quality TEXT);'
|
2014-03-10 05:18:05 +00:00
|
|
|
self.connection.execute(sql)
|
|
|
|
self.connection.commit()
|
2015-06-08 12:47:01 +00:00
|
|
|
except sqlite3.OperationalError as e:
|
2015-08-11 11:51:26 +00:00
|
|
|
if str(e) != 'table ' + providerName + ' already exists':
|
2014-03-10 05:18:05 +00:00
|
|
|
raise
|
|
|
|
|
|
|
|
# Create the table if it's not already there
|
|
|
|
try:
|
2015-08-11 11:51:26 +00:00
|
|
|
sql = 'CREATE TABLE lastUpdate (provider TEXT, time NUMERIC);'
|
2014-03-10 05:18:05 +00:00
|
|
|
self.connection.execute(sql)
|
|
|
|
self.connection.commit()
|
2015-06-08 12:47:01 +00:00
|
|
|
except sqlite3.OperationalError as e:
|
2015-08-11 11:51:26 +00:00
|
|
|
if str(e) != 'table lastUpdate already exists':
|
2014-03-10 05:18:05 +00:00
|
|
|
raise
|
|
|
|
|
|
|
|
# this will override the normal db connection
|
|
|
|
sickbeard.db.DBConnection = TestDBConnection
|
|
|
|
sickbeard.tvcache.CacheDBConnection = TestCacheDBConnection
|
|
|
|
|
|
|
|
#=================
|
|
|
|
# test functions
|
|
|
|
#=================
|
|
|
|
def setUp_test_db():
|
|
|
|
"""upgrades the db to the latest version
|
|
|
|
"""
|
|
|
|
# upgrading the db
|
2014-12-09 12:30:00 +00:00
|
|
|
db.MigrationCode(db.DBConnection())
|
2014-11-22 14:15:27 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
# fix up any db problems
|
|
|
|
db.sanityCheckDatabase(db.DBConnection(), mainDB.MainSanityCheck)
|
|
|
|
|
2014-11-22 14:15:27 +00:00
|
|
|
# and for cachedb too
|
2015-08-11 11:51:26 +00:00
|
|
|
db.upgradeDatabase(db.DBConnection('cache.db'), cache_db.InitialSchema)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-11-22 14:15:27 +00:00
|
|
|
# and for faileddb too
|
2015-08-11 11:51:26 +00:00
|
|
|
db.upgradeDatabase(db.DBConnection('failed.db'), failed_db.InitialSchema)
|
2014-11-22 14:15:27 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
# uncomment next line so leave the db intact between test and at the end
|
2014-11-22 14:15:27 +00:00
|
|
|
#return False
|
2014-12-09 12:30:00 +00:00
|
|
|
|
|
|
|
for filename in glob.glob(os.path.join(TESTDIR, TESTDBNAME) + '*'):
|
|
|
|
os.remove(filename)
|
2014-03-10 05:18:05 +00:00
|
|
|
if os.path.exists(os.path.join(TESTDIR, TESTCACHEDBNAME)):
|
|
|
|
os.remove(os.path.join(TESTDIR, TESTCACHEDBNAME))
|
2014-11-22 14:15:27 +00:00
|
|
|
if os.path.exists(os.path.join(TESTDIR, TESTFAILEDDBNAME)):
|
|
|
|
os.remove(os.path.join(TESTDIR, TESTFAILEDDBNAME))
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setUp_test_episode_file():
|
|
|
|
if not os.path.exists(FILEDIR):
|
|
|
|
os.makedirs(FILEDIR)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(FILEPATH, 'w') as f:
|
2015-08-11 11:51:26 +00:00
|
|
|
f.write('foo bar')
|
2014-03-10 05:18:05 +00:00
|
|
|
except EnvironmentError:
|
2015-06-09 11:13:00 +00:00
|
|
|
print('Unable to set up test episode')
|
2014-03-10 05:18:05 +00:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
def tearDown_test_episode_file():
|
2014-09-30 10:22:47 +00:00
|
|
|
if os.path.exists(FILEDIR):
|
|
|
|
shutil.rmtree(FILEDIR)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setUp_test_show_dir():
|
|
|
|
if not os.path.exists(SHOWDIR):
|
|
|
|
os.makedirs(SHOWDIR)
|
|
|
|
|
|
|
|
|
|
|
|
def tearDown_test_show_dir():
|
2014-09-30 10:22:47 +00:00
|
|
|
if os.path.exists(SHOWDIR):
|
|
|
|
shutil.rmtree(SHOWDIR)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
tearDown_test_db()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-06-09 11:13:00 +00:00
|
|
|
print('==================')
|
|
|
|
print('Dont call this directly')
|
|
|
|
print('==================')
|
|
|
|
print('you might want to call')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
dirList = os.listdir(TESTDIR)
|
|
|
|
for fname in dirList:
|
2015-08-11 11:51:26 +00:00
|
|
|
if (fname.find('_test') > 0) and (fname.find('pyc') < 0):
|
2015-06-09 11:13:00 +00:00
|
|
|
print('- ' + fname)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-09 11:13:00 +00:00
|
|
|
print('==================')
|
|
|
|
print('or just call all_tests.py')
|