def test_no_parameters_json(self): with open("template", "w") as template_file: template_file.write(self.mulitple_input_template_no_param) with open("not_the_standard_parameters.json", "w") as json_file: json_file.write(json.dumps(self.default_parameters)) with open("second.json", "w") as json_file: parameters = {"key1": {"subkey2": 1400}} json.dump(parameters, json_file) with open("third.json", "w") as json_file: parameters = { "key1": { "subkey1": 3000.22, } } json.dump(parameters, json_file) render_template( ["second.json", "third.json", "not_the_standard_parameters.json"], "template", "out_file", ) with open("out_file", "r") as parameter_file: expected_output = ("FILENAME\n" + "F1 1999.22\n" + "OTH 1400\n" + "OTH_TEST 3000.22") self.assertEqual(parameter_file.read(), expected_output)
def test_render_invalid(self): with TestAreaContext("templating") as tac: prod_wells = {"PROD%d" % idx: 0.3 * idx for idx in range(4)} prod_in = "well_drill.json" with open(prod_in, "w") as fout: json.dump(prod_wells, fout) with open("parameters.json", "w") as fout: json.dump(self.default_parameters, fout) with open("template_file", "w") as fout: fout.write(self.well_drill_tmpl) wells_out = "wells.out" # undefined template elements with self.assertRaises(jinja2.exceptions.UndefinedError): render_template(None, "template_file", wells_out) # file not found with self.assertRaises(ValueError): render_template(2 * prod_in, "template_file", wells_out) # no template file with self.assertRaises(TypeError): render_template(prod_in, None, wells_out) # templatefile not found with self.assertRaises(ValueError): render_template(prod_in, "template_file" + "nogo", wells_out) # no output file with self.assertRaises(TypeError): render_template(prod_in, "template_file", None)
def test_render(self): wells = {"PROD%d" % idx: 0.2 * idx for idx in range(1, 5)} wells.update( { "INJ": [ {"name": "INJ{0}".format(idx), "value": 1 - 0.2 * idx} for idx in range(1, 5) ] } ) wells_in = "well_drill.json" wells_tmpl = "well_drill_tmpl" wells_out = "wells.out" with open(wells_in, "w") as fout: json.dump(wells, fout) with open("parameters.json", "w") as fout: json.dump(self.default_parameters, fout) with open(wells_tmpl, "w") as fout: fout.write(self.well_drill_tmpl) render_template(wells_in, wells_tmpl, wells_out) self.maxDiff = None expected_template_out = [ "PROD1 takes value 0.2, implying off\n", "PROD2 takes value 0.4, implying off\n", "----------------------------------\n", "INJ1 takes value 0.8, implying on\n", "INJ2 takes value 0.6, implying on\n", "INJ3 takes value 0.4, implying off\n", "INJ4 takes value 0.2, implying off", ] with open(wells_out) as fin: output = fin.readlines() self.assertEqual(expected_template_out, output)