def test_open_file_dual(): FILE_NAME = 'main_lp.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) output_file = os.path.join(CUR_DIR, 'files/output_tests/output_dual.txt') parser = AplosParser(filename=test_file) matrices = parser.read_matrices_from_file(output_file, dual=True) ex_A = [[1, 2], [2, 5]] ex_b = [3, 2] ex_c = [9, 4] ex_Eqin = [1, 1] ex_minmax = [-1] ex_varconstr = [1, 1] ex = { 'A': ex_A, 'b': ex_b, 'c': ex_c, "Eqin": ex_Eqin, 'MinMax': ex_minmax, 'VarConstr': ex_varconstr } assert ex == matrices
def test_dim_empty(): with pytest.warns(RuntimeWarning): parser = AplosParser(text=' ') with pytest.raises(exceptions.EmptyLPException): parser.get_dimensions()
def test_get_matrix_errors(): FILE_NAME = 'error_lp_1.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) parser = AplosParser(filename=test_file) with pytest.raises(exceptions.LPErrorException): A = parser.get_matrix('A')
def test_get_matrix_empty(): with pytest.warns(RuntimeWarning): parser = AplosParser(text='') with pytest.raises(exceptions.EmptyLPException): if not parser.detect_errors(): A = parser.get_matrix('A')
def test_dim_no_detect_errors(): FILE_NAME = 'main_lp.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) parser = AplosParser(filename=test_file) with pytest.raises(exceptions.LPErrorException): parser.get_dimensions()
def test_save_to_file_empty(): with pytest.warns(RuntimeWarning): parser = AplosParser(text='') CUR_DIR = os.path.dirname(os.path.abspath(__file__)) output_file = os.path.join(CUR_DIR, 'files/output_tests/output.txt') with pytest.raises(exceptions.EmptyLPException): parser.write_matrices_to_file(output_file)
def test_open_file_error(): FILE_NAME = 'main_lp.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) output_file = os.path.join(CUR_DIR, 'files/output_tests/output_wrong.txt') parser = AplosParser(filename=test_file) with pytest.raises(exceptions.LPReadException): matrices = parser.read_matrices_from_file(output_file)
def test_open_file_non_existing(): FILE_NAME = 'main_lp.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) parser = AplosParser(filename=test_file) with pytest.raises(IOError): matrices = parser.read_matrices_from_file('a_path')
def test_get_matrix_no_arg(): FILE_NAME = 'main_lp.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) parser = AplosParser(filename=test_file) if not parser.detect_errors(): with pytest.raises(exceptions.MissingArgumentsException): A = parser.get_matrix()
def test_det_errors_no_errors(): FILE_NAME = 'main_lp.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) parser = AplosParser(filename=test_file) errors = parser.detect_errors() assert errors == []
def test_save_to_file_errors(): FILE_NAME = 'error_lp_1.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) parser = AplosParser(filename=test_file) with pytest.raises(exceptions.LPErrorException): output_file = os.path.join(CUR_DIR, 'files/output_tests/output.txt') parser.write_matrices_to_file(output_file)
def test_dim_normal(): FILE_NAME = 'secondary_lp.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) parser = AplosParser(filename=test_file) if not parser.detect_errors(): dims = parser.get_dimensions() expected = {'m': 2, 'n': 6} assert dims == expected
def test_get_matrix(): FILE_NAME = 'main_lp.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/'+FILE_NAME) parser = AplosParser(filename=test_file) if not parser.detect_errors(): A = parser.get_dual_matrix('A') b = parser.get_dual_matrix('b') c = parser.get_dual_matrix('c') Eqin = parser.get_dual_matrix('EQin') minmax = parser.get_dual_matrix('minMAX') var_constr = parser.get_dual_matrix('var_constr') expected_A = [[1,2],[2,5]] expected_b = [3,2] expected_c = [9,4] expected_Eqin = [1,1] expected_minmax = [-1] expected_var_constr = [1,1] assert A == expected_A and \ b == expected_b and \ c == expected_c and \ Eqin == expected_Eqin and \ minmax == expected_minmax and \ var_constr == expected_var_constr
def test_save_to_file_normal(): FILE_NAME = 'main_lp.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) parser = AplosParser(filename=test_file) if not parser.detect_errors(): output_file = os.path.join(CUR_DIR, 'files/output_tests/output.txt') expected_file = os.path.join(CUR_DIR, 'files/matrices_file.txt') parser.write_matrices_to_file(output_file) with open(output_file, 'r') as of, open(expected_file, 'r') as ef: assert of.read() == ef.read()
def test_det_errors_normal(): FILE_NAME = 'error_lp_2.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) with pytest.warns(RuntimeWarning): parser = AplosParser(filename=test_file) errors = parser.detect_errors() expected = [] expected.append('Min/Max is not specified for object function') expected.append("Constraint initializer 's.t' or similar is missing") assert errors == expected
def test_det_errors_no_end(): FILE_NAME = 'error_lp_1.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) with pytest.warns(RuntimeWarning): parser = AplosParser(filename=test_file) errors = parser.detect_errors(print_msg=True) expected = [] expected.append('Min/Max is not specified for object function') expected.append('No END statement found') expected.append('Constraint type missing from some constraints') expected.append('Right side argument missing from some constraints') assert errors == expected
def test_open_file_not_exists(): FILE_NAME = 'NOT_EXISTS.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) with pytest.raises(IOError): parser = AplosParser(filename=test_file)
def test_text_wrong_delimeter(): text = '''Max 3x1 +2x2| s.t. x1+2x2<=9 |2 x1+5x2<=4|End''' parser = AplosParser(text=text, delimeter='.') res = [] res.append('Max3x1+2x2|s') res.append('t') res.append('x1+2x2<=9|2x1+5x2<=4|End') assert parser.lp_lines == res
def test_open_file_no_lines(): FILE_NAME = 'empty.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) with pytest.warns(RuntimeWarning): parser = AplosParser(filename=test_file) res = [] assert parser.lp_lines == res
def test_text_custom_delimeter_line(): text = '''Max 3x1 +2x2| s.t. x1+2x2<=9 |2 x1+5x2<=4|End''' parser = AplosParser(text=text, delimeter='|') res = [] res.append('Max3x1+2x2') res.append('s.t.x1+2x2<=9') res.append('2x1+5x2<=4') res.append('End') assert parser.lp_lines == res
def test_text_custom_delimeter_comma(): text = '''Max 3x1 +2x2,s.t. x1+2x2<=9,2x1+5x2<=4,End''' parser = AplosParser(text=text, delimeter=',') res = [] res.append('Max3x1+2x2') res.append('s.t.x1+2x2<=9') res.append('2x1+5x2<=4') res.append('End') assert parser.lp_lines == res
def test_open_file_exists(): FILE_NAME = 'main_lp.txt' CUR_DIR = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME) parser = AplosParser(filename=test_file) res = [] res.append('Max3x1+2x2') res.append('s.t.x1+2x2<=9') res.append('2x1+5x2<=4') res.append('End') assert parser.lp_lines == res
def test_normal_text(): text = '''Max 3x1 +2x2 s.t. x1+2x2<=9 2x1+5x2<=4 End''' parser = AplosParser(text=text) res = [] res.append('Max3x1+2x2') res.append('s.t.x1+2x2<=9') res.append('2x1+5x2<=4') res.append('End') assert parser.lp_lines == res
def test_text_no_arguments(): with pytest.raises(exceptions.MissingArgumentsException): parser = AplosParser()
def test_empty_text(): with pytest.warns(RuntimeWarning): parser = AplosParser(text='')
def test_det_errors_empty(): with pytest.warns(RuntimeWarning), pytest.raises( exceptions.EmptyLPException): parser = AplosParser(text=' ') errors = parser.detect_errors()