import sys
from pathlib import Path
sys.path.append(str(Path('H:/python_modules/rt_model_python/rt1')))
import unittest
from pathlib import Path
from rt1.rtprocess import RTprocess
import shutil

import warnings
warnings.simplefilter('ignore')

class TestRTfits(unittest.TestCase):
    def test_parallel_processing(self):
        reader_args = [dict(gpi=i) for i in [1, 2, 3, 4]]
        config_path = Path(__file__).parent.absolute() / 'test_config.ini'

        proc = RTprocess(config_path, copy=True, autocontinue=True)

        proc.run_processing(ncpu=1, reader_args = reader_args)
        # run again to check what happens if files already exist
        proc.run_processing(ncpu=4, reader_args = reader_args)



        #----------------------------------------- check if files have been copied
        assert Path('tests/proc_test/dump01/cfg').exists(), 'folder-generation did not work'
        assert Path('tests/proc_test/dump01/results').exists(), 'folder-generation did not work'
        assert Path('tests/proc_test/dump01/dumps').exists(), 'folder-generation did not work'
        assert Path('tests/proc_test/dump01/cfg/test_config.ini').exists(), 'copying did not work'
        assert Path('tests/proc_test/dump01/cfg/parallel_processing_config.py').exists(), 'copying did not work'


        # remove the save_path directory
        print('deleting save_path directory...')
        #print('\n'.join([str(i) for i in cfg.get_process_specs()["save_path"].rglob('*')]))
        shutil.rmtree(proc.dumppath.parent)




    def test_single_core_processing(self):
        reader_args = [dict(gpi=i) for i in [1, 2, 3, 4]]
        config_path = Path(__file__).parent.absolute() / 'test_config.ini'

        proc = RTprocess(config_path, copy=True, autocontinue=True)

        proc.run_processing(ncpu=1, reader_args = reader_args)
        # run again to check what happens if files already exist
        proc.run_processing(ncpu=1, reader_args = reader_args)


        #----------------------------------------- check if files have been copied
        assert Path('tests/proc_test/dump01/cfg').exists(), 'folder-generation did not work'
        assert Path('tests/proc_test/dump01/results').exists(), 'folder-generation did not work'
        assert Path('tests/proc_test/dump01/dumps').exists(), 'folder-generation did not work'
        assert Path('tests/proc_test/dump01/cfg/test_config.ini').exists(), 'copying did not work'
        assert Path('tests/proc_test/dump01/cfg/parallel_processing_config.py').exists(), 'copying did not work'



        # remove the save_path directory
        print('deleting save_path directory...')
        #print('\n'.join([str(i) for i in cfg.get_process_specs()["save_path"].rglob('*')]))
        shutil.rmtree(proc.dumppath.parent)


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




@@ -173,16 +173,27 @@
 Tprocess
+, RTresults
 %0Aimport 
@@ -249,16 +249,74 @@
 nore')%0A%0A
+# use %22test_0_---%22   %22test_1_---%22   to ensure test-order%0A%0A
 class Te
@@ -357,16 +357,18 @@
 ef test_
+0_
 parallel
@@ -614,33 +614,33 @@
 processing(ncpu=
-1
+4
 , reader_args = 
@@ -781,18 +781,16 @@
 _args)%0A%0A
-%0A%0A
         
@@ -1363,16 +1363,315 @@
 work'%0A%0A%0A
+    def test_1_rtresults(self):%0A%0A        results = RTresults('tests/proc_test')%0A        assert hasattr(results, 'dump01'), 'dumpfolder not found by RTresults'%0A        fit = results.dump01.load_fit()%0A        cfg = results.dump01.load_cfg()%0A        dumpfiles = %5Bi for i in results.dump01.dump_files%5D%0A%0A
         
@@ -1867,39 +1867,37 @@
 .rmtree(
-proc.dumppath.parent)%0A%0A
+results._parent_path)
 %0A%0A%0A    d
@@ -1904,16 +1904,18 @@
 ef test_
+2_
 single_c
@@ -2130,33 +2130,32 @@
 ocontinue=True)%0A
-%0A
         proc.run
@@ -2205,137 +2205,8 @@
 gs)%0A
-        # run again to check what happens if files already exist%0A        proc.run_processing(ncpu=1, reader_args = reader_args)%0A%0A
 %0A   
@@ -2783,18 +2783,16 @@
  work'%0A%0A
-%0A%0A
         

update test_parallel_processing - test a bit of RTrseults as well