def test_aquifers(self): """Test AQUIFERS section using the Project class""" source_text = '\n'.join(self.TEST_TEXT) project_reader = ProjectReader() project_writer = ProjectWriter() section_from_text = project_reader.read_aquifers.read(source_text) actual_text = project_writer.write_aquifers.as_text(section_from_text) msg = '\nSet:' + source_text + '\nGet:' + actual_text self.assertTrue(match_omit(actual_text, source_text, " \t-;\n"), msg) val = section_from_text.value[0] assert val.name == "1" assert val.porosity == "0.5" assert val.wilting_point == "0.15" assert val.field_capacity == "0.30" assert val.conductivity == "0.1" assert val.conductivity_slope == "12" assert val.tension_slope == "15.0" assert val.upper_evaporation_fraction == "0.35" assert val.lower_evaporation_depth == "14.0" assert val.lower_groundwater_loss_rate == "0.002" assert val.bottom_elevation == "0.0" assert val.water_table_elevation == "3.5" assert val.unsaturated_zone_moisture == "0.40" assert val.upper_evaporation_pattern == "" val = section_from_text.value[1] assert val.name == "1" assert val.porosity == "2" assert val.wilting_point == "3" assert val.field_capacity == "4" assert val.conductivity == "5" assert val.conductivity_slope == "6" assert val.tension_slope == "7" assert val.upper_evaporation_fraction == "8" assert val.lower_evaporation_depth == "9" assert val.lower_groundwater_loss_rate == "10" assert val.bottom_elevation == "11" assert val.water_table_elevation == "12" assert val.unsaturated_zone_moisture == "13" assert val.upper_evaporation_pattern == "14" assert Aquifer.metadata.label_of("name") == "Aquifer Name" assert Aquifer.metadata.hint_of( "name") == "User-assigned aquifer name."
def test_gwf(self): """Test GWF section using the Project class""" source_text = '\n'.join(self.TEST_TEXT) project_reader = ProjectReader() project_writer = ProjectWriter() section_from_text = project_reader.read_gwf.read(source_text) actual_text = project_writer.write_gwf.as_text(section_from_text) msg = '\nSet:' + source_text + '\nGet:' + actual_text self.assertTrue(match_omit(actual_text, source_text, " \t-;\n"), msg) val = section_from_text.value[0] assert val.subcatchment_name == "CA-11" assert val.groundwater_flow_type == GroundwaterFlowType.LATERAL assert val.custom_equation == ''.join( "0.001 * (Hgw - 5) * STEP(Hgw - 5)".split()) # remove formatting
def setUp(self): """""" self.project_reader = ProjectReader() self.project_writer = ProjectWriter()
def runTest(self): directory = os.path.dirname( os.path.abspath( inspect.getframeinfo(inspect.currentframe()).filename)) inp_filename = directory + '\\' + "CrossSectionAllShapes.inp" with open(inp_filename, "r") as f: source_text = f.read() project_reader = ProjectReader() project_writer = ProjectWriter() my_project = SwmmProject() project_reader.read_file(my_project, os.path.join(directory, inp_filename)) my_xsections = my_project.xsections assert len(my_xsections.value) == 26 actual_text = project_writer.write_xsections.as_text(my_xsections) msg = '\nSet:\n' + source_text + '\nGet:\n' + actual_text self.assertTrue(match(actual_text, source_text), msg) # my_copy = project_reader.read_xsections(section_text) # copy_text = project_writer.write_xsections.as_text(my_copy) # my_copy.set_text(my_xsections.get_text()) # assert match(section_text, copy_text) for index in range( 1, 25): # Purposely not checking last line: second CUSTOM test assert my_xsections.value[index].link == str(index) assert my_xsections.value[index].shape == CrossSectionShape(index) cur_section = my_xsections.value[1] assert cur_section.geometry1 == "1.5" assert cur_section.geometry2 == "0" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "1" cur_section = my_xsections.value[2] assert cur_section.geometry1 == "11" assert cur_section.geometry2 == "12" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "6" cur_section = my_xsections.value[3] assert cur_section.geometry1 == "9" assert cur_section.geometry2 == "13" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "6" cur_section = my_xsections.value[4] assert cur_section.geometry1 == "14" assert cur_section.geometry2 == "15" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "5" cur_section = my_xsections.value[5] assert cur_section.geometry1 == "1" assert cur_section.geometry2 == "4" assert cur_section.geometry3 == "2" assert cur_section.geometry4 == "0" assert cur_section.barrels == "1" cur_section = my_xsections.value[6] assert cur_section.geometry1 == "3" assert cur_section.geometry2 == "4" assert cur_section.geometry3 == "5" assert cur_section.geometry4 == "6" assert cur_section.barrels == "2" cur_section = my_xsections.value[7] assert cur_section.geometry1 == "4" assert cur_section.geometry2 == "6" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "2" cur_section = my_xsections.value[8] assert cur_section.geometry1 == "1.167" assert cur_section.geometry2 == "1.917" assert cur_section.geometry3 == "1" assert cur_section.geometry4 == "0" assert cur_section.barrels == "9" cur_section = my_xsections.value[9] assert cur_section.geometry1 == "2.833" assert cur_section.geometry2 == "1.833" assert cur_section.geometry3 == "3" assert cur_section.geometry4 == "0" assert cur_section.barrels == "1" cur_section = my_xsections.value[10] assert cur_section.geometry1 == "1.250" assert cur_section.geometry2 == "1.750" assert cur_section.geometry3 == "19" assert cur_section.geometry4 == "0" assert cur_section.barrels == "10" cur_section = my_xsections.value[11] assert cur_section.geometry1 == "5" assert cur_section.geometry2 == "7" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "3" cur_section = my_xsections.value[12] assert cur_section.geometry1 == "13" assert cur_section.geometry2 == "15" assert cur_section.geometry3 == "17" assert cur_section.geometry4 == "0" assert cur_section.barrels == "2" cur_section = my_xsections.value[13] assert cur_section.geometry1 == "17" assert cur_section.geometry2 == "18" assert cur_section.geometry3 == "19" assert cur_section.geometry4 == "0" assert cur_section.barrels == "1" cur_section = my_xsections.value[14] assert cur_section.geometry1 == "19" assert cur_section.geometry2 == "20" assert cur_section.geometry3 == "21" assert cur_section.geometry4 == "0" assert cur_section.barrels == "13" cur_section = my_xsections.value[15] assert cur_section.geometry1 == "21" assert cur_section.geometry2 == "22" assert cur_section.geometry3 == "23" assert cur_section.geometry4 == "0" assert cur_section.barrels == "20" cur_section = my_xsections.value[16] assert cur_section.geometry1 == "24" assert cur_section.geometry2 == "0" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "23" cur_section = my_xsections.value[17] assert cur_section.geometry1 == "26" assert cur_section.geometry2 == "0" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "25" cur_section = my_xsections.value[18] assert int(float(cur_section.geometry1)) == 47 assert cur_section.geometry2 == "0" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "1" cur_section = my_xsections.value[19] assert int(cur_section.geometry1) == 19 assert cur_section.geometry2 == "0" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "6" cur_section = my_xsections.value[20] assert int(cur_section.geometry1) == 20 assert cur_section.geometry2 == "0" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "6" cur_section = my_xsections.value[21] assert cur_section.geometry1 == "17" assert cur_section.geometry2 == "0" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "7" cur_section = my_xsections.value[22] assert cur_section.geometry1 == "1.222" assert cur_section.geometry2 == "0" assert cur_section.geometry3 == "0" assert cur_section.geometry4 == "0" assert cur_section.barrels == "11" cur_section = my_xsections.value[23] # assert cur_section.transect == "Tran1" assert cur_section.geometry1 == "Tran1" cur_section = my_xsections.value[24] assert cur_section.geometry1 == "2.833" assert cur_section.curve == "curve1" assert cur_section.barrels == "4" cur_section = my_xsections.value[25] assert cur_section.geometry1 == "2.933" assert cur_section.curve == "curve2" assert cur_section.barrels == "5"
def __init__(self): """""" unittest.TestCase.__init__(self) self.project_reader = ProjectReader() self.project_writer = ProjectWriter()
def runTest(self): directory = os.path.dirname( os.path.abspath( inspect.getframeinfo(inspect.currentframe()).filename)) # Get path to swmm5.exe source_root_path = os.path.split( os.path.split(os.path.split(directory)[0])[0])[0] exe_path = os.path.join(source_root_path, "src", "Externals", "swmm", "model") exe_name = "swmm5.exe" exe_full_path = os.path.join(exe_path, exe_name) # Get example path and the paths to its first level sub directories # Assuming all tests are under Examples or immediate level sub directories example_root_path = os.path.join(directory, "Examples") example_paths = [] example_paths.append(example_root_path) example_sub_directories = ProjectTest.get_immediate_subdirectories( example_root_path) if example_sub_directories: for subdir in example_sub_directories: example_paths.append(os.path.join(example_root_path, subdir)) # Loop through all .inp files in /Examples and first-level sub directories # Record results in diff_summaries.txt under /Examples # Need to use assert -- Thinking # diff_file = os.path.join(example_root_path, "diff_summaries.txt") examples = [] # example name status = [] # example test status 'Pass' or 'Fail' remarks = [] # error message goes here html_file = os.path.join(directory, 'Test_Example_Summaries_SWMM.html') # Open html file text_file = open(html_file, "w") text_file.write('<header><h1>' + 'EXAMPLES TEST REPORT:' + '</h1></header>') # Test all examples in /Examples or first level sub directory of /Examples for example_path in example_paths: # Get current_directory for restoring it later current_directory = os.getcwd() # current test/core/ # Get example directory os.chdir(example_path) # Get all file names under this example directory example_files = os.listdir(example_path) # Copy the exe file to each example directory # exe_to_example_path = os.path.join(example_path, exe_name) # shutil.copy(exe_full_path, exe_to_example_path) # Loop through all .inp files in the /Examples folder for filename in example_files: prefix, extension = os.path.splitext(filename) if extension.lower() == ".inp": # Read .inp file, count the number of sections my_file = os.path.join(example_path, filename) ProjectReader().read_file(self.my_project, my_file) number_of_sections = len(self.my_project.sections) # Write my_project to new file .inptest new_filename = filename + "_copy" new_file = os.path.join(example_path, new_filename) ProjectWriter().write_file(self.my_project, new_file) # Read .inptest into new_project, count the number of sections, assert ProjectReader().read_file(self.new_project, new_file) new_number_of_sections = len(self.new_project.sections) assert number_of_sections == new_number_of_sections # If the numbers of sections agree, run swmm5.exe using: # swmm5.exe XXXXX1.inp XXXXXX1.rpt XXXXXX1.out # Have trouble with path, copied swmm5.exe into the Examples folder if number_of_sections == new_number_of_sections: # Run my_file inp_file = filename rpt_file = prefix + '.rpt' out_file = prefix + '.out' command_line = '"' + exe_full_path + '" ' + inp_file + ' ' + rpt_file + ' ' + out_file try: os.system(command_line) except: msg = "Error in executing{}".format(command_line) examples.append(my_file) status.append('----') remarks.append(msg) pass # Run new_file (copy of original .inp) # Must rename the .inp_copy to renameit.inp # swmm5.exe requires .inp as input files # but the duplicates should not be named as .inp # They will be renamed to .inp_copy after the run temp_file = "renameit.inp" try: os.remove(temp_file) except OSError: pass os.rename(new_filename, temp_file) inp_file = temp_file rpt_file = prefix + '_copy' + '.rpt' out_file = prefix + '_copy' + '.out' command_line = '"' + exe_full_path + '" ' + inp_file + ' ' + rpt_file + ' ' + out_file try: os.system(command_line) except: msg = "Error in executing{}".format(command_line) examples.append(new_file) status.append('----') remarks.append(msg) pass # Compare two .out files original_ = os.path.join(example_path, prefix + '.out') copy_ = os.path.join(example_path, prefix + '_copy' + '.out') # If both exists if os.path.isfile(original_) and os.path.isfile(copy_): same_ = filecmp.cmp(original_, copy_) if not same_: msg = prefix+'.out binary differ, ' \ 'results of modified differ from results of original\n' examples.append(original_) status.append('Fail') remarks.append(msg) else: msg = prefix + '.out successful matching\n' examples.append(original_) status.append('Pass') remarks.append(msg) # If the original does not produce .out '----' elif not os.path.isfile(original_): msg = prefix + '.out does not exist, ' \ 'original input did not run\n' examples.append(original_) status.append('----') remarks.append(msg) # If the original does but the copy does not produce .out 'Fail' else: msg = prefix + '_copy.out does not exist, ' \ 'original ran but modified did not \n' examples.append(original_) status.append('Fail') remarks.append(msg) # Compare two rpt files exempted_strings = [ "Analysis begun on", "Analysis ended on", "Total elapsed time", "ERROR" ] original_ = os.path.join(example_path, prefix + '.rpt') copy_ = os.path.join(example_path, prefix + '_copy' + '.rpt') if os.path.isfile(original_) and os.path.isfile(copy_): diff_msg = ProjectTest.compare_two_files( original_, copy_, exempted_strings) if diff_msg: msg = prefix + '.rpt:'\ +'results of modified differ from results of original'+'\n'\ + diff_msg + '\n' examples.append(original_) status.append('Fail') remarks.append(msg) else: msg = prefix + '.rpt successful matching' + '\n' examples.append(original_) status.append('Pass') remarks.append(msg) # If the original does not produce .out '----' elif not os.path.isfile(original_): msg = prefix + '.rpt does not exist, ' \ 'original input did not run\n' examples.append(original_) status.append('----') remarks.append(msg) # If the original does but the copy does not produce .out 'Fail' else: msg = prefix + '_copy.rpt does not exist,' \ 'original ran but modified did not\n' examples.append(original_) status.append('Fail') remarks.append(msg) # Rename file to .inp_test because use .inp only for originals os.rename(temp_file, new_filename) # Do clean-up in the example directory: # try: # # Remove swmm5.exe from the example path # # os.remove(exe_to_example_path) # # Keep the outputs for examining them. # # os.remove(original_) # # os.remove(copy_) # except: # pass # Return to current directory -- May not need since relative directory was not used anywhere os.chdir(current_directory) # Write HTML table # Print opening HTML tags ------------------------- text_file.write("<html><body><table border='1'>") # Print the content of the table, line by line ---- for i in range(0, len(examples)): cur_status = status[i] if cur_status == 'Fail': cur_status = '<font color="red">' + status[i] + '</font>' text_file.write( "<tr><td>" + examples[i].replace(example_root_path, '').strip(os.pathsep) + "</td><td>" + cur_status + "</td><td>" + remarks[i].replace(example_root_path.strip(os.pathsep), '') + "</td></tr>") # Print closing HTML tags ------------------------- text_file.write("</table></body></html>") text_file.close() try: webbrowser.open_new_tab('file://' + html_file) except: print("Error writing test results to " + html_file)