Skip to content

Commit 8fb922a

Browse files
committed
[WIP/TEST] Trying to add server tests
1 parent a9a8e32 commit 8fb922a

File tree

6 files changed

+343
-8
lines changed

6 files changed

+343
-8
lines changed

gempy_engine/API/server/_server_functions.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import logging
33

44
from gempy_engine.core.data.engine_grid import EngineGrid
5-
from core.data.regular_grid import RegularGrid
65
from gempy_engine.core.data.input_data_descriptor import InputDataDescriptor
76
from gempy_engine.core.data.interpolation_input import InterpolationInput
87
from gempy_engine.core.data.kernel_classes.server.input_parser import GemPyInput
8+
from gempy_engine.core.data.regular_grid import RegularGrid
99

1010

1111
def setup_logger():
@@ -34,10 +34,12 @@ def setup_logger():
3434
def process_input(gempy_input: GemPyInput, logger: logging.Logger) -> (InputDataDescriptor, InterpolationInput, int):
3535

3636
logger.debug("Input grid:", gempy_input.interpolation_input.grid)
37+
38+
regular_grid: EngineGrid = EngineGrid.from_regular_grid(regular_grid=RegularGrid.from_schema(gempy_input.interpolation_input.grid))
39+
40+
# BUG: Adapt this to the new grid
41+
# gempy_input.interpolation_input.grid = regular_grid
3742

38-
gempy_input.interpolation_input.grid = EngineGrid.from_regular_grid(
39-
regular_grid=RegularGrid.from_schema(gempy_input.interpolation_input.grid)
40-
)
4143
interpolation_input: InterpolationInput = InterpolationInput.from_schema(gempy_input.interpolation_input)
4244
input_data_descriptor: InputDataDescriptor = InputDataDescriptor.from_schema(gempy_input.input_data_descriptor)
4345
n_stack = len(input_data_descriptor.stack_structure.masking_descriptor)

gempy_engine/API/server/main_server_pro.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
print("Pydantic is not installed. No server capabilities will be available.")
3030
BaseModel = object
3131

32-
app = FastAPI()
33-
# Start the server: uvicorn gempy_engine.API.server.main_server:app
32+
gempy_engine_App = FastAPI(debug=True)
33+
logger = setup_logger()
34+
# * Start the server: uvicorn gempy_engine.API.server.main_server:app
3435

3536

3637
# region InterpolationOptions
@@ -47,12 +48,12 @@
4748

4849
# endregion
4950

50-
logger = setup_logger()
5151

5252
# BackendTensor.change_backend(AvailableBackends.numpy, use_gpu=False, pykeops_enabled=True)
5353

54-
@app.post("/")
54+
@gempy_engine_App.post("/")
5555
def compute_gempy_model(gempy_input: GemPyInput):
56+
print("Running gempy model...")
5657
logger.info("Running GemPy Engine")
5758

5859
input_data_descriptor, interpolation_input, n_stack = process_input(

tests/test_server/2features.json

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"interpolation_input": {
3+
"surface_points": {
4+
"sp_coords": [
5+
[
6+
5.0,
7+
2.0,
8+
10.0
9+
],
10+
[
11+
5.0,
12+
15.0,
13+
10.0
14+
],
15+
[
16+
15.0,
17+
15.0,
18+
10.0
19+
],
20+
[
21+
15.0,
22+
2.0,
23+
10.0
24+
],
25+
[
26+
2.0,
27+
2.0,
28+
7.0
29+
],
30+
[
31+
15.0,
32+
2.0,
33+
7.0
34+
],
35+
[
36+
15.0,
37+
15.0,
38+
2.0
39+
],
40+
[
41+
2.0,
42+
15.0,
43+
2.0
44+
]
45+
]
46+
},
47+
"orientations": {
48+
"dip_positions": [
49+
[
50+
8.0,
51+
8.0,
52+
10.0
53+
],
54+
[
55+
8.0,
56+
8.0,
57+
4.0
58+
]
59+
],
60+
"dip_gradients": [
61+
[
62+
0.0,
63+
0.0,
64+
1.0
65+
],
66+
[
67+
3.05954E-08,
68+
0.535237432,
69+
0.844701648
70+
]
71+
]
72+
}
73+
},
74+
"input_data_descriptor": {
75+
"masking_descriptor": [
76+
1,
77+
1
78+
],
79+
"number_of_points_per_surface": [
80+
4,
81+
4
82+
],
83+
"number_of_points_per_stack": [
84+
4,
85+
4
86+
],
87+
"number_of_surfaces_per_stack": [
88+
1,
89+
1
90+
],
91+
"number_of_orientations_per_stack": [
92+
1,
93+
1
94+
]
95+
}
96+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"interpolation_input": {
3+
"surface_points": {
4+
"sp_coords": [
5+
[
6+
5.0,
7+
2.0,
8+
10.0
9+
],
10+
[
11+
5.0,
12+
15.0,
13+
10.0
14+
],
15+
[
16+
15.0,
17+
15.0,
18+
10.0
19+
],
20+
[
21+
15.0,
22+
2.0,
23+
10.0
24+
],
25+
[
26+
2.0,
27+
2.0,
28+
7.0
29+
],
30+
[
31+
15.0,
32+
2.0,
33+
7.0
34+
],
35+
[
36+
15.0,
37+
15.0,
38+
2.0
39+
],
40+
[
41+
2.0,
42+
15.0,
43+
2.0
44+
]
45+
]
46+
},
47+
"orientations": {
48+
"dip_positions": [
49+
[
50+
8.0,
51+
8.0,
52+
10.0
53+
],
54+
[
55+
8.0,
56+
8.0,
57+
4.0
58+
]
59+
],
60+
"dip_gradients": [
61+
[
62+
0.0,
63+
0.0,
64+
1.0
65+
],
66+
[
67+
3.05954E-08,
68+
0.535237432,
69+
0.844701648
70+
]
71+
]
72+
},
73+
"grid": null
74+
},
75+
"input_data_descriptor": {
76+
"masking_descriptor": [
77+
1,
78+
1
79+
],
80+
"number_of_points_per_surface": [
81+
4,
82+
4
83+
],
84+
"number_of_points_per_stack": [
85+
4,
86+
4
87+
],
88+
"number_of_surfaces_per_stack": [
89+
1,
90+
1
91+
],
92+
"number_of_orientations_per_stack": [
93+
1,
94+
1
95+
]
96+
}
97+
}

tests/test_server/example.json

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"interpolation_input": {
3+
"surface_points": {
4+
"sp_coords": [
5+
[
6+
0.25010,
7+
0.50010,
8+
0.37510
9+
],
10+
[
11+
0.50010,
12+
0.50010,
13+
0.37510
14+
],
15+
[
16+
0.66677,
17+
0.50010,
18+
0.41677
19+
],
20+
[
21+
0.70843,
22+
0.50010,
23+
0.47510
24+
],
25+
[
26+
0.75010,
27+
0.50010,
28+
0.54177
29+
],
30+
[
31+
0.58343,
32+
0.50010,
33+
0.39177
34+
],
35+
[
36+
0.73343,
37+
0.50010,
38+
0.50010
39+
]
40+
]
41+
},
42+
"orientations": {
43+
"dip_positions": [
44+
[
45+
0.25010,
46+
0.50010,
47+
0.54177
48+
],
49+
[
50+
0.66677,
51+
0.50010,
52+
0.62510
53+
]
54+
],
55+
"dip_gradients": [
56+
[
57+
0,
58+
0,
59+
1
60+
],
61+
[
62+
-0.6,
63+
0,
64+
0.8
65+
]
66+
]
67+
},
68+
"grid": null
69+
},
70+
"input_data_descriptor": {
71+
"number_of_points_per_surface": [7],
72+
"number_of_points_per_stack": [7],
73+
"number_of_orientations_per_stack": [2],
74+
"number_of_surfaces_per_stack": [1],
75+
"masking_descriptor": [1]
76+
}
77+
}

tests/test_server/tests_server_I.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import json
2+
import os
3+
4+
import pytest
5+
from fastapi.testclient import TestClient
6+
7+
from gempy_engine.API.server.main_server_pro import gempy_engine_App
8+
9+
# Define paths relative to the project root
10+
11+
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
12+
EXAMPLE_JSON_PATH = os.path.join(BASE_PATH, "example.json")
13+
FEATURES_JSON_PATH = os.path.join(BASE_PATH, "2features.json")
14+
15+
16+
def load_request_data(json_file_path):
17+
"""Load JSON data from a file path"""
18+
try:
19+
with open(json_file_path, 'r') as file:
20+
return json.load(file)
21+
except FileNotFoundError as e:
22+
raise FileNotFoundError(f"Error: File '{json_file_path}' not found") from e
23+
except json.JSONDecodeError as e:
24+
raise json.JSONDecodeError(f"Error: File '{json_file_path}' contains invalid JSON", e.doc, e.pos) from e
25+
26+
27+
@pytest.fixture
28+
def client():
29+
"""Create a test client for FastAPI app"""
30+
return TestClient(gempy_engine_App)
31+
32+
33+
def test_post_example_json(client):
34+
"""Test POST request with example.json data"""
35+
data = load_request_data(EXAMPLE_JSON_PATH)
36+
assert data is not None, f"Failed to load data from {EXAMPLE_JSON_PATH}"
37+
38+
response = client.post(
39+
"/",
40+
json=data,
41+
headers={"Content-Type": "application/json"}
42+
)
43+
44+
assert response.status_code == 200
45+
result = response.json()
46+
assert "result" in result or "scalar_field" in result
47+
48+
49+
def test_post_features_json(client):
50+
"""Test POST request with 2features.json data"""
51+
data = load_request_data(FEATURES_JSON_PATH)
52+
assert data is not None, f"Failed to load data from {FEATURES_JSON_PATH}"
53+
54+
response = client.post(
55+
"/",
56+
json=data,
57+
headers={"Content-Type": "application/json"}
58+
)
59+
60+
assert response.status_code == 200
61+
result = response.json()
62+
assert "result" in result or "scalar_field" in result

0 commit comments

Comments
 (0)