def test_multi_metadata(): '''Tests generate year template by comparing to known good result''' multi_sim = SolveTemplate(cfgfile='tests/testConfig.cfg') meta = multi_sim.generate_metadata() with open('tests/metadata.json', 'r') as test_meta: metad = json.load(test_meta) assert json.dumps(meta, indent=2) == json.dumps(metad, indent=2)
def test_multi_metadata(): '''Tests generate year template by comparing to known good result''' X = SolveTemplate(cfgfile='tests/Sample.cfg') meta = X.generate_metadata() with open('tests/metadata.json', 'r') as f: metad = json.load(f) assert json.dumps(meta, indent=2) == json.dumps(metad, indent=2)
def test_multi_template_second(): '''Tests generate second (and later) year template by comparing to known good result''' X = SolveTemplate(cfgfile='tests/Sample.cfg') X.generateyeartemplate(X.Years[1], test=True) with open(X.tmpdir + 'Sim2025.dat') as ff: fromfile = ff.readlines() with open('tests/Sim2025.dat') as tf: tofile = tf.readlines() # Check that they are mostly the same, but for tempdir entries d = SequenceMatcher(None, fromfile, tofile) assert d.ratio() >= 0.99657534
def test_multi_template_first(): '''Tests generate first year template by comparing to known good result''' multi_sim = SolveTemplate(cfgfile='tests/testConfig.cfg') multi_sim.generateyeartemplate(multi_sim.Years[0], test=True) testfile = str(multi_sim.wrkdir) + '/Sim2020.dat' text1 = open(testfile).readlines() text2 = open('tests/Sim2020.dat').readlines() if filecmp.cmp(testfile, 'tests/Sim2020.dat', shallow=False): assert True s = SequenceMatcher(None, text1, text2) print(s.ratio()) assert s.ratio() > 0.999
def test_multi_template_second(delete_sim2025_dat): '''Tests generate second (and later) year template by comparing to known good result''' multi_sim = SolveTemplate(cfgfile='tests/testConfig.cfg', wrkdir=Path('')) multi_sim.generateyeartemplate(multi_sim.Years[1], test=True) testfile = str(multi_sim.wrkdir) + '/Sim2025.dat' text1 = open(testfile).readlines() text2 = open('tests/Sim2025.dat').readlines() if filecmp.cmp( str(multi_sim.wrkdir) + '/Sim2025.dat', 'tests/Sim2025.dat'): assert True s = SequenceMatcher(None, text1, text2) assert s.ratio() > 0.999
def test_multi_bad_file(option, value): ''' Assert validate bad config option''' fp = tempfile.NamedTemporaryFile() with open('tests/Sample.cfg') as fin: with open(fp.name, 'w') as fo: for line in fin: if option in line: line = value + '\n' fo.write(line) with pytest.raises(OSError): SolveTemplate(cfgfile=fp.name)
def test_multi_bad_file(option, value): ''' Assert that multi detects missing files in config''' with open('tests/testConfig.cfg') as sample: with tempfile.NamedTemporaryFile(mode='w', dir='tests') as temp_sample: for line in sample: if option in line: line = value + '\n' temp_sample.write(line) temp_sample.flush() with pytest.raises(OSError): SolveTemplate(cfgfile=temp_sample.name)
def test_multi_bad_cfg(option, value): ''' Assert validate bad config option by replacing known bad options in sample file''' with open('tests/testConfig.cfg') as sample: with tempfile.NamedTemporaryFile(mode='w', dir='tests') as temp_sample: for line in sample: if option in line: line = value + '\n' temp_sample.write(line) temp_sample.flush() with pytest.raises(ValueError): SolveTemplate(cfgfile=temp_sample.name)
action='store_true') parser.add_argument( "-k", "--keepfiles", help="Save generated files onto a folder with the same name as the configuration file", action='store_true') # parse arguments into args structure args = parser.parse_args() # Read configuration file name from cfgfile = args.config # create Multi year simulation X = SolveTemplate(cfgfile, solver=args.solver, log=args.log) # make a temporary directoy if args.keepfiles: path = cfgfile.split(".")[0] + '/' if not os.path.exists(path): os.mkdir(path) X.tmpdir = path # instruct the solver to launch the multi year simulation print("openCEM msolve.py: Runtime %s (pre solver)" % str( datetime.timedelta(seconds=(time.time() - start_time)))) X.solve() print("openCEM msolve.py: Runtime %s (post solver)" % str( datetime.timedelta(seconds=(time.time() - start_time))))
"--templatetest", help= "Testing mode where only short dispatch periods are use. Warning, it disables clustering", action="store_true", ) # parse arguments into args structure args = parser.parse_args() # Read configuration file name from cfgfile = args.config SIM_DIR = cfgfile.split(".")[0] + "/" if not os.path.exists(SIM_DIR): os.mkdir(SIM_DIR) # create Multi year simulation X = SolveTemplate(cfgfile, solver=args.solver, log=args.log, tmpdir=SIM_DIR, resume=args.resume, templatetest=args.templatetest) # instruct the solver to launch the multi year simulation print("openCEM msolve.py: Runtime %s (pre solver)" % str(datetime.timedelta(seconds=(time.time() - start_time)))) X.solve() print("openCEM msolve.py: Runtime %s (post solver)" % str(datetime.timedelta(seconds=(time.time() - start_time))))
def test_multi_template_first(): '''Tests generate first year template by comparing to known good result''' X = SolveTemplate(cfgfile='tests/Sample.cfg') X.generateyeartemplate(X.Years[0], test=True) assert filecmp.cmp(X.tmpdir + 'Sim2020.dat', 'tests/Sim2020.dat')
def test_multi_conf_file_not_found(): ''' Fail if file does not exist''' with pytest.raises(FileNotFoundError): SolveTemplate(cfgfile='Nofile.cfg')
def test_multi_get_model_options(year, value): '''Test that model options are generated for each year''' multi_sim = SolveTemplate(cfgfile='tests/testConfig.cfg') options = multi_sim.get_model_options(year) assert options.build_intercon_manual == value