|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import unittest |
| 4 | +from contextlib import redirect_stdout |
| 5 | +from io import StringIO |
| 6 | +from onnx import TensorProto |
| 7 | +from onnx.helper import ( |
| 8 | + make_graph, |
| 9 | + make_model, |
| 10 | + make_node, |
| 11 | + make_opsetid, |
| 12 | + make_tensor_value_info, |
| 13 | +) |
| 14 | +from onnx_array_api.ext_test_case import ExtTestCase |
| 15 | +from onnx_array_api._command_lines_parser import ( |
| 16 | + get_main_parser, |
| 17 | + get_parser_translate, |
| 18 | + main, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class TestCommandLines1(ExtTestCase): |
| 23 | + def test_main_parser(self): |
| 24 | + st = StringIO() |
| 25 | + with redirect_stdout(st): |
| 26 | + get_main_parser().print_help() |
| 27 | + text = st.getvalue() |
| 28 | + self.assertIn("translate", text) |
| 29 | + |
| 30 | + def test_parser_translate(self): |
| 31 | + st = StringIO() |
| 32 | + with redirect_stdout(st): |
| 33 | + get_parser_translate().print_help() |
| 34 | + text = st.getvalue() |
| 35 | + self.assertIn("model", text) |
| 36 | + |
| 37 | + def test_command_translate(self): |
| 38 | + X = make_tensor_value_info("X", TensorProto.FLOAT, [None, None]) |
| 39 | + Y = make_tensor_value_info("Y", TensorProto.FLOAT, [5, 6]) |
| 40 | + Z = make_tensor_value_info("Z", TensorProto.FLOAT, [None, None]) |
| 41 | + graph = make_graph( |
| 42 | + [ |
| 43 | + make_node("Add", ["X", "Y"], ["res"]), |
| 44 | + make_node("Cos", ["res"], ["Z"]), |
| 45 | + ], |
| 46 | + "g", |
| 47 | + [X, Y], |
| 48 | + [Z], |
| 49 | + ) |
| 50 | + onnx_model = make_model(graph, opset_imports=[make_opsetid("", 18)]) |
| 51 | + |
| 52 | + with tempfile.TemporaryDirectory() as root: |
| 53 | + model_file = os.path.join(root, "model.onnx") |
| 54 | + with open(model_file, "wb") as f: |
| 55 | + f.write(onnx_model.SerializeToString()) |
| 56 | + |
| 57 | + args = ["translate", "-m", model_file] |
| 58 | + st = StringIO() |
| 59 | + with redirect_stdout(st): |
| 60 | + main(args) |
| 61 | + |
| 62 | + code = st.getvalue() |
| 63 | + self.assertIn("model = make_model(", code) |
| 64 | + |
| 65 | + args = ["translate", "-m", model_file, "-a", "light"] |
| 66 | + st = StringIO() |
| 67 | + with redirect_stdout(st): |
| 68 | + main(args) |
| 69 | + |
| 70 | + code = st.getvalue() |
| 71 | + self.assertIn("start(opset=", code) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + unittest.main(verbosity=2) |
0 commit comments