|
| 1 | +import json |
1 | 2 | import glob |
2 | 3 | import os |
3 | 4 | import shutil |
4 | 5 | import unittest |
5 | 6 |
|
6 | 7 | from difflib import Differ |
7 | 8 |
|
8 | | -from prms_python import modify_params, Parameters, Simulation |
| 9 | +from prms_python import modify_params, Parameters, Scenario, Simulation |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class TestSimulations(unittest.TestCase): |
@@ -55,43 +56,96 @@ def test_simulation_w_simdir(self): |
55 | 56 | self.assertIn('inputs', gs) |
56 | 57 | self.assertIn('outputs', gs) |
57 | 58 |
|
58 | | - gi = [ |
59 | | - os.path.basename(f) |
60 | | - for f in |
61 | | - glob.glob(os.path.join(self.simulation_dir, 'inputs', '*')) |
62 | | - ] |
63 | | - self.assertIn('control', gi) |
64 | | - self.assertIn('parameters', gi) |
65 | | - self.assertIn('data', gi) |
66 | | - |
67 | | - go = [ |
68 | | - os.path.basename(f) |
69 | | - for f in |
70 | | - glob.glob(os.path.join(self.simulation_dir, 'outputs', '*')) |
71 | | - ] |
72 | | - self.assertIn('prms_ic.out', go) |
73 | | - self.assertIn('prms.out', go) |
74 | | - self.assertIn('statvar.dat', go) |
75 | | - self.assertIn('animation.out.nhru', go) |
| 59 | + assert_valid_input_dir( |
| 60 | + self, os.path.join(self.simulation_dir, 'inputs') |
| 61 | + ) |
| 62 | + assert_valid_output_dir( |
| 63 | + self, os.path.join(self.simulation_dir, 'outputs') |
| 64 | + ) |
76 | 65 |
|
77 | 66 |
|
78 | 67 | class TestScenarios(unittest.TestCase): |
79 | 68 |
|
80 | 69 | def setUp(self): |
81 | | - pass |
| 70 | + |
| 71 | + self.test_data_dir = os.path.join('test', 'data') |
| 72 | + |
| 73 | + self.test_model_data_dir = os.path.join( |
| 74 | + 'test', 'data', 'models', 'lbcd' |
| 75 | + ) |
| 76 | + |
| 77 | + self.scenario_dir = os.path.join(self.test_data_dir, 'tmp_scenario') |
82 | 78 |
|
83 | 79 | def tearDown(self): |
84 | | - pass |
| 80 | + |
| 81 | + if os.path.exists(self.scenario_dir): |
| 82 | + shutil.rmtree(self.scenario_dir) |
85 | 83 |
|
86 | 84 | def test_create_scenario(self): |
87 | 85 | """a simulation setup should create a simulation directory""" |
88 | | - assert False |
| 86 | + |
| 87 | + s = Scenario( |
| 88 | + self.test_model_data_dir, self.scenario_dir, |
| 89 | + title='Scenario Uno', description='test scenario for prms_python' |
| 90 | + ) |
| 91 | + |
| 92 | + param_mods = { |
| 93 | + 'snow_adj': lambda x: 1.1*x, |
| 94 | + 'rad_trncf': lambda x: 0.9*x |
| 95 | + } |
| 96 | + s.build_scenario(param_mod_funs=param_mods) |
| 97 | + |
| 98 | + assert_valid_input_dir(self, self.scenario_dir) # os.path.join(self.scenario_dir, 'inputs')) |
| 99 | + |
| 100 | + s.run() |
| 101 | + |
| 102 | + assert_valid_input_dir( |
| 103 | + self, os.path.join(self.scenario_dir, 'inputs') |
| 104 | + ) |
| 105 | + assert_valid_output_dir( |
| 106 | + self, os.path.join(self.scenario_dir, 'outputs') |
| 107 | + ) |
| 108 | + |
| 109 | + md_json_path = os.path.join(self.scenario_dir, 'metadata.json') |
| 110 | + assert os.path.isfile(md_json_path) |
| 111 | + |
| 112 | + md_json = json.loads(open(md_json_path).read()) |
| 113 | + assert md_json['title'] == 'Scenario Uno' |
| 114 | + assert md_json['description'] == 'test scenario for prms_python' |
| 115 | + assert 'start_datetime' in md_json |
| 116 | + assert 'end_datetime' in md_json |
| 117 | + assert 'mod_funs_dict' in md_json |
89 | 118 |
|
90 | 119 | def test_create_many_scenarios(self): |
91 | 120 | "create_many_simulations should create many simulation directories" |
92 | 121 | assert False |
93 | 122 |
|
94 | 123 |
|
| 124 | +def assert_valid_input_dir(test_case, d): |
| 125 | + |
| 126 | + gs = [ |
| 127 | + os.path.basename(f) |
| 128 | + for f in glob.glob(os.path.join(d, '*')) |
| 129 | + ] |
| 130 | + |
| 131 | + test_case.assertIn('control', gs, d) |
| 132 | + test_case.assertIn('parameters', gs, d) |
| 133 | + test_case.assertIn('data', gs, d) |
| 134 | + |
| 135 | + |
| 136 | +def assert_valid_output_dir(test_case, d): |
| 137 | + |
| 138 | + go = [ |
| 139 | + os.path.basename(f) |
| 140 | + for f in |
| 141 | + glob.glob(os.path.join(d, '*')) |
| 142 | + ] |
| 143 | + test_case.assertIn('prms_ic.out', go) |
| 144 | + test_case.assertIn('prms.out', go) |
| 145 | + test_case.assertIn('statvar.dat', go) |
| 146 | + test_case.assertIn('animation.out.nhru', go) |
| 147 | + |
| 148 | + |
95 | 149 | class TestParameters(unittest.TestCase): |
96 | 150 |
|
97 | 151 | def setUp(self): |
|
0 commit comments