genlayer-test 0.1.3__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/PKG-INFO +1 -1
  2. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/genlayer_test.egg-info/PKG-INFO +1 -1
  3. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/genlayer_test.egg-info/SOURCES.txt +2 -0
  4. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/genlayer_test.egg-info/top_level.txt +1 -0
  5. genlayer_test-0.2.0/gltest/artifacts/__init__.py +6 -0
  6. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/artifacts/contract.py +87 -12
  7. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/glchain/contract.py +41 -5
  8. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/pyproject.toml +1 -1
  9. genlayer_test-0.2.0/tests/artifact/contracts/duplicate_ic_contract_1.py +22 -0
  10. genlayer_test-0.2.0/tests/artifact/contracts/duplicate_ic_contract_2.py +22 -0
  11. genlayer_test-0.2.0/tests/artifact/test_contract_definition.py +347 -0
  12. genlayer_test-0.1.3/gltest/artifacts/__init__.py +0 -3
  13. genlayer_test-0.1.3/tests/artifact/test_contract_definition.py +0 -92
  14. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/LICENSE +0 -0
  15. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/README.md +0 -0
  16. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/genlayer_test.egg-info/dependency_links.txt +0 -0
  17. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/genlayer_test.egg-info/entry_points.txt +0 -0
  18. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/genlayer_test.egg-info/requires.txt +0 -0
  19. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/__init__.py +0 -0
  20. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/assertions.py +0 -0
  21. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/exceptions.py +0 -0
  22. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/glchain/__init__.py +0 -0
  23. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/glchain/account.py +0 -0
  24. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/glchain/client.py +0 -0
  25. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/helpers/__init__.py +0 -0
  26. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/helpers/fixture_snapshot.py +0 -0
  27. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/helpers/take_snapshot.py +0 -0
  28. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/plugin_config.py +0 -0
  29. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/plugin_hooks.py +0 -0
  30. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest/types.py +0 -0
  31. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/gltest_cli/main.py +0 -0
  32. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/setup.cfg +0 -0
  33. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/artifact/contracts/not_ic_contract.py +0 -0
  34. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/assertions/test_assertions.py +0 -0
  35. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/football_prediction_market.py +0 -0
  36. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/intelligent_oracle.py +0 -0
  37. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/intelligent_oracle_factory.py +0 -0
  38. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/llm_erc20.py +0 -0
  39. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/log_indexer.py +0 -0
  40. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/multi_file_contract/__init__.py +0 -0
  41. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/multi_file_contract/other.py +0 -0
  42. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/multi_read_erc20.py +0 -0
  43. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/multi_tenant_storage.py +0 -0
  44. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/read_erc20.py +0 -0
  45. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/storage.py +0 -0
  46. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/user_storage.py +0 -0
  47. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/contracts/wizard_of_coin.py +0 -0
  48. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_football_prediction_market.py +0 -0
  49. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_intelligent_oracle_factory.py +0 -0
  50. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_llm_erc20.py +0 -0
  51. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_log_indexer.py +0 -0
  52. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_multi_file_contract.py +0 -0
  53. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_multi_file_contract_legacy.py +0 -0
  54. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_multi_read_erc20.py +0 -0
  55. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_multi_tenant_storage.py +0 -0
  56. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_read_erc20.py +0 -0
  57. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_storage.py +0 -0
  58. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_storage_legacy.py +0 -0
  59. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_user_storage.py +0 -0
  60. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/examples/tests/test_wizard_of_coin.py +0 -0
  61. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/plugin/conftest.py +0 -0
  62. {genlayer_test-0.1.3 → genlayer_test-0.2.0}/tests/plugin/test_plugin_hooks.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: genlayer-test
3
- Version: 0.1.3
3
+ Version: 0.2.0
4
4
  Summary: GenLayer Testing Suite
5
5
  Author: GenLayer
6
6
  License-Expression: MIT
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: genlayer-test
3
- Version: 0.1.3
3
+ Version: 0.2.0
4
4
  Summary: GenLayer Testing Suite
5
5
  Author: GenLayer
6
6
  License-Expression: MIT
@@ -24,6 +24,8 @@ gltest/helpers/fixture_snapshot.py
24
24
  gltest/helpers/take_snapshot.py
25
25
  gltest_cli/main.py
26
26
  tests/artifact/test_contract_definition.py
27
+ tests/artifact/contracts/duplicate_ic_contract_1.py
28
+ tests/artifact/contracts/duplicate_ic_contract_2.py
27
29
  tests/artifact/contracts/not_ic_contract.py
28
30
  tests/assertions/test_assertions.py
29
31
  tests/examples/contracts/football_prediction_market.py
@@ -1,4 +1,5 @@
1
1
  dist
2
2
  gltest
3
3
  gltest_cli
4
+ scripts
4
5
  tests
@@ -0,0 +1,6 @@
1
+ from .contract import (
2
+ find_contract_definition_from_name,
3
+ find_contract_definition_from_path,
4
+ )
5
+
6
+ __all__ = ["find_contract_definition_from_name", "find_contract_definition_from_path"]
@@ -20,6 +20,8 @@ class ContractDefinition:
20
20
 
21
21
  def search_path_by_class_name(contracts_dir: Path, contract_name: str) -> Path:
22
22
  """Search for a file by class name in the contracts directory."""
23
+ matching_files = []
24
+
23
25
  for file_path in contracts_dir.rglob("*"):
24
26
  if not file_path.suffix in [".gpy", ".py"]:
25
27
  continue
@@ -40,10 +42,24 @@ def search_path_by_class_name(contracts_dir: Path, contract_name: str) -> Path:
40
42
  and base.value.id == "gl"
41
43
  and base.attr == "Contract"
42
44
  ):
43
- return file_path
45
+ matching_files.append(file_path)
46
+ break
47
+ break
44
48
  except Exception as e:
45
- raise ValueError(f"Error reading file {file_path}: {e}")
46
- raise FileNotFoundError(f"Contract {contract_name} not found at: {contracts_dir}")
49
+ raise ValueError(f"Error reading file {file_path}: {e}") from e
50
+
51
+ if len(matching_files) == 0:
52
+ raise FileNotFoundError(
53
+ f"Contract {contract_name} not found at: {contracts_dir}"
54
+ )
55
+ if len(matching_files) > 1:
56
+ file_paths_str = ", ".join(str(f) for f in matching_files)
57
+ raise ValueError(
58
+ f"Multiple contracts named '{contract_name}' found in contracts directory. "
59
+ f"Found in files: {file_paths_str}. Please ensure contract names are unique."
60
+ ) from None
61
+
62
+ return matching_files[0]
47
63
 
48
64
 
49
65
  def compute_contract_code(
@@ -71,14 +87,35 @@ def compute_contract_code(
71
87
  return buffer.getvalue()
72
88
 
73
89
 
74
- def find_contract_definition(contract_name: str) -> Optional[ContractDefinition]:
75
- """
76
- Search in the contracts directory for a contract definition.
77
- """
78
- contracts_dir = get_contracts_dir()
79
- if not contracts_dir.exists():
80
- raise FileNotFoundError(f"Contracts directory not found at: {contracts_dir}")
81
- main_file_path = search_path_by_class_name(contracts_dir, contract_name)
90
+ def _extract_contract_name_from_file(file_path: Path) -> str:
91
+ """Extract contract name from a file by parsing the AST."""
92
+ try:
93
+ with open(file_path, "r") as f:
94
+ content = f.read()
95
+ tree = ast.parse(content)
96
+
97
+ # Search for class definitions that inherit from gl.Contract
98
+ for node in ast.walk(tree):
99
+ if isinstance(node, ast.ClassDef):
100
+ for base in node.bases:
101
+ if (
102
+ isinstance(base, ast.Attribute)
103
+ and isinstance(base.value, ast.Name)
104
+ and base.value.id == "gl"
105
+ and base.attr == "Contract"
106
+ ):
107
+ return node.name
108
+ except Exception as e:
109
+ raise ValueError(f"Error parsing contract file {file_path}: {e}") from e
110
+
111
+ raise ValueError(f"No valid contract class found in {file_path}")
112
+
113
+
114
+ def _create_contract_definition(
115
+ main_file_path: Path, contract_name: str
116
+ ) -> ContractDefinition:
117
+ """Create a ContractDefinition from a main file path and contract name."""
118
+ # Determine if it's a multifile contract
82
119
  main_file_dir = main_file_path.parent
83
120
  runner_file_path = None
84
121
  if main_file_path.name in ["__init__.py", "__init__.gpy"]:
@@ -87,9 +124,47 @@ def find_contract_definition(contract_name: str) -> Optional[ContractDefinition]
87
124
  if not runner_file_path.exists():
88
125
  # No runner file, so it's a single file contract
89
126
  runner_file_path = None
127
+
128
+ # Compute contract code
129
+ contract_code = compute_contract_code(main_file_path, runner_file_path)
130
+
90
131
  return ContractDefinition(
91
132
  contract_name=contract_name,
92
- contract_code=compute_contract_code(main_file_path, runner_file_path),
133
+ contract_code=contract_code,
93
134
  main_file_path=main_file_path,
94
135
  runner_file_path=runner_file_path,
95
136
  )
137
+
138
+
139
+ def find_contract_definition_from_name(
140
+ contract_name: str,
141
+ ) -> Optional[ContractDefinition]:
142
+ """
143
+ Search in the contracts directory for a contract definition.
144
+ """
145
+ contracts_dir = get_contracts_dir()
146
+ if not contracts_dir.exists():
147
+ raise FileNotFoundError(f"Contracts directory not found at: {contracts_dir}")
148
+
149
+ main_file_path = search_path_by_class_name(contracts_dir, contract_name)
150
+ return _create_contract_definition(main_file_path, contract_name)
151
+
152
+
153
+ def find_contract_definition_from_path(
154
+ contract_file_path: Union[str, Path],
155
+ ) -> ContractDefinition:
156
+ """
157
+ Create a ContractDefinition from a given file path relative to the contracts directory.
158
+ """
159
+ contracts_dir = get_contracts_dir()
160
+ if not contracts_dir.exists():
161
+ raise FileNotFoundError(f"Contracts directory not found at: {contracts_dir}")
162
+
163
+ # Resolve the file path relative to contracts directory
164
+ main_file_path = contracts_dir / contract_file_path
165
+ if not main_file_path.exists():
166
+ raise FileNotFoundError(f"Contract file not found at: {main_file_path}")
167
+
168
+ contract_name = _extract_contract_name_from_file(main_file_path)
169
+
170
+ return _create_contract_definition(main_file_path, contract_name)
@@ -4,8 +4,12 @@ from eth_typing import (
4
4
  )
5
5
  from eth_account.signers.local import LocalAccount
6
6
  from typing import Union
7
+ from pathlib import Path
7
8
  from dataclasses import dataclass
8
- from gltest.artifacts import find_contract_definition
9
+ from gltest.artifacts import (
10
+ find_contract_definition_from_name,
11
+ find_contract_definition_from_path,
12
+ )
9
13
  from gltest.assertions import tx_execution_failed
10
14
  from gltest.exceptions import DeploymentError
11
15
  from .client import get_gl_client
@@ -146,13 +150,13 @@ class ContractFactory:
146
150
  contract_code: str
147
151
 
148
152
  @classmethod
149
- def from_artifact(
153
+ def from_name(
150
154
  cls: Type["ContractFactory"], contract_name: str
151
155
  ) -> "ContractFactory":
152
156
  """
153
157
  Create a ContractFactory instance given the contract name.
154
158
  """
155
- contract_info = find_contract_definition(contract_name)
159
+ contract_info = find_contract_definition_from_name(contract_name)
156
160
  if contract_info is None:
157
161
  raise ValueError(
158
162
  f"Contract {contract_name} not found in the contracts directory"
@@ -161,6 +165,19 @@ class ContractFactory:
161
165
  contract_name=contract_name, contract_code=contract_info.contract_code
162
166
  )
163
167
 
168
+ @classmethod
169
+ def from_file_path(
170
+ cls: Type["ContractFactory"], contract_file_path: Union[str, Path]
171
+ ) -> "ContractFactory":
172
+ """
173
+ Create a ContractFactory instance given the contract file path.
174
+ """
175
+ contract_info = find_contract_definition_from_path(contract_file_path)
176
+ return cls(
177
+ contract_name=contract_info.contract_name,
178
+ contract_code=contract_info.contract_code,
179
+ )
180
+
164
181
  def build_contract(
165
182
  self,
166
183
  contract_address: Union[Address, ChecksumAddress],
@@ -237,8 +254,27 @@ class ContractFactory:
237
254
  ) from e
238
255
 
239
256
 
240
- def get_contract_factory(contract_name: str) -> ContractFactory:
257
+ def get_contract_factory(
258
+ contract_name: Optional[str] = None,
259
+ contract_file_path: Optional[Union[str, Path]] = None,
260
+ ) -> ContractFactory:
241
261
  """
242
262
  Get a ContractFactory instance for a contract.
263
+
264
+ Args:
265
+ contract_name: Name of the contract to load from artifacts
266
+ contract_file_path: Path to the contract file to load directly
267
+
268
+ Note: Exactly one of contract_name or contract_file_path must be provided.
243
269
  """
244
- return ContractFactory.from_artifact(contract_name)
270
+ if contract_name is not None and contract_file_path is not None:
271
+ raise ValueError(
272
+ "Only one of contract_name or contract_file_path should be provided"
273
+ )
274
+
275
+ if contract_name is None and contract_file_path is None:
276
+ raise ValueError("Either contract_name or contract_file_path must be provided")
277
+
278
+ if contract_name is not None:
279
+ return ContractFactory.from_name(contract_name)
280
+ return ContractFactory.from_file_path(contract_file_path)
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "genlayer-test"
7
- version = "0.1.3"
7
+ version = "0.2.0"
8
8
  description = "GenLayer Testing Suite"
9
9
  authors = [
10
10
  { name = "GenLayer" }
@@ -0,0 +1,22 @@
1
+ # { "Depends": "py-genlayer:test" }
2
+
3
+ from genlayer import *
4
+
5
+
6
+ # contract class
7
+ class DuplicateContract(gl.Contract):
8
+ storage: str
9
+
10
+ # constructor
11
+ def __init__(self, initial_storage: str):
12
+ self.storage = initial_storage
13
+
14
+ # read methods must be annotated with view
15
+ @gl.public.view
16
+ def get_storage(self) -> str:
17
+ return self.storage
18
+
19
+ # write method
20
+ @gl.public.write
21
+ def update_storage(self, new_storage: str) -> None:
22
+ self.storage = new_storage
@@ -0,0 +1,22 @@
1
+ # { "Depends": "py-genlayer:test" }
2
+
3
+ from genlayer import *
4
+
5
+
6
+ # contract class
7
+ class DuplicateContract(gl.Contract):
8
+ storage: str
9
+
10
+ # constructor
11
+ def __init__(self, initial_storage: str):
12
+ self.storage = initial_storage
13
+
14
+ # read methods must be annotated with view
15
+ @gl.public.view
16
+ def get_storage(self) -> str:
17
+ return self.storage
18
+
19
+ # write method
20
+ @gl.public.write
21
+ def update_storage(self, new_storage: str) -> None:
22
+ self.storage = new_storage
@@ -0,0 +1,347 @@
1
+ import pytest
2
+ from gltest.artifacts.contract import (
3
+ find_contract_definition_from_name,
4
+ find_contract_definition_from_path,
5
+ compute_contract_code,
6
+ )
7
+ from gltest.plugin_config import set_contracts_dir
8
+ from pathlib import Path
9
+
10
+
11
+ def test_single_file():
12
+ """
13
+ Test finding a contract definition by name for a single-file contract.
14
+
15
+ Verifies that the function correctly identifies and loads a contract
16
+ from a single Python file, extracting the contract name and computing
17
+ the contract code without any additional runner files.
18
+ """
19
+ set_contracts_dir(".")
20
+ contract_definition = find_contract_definition_from_name("PredictionMarket")
21
+
22
+ assert contract_definition.contract_name == "PredictionMarket"
23
+
24
+ # Assert complete contract definition
25
+ expected_main_file_path = Path("examples/contracts/football_prediction_market.py")
26
+ expected_runner_file_path = None
27
+ contract_code = compute_contract_code(
28
+ expected_main_file_path, expected_runner_file_path
29
+ )
30
+ assert contract_definition.contract_code == contract_code
31
+ assert (
32
+ str(contract_definition.main_file_path)
33
+ == "examples/contracts/football_prediction_market.py"
34
+ )
35
+ assert contract_definition.runner_file_path is None
36
+
37
+
38
+ def test_multiple_files():
39
+ """
40
+ Test finding a contract definition by name for a multi-file contract.
41
+
42
+ Verifies that the function correctly identifies and loads a contract
43
+ from a multi-file structure with __init__.py and runner.json,
44
+ properly packaging all files into a ZIP archive for deployment.
45
+ """
46
+ set_contracts_dir(".")
47
+ contract_definition = find_contract_definition_from_name("MultiFileContract")
48
+
49
+ assert contract_definition.contract_name == "MultiFileContract"
50
+
51
+ # Assert complete contract definition
52
+ expected_main_file_path = Path("examples/contracts/multi_file_contract/__init__.py")
53
+ expected_runner_file_path = Path(
54
+ "examples/contracts/multi_file_contract/runner.json"
55
+ )
56
+ assert contract_definition.main_file_path == expected_main_file_path
57
+ assert contract_definition.runner_file_path == expected_runner_file_path
58
+ contract_code = compute_contract_code(
59
+ expected_main_file_path, expected_runner_file_path
60
+ )
61
+ assert contract_definition.contract_code == contract_code
62
+
63
+
64
+ def test_single_file_legacy():
65
+ """
66
+ Test finding a contract definition by name for a legacy single-file contract.
67
+
68
+ Verifies that the function correctly handles legacy .gpy files,
69
+ maintaining backward compatibility with older contract formats
70
+ while extracting contract name and computing contract code.
71
+ """
72
+ set_contracts_dir(".")
73
+ contract_definition = find_contract_definition_from_name("StorageLegacy")
74
+
75
+ # Assert complete contract definition
76
+ assert contract_definition.contract_name == "StorageLegacy"
77
+ expected_main_file_path = Path("examples/contracts/storage_legacy.gpy")
78
+ expected_runner_file_path = None
79
+ contract_code = compute_contract_code(
80
+ expected_main_file_path, expected_runner_file_path
81
+ )
82
+ assert contract_definition.contract_code == contract_code
83
+ assert (
84
+ str(contract_definition.main_file_path)
85
+ == "examples/contracts/storage_legacy.gpy"
86
+ )
87
+ assert contract_definition.runner_file_path is None
88
+
89
+
90
+ def test_multiple_files_legacy():
91
+ """
92
+ Test finding a contract definition by name for a legacy multi-file contract.
93
+
94
+ Verifies that the function correctly handles legacy multi-file contracts
95
+ with .gpy extension and runner.json, ensuring proper packaging and
96
+ backward compatibility with older contract structures.
97
+ """
98
+ set_contracts_dir(".")
99
+ contract_definition = find_contract_definition_from_name("MultiFileContractLegacy")
100
+
101
+ # Assert complete contract definition
102
+ assert contract_definition.contract_name == "MultiFileContractLegacy"
103
+ expected_main_file_path = Path(
104
+ "examples/contracts/multi_file_contract_legacy/__init__.gpy"
105
+ )
106
+ expected_runner_file_path = Path(
107
+ "examples/contracts/multi_file_contract_legacy/runner.json"
108
+ )
109
+ assert contract_definition.main_file_path == expected_main_file_path
110
+ assert contract_definition.runner_file_path == expected_runner_file_path
111
+ contract_code = compute_contract_code(
112
+ expected_main_file_path, expected_runner_file_path
113
+ )
114
+ assert contract_definition.contract_code == contract_code
115
+
116
+
117
+ def test_class_is_not_intelligent_contract():
118
+ """
119
+ Test error handling when searching for a non-existent contract by name.
120
+
121
+ Verifies that the function raises FileNotFoundError when attempting
122
+ to find a contract that doesn't exist in the contracts directory,
123
+ ensuring proper error handling for invalid contract names.
124
+ """
125
+ set_contracts_dir(".")
126
+
127
+ with pytest.raises(FileNotFoundError):
128
+ _ = find_contract_definition_from_name("NotICContract")
129
+
130
+
131
+ def test_find_from_path_single_file():
132
+ """
133
+ Test finding a contract definition by file path for a single-file contract.
134
+
135
+ Verifies that the function correctly loads a contract when given a relative
136
+ path to a single Python file, extracting the contract name via AST parsing
137
+ and computing the contract code without additional runner files.
138
+ """
139
+ set_contracts_dir(".")
140
+ contract_definition = find_contract_definition_from_path(
141
+ "examples/contracts/football_prediction_market.py"
142
+ )
143
+
144
+ assert contract_definition.contract_name == "PredictionMarket"
145
+
146
+ # Assert complete contract definition
147
+ expected_main_file_path = Path("examples/contracts/football_prediction_market.py")
148
+ expected_runner_file_path = None
149
+ contract_code = compute_contract_code(
150
+ expected_main_file_path, expected_runner_file_path
151
+ )
152
+ assert contract_definition.contract_code == contract_code
153
+ assert (
154
+ str(contract_definition.main_file_path)
155
+ == "examples/contracts/football_prediction_market.py"
156
+ )
157
+ assert contract_definition.runner_file_path is None
158
+
159
+
160
+ def test_find_from_path_multiple_files():
161
+ """
162
+ Test finding a contract definition by file path for a multi-file contract.
163
+
164
+ Verifies that the function correctly loads a contract when given a relative
165
+ path to __init__.py in a multi-file structure, automatically detecting
166
+ the associated runner.json and packaging all files appropriately.
167
+ """
168
+ set_contracts_dir(".")
169
+ contract_definition = find_contract_definition_from_path(
170
+ "examples/contracts/multi_file_contract/__init__.py"
171
+ )
172
+
173
+ assert contract_definition.contract_name == "MultiFileContract"
174
+
175
+ # Assert complete contract definition
176
+ expected_main_file_path = Path("examples/contracts/multi_file_contract/__init__.py")
177
+ expected_runner_file_path = Path(
178
+ "examples/contracts/multi_file_contract/runner.json"
179
+ )
180
+ assert contract_definition.main_file_path == expected_main_file_path
181
+ assert contract_definition.runner_file_path == expected_runner_file_path
182
+ contract_code = compute_contract_code(
183
+ expected_main_file_path, expected_runner_file_path
184
+ )
185
+ assert contract_definition.contract_code == contract_code
186
+
187
+
188
+ def test_find_from_path_single_file_legacy():
189
+ """
190
+ Test finding a contract definition by file path for a legacy single-file contract.
191
+
192
+ Verifies that the function correctly handles legacy .gpy files when accessed
193
+ by file path, maintaining backward compatibility while extracting contract
194
+ name via AST parsing and computing appropriate contract code.
195
+ """
196
+ set_contracts_dir(".")
197
+ contract_definition = find_contract_definition_from_path(
198
+ "examples/contracts/storage_legacy.gpy"
199
+ )
200
+
201
+ # Assert complete contract definition
202
+ assert contract_definition.contract_name == "StorageLegacy"
203
+ expected_main_file_path = Path("examples/contracts/storage_legacy.gpy")
204
+ expected_runner_file_path = None
205
+ contract_code = compute_contract_code(
206
+ expected_main_file_path, expected_runner_file_path
207
+ )
208
+ assert contract_definition.contract_code == contract_code
209
+ assert (
210
+ str(contract_definition.main_file_path)
211
+ == "examples/contracts/storage_legacy.gpy"
212
+ )
213
+ assert contract_definition.runner_file_path is None
214
+
215
+
216
+ def test_find_from_path_multiple_files_legacy():
217
+ """
218
+ Test finding a contract definition by file path for a legacy multi-file contract.
219
+
220
+ Verifies that the function correctly handles legacy multi-file contracts
221
+ with .gpy extension when accessed by file path, properly detecting
222
+ runner.json and maintaining backward compatibility with older structures.
223
+ """
224
+ set_contracts_dir(".")
225
+ contract_definition = find_contract_definition_from_path(
226
+ "examples/contracts/multi_file_contract_legacy/__init__.gpy"
227
+ )
228
+
229
+ # Assert complete contract definition
230
+ assert contract_definition.contract_name == "MultiFileContractLegacy"
231
+ expected_main_file_path = Path(
232
+ "examples/contracts/multi_file_contract_legacy/__init__.gpy"
233
+ )
234
+ expected_runner_file_path = Path(
235
+ "examples/contracts/multi_file_contract_legacy/runner.json"
236
+ )
237
+ assert contract_definition.main_file_path == expected_main_file_path
238
+ assert contract_definition.runner_file_path == expected_runner_file_path
239
+ contract_code = compute_contract_code(
240
+ expected_main_file_path, expected_runner_file_path
241
+ )
242
+ assert contract_definition.contract_code == contract_code
243
+
244
+
245
+ def test_find_from_path_file_not_found():
246
+ """
247
+ Test error handling when the specified contract file doesn't exist.
248
+
249
+ Verifies that the function raises FileNotFoundError with appropriate
250
+ error message when attempting to load a contract from a non-existent
251
+ file path relative to the contracts directory.
252
+ """
253
+ set_contracts_dir(".")
254
+
255
+ with pytest.raises(FileNotFoundError, match="Contract file not found at:"):
256
+ _ = find_contract_definition_from_path("nonexistent/contract.py")
257
+
258
+
259
+ def test_find_from_path_contracts_dir_not_found():
260
+ """
261
+ Test error handling when the contracts directory doesn't exist.
262
+
263
+ Verifies that the function raises FileNotFoundError with appropriate
264
+ error message when the configured contracts directory is invalid,
265
+ ensuring proper validation before attempting file operations.
266
+ """
267
+ set_contracts_dir("nonexistent_directory")
268
+
269
+ with pytest.raises(FileNotFoundError, match="Contracts directory not found at:"):
270
+ _ = find_contract_definition_from_path("some/contract.py")
271
+
272
+
273
+ def test_find_from_path_no_valid_contract_class():
274
+ """
275
+ Test error handling when a file exists but contains no valid contract class.
276
+
277
+ Verifies that the function raises ValueError with appropriate error message
278
+ when attempting to load a file that exists but doesn't contain a class
279
+ that inherits from gl.Contract, ensuring proper AST parsing validation.
280
+ """
281
+ set_contracts_dir(".")
282
+
283
+ with pytest.raises(ValueError, match="No valid contract class found in"):
284
+ _ = find_contract_definition_from_path("artifact/contracts/not_ic_contract.py")
285
+
286
+
287
+ def test_multiple_contracts_same_name():
288
+ """
289
+ Test error handling when multiple contracts with the same name exist.
290
+
291
+ Verifies that the function raises ValueError with appropriate error message
292
+ when multiple files contain contracts with the same name, listing all
293
+ duplicate file locations and providing guidance for resolution.
294
+ """
295
+ set_contracts_dir(".")
296
+
297
+ with pytest.raises(
298
+ ValueError,
299
+ match=r"Multiple contracts named 'DuplicateContract' found in contracts directory\. Found in files: .+\. Please ensure contract names are unique\.",
300
+ ):
301
+ _ = find_contract_definition_from_name("DuplicateContract")
302
+
303
+
304
+ def test_duplicate_contract_error_message_format():
305
+ """
306
+ Test that the duplicate contract error message contains all expected elements.
307
+
308
+ Verifies that when multiple contracts with the same name are found, the error
309
+ message includes the contract name, mentions "contracts directory", lists
310
+ file paths, and provides clear guidance about ensuring uniqueness.
311
+ """
312
+ set_contracts_dir(".")
313
+
314
+ try:
315
+ _ = find_contract_definition_from_name("DuplicateContract")
316
+ pytest.fail("Expected ValueError for duplicate contracts")
317
+ except ValueError as e:
318
+ error_message = str(e)
319
+ # Verify error message contains key components
320
+ assert "Multiple contracts named 'DuplicateContract' found" in error_message
321
+ assert "contracts directory" in error_message
322
+ assert "Found in files:" in error_message
323
+ assert "Please ensure contract names are unique" in error_message
324
+ # Verify that multiple file paths are mentioned (comma-separated)
325
+ assert (
326
+ "," in error_message
327
+ or len(error_message.split("Found in files: ")[1].split(".")[0]) > 0
328
+ )
329
+ except Exception as e:
330
+ pytest.fail(f"Expected ValueError but got {type(e).__name__}: {e}")
331
+
332
+
333
+ def test_single_contract_still_works_with_duplicate_detection():
334
+ """
335
+ Test that normal single contract loading still works after duplicate detection changes.
336
+
337
+ Verifies that the enhanced search_path_by_class_name function doesn't break
338
+ the normal case where only one contract with a given name exists, ensuring
339
+ backward compatibility with existing functionality.
340
+ """
341
+ set_contracts_dir(".")
342
+
343
+ # This should work normally - no duplicates expected for PredictionMarket
344
+ contract_definition = find_contract_definition_from_name("PredictionMarket")
345
+ assert contract_definition.contract_name == "PredictionMarket"
346
+ assert contract_definition.main_file_path is not None
347
+ assert "football_prediction_market.py" in str(contract_definition.main_file_path)
@@ -1,3 +0,0 @@
1
- from .contract import find_contract_definition
2
-
3
- __all__ = ["find_contract_definition"]
@@ -1,92 +0,0 @@
1
- import pytest
2
- from gltest.artifacts.contract import (
3
- find_contract_definition,
4
- compute_contract_code,
5
- )
6
- from gltest.plugin_config import set_contracts_dir
7
- from pathlib import Path
8
-
9
-
10
- def test_single_file():
11
- set_contracts_dir(".")
12
- contract_definition = find_contract_definition("PredictionMarket")
13
-
14
- assert contract_definition.contract_name == "PredictionMarket"
15
-
16
- # Assert complete contract definition
17
- expected_main_file_path = Path("examples/contracts/football_prediction_market.py")
18
- expected_runner_file_path = None
19
- contract_code = compute_contract_code(
20
- expected_main_file_path, expected_runner_file_path
21
- )
22
- assert contract_definition.contract_code == contract_code
23
- assert (
24
- str(contract_definition.main_file_path)
25
- == "examples/contracts/football_prediction_market.py"
26
- )
27
- assert contract_definition.runner_file_path is None
28
-
29
-
30
- def test_multiple_files():
31
- set_contracts_dir(".")
32
- contract_definition = find_contract_definition("MultiFileContract")
33
-
34
- assert contract_definition.contract_name == "MultiFileContract"
35
-
36
- # Assert complete contract definition
37
- expected_main_file_path = Path("examples/contracts/multi_file_contract/__init__.py")
38
- expected_runner_file_path = Path(
39
- "examples/contracts/multi_file_contract/runner.json"
40
- )
41
- assert contract_definition.main_file_path == expected_main_file_path
42
- assert contract_definition.runner_file_path == expected_runner_file_path
43
- contract_code = compute_contract_code(
44
- expected_main_file_path, expected_runner_file_path
45
- )
46
- assert contract_definition.contract_code == contract_code
47
-
48
-
49
- def test_single_file_legacy():
50
- set_contracts_dir(".")
51
- contract_definition = find_contract_definition("StorageLegacy")
52
-
53
- # Assert complete contract definition
54
- assert contract_definition.contract_name == "StorageLegacy"
55
- expected_main_file_path = Path("examples/contracts/storage_legacy.gpy")
56
- expected_runner_file_path = None
57
- contract_code = compute_contract_code(
58
- expected_main_file_path, expected_runner_file_path
59
- )
60
- assert contract_definition.contract_code == contract_code
61
- assert (
62
- str(contract_definition.main_file_path)
63
- == "examples/contracts/storage_legacy.gpy"
64
- )
65
- assert contract_definition.runner_file_path is None
66
-
67
-
68
- def test_multiple_files_legacy():
69
- set_contracts_dir(".")
70
- contract_definition = find_contract_definition("MultiFileContractLegacy")
71
-
72
- # Assert complete contract definition
73
- assert contract_definition.contract_name == "MultiFileContractLegacy"
74
- expected_main_file_path = Path(
75
- "examples/contracts/multi_file_contract_legacy/__init__.gpy"
76
- )
77
- expected_runner_file_path = Path(
78
- "examples/contracts/multi_file_contract_legacy/runner.json"
79
- )
80
- assert contract_definition.main_file_path == expected_main_file_path
81
- assert contract_definition.runner_file_path == expected_runner_file_path
82
- contract_code = compute_contract_code(
83
- expected_main_file_path, expected_runner_file_path
84
- )
85
- assert contract_definition.contract_code == contract_code
86
-
87
-
88
- def test_class_is_not_intelligent_contract():
89
- set_contracts_dir(".")
90
-
91
- with pytest.raises(FileNotFoundError):
92
- _ = find_contract_definition("NotICContract")
File without changes
File without changes
File without changes