def test_lines_with_targets_and_source(self, level: int, targets: List[str]): x = py.Line.INDENT_UNIT source = "bar" assert [str(l) for l in py.Import(targets, source).lines(level)] == [ x * level + f"from {source} import {target}" for target in targets ]
def test_lines_with_hidden_comments(self, level: int): x = py.Line.INDENT_UNIT stmt = py.Import(["a", "b", "c"], comments=["1", "2"]) assert [str(l) for l in stmt.lines(level, comments=False)] == [ x * level + "import a", x * level + "import b", x * level + "import c", ]
def locust_imports() -> py.Program: is_post_1 = py.BinaryOp(py.Symbol("LOCUST_MAJOR_VERSION"), ">=", py.Literal(1),) imports_pre_1 = [ py.Import( ["HttpLocust", "TaskSequence", "TaskSet", "seq_task", "task"], source="locust", ) ] imports_post_1 = [ py.Import( ["HttpUser", "SequentialTaskSet", "TaskSet", "task"], source="locust", ), py.Assignment("HttpLocust", py.Symbol("HttpUser")), py.Assignment("TaskSequence", py.Symbol("SequentialTaskSet")), py.Function("seq_task", ["_"], [py.Return(py.Symbol("task"))]), ] return [ py.IfElse([(is_post_1, imports_post_1)], imports_pre_1), ]
def locust_program(scenarios: Sequence[Scenario]) -> py.Program: """ Converts a ScenarioGroup into a Locust File. """ global_code_blocks = { # TODO: Replace me with a plugin framework that accesses the full tree. # See https://github.com/zalando-incubator/Transformer/issues/11. block_name: py.OpaqueBlock("\n".join(block), comments=[block_name]) for scenario in scenarios for block_name, block in scenario.global_code_blocks.items() } return [ py.Import(["re"], comments=[LOCUSTFILE_COMMENT]), py.Import( ["HttpLocust", "TaskSequence", "TaskSet", "seq_task", "task"], source="locust", ), *locust_classes(scenarios), *global_code_blocks.values(), ]
def locust_detected_version() -> py.Program: return [ py.Import(["LooseVersion"], source="distutils.version"), py.Import(["__version__"], source="locust"), py.OpaqueBlock("LOCUST_MAJOR_VERSION = LooseVersion(__version__).version[0]"), ]
def test_repr(self): stmt = py.Import(targets=["a", "b"], comments=["hi"]) assert ( repr(stmt) == f"Import(targets=['a', 'b'], source=None, alias=None, comments=['hi'])" )
def test_lines_with_multiple_targets_and_alias_raises_error( self, level: int): i = py.Import(["safe"], alias="A") i.targets.append("oops") with pytest.raises(ValueError): i.lines(level)
def test_init_with_multiple_targets_and_alias_raises_error(self): with pytest.raises(ValueError): py.Import(["a", "b"], alias="c")
def test_lines_with_single_target_and_alias(self, level: int): x = py.Line.INDENT_UNIT name = "transformer.python" alias = "py" assert [str(l) for l in py.Import([name], alias=alias).lines(level) ] == [x * level + f"import {name} as {alias}"]
def test_lines_with_multiple_targets(self, level: int): x = py.Line.INDENT_UNIT names = ["locust.http", "math", "a.b.c"] assert [str(l) for l in py.Import(names).lines(level) ] == [x * level + f"import {name}" for name in names]
def test_lines_with_single_target(self, level: int): x = py.Line.INDENT_UNIT name = "locust.http" assert [str(l) for l in py.Import([name]).lines(level) ] == [x * level + f"import {name}"]
def test_lines_without_targets_raises_error(self, level: int): i = py.Import(["safe"]) i.targets.clear() with pytest.raises(ValueError): i.lines(level)
def test_init_without_targets_raises_error(self): with pytest.raises(ValueError): py.Import([])