Skip to content
Snippets Groups Projects
Commit b735be08 authored by Alex Buts's avatar Alex Buts
Browse files

Re #15970 Initial commit, some unit tests

parent 037d1c96
No related merge requests found
......@@ -283,7 +283,9 @@ class UserProperties(object):
return instrument, start_date, cycle, rb_folder_or_id, rb_exist
def get_all_instruments(self):
""" Return list of all instruments, user is working on during this cycle"""
return mcf._user._instrument.values()
#
# --------------------------------------------------------------------#
#
......
import os
import sys
import unittest
import shutil
import datetime
import time
import platform
#sys.path.append(r'c:\Mantid\_builds\br_master\bin\Release')
from mantid import config
from Direct.ISISDirecInelasticConfig import UserProperties,MantidConfigDirectInelastic
......@@ -69,21 +71,31 @@ class ISISDirectInelasticConfigTest(unittest.TestCase):
def makeFakeSourceReductionFile(self,mcf,contents=None):
instr_name = mcf._user.instrument
all_instr_names = mcf._user.get_all_instruments()
all_files = []
for instr in all_instr_names:
instr_name = instr
file_path = os.path.join(self.UserScriptRepoDir,'direct_inelastic',instr_name.upper())
if not os.path.exists(file_path):
os.makedirs(file_path)
file_path = os.path.join(self.UserScriptRepoDir,'direct_inelastic',instr_name.upper())
if not os.path.exists(file_path):
os.makedirs(file_path)
file_name = mcf._sample_reduction_file(instr_name)
full_file = os.path.join(file_path,file_name)
if os.path.isfile(full_file):
os.remove(full_file)
fh=open(full_file,'w')
fh.write('#Test reduction file\n')
fh.write('Contents={0}'.format(contents))
fh.close()
return full_file
file_name = mcf._sample_reduction_file(instr_name)
all_files.append(file_name)
full_file = os.path.join(file_path,file_name)
if os.path.isfile(full_file):
os.remove(full_file)
fh=open(full_file,'w')
fh.write('#Test reduction file\n')
if contents is None:
fh.write('Contents=Fake_reduction_file_for_{0}'.format(instr_name))
else:
fh.write('Contents={0}'.format(contents))
fh.close()
if len(all_files) > 1:
return all_files
else:
return full_file
def _tear_down(self):
......@@ -240,7 +252,7 @@ class ISISDirectInelasticConfigTest(unittest.TestCase):
mcf.init_user(user)
fake_source=self.makeFakeSourceReductionFile(mcf)
self.makeFakeSourceReductionFile(mcf)
self.assertEqual(len(mcf._dynamic_configuration),6)
self.assertEqual(mcf._dynamic_configuration[1],'default.instrument=MERLIN')
......@@ -415,13 +427,108 @@ class ISISDirectInelasticConfigTest(unittest.TestCase):
rb_folder1 = user3.rb_dir
self.assertEqual(rb_folder,rb_folder1)
def test_copy_multiplpe_ucf(self):
# script verifies the presence of a folder, not its contents.
# for the script to work, let's run it on default save directory
MantidDir = os.path.split(os.path.realpath(__file__))[0]
HomeRootDir = self.get_save_dir()
mcf = MantidConfigDirectInelastic(MantidDir,HomeRootDir,self.UserScriptRepoDir,self.MapMaskDir)
user = UserProperties(self.userID)
user.set_user_properties(self.instrument,self.rbdir,self.cycle,self.start_date)
rbnum2='RB1999000'
targetDir = self.get_save_dir()
rbdir2 = os.path.join(targetDir,self.userID,rbnum2)
if not os.path.exists(rbdir2):
os.makedirs(rbdir2)
user.set_user_properties('MARI',rbdir2,'CYCLE20001','20000124')
rbnum3='RB1204000'
rbdir3 = os.path.join(targetDir,self.userID,rbnum3)
if not os.path.exists(rbdir3):
os.makedirs(rbdir3)
user.set_user_properties('MAPS',rbdir3,'CYCLE20044','20041207')
# clear up the previous
if os.path.exists(os.path.join(self.userRootDir,'.mantid')):
shutil.rmtree(os.path.join(self.userRootDir,'.mantid'))
mcf.init_user(user)
self.makeFakeSourceReductionFile(mcf)
self.assertEqual(len(mcf._dynamic_configuration),6)
self.assertEqual(mcf._dynamic_configuration[1],'default.instrument=MERLIN')
mcf.generate_config()
config_file = os.path.join(self.userRootDir,'.mantid','Mantid.user.properties')
self.assertTrue(os.path.exists(os.path.join(self.userRootDir,'.mantid')))
self.assertTrue(os.path.exists(config_file))
self.assertTrue(mcf.config_need_replacing(config_file))
start_date = user.start_date
date_in_apast=datetime.date(start_date.year,start_date.month,start_date.day-1)
time_in_a_past = time.mktime(date_in_apast.timetuple())
os.utime(config_file,(time_in_a_past,time_in_a_past))
self.assertFalse(mcf.config_need_replacing(config_file))
#--------------------------------------------------------------------
user1ID = 'tuf299966'
user1RootDir = os.path.join(self.get_save_dir(),user1ID)
if not os.path.exists(user1RootDir):
os.makedirs(user1RootDir)
#
user1 = UserProperties(user1ID)
user1.set_user_properties('MARI',rbdir2,'CYCLE20991','20990124')
mcf.init_user(user1)
source_file = self.makeFakeSourceReductionFile(mcf)
mcf.generate_config()
self.assertEqual(len(mcf._dynamic_configuration),6)
self.assertEqual(mcf._dynamic_configuration[1],'default.instrument=MARI')
config_file = os.path.join(self.userRootDir,'.mantid','Mantid.user.properties')
self.assertTrue(os.path.exists(os.path.join(user1RootDir,'.mantid')))
self.assertTrue(os.path.exists(config_file))
#
# Check sample reduction file
#
full_rb_path = rbdir2
cycle_id = user1.cycleID
instr = user1.instrument
target_file = mcf._target_reduction_file(instr,cycle_id)
full_target_file = os.path.join(full_rb_path,target_file)
self.assertTrue(os.path.exists(full_target_file))
# Fresh target file should always be replaced
self.assertTrue(mcf.script_need_replacing(full_target_file))
# modify target file access time:
access_time = os.path.getmtime(full_target_file)
now = time.time()
os.utime(full_target_file,(access_time ,now))
# should not replace modified target file
self.assertFalse(mcf.script_need_replacing(full_target_file))
#--------------------------------------------------------------------
# clean up
if os.path.exists(os.path.join(self.userRootDir,'.mantid')):
shutil.rmtree(os.path.join(self.userRootDir,'.mantid'),ignore_errors=True)
if os.path.exists(rbdir2):
shutil.rmtree(rbdir2,ignore_errors=True)
if os.path.exists(rbdir3):
shutil.rmtree(rbdir3,ignore_errors=True)
if os.path.exists(user1RootDir):
shutil.rmtree(user1RootDir,ignore_errors=True)
#
if __name__=="__main__":
#test = ISISDirectInelasticConfigTest('test_UserProperties')
#test._set_up()
#test.run()
#test._tear_down()
test = ISISDirectInelasticConfigTest('test_copy_multiplpe_ucf')
test._set_up()
test.run()
test._tear_down()
unittest.main()
#unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment