-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_blockchain_integration.py
More file actions
215 lines (165 loc) · 6.22 KB
/
test_blockchain_integration.py
File metadata and controls
215 lines (165 loc) · 6.22 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
"""
Test script for blockchain integration in mesh voting application.
"""
import asyncio
import sys
import os
from unittest.mock import Mock, MagicMock
# Add current directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from mesh.voting_app import MeshVotingApp, VoteOption
class MockMeshNetwork:
"""Mock mesh network for testing."""
def __init__(self):
self.message_handlers = {}
def register_message_handler(self, message_type, handler):
self.message_handlers[message_type] = handler
async def broadcast_message(self, message_type, payload):
pass # Mock implementation
async def test_blockchain_initialization():
"""Test blockchain component initialization."""
print("Testing blockchain component initialization...")
# Create mock mesh network
mesh_network = MockMeshNetwork()
# Test with blockchain enabled (if imports work)
try:
app = MeshVotingApp("test_node", mesh_network)
print("✓ MeshVotingApp initialized successfully")
# Check if blockchain components are initialized
if hasattr(app, 'blockchain_enabled') and app.blockchain_enabled:
print("✓ Blockchain integration enabled")
if app.polkadot_bridge is not None:
print("✓ PolkadotBridge initialized")
else:
print("✗ PolkadotBridge not initialized")
if app.message_recorder is not None:
print("✓ MeshMessageRecorder initialized")
else:
print("✗ MeshMessageRecorder not initialized")
else:
print("ℹ Blockchain integration disabled (imports failed)")
except Exception as e:
print(f"✗ Failed to initialize MeshVotingApp: {e}")
return False
return True
async def test_vote_casting_with_blockchain():
"""Test vote casting with blockchain recording."""
print("\nTesting vote casting with blockchain recording...")
mesh_network = MockMeshNetwork()
app = MeshVotingApp("test_node", mesh_network)
# Create a voting session
options = [
VoteOption("opt1", "Option 1", "First choice"),
VoteOption("opt2", "Option 2", "Second choice")
]
try:
session_id = await app.create_voting_session(
title="Test Session",
description="Blockchain integration test",
options=options,
duration_minutes=5,
eligible_voters=["test_node"]
)
print(f"✓ Created voting session: {session_id}")
# Cast a vote
success = await app.cast_vote(session_id, "opt1")
if success:
print("✓ Vote cast successfully")
else:
print("✗ Failed to cast vote")
return False
# Check if blockchain recording was attempted
if app.blockchain_enabled:
print("✓ Blockchain recording attempted (check logs for details)")
else:
print("ℹ Blockchain recording skipped (disabled)")
except Exception as e:
print(f"✗ Error during vote casting: {e}")
return False
return True
async def test_error_handling():
"""Test error handling when blockchain components fail."""
print("\nTesting error handling...")
mesh_network = MockMeshNetwork()
# Test with mocked failing blockchain components
try:
app = MeshVotingApp("test_node", mesh_network)
# Manually set blockchain components to None to simulate failure
app.polkadot_bridge = None
app.message_recorder = None
app.blockchain_enabled = True
# Create session and cast vote - should handle errors gracefully
options = [VoteOption("opt1", "Option 1")]
session_id = await app.create_voting_session(
title="Error Test",
description="Testing error handling",
options=options,
duration_minutes=1,
eligible_voters=["test_node"]
)
success = await app.cast_vote(session_id, "opt1")
if success:
print("✓ Vote casting handled blockchain errors gracefully")
else:
print("✗ Vote casting failed due to errors")
return False
except Exception as e:
print(f"✗ Unexpected error in error handling test: {e}")
return False
return True
async def test_session_metadata():
"""Test session metadata recording."""
print("\nTesting session metadata recording...")
mesh_network = MockMeshNetwork()
app = MeshVotingApp("test_node", mesh_network)
# Create session with specific metadata
options = [
VoteOption("opt1", "Yes", "Approve the proposal"),
VoteOption("opt2", "No", "Reject the proposal")
]
session_id = await app.create_voting_session(
title="Metadata Test Session",
description="Testing metadata recording",
options=options,
duration_minutes=10,
eligible_voters=["test_node", "node2"],
consensus_threshold=0.6
)
# Cast vote and check if metadata would be recorded correctly
success = await app.cast_vote(session_id, "opt1")
if success:
print("✓ Session metadata recording test completed")
# In a real test, we'd verify the metadata sent to blockchain
else:
print("✗ Failed to test session metadata")
return False
return True
async def main():
"""Run all tests."""
print("Starting blockchain integration tests for mesh voting app...\n")
tests = [
test_blockchain_initialization,
test_vote_casting_with_blockchain,
test_error_handling,
test_session_metadata
]
passed = 0
total = len(tests)
for test in tests:
try:
if await test():
passed += 1
except Exception as e:
print(f"✗ Test {test.__name__} failed with exception: {e}")
print(f"\n{'='*50}")
print(f"Test Results: {passed}/{total} tests passed")
if passed == total:
print("✓ All tests passed!")
return True
else:
print("✗ Some tests failed")
return False
if __name__ == "__main__":
success = asyncio.run(main())
sys.exit(0 if success else 1)