-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathtesting.py
More file actions
92 lines (78 loc) · 3.37 KB
/
testing.py
File metadata and controls
92 lines (78 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import requests
import sys
import importlib.util
import ast
import types
class FakeResponse:
def __init__(self):
self.status_code = 200
self.text = '{"data":{"id":"bitcoin","rank":"1","symbol":"BTC","name":"Bitcoin","supply":"19823321.0000000000000000","maxSupply":"21000000.0000000000000000","marketCapUsd":"1939613325892.4607145113457500","volumeUsd24Hr":"12341417371.3505338276601668","priceUsd":"97845.0243","changePercent24Hr":"1.4324165997531723","vwap24Hr":"96203.8859537212418977","explorer":"https://blockchain.info/"},"timestamp":1739399343596}'
def json(*args):
return {
"data": {
"id": "bitcoin",
"rank": "1",
"symbol": "BTC",
"name": "Bitcoin",
"supply": "19823321.0000000000000000",
"maxSupply": "21000000.0000000000000000",
"marketCapUsd": "1939613325892.4607145113457500",
"volumeUsd24Hr": "12341417371.3505338276601668",
"priceUsd": "97845.0243",
"changePercent24Hr": "1.4324165997531723",
"vwap24Hr": "96203.8859537212418977",
"explorer": "https://blockchain.info/"
},
"timestamp": 1739399343596
}
# Mock raise_for_status
def raise_for_status(self):
pass
def fake_request(method, url, **kwargs):
if method == "GET":
return FakeResponse()
else:
return requests.request(method, url, **kwargs)
requests.get = lambda *args, **kwargs: FakeResponse()
requests.request = fake_request
with open("bitcoin.py", "r") as f:
source = f.read()
tree = ast.parse(source)
# Find the if __name__ == "__main__" block
main_guard_node = None
for node in ast.walk(tree):
if isinstance(node, ast.If):
# Check if this is the __name__ == "__main__" pattern
if (isinstance(node.test, ast.Compare) and
isinstance(node.test.left, ast.Name) and
node.test.left.id == "__name__" and
len(node.test.ops) == 1 and
isinstance(node.test.ops[0], ast.Eq) and
len(node.test.comparators) == 1 and
isinstance(node.test.comparators[0], ast.Constant) and
node.test.comparators[0].value == "__main__"):
main_guard_node = node
break
# Load and execute the module
spec = importlib.util.spec_from_file_location("bitcoin", "bitcoin.py")
bitcoin = importlib.util.module_from_spec(spec)
sys.modules['bitcoin'] = bitcoin
# If there's a main guard, we need to handle it specially
if main_guard_node:
# Create a modified AST that excludes the if __name__ == "__main__" block
# This prevents the guarded code from running during import
modified_tree = ast.Module(body=[], type_ignores=[])
for node in tree.body:
if node != main_guard_node:
modified_tree.body.append(node)
# Compile and execute the modified module (without the main guard)
code = compile(modified_tree, "bitcoin.py", "exec")
exec(code, bitcoin.__dict__)
# Now execute the code that was inside the main guard
# Create a new module with just the guarded code
guarded_code = ast.Module(body=main_guard_node.body, type_ignores=[])
code = compile(guarded_code, "<main_guard>", "exec")
exec(code, bitcoin.__dict__)
else:
# No main guard, just execute normally
spec.loader.exec_module(bitcoin)