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