def test_exec_module_no_contracts(): text = """ import deal print(1) """ text = dedent(text) DealLoader(loader=SubLoader(ok=True, text=text)).exec_module(FakeModule) with pytest.raises(FakeException): DealLoader(loader=SubLoader(ok=False, text=text)).exec_module(FakeModule)
def test_exec_module(): text = """ import deal deal.module_load(deal.has()) print(1) """ text = dedent(text) with pytest.raises(FakeException): DealLoader(loader=SubLoader(ok=False, text=text)).exec_module(FakeModule) with pytest.raises(deal.SilentContractError): DealLoader(loader=SubLoader(ok=True, text=text)).exec_module(FakeModule)
def test_not_path_loader(): class SubLoaderNoGetSource: def exec_module(self, module): assert module is TestModule raise TestException with pytest.raises(TestException): DealLoader(loader=SubLoaderNoGetSource()).exec_module(TestModule)
def test_exec_module_invalid_contract_called(): text = """ import deal deal.module_load(something()) print(1) """ text = dedent(text) with pytest.raises(RuntimeError, match='unsupported contract:.+'): DealLoader(loader=SubLoader(ok=False, text=text)).exec_module(FakeModule)
def test_get_contracts(): text = """ import deal a = 1 1 / 0 not_a_deal.module_load(something) deal.module_load(deal.silent) """ text = dedent(text) tree = ast.parse(text) print(ast.dump(tree)) nodes = DealLoader._get_contracts(tree=tree) assert [get_name(node) for node in nodes] == ['deal.silent']
def test_exec_contract(text, expected): tree = ast.parse(text) print(ast.dump(tree)) actual = DealLoader._exec_contract(node=tree.body[0].value) assert actual == expected
def test_exec_module_no_source(): text = None DealLoader(loader=SubLoader(ok=True, text=text)).exec_module(TestModule) with pytest.raises(TestException): DealLoader( loader=SubLoader(ok=False, text=text)).exec_module(TestModule)