1+ # tests/test_backend.py
2+ import pytest
3+ from flask_testing import TestCase
4+ from backend import app # Import your Flask app from backend.py
5+ import json
6+ import os
7+
8+ class TestCompilation (TestCase ):
9+ def create_app (self ):
10+ app .config ['TESTING' ] = True
11+ return app
12+
13+ def test_compilation_success (self ):
14+ # Accessing any endpoint implies successful app startup, which depends on successful compilation.
15+ response = self .client .get ('/help' )
16+ self .assert200 (response )
17+
18+ class TestHelpEndpoint (TestCase ):
19+ def create_app (self ):
20+ app .config ['TESTING' ] = True
21+ return app
22+
23+ def test_help_endpoint_success (self ):
24+ response = self .client .get ('/help' )
25+ self .assert200 (response )
26+ self .assert_content_type (response , "text/plain" ) # Keep this for now
27+
28+ class TestRunSimulationEndpoint (TestCase ):
29+ def create_app (self ):
30+ app .config ['TESTING' ] = True
31+ return app
32+
33+ def test_run_simulation_success (self ):
34+ # Basic successful run test - adjust parameters as needed for a quick test
35+ simulation_input = {
36+ "algorithm" : "FirstFit" ,
37+ "networkType" : 1 ,
38+ "goalConnections" : 1000 , # Reduced goal connections for faster test
39+ "confidence" : 0.05 ,
40+ "lambdaParam" : 1 ,
41+ "mu" : 10 ,
42+ "network" : "NSFNet" ,
43+ "bitrate" : "fixed-rate" ,
44+ "K" : 3
45+ }
46+ response = self .client .post ('/run_simulation' ,
47+ data = json .dumps (simulation_input ),
48+ content_type = 'application/json' )
49+ self .assert200 (response )
50+ response_json = json .loads (response .data .decode ('utf-8' ))
51+ self .assertIn ("output" , response_json )
52+ self .assertEqual (response_json .get ("error" ), "" ) # Expecting no error
53+
54+ def test_run_simulation_compilation_error (self ):
55+ # Simulate compilation error by modifying the executable path (or similar)
56+ original_executable_path = "./src/simulation.out"
57+ temp_executable_path = "./src/simulation.out.temp_error"
58+ os .rename (original_executable_path , temp_executable_path ) # Rename to break execution
59+
60+ response = self .client .post ('/run_simulation' ,
61+ data = json .dumps ({}), # Empty data should still trigger the error check
62+ content_type = 'application/json' )
63+ self .assert_status (response , 500 ) # Use assert_status here
64+ response_json = json .loads (response .data .decode ('utf-8' ))
65+ self .assertIn ("Compilation failed" , response_json .get ("error" ))
66+
67+ os .rename (temp_executable_path , original_executable_path ) # Rename back to fix
68+
69+ def test_run_simulation_executable_not_found (self ):
70+ # Temporarily remove the executable
71+ original_executable_path = "./src/simulation.out"
72+ temp_executable_path = "./src/simulation.out.temp_error"
73+
74+ # Check if executable exists before attempting to rename
75+ if os .path .exists (original_executable_path ): # Add this check
76+ os .rename (original_executable_path , temp_executable_path ) # Rename to break execution
77+ else :
78+ print (f"Warning: Executable not found at { original_executable_path } , test may not be valid." ) # Warning if not found
79+
80+
81+ # Recompile to ensure COMPILE_ERROR is None for this specific test case
82+ from backend import compile_simulation
83+ compile_simulation () # Recompile so COMPILE_ERROR is cleared if previous test failed
84+
85+ response = self .client .post ('/run_simulation' ,
86+ data = json .dumps ({}),
87+ content_type = 'application/json' )
88+ self .assert_status (response , 400 ) # Use assert_status here
89+ response_json = json .loads (response .data .decode ('utf-8' ))
90+ self .assertIn ("Simulation executable not found" , response_json .get ("error" ))
91+
92+ # Rename back only if the original path existed and we renamed it
93+ if os .path .exists (temp_executable_path ): # Check if temp file exists (meaning we renamed)
94+ os .rename (temp_executable_path , original_executable_path ) # Rename back
95+
96+
97+ class TestRunSimulationExecutionFailure (TestCase ): # Group tests for execution failures
98+ def create_app (self ):
99+ app .config ['TESTING' ] = True
100+ return app
101+
102+ def test_invalid_lambda_param (self ):
103+ simulation_input = { "lambdaParam" : 0 } # Invalid lambda
104+ response = self .client .post ('/run_simulation' ,
105+ data = json .dumps (simulation_input ),
106+ content_type = 'application/json' )
107+ self .assert_status (response , 500 ) # Use assert_status
108+ response_json = json .loads (response .data .decode ('utf-8' ))
109+ self .assertIn ("Simulation execution failed" , response_json .get ("error" ))
110+
111+ def test_invalid_mu_param (self ):
112+ simulation_input = { "mu" : 0 } # Invalid mu
113+ response = self .client .post ('/run_simulation' ,
114+ data = json .dumps (simulation_input ),
115+ content_type = 'application/json' )
116+ self .assert_status (response , 500 ) # Use assert_status
117+ response_json = json .loads (response .data .decode ('utf-8' ))
118+ self .assertIn ("Simulation execution failed" , response_json .get ("error" ))
119+
120+ def test_invalid_goal_connections (self ):
121+ simulation_input = { "goalConnections" : 0 } # Invalid goalConnections
122+ response = self .client .post ('/run_simulation' ,
123+ data = json .dumps (simulation_input ),
124+ content_type = 'application/json' )
125+ self .assert_status (response , 500 ) # Use assert_status
126+ response_json = json .loads (response .data .decode ('utf-8' ))
127+ self .assertIn ("Simulation execution failed" , response_json .get ("error" ))
128+
129+ def test_invalid_network_type (self ):
130+ simulation_input = { "networkType" : 99 } # Invalid networkType
131+ response = self .client .post ('/run_simulation' ,
132+ data = json .dumps (simulation_input ),
133+ content_type = 'application/json' )
134+ self .assert_status (response , 500 ) # Use assert_status
135+ response_json = json .loads (response .data .decode ('utf-8' ))
136+ self .assertIn ("Simulation execution failed" , response_json .get ("error" ))
137+
138+ def test_invalid_confidence_param_too_high (self ):
139+ simulation_input = { "confidence" : 2.0 } # Invalid confidence (greater than 1)
140+ response = self .client .post ('/run_simulation' ,
141+ data = json .dumps (simulation_input ),
142+ content_type = 'application/json' )
143+ self .assert_status (response , 500 ) # Use assert_status
144+ response_json = json .loads (response .data .decode ('utf-8' ))
145+ self .assertIn ("Simulation execution failed" , response_json .get ("error" ))
146+
147+ def test_invalid_confidence_param_too_low (self ):
148+ simulation_input = { "confidence" : 0.0 } # Invalid confidence (equal to 0)
149+ response = self .client .post ('/run_simulation' ,
150+ data = json .dumps (simulation_input ),
151+ content_type = 'application/json' )
152+ self .assert_status (response , 500 ) # Use assert_status
153+ response_json = json .loads (response .data .decode ('utf-8' ))
154+ self .assertIn ("Simulation execution failed" , response_json .get ("error" ))
155+
156+ def test_invalid_algorithm_param (self ):
157+ simulation_input = { "algorithm" : "InvalidAlgorithm" } # Invalid algorithm
158+ response = self .client .post ('/run_simulation' ,
159+ data = json .dumps (simulation_input ),
160+ content_type = 'application/json' )
161+ self .assert_status (response , 500 ) # Use assert_status
162+ response_json = json .loads (response .data .decode ('utf-8' ))
163+ self .assertIn ("Simulation execution failed" , response_json .get ("error" ))
164+
165+ def test_invalid_K_param (self ):
166+ simulation_input = { "K" : 0 } # Invalid K (less than 1)
167+ response = self .client .post ('/run_simulation' ,
168+ data = json .dumps (simulation_input ),
169+ content_type = 'application/json' )
170+ self .assert_status (response , 500 ) # Use assert_status
171+ response_json = json .loads (response .data .decode ('utf-8' ))
172+ self .assertIn ("Simulation execution failed" , response_json .get ("error" ))
173+
174+ def test_invalid_network_name (self ):
175+ simulation_input = { "network" : "TestNetwork" } # Invalid network name
176+ response = self .client .post ('/run_simulation' ,
177+ data = json .dumps (simulation_input ),
178+ content_type = 'application/json' )
179+ self .assert_status (response , 500 ) # Use assert_status
180+ response_json = json .loads (response .data .decode ('utf-8' ))
181+ self .assertIn ("Simulation execution failed" , response_json .get ("error" ))
182+
183+ def test_invalid_bitrate_param (self ):
184+ simulation_input = { "bitrate" : "invalid-rate" } # Invalid bitrate
185+ response = self .client .post ('/run_simulation' ,
186+ data = json .dumps (simulation_input ),
187+ content_type = 'application/json' )
188+ self .assert_status (response , 500 ) # Use assert_status
189+ response_json = json .loads (response .data .decode ('utf-8' ))
190+ self .assertIn ("Simulation execution failed" , response_json .get ("error" ))
0 commit comments