import test_base
from BackupUtils import *
import unittest

# Test BackupUtils class
# TODO: check if db tests work on windows too
# WARNING: need to be root for postgres db test to work ...
class TestBackupLocal(unittest.TestCase):
	__source = [test_base.getInFilePath("to_backup_1.txt")]
	__backupTmpDir = test_base.tmpDirPath
	
	def setUp(self):
		self.bb = BackupUtils()
		
	def testDbBackupInfo(self):
		dbbi1 = DbBackupInfo()
		dbbi2 = DbBackupInfo("pg","test","postgres")
		self.assertEquals(dbbi2.dbType, DbBackupInfo.TYPE_PG)
		dbbi3 = DbBackupInfo(DbBackupInfo.TYPE_MYSQL,"test","root")
		
	def testGetBackupFileName(self):
		name1 = "xxx"
		name2 = "xxx.tgz"
		extension = "tgz"
		exp1 = "xxx.tgz"
		out1 = self.bb.getBackupFileName(name1,extension)
		out2 = self.bb.getBackupFileName(name2,extension)
		self.assertEquals(out1,exp1)
		self.assertEquals(out2,exp1)
	
	def testGetOutputFilePath(self):
		inSource = self.__source[0]
		inDest = self.__backupTmpDir
		extension = 'tgz'
		out = os.path.join(inDest,os.path.split(inSource)[1] + '.tgz')
		tmp1 =  self.bb.getOutputFilePath(inSource,inDest,'tgz')
		self.assertEquals(out,tmp1)
	
	def testAddFilesToTarArchive(self):
		# checks we can backup something trivial without problems:
		self.bb.addFilesToTarArchive(self.__source, self.__backupTmpDir)
		# TODO: put in test that archive has been created.
	
	# WARNING: for this test to work you will need to execute as root
	def testBackupDbPostgres(self):
		if os.name == "posix":
			dbInfo = DbBackupInfo()
			dbInfo.dbName = "test"
			dbInfo.username = "postgres"
			# only for ahab to test working on a proper db
			#dbInfo.dbName = "rookd5main"
			self.bb.dbBackup(dbInfo, os.path.join(self.__backupTmpDir, dbInfo.dbName + ".sql.gz" ))
		else:
			pass
	
	def testBackupDbMysql(self):
		if os.name == "posix":
			dbInfo = DbBackupInfo(DbBackupInfo.TYPE_MYSQL, "test", "root", "penci12")
			self.bb.dbBackup(dbInfo, os.path.join(self.__backupTmpDir,  "mysql_" + dbInfo.dbName + ".sql.gz" ))
		else:
			pass
	

if __name__ == "__main__":
	unittest.main()
