|
| 1 | +import ast |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from ariadne_codegen.codegen import generate_arguments, generate_method_definition |
| 5 | + |
| 6 | + |
| 7 | +def test_generate_method_definition_with_minimal_parameters(): |
| 8 | + """Test generate_method_definition with only required parameters.""" |
| 9 | + name = "test_method" |
| 10 | + arguments = generate_arguments() |
| 11 | + return_type = ast.Name(id="str") |
| 12 | + |
| 13 | + result = generate_method_definition(name, arguments, return_type) |
| 14 | + |
| 15 | + assert isinstance(result, ast.FunctionDef) |
| 16 | + assert result.name == name |
| 17 | + assert result.args == arguments |
| 18 | + assert result.returns == return_type |
| 19 | + assert result.lineno == 1 |
| 20 | + assert len(result.body) == 1 |
| 21 | + assert isinstance(result.body[0], ast.Pass) |
| 22 | + assert result.decorator_list == [] |
| 23 | + |
| 24 | + |
| 25 | +def test_generate_method_definition_with_description(): |
| 26 | + """Test generate_method_definition with description adds docstring.""" |
| 27 | + name = "test_method" |
| 28 | + arguments = generate_arguments() |
| 29 | + return_type = ast.Name(id="str") |
| 30 | + description = "This is a test method" |
| 31 | + |
| 32 | + result = generate_method_definition( |
| 33 | + name, arguments, return_type, description=description |
| 34 | + ) |
| 35 | + |
| 36 | + assert len(result.body) == 2 |
| 37 | + assert isinstance(result.body[0], ast.Expr) |
| 38 | + assert isinstance(result.body[0].value, ast.Constant) |
| 39 | + assert result.body[0].value.value == description |
| 40 | + assert isinstance(result.body[1], ast.Pass) |
| 41 | + |
| 42 | + |
| 43 | +def test_generate_method_definition_with_custom_body(): |
| 44 | + """Test generate_method_definition with custom body.""" |
| 45 | + name = "test_method" |
| 46 | + arguments = generate_arguments() |
| 47 | + return_type = ast.Name(id="str") |
| 48 | + body = [ast.Return(value=ast.Constant(value="test"))] |
| 49 | + |
| 50 | + result = generate_method_definition(name, arguments, return_type, body=body) |
| 51 | + |
| 52 | + assert result.body == body |
| 53 | + assert len(result.body) == 1 |
| 54 | + assert isinstance(result.body[0], ast.Return) |
| 55 | + |
| 56 | + |
| 57 | +def test_generate_method_definition_with_description_and_custom_body(): |
| 58 | + """Test generate_method_definition with both description and custom body.""" |
| 59 | + name = "test_method" |
| 60 | + arguments = generate_arguments() |
| 61 | + return_type = ast.Name(id="str") |
| 62 | + description = "Test description" |
| 63 | + body = [ast.Return(value=ast.Constant(value="test"))] |
| 64 | + |
| 65 | + result = generate_method_definition( |
| 66 | + name, arguments, return_type, description=description, body=body |
| 67 | + ) |
| 68 | + |
| 69 | + assert len(result.body) == 2 |
| 70 | + assert isinstance(result.body[0], ast.Expr) |
| 71 | + assert result.body[0].value.value == description |
| 72 | + assert isinstance(result.body[1], ast.Return) |
| 73 | + |
| 74 | + |
| 75 | +def test_generate_method_definition_with_custom_lineno(): |
| 76 | + """Test generate_method_definition with custom line number.""" |
| 77 | + name = "test_method" |
| 78 | + arguments = generate_arguments() |
| 79 | + return_type = ast.Name(id="str") |
| 80 | + lineno = 42 |
| 81 | + |
| 82 | + result = generate_method_definition(name, arguments, return_type, lineno=lineno) |
| 83 | + |
| 84 | + assert result.lineno == lineno |
| 85 | + |
| 86 | + |
| 87 | +def test_generate_method_definition_with_decorators(): |
| 88 | + """Test generate_method_definition with decorator list.""" |
| 89 | + name = "test_method" |
| 90 | + arguments = generate_arguments() |
| 91 | + return_type = ast.Name(id="str") |
| 92 | + decorators = [ast.Name(id="property"), ast.Name(id="staticmethod")] |
| 93 | + |
| 94 | + result = generate_method_definition( |
| 95 | + name, arguments, return_type, decorator_list=decorators |
| 96 | + ) |
| 97 | + |
| 98 | + assert result.decorator_list == decorators |
| 99 | + |
| 100 | + |
| 101 | +def test_generate_method_definition_with_subscript_return_type(): |
| 102 | + """Test generate_method_definition with Subscript return type.""" |
| 103 | + name = "test_method" |
| 104 | + arguments = generate_arguments() |
| 105 | + return_type = ast.Subscript(value=ast.Name(id="List"), slice=ast.Name(id="str")) |
| 106 | + |
| 107 | + result = generate_method_definition(name, arguments, return_type) |
| 108 | + |
| 109 | + assert result.returns == return_type |
| 110 | + assert isinstance(result.returns, ast.Subscript) |
| 111 | + |
| 112 | + |
| 113 | +@mock.patch("sys.version_info", (3, 12, 0)) |
| 114 | +def test_generate_method_definition_python_312_or_later(): |
| 115 | + """Test generate_method_definition includes type_params for Python 3.12+.""" |
| 116 | + name = "test_method" |
| 117 | + arguments = generate_arguments() |
| 118 | + return_type = ast.Name(id="str") |
| 119 | + |
| 120 | + result = generate_method_definition(name, arguments, return_type) |
| 121 | + |
| 122 | + assert hasattr(result, "type_params") |
| 123 | + assert result.type_params == [] |
| 124 | + |
| 125 | + |
| 126 | +@mock.patch("sys.version_info", (3, 11, 0)) |
| 127 | +def test_generate_method_definition_python_311_or_earlier(): |
| 128 | + """Test generate_method_definition doesn't include type_params for Python < 3.12.""" |
| 129 | + name = "test_method" |
| 130 | + arguments = generate_arguments() |
| 131 | + return_type = ast.Name(id="str") |
| 132 | + |
| 133 | + result = generate_method_definition(name, arguments, return_type) |
| 134 | + |
| 135 | + # In Python < 3.12, type_params shouldn't be set |
| 136 | + # We check that the function still works correctly |
| 137 | + assert isinstance(result, ast.FunctionDef) |
| 138 | + assert result.name == name |
| 139 | + |
| 140 | + |
| 141 | +def test_generate_method_definition_all_parameters(): |
| 142 | + """Test generate_method_definition with all parameters specified.""" |
| 143 | + name = "complex_method" |
| 144 | + arguments = generate_arguments() |
| 145 | + return_type = ast.Subscript(value=ast.Name(id="Optional"), slice=ast.Name(id="int")) |
| 146 | + description = "A complex method with all parameters" |
| 147 | + body = [ |
| 148 | + ast.Assign(targets=[ast.Name(id="x")], value=ast.Constant(value=1)), |
| 149 | + ast.Return(value=ast.Name(id="x")), |
| 150 | + ] |
| 151 | + lineno = 10 |
| 152 | + decorators = [ast.Name(id="classmethod")] |
| 153 | + |
| 154 | + result = generate_method_definition( |
| 155 | + name, arguments, return_type, description, body, lineno, decorators |
| 156 | + ) |
| 157 | + |
| 158 | + assert result.name == name |
| 159 | + assert result.args == arguments |
| 160 | + assert result.returns == return_type |
| 161 | + assert result.lineno == lineno |
| 162 | + assert result.decorator_list == decorators |
| 163 | + assert len(result.body) == 3 # docstring + 2 body statements |
| 164 | + assert isinstance(result.body[0], ast.Expr) # docstring |
| 165 | + assert result.body[0].value.value == description |
0 commit comments